diff --git a/docs/api.rst b/docs/api.rst index b0591a4b8..f73880a4b 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -61,10 +61,14 @@ Owned Types TODO: owned type description -.. c:type:: z_owned_bytes_t +.. c:type:: z_owned_slice_t Represents an array of bytes. +.. c:type:: z_owned_bytes_t + + Represents an array of bytes container. + .. c:type:: z_owned_string_t Represents a string without null-terminator. @@ -130,10 +134,14 @@ Loaned Types TODO: loaned type description -.. c:type:: z_loaned_bytes_t +.. c:type:: z_loaned_slice_t Represents an array of bytes. +.. c:type:: z_loaned_bytes_t + + Represents an array of bytes container. + .. c:type:: z_loaned_string_t Represents a string without null-terminator. @@ -250,7 +258,7 @@ Macros Primitives ~~~~~~~~~~ -.. autocfunction:: primitives.h::z_view_str_wrap +.. autocfunction:: primitives.h::z_view_string_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 @@ -282,9 +290,34 @@ Primitives .. 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_slice_data +.. autocfunction:: primitives.h::z_slice_len +.. autocfunction:: primitives.h::z_bytes_decode_into_int8 +.. autocfunction:: primitives.h::z_bytes_decode_into_int16 +.. autocfunction:: primitives.h::z_bytes_decode_into_int32 +.. autocfunction:: primitives.h::z_bytes_decode_into_int64 +.. autocfunction:: primitives.h::z_bytes_decode_into_uint8 +.. autocfunction:: primitives.h::z_bytes_decode_into_uint16 +.. autocfunction:: primitives.h::z_bytes_decode_into_uint32 +.. autocfunction:: primitives.h::z_bytes_decode_into_uint64 +.. autocfunction:: primitives.h::z_bytes_decode_into_float +.. autocfunction:: primitives.h::z_bytes_decode_into_double +.. autocfunction:: primitives.h::z_bytes_decode_into_slice .. autocfunction:: primitives.h::z_bytes_decode_into_string +.. autocfunction:: primitives.h::z_bytes_encode_from_int8 +.. autocfunction:: primitives.h::z_bytes_encode_from_int16 +.. autocfunction:: primitives.h::z_bytes_encode_from_int32 +.. autocfunction:: primitives.h::z_bytes_encode_from_int64 +.. autocfunction:: primitives.h::z_bytes_encode_from_uint8 +.. autocfunction:: primitives.h::z_bytes_encode_from_uint16 +.. autocfunction:: primitives.h::z_bytes_encode_from_uint32 +.. autocfunction:: primitives.h::z_bytes_encode_from_uint64 +.. autocfunction:: primitives.h::z_bytes_encode_from_float +.. autocfunction:: primitives.h::z_bytes_encode_from_double +.. autocfunction:: primitives.h::z_bytes_encode_from_slice +.. autocfunction:: primitives.h::z_bytes_encode_from_slice_copy .. autocfunction:: primitives.h::z_bytes_encode_from_string +.. autocfunction:: primitives.h::z_bytes_encode_from_string_copy .. autocfunction:: primitives.h::z_timestamp_check .. autocfunction:: primitives.h::z_query_target_default .. autocfunction:: primitives.h::z_query_consolidation_auto @@ -303,6 +336,7 @@ Primitives .. autocfunction:: primitives.h::z_closure_zid .. autocfunction:: primitives.h::z_sample_loan .. autocfunction:: primitives.h::z_string_data +.. autocfunction:: primitives.h::z_string_len .. autocfunction:: primitives.h::z_scout .. autocfunction:: primitives.h::z_open .. autocfunction:: primitives.h::z_close diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index a4a5a9f2d..8bbe1fdc3 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -116,11 +116,9 @@ void loop() { z_get_options_t opts; z_get_options_default(&opts); // Value encoding + z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { - z_view_string_t value_str; - z_view_string_wrap(&value_str, VALUE); - z_owned_bytes_t payload; - z_bytes_encode_from_string(&payload, z_view_string_loan(&value_str)); + z_bytes_encode_from_string(&payload, VALUE); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index da225a613..9ff9b936a 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -43,24 +43,21 @@ 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); // Reply value encoding - z_view_string_t reply_str; - z_view_string_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; - z_bytes_encode_from_string(&reply_payload, z_view_string_loan(&reply_str)); + z_bytes_encode_from_string(&reply_payload, VALUE); z_query_reply(query, z_view_keyexpr_loan(&ke), z_bytes_move(&reply_payload), NULL); diff --git a/examples/arduino/z_sub.ino b/examples/arduino/z_sub.ino index 990ab7580..163368ed7 100644 --- a/examples/arduino/z_sub.ino +++ b/examples/arduino/z_sub.ino @@ -37,15 +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); - std::string val((const char *)z_sample_payload(sample)->start, z_sample_payload(sample)->len); + 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_get.c b/examples/espidf/z_get.c index 7c53b86b5..959d58c23 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -163,11 +163,9 @@ void app_main() { z_get_options_t opts; z_get_options_default(&opts); // Value encoding + z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { - z_view_string_t value_str; - z_view_string_wrap(&value_str, VALUE); - z_owned_bytes_t payload; - z_bytes_encode_from_string(&payload, z_loan(value_str)); + z_bytes_encode_from_string(&payload, VALUE); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index d10e25f57..160d0d475 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -110,20 +110,19 @@ 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_view_string_t reply_str; - z_view_string_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; - z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + z_bytes_encode_from_string(&reply_payload, VALUE); 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 d496734e6..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)payload->len, payload->start); + 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_get.c b/examples/freertos_plus_tcp/z_get.c index d91bb6065..671b0501b 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -87,11 +87,9 @@ void app_main(void) { z_get_options_t opts; z_get_options_default(&opts); // Value encoding + z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { - z_view_string_t value_str; - z_view_string_wrap(&value_str, VALUE); - z_owned_bytes_t payload; - z_bytes_encode_from_string(&payload, z_loan(value_str)); + z_bytes_encode_from_string(&payload, VALUE); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index ca3444034..923fef662 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -38,20 +38,18 @@ 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 - z_view_string_t reply_str; - z_view_string_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; - z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + z_bytes_encode_from_string(&reply_payload, VALUE); 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_sub.c b/examples/freertos_plus_tcp/z_sub.c index d0afc19ad..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)payload->len, - payload->start); + 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 aa24456ee..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)payload->len, - payload->start); - 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_get.cpp b/examples/mbed/z_get.cpp index ec2f2cef5..215788be8 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -84,11 +84,9 @@ int main(int argc, char **argv) { z_get_options_t opts; z_get_options_default(&opts); // Value encoding + z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { - z_view_string_t value_str; - z_view_string_wrap(&value_str, VALUE); - z_owned_bytes_t payload; - z_bytes_encode_from_string(&payload, z_view_string_loan(&value_str)); + z_bytes_encode_from_string(&payload, VALUE); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index 7be7f79da..12b34dcb9 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -40,18 +40,16 @@ 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_view_string_t reply_str; - z_view_string_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; - z_bytes_encode_from_string(&reply_payload, z_view_string_loan(&reply_str)); + z_bytes_encode_from_string(&reply_payload, VALUE); 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/mbed/z_sub.cpp b/examples/mbed/z_sub.cpp index 20ea106d4..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)payload->len, payload->start); + 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_get.c b/examples/unix/c11/z_get.c index 6066ac2e7..61c7df1ed 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -134,12 +134,10 @@ int main(int argc, char **argv) { printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts; z_get_options_default(&opts); - + // Value encoding + z_owned_bytes_t payload; if (value != NULL) { - z_view_string_t value_str; - z_view_string_wrap(&value_str, value); - z_owned_bytes_t payload; - z_bytes_encode_from_string(&payload, z_loan(value_str)); + z_bytes_encode_from_string(&payload, value); opts.payload = &payload; } #if Z_FEATURE_ATTACHMENT == 1 diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index e2eef163c..0091e14a5 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -91,11 +91,9 @@ int main(int argc, char **argv) { z_get_options_t opts; z_get_options_default(&opts); // Value encoding + z_owned_bytes_t payload; if (value != NULL) { - z_view_string_t value_str; - z_view_string_wrap(&value_str, value); - z_owned_bytes_t payload; - z_bytes_encode_from_string(&payload, z_loan(value_str)); + z_bytes_encode_from_string(&payload, value); opts.payload = &payload; } z_owned_reply_ring_channel_t channel; diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index 8be8fb256..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, payload->start, payload->len, 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 6e7506663..7dab57c57 100644 --- a/examples/unix/c11/z_pull.c +++ b/examples/unix/c11/z_pull.c @@ -90,9 +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); - 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_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 73ee65683..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)) { @@ -71,10 +71,8 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { #endif // Reply value encoding - z_view_string_t reply_str; - z_view_string_wrap(&reply_str, value); z_owned_bytes_t reply_payload; - z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + z_bytes_encode_from_string(&reply_payload, value); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); z_drop(z_move(keystr)); diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index 05091d334..22888c46f 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -106,20 +106,19 @@ 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 - z_view_string_t reply_str; - z_view_string_wrap(&reply_str, value); z_owned_bytes_t reply_payload; - z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + z_bytes_encode_from_string(&reply_payload, value); z_query_reply(q, z_query_keyexpr(q), z_move(reply_payload), &options); z_drop(z_move(keystr)); diff --git a/examples/unix/c11/z_sub.c b/examples/unix/c11/z_sub.c index a91133fa6..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)payload->len, - payload->start); + 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 02a793961..94e341eec 100644 --- a/examples/unix/c11/z_sub_channel.c +++ b/examples/unix/c11/z_sub_channel.c @@ -80,9 +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); - 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_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 97bcacfc0..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)payload->len, - payload->start); + 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_get.c b/examples/unix/c99/z_get.c index e11827800..e088641ae 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -122,11 +122,9 @@ int main(int argc, char **argv) { z_get_options_t opts; z_get_options_default(&opts); // Value encoding + z_owned_bytes_t payload; if (value != NULL) { - z_view_string_t value_str; - z_view_string_wrap(&value_str, value); - z_owned_bytes_t payload; - z_bytes_encode_from_string(&payload, z_view_string_loan(&value_str)); + z_bytes_encode_from_string(&payload, value); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/unix/c99/z_pong.c b/examples/unix/c99/z_pong.c index 533dc369a..b40030c9e 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, payload->start, payload->len, NULL); + z_owned_slice_t value; + z_bytes_decode_into_slice(z_sample_payload(sample), &value); + z_publisher_put(pub, z_slice_data(z_slice_loan(&value)), z_slice_len(z_slice_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 9bdb4a910..b27f792c5 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); @@ -46,10 +46,8 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_reply_options_default(&options); options.encoding = z_encoding_move(&encoding); // Reply value encoding - z_view_string_t reply_str; - z_view_string_wrap(&reply_str, value); z_owned_bytes_t reply_payload; - z_bytes_encode_from_string(&reply_payload, z_view_string_loan(&reply_str)); + z_bytes_encode_from_string(&reply_payload, value); 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/unix/c99/z_sub.c b/examples/unix/c99/z_sub.c index 3495d167a..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)payload->len, - payload->start); + 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 c83d059ba..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)payload->len, - payload->start); + 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_get.c b/examples/windows/z_get.c index 42fdfea55..a9c06a0bf 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -86,11 +86,9 @@ int main(int argc, char **argv) { z_get_options_t opts; z_get_options_default(&opts); // Value encoding + z_owned_bytes_t payload; if (value != NULL) { - z_view_string_t value_str; - z_view_string_wrap(&value_str, value); - z_owned_bytes_t payload; - z_bytes_encode_from_string(&payload, z_loan(value_str)); + z_bytes_encode_from_string(&payload, value); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/windows/z_pong.c b/examples/windows/z_pong.c index 8be8fb256..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, payload->start, payload->len, 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 a6130c3b7..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); @@ -46,10 +46,8 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_reply_options_default(&options); options.encoding = z_move(encoding); // Reply value encoding - z_view_string_t reply_str; - z_view_string_wrap(&reply_str, value); z_owned_bytes_t reply_payload; - z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + z_bytes_encode_from_string(&reply_payload, value); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); diff --git a/examples/windows/z_sub.c b/examples/windows/z_sub.c index 5ca826633..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)payload->len, - payload->start); + 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 5aed70bdc..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)payload->len, - payload->start); + 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_get.c b/examples/zephyr/z_get.c index 397b3d340..853d5dfb9 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -81,11 +81,9 @@ int main(int argc, char **argv) { z_get_options_default(&opts); opts.target = Z_QUERY_TARGET_ALL; // Value encoding + z_owned_bytes_t payload; if (strcmp(VALUE, "") != 0) { - z_view_string_t value_str; - z_view_string_wrap(&value_str, VALUE); - z_owned_bytes_t payload; - z_bytes_encode_from_string(&payload, z_loan(value_str)); + z_bytes_encode_from_string(&payload, VALUE); opts.payload = &payload; } z_owned_closure_reply_t callback; diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index f210cf1b3..60a2e0261 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -40,18 +40,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)), 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_view_string_t reply_str; - z_view_string_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; - z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + z_bytes_encode_from_string(&reply_payload, VALUE); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); z_drop(z_move(keystr)); diff --git a/examples/zephyr/z_sub.c b/examples/zephyr/z_sub.c index bda16ff13..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)payload->len, - payload->start); + 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/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 58287f5fa..851f6f525 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -47,6 +47,7 @@ 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_slice_t : z_slice_loan, \ z_owned_bytes_t : z_bytes_loan, \ z_owned_encoding_t : z_encoding_loan \ )(&x) @@ -64,6 +65,7 @@ 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_slice_t : z_slice_loan_mut, \ z_owned_bytes_t : z_bytes_loan_mut \ )(&x) /** @@ -87,6 +89,8 @@ z_owned_sample_t * : z_sample_drop, \ z_owned_query_t * : z_query_drop, \ z_owned_encoding_t * : z_encoding_drop, \ + z_owned_slice_t *: z_slice_drop, \ + z_owned_bytes_t *: z_bytes_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,6 +129,7 @@ z_owned_hello_t : z_hello_check, \ z_owned_string_t : z_string_check, \ z_owned_string_array_t : z_string_array_check, \ + z_owned_slice_t : z_slice_check, \ z_owned_bytes_t : z_bytes_check, \ z_owned_sample_t : z_sample_check, \ z_owned_query_t : z_query_check, \ @@ -178,6 +183,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_slice_t : z_slice_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, \ @@ -228,6 +234,8 @@ z_owned_reply_t * : z_reply_null, \ z_owned_hello_t * : z_hello_null, \ z_owned_string_t * : z_string_null, \ + z_owned_slice_t *: z_slice_null, \ + z_owned_bytes_t *: z_bytes_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, \ @@ -283,6 +291,7 @@ template <> struct zenoh_loan_type { typedef const z_loaned_str 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_slice_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); } @@ -298,6 +307,7 @@ template <> inline const z_loaned_string_t* z_loan(const z_view_string_t& x) { r 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_slice_t* z_loan(const z_owned_slice_t& x) { return z_slice_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 @@ -318,6 +328,7 @@ template <> struct zenoh_loan_mut_type { typedef z_loaned_strin 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_slice_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); } @@ -333,6 +344,7 @@ template <> inline z_loaned_string_t* z_loan_mut(z_view_string_t& x) { return z_ 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_slice_t* z_loan_mut(z_owned_slice_t& x) { return z_slice_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 diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 0653ffab6..d00aa8098 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -32,8 +32,6 @@ extern "C" { #endif /********* Data Types Handlers *********/ -#define z_bytes_wrap _z_bytes_wrap - /** * Builds a :c:type:`z_view_string_t` by wrapping a ``const char *`` string. * @@ -420,16 +418,159 @@ int8_t z_encoding_null(z_owned_encoding_t *encoding); */ const z_loaned_bytes_t *z_value_payload(const z_loaned_value_t *value); +/** + * Gets date pointer of a bytes array. + * + * Parameters: + * slice: Pointer to a :c:type:`z_loaned_slice_t` to get data from. + * + * Return: + * The data pointer. + */ +const uint8_t *z_slice_data(const z_loaned_slice_t *slice); + /** * Gets total number of bytes in a bytes array. * * Parameters: - * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to get length from. + * slice: Pointer to a :c:type:`z_loaned_slice_t` to get length from. * * Return: * The number of bytes. */ -size_t z_bytes_len(const z_loaned_bytes_t *bytes); +size_t z_slice_len(const z_loaned_slice_t *slice); + +/** + * Decodes data into a `int8_t` signed integer. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * dst: Pointer to an uninitialized :c:type:`int8_t` to contain the decoded int. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_decode_into_int8(const z_loaned_bytes_t *bytes, int8_t *dst); + +/** + * Decodes data into a `int16_t` signed integer. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * dst: Pointer to an uninitialized :c:type:`int16_t` to contain the decoded int. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_decode_into_int16(const z_loaned_bytes_t *bytes, int16_t *dst); + +/** + * Decodes data into a `int32_t` signed integer. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * dst: Pointer to an uninitialized :c:type:`int32_t` to contain the decoded int. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_decode_into_int32(const z_loaned_bytes_t *bytes, int32_t *dst); + +/** + * Decodes data into a `int64_t` signed integer. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * dst: Pointer to an uninitialized :c:type:`int64_t` to contain the decoded int. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_decode_into_int64(const z_loaned_bytes_t *bytes, int64_t *dst); + +/** + * Decodes data into a `uint8_t` unsigned integer. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * dst: Pointer to an uninitialized :c:type:`uint8_t` to contain the decoded int. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_decode_into_uint8(const z_loaned_bytes_t *bytes, uint8_t *dst); + +/** + * Decodes data into a `uint16_t` unsigned integer. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * dst: Pointer to an uninitialized :c:type:`uint16_t` to contain the decoded int. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_decode_into_uint16(const z_loaned_bytes_t *bytes, uint16_t *dst); + +/** + * Decodes data into a `uint32_t` unsigned integer. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * dst: Pointer to an uninitialized :c:type:`uint32_t` to contain the decoded int. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_decode_into_uint32(const z_loaned_bytes_t *bytes, uint32_t *dst); + +/** + * Decodes data into a `uint64_t` unsigned integer. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * dst: Pointer to an uninitialized :c:type:`uint64_t` to contain the decoded int. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_decode_into_uint64(const z_loaned_bytes_t *bytes, uint64_t *dst); + +/** + * Decodes data into a `float` floating number. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * dst: Pointer to an uninitialized :c:type:`float` to contain the decoded float. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_decode_into_float(const z_loaned_bytes_t *bytes, float *dst); + +/** + * Decodes data into a `double` floating number. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * dst: Pointer to an uninitialized :c:type:`double` to contain the decoded float. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_decode_into_double(const z_loaned_bytes_t *bytes, double *dst); + +/** + * Decodes data into a :c:type:`z_owned_slice_t` + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * str: Pointer to an uninitialized :c:type:`z_owned_slice_t` to contain the decoded slice. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_decode_into_slice(const z_loaned_bytes_t *bytes, z_owned_slice_t *dst); /** * Decodes data into a :c:type:`z_owned_string_t` @@ -444,16 +585,172 @@ size_t z_bytes_len(const z_loaned_bytes_t *bytes); int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *str); /** - * Encodes a string into a :c:type:`z_owned_bytes_t` + * Encodes a signed integer into a :c:type:`z_owned_bytes_t` + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int. + * val: `int8_t` value to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_int8(z_owned_bytes_t *bytes, int8_t val); + +/** + * Encodes a signed integer into a :c:type:`z_owned_bytes_t` + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int. + * val: `int16_t` value to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_int16(z_owned_bytes_t *bytes, int16_t val); + +/** + * Encodes a signed integer into a :c:type:`z_owned_bytes_t` * * Parameters: - * buffer: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded string. + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int. + * val: `int32_t` value to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_int32(z_owned_bytes_t *bytes, int32_t val); + +/** + * Encodes a signed integer into a :c:type:`z_owned_bytes_t` + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int. + * val: `int64_t` value to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_int64(z_owned_bytes_t *bytes, int64_t val); + +/** + * Encodes an unsigned integer into a :c:type:`z_owned_bytes_t` + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int. + * val: `uint8_t` value to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_uint8(z_owned_bytes_t *bytes, uint8_t val); + +/** + * Encodes an unsigned integer into a :c:type:`z_owned_bytes_t` + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int. + * val: `uint16_t` value to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_uint16(z_owned_bytes_t *bytes, uint16_t val); + +/** + * Encodes an unsigned integer into a :c:type:`z_owned_bytes_t` + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int. + * val: `uint32_t` value to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_uint32(z_owned_bytes_t *bytes, uint32_t val); + +/** + * Encodes an unsigned integer into a :c:type:`z_owned_bytes_t` + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int. + * val: `uint64_t` value to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_uint64(z_owned_bytes_t *bytes, uint64_t val); + +/** + * Encodes a floating number into a :c:type:`z_owned_bytes_t` + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int. + * val: `float` value to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_float(z_owned_bytes_t *bytes, float val); + +/** + * Encodes a floating number into a :c:type:`z_owned_bytes_t` + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int. + * val: `double` value to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_double(z_owned_bytes_t *bytes, double val); + +/** + * Encodes a slice into a :c:type:`z_owned_bytes_t` by aliasing + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded slice. + * str: Pointer to the slice to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, size_t len); + +/** + * Encodes a slice into a :c:type:`z_owned_bytes_t` by copying + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded slice. + * str: Pointer to the slice to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_encode_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t *data, size_t len); + +/** + * Encodes a string into a :c:type:`z_owned_bytes_t` by aliasing + * + * Parameters: + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded string. + * s: 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 *bytes, const char *s); + +/** + * Encodes a string into a :c:type:`z_owned_bytes_t` by copying + * + * Parameters: + * bytes: 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 *str); +int8_t z_bytes_encode_from_string_copy(z_owned_bytes_t *bytes, const char *s); /** * Checks validity of a timestamp @@ -711,6 +1008,7 @@ _OWNED_FUNCTIONS(z_loaned_reply_t, z_owned_reply_t, reply) _OWNED_FUNCTIONS(z_loaned_string_array_t, z_owned_string_array_t, string_array) _OWNED_FUNCTIONS(z_loaned_sample_t, z_owned_sample_t, sample) _OWNED_FUNCTIONS(z_loaned_query_t, z_owned_query_t, query) +_OWNED_FUNCTIONS(z_loaned_slice_t, z_owned_slice_t, slice) _OWNED_FUNCTIONS(z_loaned_bytes_t, z_owned_bytes_t, bytes) _OWNED_FUNCTIONS(z_loaned_value_t, z_owned_value_t, value) @@ -765,6 +1063,17 @@ const z_loaned_sample_t *z_sample_loan(const z_owned_sample_t *sample); */ const char *z_string_data(const z_loaned_string_t *str); +/** + * Gets string length from a :c:type:`z_loaned_string_t`. + * + * Parameters: + * str: Pointer to a :c:type:`z_loaned_string_t` to get length from. + * + * Return: + * Length of the string. + */ +size_t z_string_len(const z_loaned_string_t *str); + /************* Primitives **************/ /** * Scouts for other Zenoh entities like routers and/or peers. diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 70c19885e..96576a0de 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -15,9 +15,9 @@ #ifndef INCLUDE_ZENOH_PICO_API_TYPES_H #define INCLUDE_ZENOH_PICO_API_TYPES_H -#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/list.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/net/encoding.h" #include "zenoh-pico/net/publish.h" @@ -82,6 +82,15 @@ typedef _z_timestamp_t z_timestamp_t; * size_t len: The length of the bytes array. * uint8_t *start: A pointer to the bytes array. */ +_OWNED_TYPE_PTR(_z_slice_t, slice) +_LOANED_TYPE(_z_slice_t, slice) + +/** + * Represents a container for slices. + * + * Members: + * _z_slice_t slice: content of the container. + */ _OWNED_TYPE_PTR(_z_bytes_t, bytes) _LOANED_TYPE(_z_bytes_t, bytes) @@ -189,7 +198,7 @@ _LOANED_TYPE(_z_query_rc_t, query) * * Members: * 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. + * z_loaned_slice_t* suffix: The suffix of this encoding. It MUST be a valid UTF-8 string. */ _OWNED_TYPE_PTR(_z_encoding_t, encoding) _LOANED_TYPE(_z_encoding_t, encoding) @@ -314,10 +323,10 @@ typedef struct { * Represents the configuration used to configure a get operation sent via :c:func:`z_get`. * * Members: - * z_query_target_t target: The queryables that should be targeted by this get. - * z_query_consolidation_t consolidation: The replies consolidation strategy to apply on replies. * z_owned_bytes_t payload: The payload to include in the query. * z_owned_encoding_t *encoding: Payload encoding. + * z_query_consolidation_t consolidation: The replies consolidation strategy to apply on replies. + * z_query_target_t target: The queryables that should be targeted by this get. */ typedef struct { z_owned_bytes_t *payload; @@ -424,7 +433,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_loaned_slice_t* zid: The Zenoh ID of the scouted entity (empty if absent). * z_loaned_string_array_t locators: The locators of the scouted entity. */ _OWNED_TYPE_PTR(_z_hello_t, hello) @@ -625,22 +634,22 @@ 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; +struct _z_slice_pair_t { + _z_slice_t *key; + _z_slice_t *value; }; -void _z_bytes_pair_clear(struct _z_bytes_pair_t *this_); +void _z_slice_pair_clear(struct _z_slice_pair_t *this_); -_Z_ELEM_DEFINE(_z_bytes_pair, struct _z_bytes_pair_t, _z_noop_size, _z_bytes_pair_clear, _z_noop_copy) -_Z_LIST_DEFINE(_z_bytes_pair, struct _z_bytes_pair_t) +_Z_ELEM_DEFINE(_z_slice_pair, struct _z_slice_pair_t, _z_noop_size, _z_slice_pair_clear, _z_noop_copy) +_Z_LIST_DEFINE(_z_slice_pair, struct _z_slice_pair_t) /** * A map of maybe-owned vector of bytes to maybe-owned vector of bytes. */ // TODO(sashacmc): z_owned_bytes_map_t for attachment typedef struct z_owned_bytes_map_t { - _z_bytes_pair_list_t *_inner; + _z_slice_pair_list_t *_inner; } z_owned_bytes_map_t; /** @@ -681,7 +690,7 @@ z_owned_bytes_map_t z_bytes_map_from_attachment_aliasing(z_attachment_t this_); * - `this` has no value associated to `key` */ -z_loaned_bytes_t *z_bytes_map_get(const z_owned_bytes_map_t *this_, z_loaned_bytes_t *key); +z_loaned_slice_t *z_bytes_map_get(const z_owned_bytes_map_t *this_, z_loaned_slice_t *key); /** * Associates `value` to `key` in the map, aliasing them. @@ -692,7 +701,7 @@ z_loaned_bytes_t *z_bytes_map_get(const z_owned_bytes_map_t *this_, z_loaned_byt * 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_loaned_bytes_t *key, z_loaned_bytes_t *value); +void z_bytes_map_insert_by_alias(const z_owned_bytes_map_t *this_, z_loaned_slice_t *key, z_loaned_slice_t *value); /** * Associates `value` to `key` in the map, copying them to obtain ownership: `key` and `value` are not aliased past the @@ -701,7 +710,7 @@ void z_bytes_map_insert_by_alias(const z_owned_bytes_map_t *this_, z_loaned_byte * 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_loaned_bytes_t *key, z_loaned_bytes_t *value); +void z_bytes_map_insert_by_copy(const z_owned_bytes_map_t *this_, z_loaned_slice_t *key, z_loaned_slice_t *value); /** * Iterates over the key-value pairs in the map. @@ -734,9 +743,9 @@ z_owned_bytes_map_t z_bytes_map_null(void); /** * Returns a view of `str` using `strlen` (this constructor should not be used on untrusted input). * - * `str == NULL` will cause this to return `z_bytes_null()` + * `str == NULL` will cause this to return `z_slice_null()` */ -int8_t z_bytes_from_str(z_owned_bytes_t *bytes, const char *str); +int8_t z_bytes_from_str(z_owned_slice_t *bytes, const char *str); #ifdef __cplusplus } diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h deleted file mode 100644 index 0002bc257..000000000 --- a/include/zenoh-pico/collections/bytes.h +++ /dev/null @@ -1,53 +0,0 @@ -// -// Copyright (c) 2022 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -#ifndef ZENOH_PICO_COLLECTIONS_BYTES_H -#define ZENOH_PICO_COLLECTIONS_BYTES_H - -#include -#include -#include - -/*-------- Bytes --------*/ -/** - * An array of bytes. - * - * Members: - * size_t len: The length of the bytes array. - * uint8_t *val: A pointer to the bytes array. - */ -typedef struct { - size_t len; - const uint8_t *start; - _Bool _is_alloc; -} _z_bytes_t; - -_z_bytes_t _z_bytes_empty(void); -inline static _Bool _z_bytes_check(_z_bytes_t value) { return value.start != NULL; } -int8_t _z_bytes_init(_z_bytes_t *bs, size_t capacity); -_z_bytes_t _z_bytes_make(size_t capacity); -_z_bytes_t _z_bytes_wrap(const uint8_t *bs, size_t len); -_z_bytes_t _z_bytes_steal(_z_bytes_t *b); - -void _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src); -_z_bytes_t _z_bytes_duplicate(const _z_bytes_t *src); -void _z_bytes_move(_z_bytes_t *dst, _z_bytes_t *src); -void _z_bytes_reset(_z_bytes_t *bs); -_Bool _z_bytes_is_empty(const _z_bytes_t *bs); - -_Bool _z_bytes_eq(const _z_bytes_t *left, const _z_bytes_t *right); -void _z_bytes_clear(_z_bytes_t *bs); -void _z_bytes_free(_z_bytes_t **bs); - -#endif /* ZENOH_PICO_COLLECTIONS_BYTES_H */ diff --git a/include/zenoh-pico/collections/slice.h b/include/zenoh-pico/collections/slice.h new file mode 100644 index 000000000..e491a2041 --- /dev/null +++ b/include/zenoh-pico/collections/slice.h @@ -0,0 +1,85 @@ +// +// Copyright (c) 2022 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#ifndef ZENOH_PICO_COLLECTIONS_SLICE_H +#define ZENOH_PICO_COLLECTIONS_SLICE_H + +#include +#include +#include + +/*-------- Slice --------*/ +/** + * An array of bytes. + * + * Members: + * size_t len: The length of the bytes array. + * uint8_t *start: A pointer to the bytes array. + * _Bool _is_alloc: Indicates if memory has been allocated by this module + */ +typedef struct { + size_t len; + const uint8_t *start; + _Bool _is_alloc; +} _z_slice_t; + +_z_slice_t _z_slice_empty(void); +inline static _Bool _z_slice_check(_z_slice_t value) { return value.start != NULL; } +int8_t _z_slice_init(_z_slice_t *bs, size_t capacity); +_z_slice_t _z_slice_make(size_t capacity); +_z_slice_t _z_slice_wrap(const uint8_t *bs, size_t len); +_z_slice_t _z_slice_steal(_z_slice_t *b); +void _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src); +_z_slice_t _z_slice_duplicate(const _z_slice_t *src); +void _z_slice_move(_z_slice_t *dst, _z_slice_t *src); +void _z_slice_reset(_z_slice_t *bs); +_Bool _z_slice_is_empty(const _z_slice_t *bs); +_Bool _z_slice_eq(const _z_slice_t *left, const _z_slice_t *right); +void _z_slice_clear(_z_slice_t *bs); +void _z_slice_free(_z_slice_t **bs); + +/*-------- Bytes --------*/ +/** + * A container for slices. + * + * Members: + * _z_slice_t slice: content of the container. + */ +typedef struct { + _z_slice_t _slice; +} _z_bytes_t; + +_Bool _z_bytes_check(_z_bytes_t bytes); +_z_bytes_t _z_bytes_null(void); +_z_bytes_t _z_bytes_make(size_t capacity); +void _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src); +_z_bytes_t _z_bytes_duplicate(const _z_bytes_t *src); +void _z_bytes_move(_z_bytes_t *dst, _z_bytes_t *src); +void _z_bytes_clear(_z_bytes_t *bytes); +void _z_bytes_free(_z_bytes_t **bs); +uint8_t _z_bytes_to_uint8(const _z_bytes_t *bs); +uint16_t _z_bytes_to_uint16(const _z_bytes_t *bs); +uint32_t _z_bytes_to_uint32(const _z_bytes_t *bs); +uint64_t _z_bytes_to_uint64(const _z_bytes_t *bs); +float _z_bytes_to_float(const _z_bytes_t *bs); +double _z_bytes_to_double(const _z_bytes_t *bs); +_z_slice_t _z_bytes_to_slice(const _z_bytes_t *bytes); +_z_bytes_t _z_bytes_from_uint8(uint8_t val); +_z_bytes_t _z_bytes_from_uint16(uint16_t val); +_z_bytes_t _z_bytes_from_uint32(uint32_t val); +_z_bytes_t _z_bytes_from_uint64(uint64_t val); +_z_bytes_t _z_bytes_from_float(float val); +_z_bytes_t _z_bytes_from_double(double val); + +#endif /* ZENOH_PICO_COLLECTIONS_SLICE_H */ diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index 2c7f95bdd..e03139cfe 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -15,10 +15,10 @@ #define ZENOH_PICO_COLLECTIONS_STRING_H #include "zenoh-pico/collections/array.h" -#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/intmap.h" #include "zenoh-pico/collections/list.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/collections/vec.h" /*-------- str --------*/ @@ -81,8 +81,8 @@ 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_string_t _z_string_convert_bytes(const _z_slice_t *bs); +_z_string_t _z_string_from_bytes(const _z_slice_t *bs); _Z_ELEM_DEFINE(_z_string, _z_string_t, _z_string_size, _z_string_clear, _z_string_copy) diff --git a/include/zenoh-pico/link/link.h b/include/zenoh-pico/link/link.h index 75e2a02a7..fd4906daf 100644 --- a/include/zenoh-pico/link/link.h +++ b/include/zenoh-pico/link/link.h @@ -94,8 +94,8 @@ typedef int8_t (*_z_f_link_listen)(struct _z_link_t *self); typedef void (*_z_f_link_close)(struct _z_link_t *self); typedef size_t (*_z_f_link_write)(const struct _z_link_t *self, const uint8_t *ptr, size_t len); typedef size_t (*_z_f_link_write_all)(const struct _z_link_t *self, const uint8_t *ptr, size_t len); -typedef size_t (*_z_f_link_read)(const struct _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr); -typedef size_t (*_z_f_link_read_exact)(const struct _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr); +typedef size_t (*_z_f_link_read)(const struct _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr); +typedef size_t (*_z_f_link_read_exact)(const struct _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr); typedef void (*_z_f_link_free)(struct _z_link_t *self); typedef struct _z_link_t { @@ -141,7 +141,7 @@ int8_t _z_open_link(_z_link_t *zl, const char *locator); int8_t _z_listen_link(_z_link_t *zl, const char *locator); int8_t _z_link_send_wbuf(const _z_link_t *zl, const _z_wbuf_t *wbf); -size_t _z_link_recv_zbuf(const _z_link_t *zl, _z_zbuf_t *zbf, _z_bytes_t *addr); -size_t _z_link_recv_exact_zbuf(const _z_link_t *zl, _z_zbuf_t *zbf, size_t len, _z_bytes_t *addr); +size_t _z_link_recv_zbuf(const _z_link_t *zl, _z_zbuf_t *zbf, _z_slice_t *addr); +size_t _z_link_recv_exact_zbuf(const _z_link_t *zl, _z_zbuf_t *zbf, size_t len, _z_slice_t *addr); #endif /* ZENOH_PICO_LINK_H */ diff --git a/include/zenoh-pico/net/encoding.h b/include/zenoh-pico/net/encoding.h index eb71cc4aa..cb17a1622 100644 --- a/include/zenoh-pico/net/encoding.h +++ b/include/zenoh-pico/net/encoding.h @@ -15,13 +15,13 @@ #define ZENOH_PICO_ENCODING_NETAPI_H #include "zenoh-pico/api/constants.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" /** * A zenoh encoding. */ typedef struct _z_encoding_t { - _z_bytes_t schema; + _z_slice_t schema; uint16_t id; } _z_encoding_t; diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index bd62ea5b3..fb458f9a2 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -35,6 +35,7 @@ typedef struct _z_query_t { _Bool _anyke; } _z_query_t; +_z_query_t _z_query_null(void); void _z_query_clear(_z_query_t *q); _Z_REFCOUNT_DEFINE(_z_query, _z_query) @@ -47,7 +48,7 @@ typedef struct { } _z_queryable_t; #if Z_FEATURE_QUERYABLE == 1 -_z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, const _z_bytes_t *parameters, +_z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, const _z_slice_t *parameters, _z_session_t *zn, uint32_t request_id, z_attachment_t att); void _z_queryable_clear(_z_queryable_t *qbl); void _z_queryable_free(_z_queryable_t **qbl); diff --git a/include/zenoh-pico/net/reply.h b/include/zenoh-pico/net/reply.h index 78904dbfd..c240d3841 100644 --- a/include/zenoh-pico/net/reply.h +++ b/include/zenoh-pico/net/reply.h @@ -28,7 +28,7 @@ * * Members: * _z_sample_t data: a :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_slice_t replier_id: The id of the replier that sent this reply. * */ typedef struct _z_reply_data_t { @@ -61,7 +61,7 @@ _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, +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_slice_t *payload, const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, z_attachment_t att); diff --git a/include/zenoh-pico/net/sample.h b/include/zenoh-pico/net/sample.h index 36d9c54a5..30e7ac97a 100644 --- a/include/zenoh-pico/net/sample.h +++ b/include/zenoh-pico/net/sample.h @@ -25,7 +25,7 @@ * * Members: * _z_keyexpr_t key: The resource key of this data sample. - * _z_bytes_t value: The value of this data sample. + * _z_slice_t value: The value of this data sample. * _z_encoding_t encoding: The encoding for the value of this data sample. */ typedef struct _z_sample_t { @@ -58,7 +58,7 @@ void _z_sample_free(_z_sample_t **sample); void _z_sample_copy(_z_sample_t *dst, const _z_sample_t *src); _z_sample_t _z_sample_duplicate(const _z_sample_t *src); -_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t *payload, _z_timestamp_t timestamp, +_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_slice_t *payload, _z_timestamp_t timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, const _z_qos_t qos, const z_attachment_t att); diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index e2c1c3946..cfa32cbad 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -63,13 +63,15 @@ int8_t _z_zint32_decode(uint32_t *zint, _z_zbuf_t *buf); int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *buf); int8_t _z_zsize_decode(_z_zint_t *zint, _z_zbuf_t *buf); -int8_t _z_bytes_val_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); -int8_t _z_bytes_val_decode(_z_bytes_t *bs, _z_zbuf_t *buf); -int8_t _z_bytes_val_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf); - -int8_t _z_bytes_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); -size_t _z_bytes_encode_len(const _z_bytes_t *bs); -int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *buf); +int8_t _z_slice_val_encode(_z_wbuf_t *buf, const _z_slice_t *bs); +int8_t _z_slice_val_decode(_z_slice_t *bs, _z_zbuf_t *buf); +int8_t _z_slice_val_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf); + +int8_t _z_slice_encode(_z_wbuf_t *buf, const _z_slice_t *bs); +size_t _z_slice_encode_len(const _z_slice_t *bs); +int8_t _z_slice_decode(_z_slice_t *bs, _z_zbuf_t *buf); +int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf); +int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs); 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); diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 1b5119948..5354dc2e4 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -20,9 +20,9 @@ #include #include "zenoh-pico/api/constants.h" -#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/refcount.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" #include "zenoh-pico/net/encoding.h" @@ -74,7 +74,7 @@ typedef struct { * Returning `0` is treated as `continue`. * Returning any other value is treated as `break`. */ -typedef int8_t (*z_attachment_iter_body_t)(_z_bytes_t key, _z_bytes_t value, void *context); +typedef int8_t (*z_attachment_iter_body_t)(_z_slice_t key, _z_slice_t value, void *context); /** * The driver of a loop over an attachment's key-value pairs. * @@ -106,7 +106,7 @@ typedef struct z_attachment_t { typedef struct { union { z_attachment_t decoded; - _z_bytes_t encoded; + _z_slice_t encoded; } body; _Bool is_encoded; } _z_owned_encoded_attachment_t; @@ -118,7 +118,7 @@ z_attachment_t _z_encoded_as_attachment(const _z_owned_encoded_attachment_t *att _Bool z_attachment_check(const z_attachment_t *attachment); int8_t z_attachment_iterate(z_attachment_t this_, z_attachment_iter_body_t body, void *ctx); -_z_bytes_t z_attachment_get(z_attachment_t this_, _z_bytes_t key); +_z_slice_t z_attachment_get(z_attachment_t this_, _z_slice_t key); /** * Estimate the length of an attachment once encoded. @@ -223,8 +223,8 @@ typedef struct { * Represents a Zenoh value. * * Members: - * _z_encoding_t encoding: The encoding of the `payload`. * _z_bytes_t payload: The payload of this zenoh value. + * _z_encoding_t encoding: The encoding of the `payload`. */ typedef struct { _z_bytes_t payload; @@ -240,7 +240,7 @@ void _z_value_free(_z_value_t **hello); * A hello message returned by a zenoh entity to a scout message sent with :c:func:`_z_scout`. * * Members: - * _z_bytes_t zid: The Zenoh ID of the scouted entity (empty if absent). + * _z_slice_t zid: The Zenoh ID of the scouted entity (empty if absent). * _z_string_vec_t locators: The locators of the scouted entity. * z_whatami_t whatami: The kind of zenoh entity. */ diff --git a/include/zenoh-pico/protocol/definitions/message.h b/include/zenoh-pico/protocol/definitions/message.h index 700c63967..39d09dff8 100644 --- a/include/zenoh-pico/protocol/definitions/message.h +++ b/include/zenoh-pico/protocol/definitions/message.h @@ -55,7 +55,7 @@ typedef struct { _z_encoding_t encoding; _z_source_info_t _ext_source_info; - _z_bytes_t _payload; + _z_slice_t _payload; } _z_msg_err_t; void _z_msg_err_clear(_z_msg_err_t *err); @@ -73,7 +73,7 @@ static inline void _z_msg_del_clear(_z_msg_del_t *del) { (void)del; } typedef struct { _z_m_push_commons_t _commons; - _z_bytes_t _payload; + _z_slice_t _payload; _z_encoding_t _encoding; #if Z_FEATURE_ATTACHMENT == 1 _z_owned_encoded_attachment_t _attachment; @@ -98,7 +98,7 @@ void _z_msg_put_clear(_z_msg_put_t *); #define _Z_FLAG_Z_Q_C 0x20 // 1 << 5 | Consolidation if C==1 then consolidation is present #define _Z_FLAG_Z_Q_P 0x40 // 1 << 6 | Params if P==1 then parameters are present typedef struct { - _z_bytes_t _parameters; + _z_slice_t _parameters; _z_source_info_t _ext_info; _z_value_t _ext_value; z_consolidation_mode_t _consolidation; diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index b7ab81822..28e9f04b8 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -18,7 +18,7 @@ #include #include "zenoh-pico/api/constants.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/declarations.h" #include "zenoh-pico/protocol/definitions/interest.h" @@ -291,7 +291,7 @@ _Z_ELEM_DEFINE(_z_network_message, _z_network_message_t, _z_noop_size, _z_n_msg_ _Z_VEC_DEFINE(_z_network_message, _z_network_message_t) void _z_msg_fix_mapping(_z_zenoh_message_t *msg, uint16_t mapping); -_z_network_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes_t) parameters, _z_zint_t qid, +_z_network_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_slice_t) parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value, uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 diff --git a/include/zenoh-pico/protocol/definitions/transport.h b/include/zenoh-pico/protocol/definitions/transport.h index 3bcb5ce33..30c064585 100644 --- a/include/zenoh-pico/protocol/definitions/transport.h +++ b/include/zenoh-pico/protocol/definitions/transport.h @@ -305,7 +305,7 @@ void _z_t_msg_join_clear(_z_t_msg_join_t *msg); // typedef struct { _z_id_t _zid; - _z_bytes_t _cookie; + _z_slice_t _cookie; uint16_t _batch_size; z_whatami_t _whatami; uint8_t _req_id_res; @@ -348,7 +348,7 @@ void _z_t_msg_init_clear(_z_t_msg_init_t *msg); typedef struct { _z_zint_t _lease; _z_zint_t _initial_sn; - _z_bytes_t _cookie; + _z_slice_t _cookie; } _z_t_msg_open_t; void _z_t_msg_open_clear(_z_t_msg_open_t *msg); @@ -472,7 +472,7 @@ void _z_t_msg_frame_clear(_z_t_msg_frame_t *msg); // +---------------+ // typedef struct { - _z_bytes_t _payload; + _z_slice_t _payload; _z_zint_t _sn; } _z_t_msg_fragment_t; void _z_t_msg_fragment_clear(_z_t_msg_fragment_t *msg); @@ -500,15 +500,15 @@ void _z_t_msg_clear(_z_transport_message_t *msg); _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_id_t zid, _z_conduit_sn_list_t next_sn); _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid); -_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, _z_bytes_t cookie); -_z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie); +_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, _z_slice_t cookie); +_z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_slice_t cookie); _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn); _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only); _z_transport_message_t _z_t_msg_make_keep_alive(void); _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_network_message_vec_t messages, _Bool is_reliable); _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable); _z_transport_message_t _z_t_msg_make_fragment_header(_z_zint_t sn, _Bool is_reliable, _Bool is_last); -_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_bytes_t messages, _Bool is_reliable, _Bool is_last); +_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_slice_t messages, _Bool is_reliable, _Bool is_last); /*------------------ Copy ------------------*/ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg); diff --git a/include/zenoh-pico/protocol/ext.h b/include/zenoh-pico/protocol/ext.h index 118c1ed14..cba1ab55d 100644 --- a/include/zenoh-pico/protocol/ext.h +++ b/include/zenoh-pico/protocol/ext.h @@ -68,7 +68,7 @@ void _z_msg_ext_clear_zint(_z_msg_ext_zint_t *ext); /*------------------ Unknown Extension ------------------*/ typedef struct { - _z_bytes_t _val; + _z_slice_t _val; } _z_msg_ext_zbuf_t; void _z_msg_ext_clear_zbuf(_z_msg_ext_zbuf_t *ext); @@ -88,7 +88,7 @@ void _z_msg_ext_clear(_z_msg_ext_t *ext); /*------------------ Builders ------------------*/ _z_msg_ext_t _z_msg_ext_make_unit(uint8_t id); _z_msg_ext_t _z_msg_ext_make_zint(uint8_t id, _z_zint_t zid); -_z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, _z_bytes_t zbuf); +_z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, _z_slice_t zbuf); /*------------------ Copy ------------------*/ void _z_msg_ext_copy(_z_msg_ext_t *clone, const _z_msg_ext_t *ext); diff --git a/include/zenoh-pico/protocol/iobuf.h b/include/zenoh-pico/protocol/iobuf.h index 90af275fe..1f29a9b56 100644 --- a/include/zenoh-pico/protocol/iobuf.h +++ b/include/zenoh-pico/protocol/iobuf.h @@ -19,8 +19,8 @@ #include #include -#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/element.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/collections/vec.h" /*------------------ IOSli ------------------*/ @@ -48,7 +48,7 @@ void _z_iosli_write_bytes(_z_iosli_t *ios, const uint8_t *bs, size_t offset, siz void _z_iosli_put(_z_iosli_t *ios, uint8_t b, size_t pos); void _z_iosli_reset(_z_iosli_t *ios); -_z_bytes_t _z_iosli_to_bytes(const _z_iosli_t *ios); +_z_slice_t _z_iosli_to_bytes(const _z_iosli_t *ios); size_t _z_iosli_size(const _z_iosli_t *ios); void _z_iosli_clear(_z_iosli_t *ios); @@ -67,7 +67,7 @@ typedef struct { _z_zbuf_t _z_zbuf_make(size_t capacity); _z_zbuf_t _z_zbuf_view(_z_zbuf_t *zbf, size_t length); /// Constructs a _borrowing_ reader on `slice` -_z_zbuf_t _z_zbytes_as_zbuf(_z_bytes_t slice); +_z_zbuf_t _z_slice_as_zbuf(_z_slice_t slice); size_t _z_zbuf_capacity(const _z_zbuf_t *zbf); uint8_t const *_z_zbuf_start(const _z_zbuf_t *zbf); diff --git a/include/zenoh-pico/session/subscription.h b/include/zenoh-pico/session/subscription.h index d15a60cf0..a85dac0a7 100644 --- a/include/zenoh-pico/session/subscription.h +++ b/include/zenoh-pico/session/subscription.h @@ -27,7 +27,7 @@ _z_subscription_rc_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_loc _z_subscription_rc_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *sub); -int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t payload, +int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_slice_t payload, const _z_encoding_t encoding, const _z_zint_t kind, const _z_timestamp_t timestamp, const _z_n_qos_t qos, const z_attachment_t att); void _z_unregister_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_rc_t *sub); diff --git a/include/zenoh-pico/system/link/raweth.h b/include/zenoh-pico/system/link/raweth.h index ce956c835..418107f23 100644 --- a/include/zenoh-pico/system/link/raweth.h +++ b/include/zenoh-pico/system/link/raweth.h @@ -84,7 +84,7 @@ typedef struct { int8_t _z_open_raweth(_z_sys_net_socket_t *sock, const char *interface); size_t _z_send_raweth(const _z_sys_net_socket_t *sock, const void *buff, size_t buff_len); -size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr, +size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_slice_t *addr, const _zp_raweth_whitelist_array_t *whitelist); int8_t _z_close_raweth(_z_sys_net_socket_t *sock); size_t _z_raweth_ntohs(size_t val); diff --git a/include/zenoh-pico/system/link/udp.h b/include/zenoh-pico/system/link/udp.h index 794cff091..e9f17acb9 100644 --- a/include/zenoh-pico/system/link/udp.h +++ b/include/zenoh-pico/system/link/udp.h @@ -49,9 +49,9 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *socksend, const _z_sys_net_endpoint_t rep, const _z_sys_net_endpoint_t lep); size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, - const _z_sys_net_endpoint_t lep, _z_bytes_t *ep); + const _z_sys_net_endpoint_t lep, _z_slice_t *ep); size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep, - _z_bytes_t *ep); + _z_slice_t *ep); size_t _z_send_udp_multicast(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t rep); #endif diff --git a/include/zenoh-pico/transport/manager.h b/include/zenoh-pico/transport/manager.h index faac8c25e..f817c2494 100644 --- a/include/zenoh-pico/transport/manager.h +++ b/include/zenoh-pico/transport/manager.h @@ -15,7 +15,7 @@ #ifndef INCLUDE_ZENOH_PICO_TRANSPORT_MANAGER_H #define INCLUDE_ZENOH_PICO_TRANSPORT_MANAGER_H -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/link/manager.h" #include "zenoh-pico/transport/transport.h" diff --git a/include/zenoh-pico/transport/multicast/rx.h b/include/zenoh-pico/transport/multicast/rx.h index 5a74d11cf..8357e8291 100644 --- a/include/zenoh-pico/transport/multicast/rx.h +++ b/include/zenoh-pico/transport/multicast/rx.h @@ -17,8 +17,8 @@ #include "zenoh-pico/transport/transport.h" -int8_t _z_multicast_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_bytes_t *addr); +int8_t _z_multicast_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_slice_t *addr); int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, - _z_bytes_t *addr); + _z_slice_t *addr); #endif /* ZENOH_PICO_TRANSPORT_LINK_RX_H */ diff --git a/include/zenoh-pico/transport/raweth/rx.h b/include/zenoh-pico/transport/raweth/rx.h index a8f281369..4484a2e43 100644 --- a/include/zenoh-pico/transport/raweth/rx.h +++ b/include/zenoh-pico/transport/raweth/rx.h @@ -17,7 +17,7 @@ #include "zenoh-pico/transport/transport.h" -int8_t _z_raweth_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_bytes_t *addr); -int8_t _z_raweth_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_bytes_t *addr); +int8_t _z_raweth_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_slice_t *addr); +int8_t _z_raweth_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_slice_t *addr); #endif /* ZENOH_PICO_RAWETH_RX_H */ diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index 76921bd9c..d00c3c8de 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -18,8 +18,8 @@ #include #include -#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/element.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/config.h" #include "zenoh-pico/link/link.h" #include "zenoh-pico/protocol/core.h" @@ -33,7 +33,7 @@ typedef struct { #endif _z_id_t _remote_zid; - _z_bytes_t _remote_addr; + _z_slice_t _remote_addr; _z_conduit_sn_list_t _sn_rx_sns; // SN numbers diff --git a/include/zenoh-pico/utils/uuid.h b/include/zenoh-pico/utils/uuid.h index b0e487235..4bd581a91 100644 --- a/include/zenoh-pico/utils/uuid.h +++ b/include/zenoh-pico/utils/uuid.h @@ -22,14 +22,3 @@ * uuid_str: A valid UUID string. */ void _z_uuid_to_bytes(uint8_t *bytes, const char *uuid_str); - -/** - * Converts a byte array representing an UUID to its string representation. - * - * Parameters: - * bytes: Pointer to the byte array containing the UUID. - * - * Returns: - * The `char *` of the UUID in string format. - */ -char *_z_bytes_to_uuid(const uint8_t *uuid); diff --git a/src/api/api.c b/src/api/api.c index f2bed7e36..29b7d23d2 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -20,7 +20,7 @@ #include "zenoh-pico/api/primitives.h" #include "zenoh-pico/api/types.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/config.h" #include "zenoh-pico/net/config.h" #include "zenoh-pico/net/filtering.h" @@ -219,7 +219,6 @@ int8_t zp_scouting_config_insert(z_loaned_scouting_config_t *sc, uint8_t key, co return _zp_config_insert(sc, key, value); } -// 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)); @@ -239,8 +238,8 @@ 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); + if (!_z_slice_is_empty(&encoding->_val->schema)) { + _z_slice_clear(&encoding->_val->schema); } z_free(encoding->_val); } @@ -260,7 +259,71 @@ static _z_encoding_t _z_encoding_from_owned(const z_owned_encoding_t *encoding) 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; } +const uint8_t *z_slice_data(const z_loaned_slice_t *slice) { return slice->start; } + +size_t z_slice_len(const z_loaned_slice_t *slice) { return slice->len; } + +int8_t z_bytes_decode_into_int8(const z_loaned_bytes_t *bytes, int8_t *dst) { + *dst = (int8_t)_z_bytes_to_uint8(bytes); + return _Z_RES_OK; +} + +int8_t z_bytes_decode_into_int16(const z_loaned_bytes_t *bytes, int16_t *dst) { + *dst = (int16_t)_z_bytes_to_uint16(bytes); + return _Z_RES_OK; +} + +int8_t z_bytes_decode_into_int32(const z_loaned_bytes_t *bytes, int32_t *dst) { + *dst = (int32_t)_z_bytes_to_uint32(bytes); + return _Z_RES_OK; +} + +int8_t z_bytes_decode_into_int64(const z_loaned_bytes_t *bytes, int64_t *dst) { + *dst = (int64_t)_z_bytes_to_uint64(bytes); + return _Z_RES_OK; +} + +int8_t z_bytes_decode_into_uint8(const z_loaned_bytes_t *bytes, uint8_t *dst) { + *dst = _z_bytes_to_uint8(bytes); + return _Z_RES_OK; +} + +int8_t z_bytes_decode_into_uint16(const z_loaned_bytes_t *bytes, uint16_t *dst) { + *dst = _z_bytes_to_uint16(bytes); + return _Z_RES_OK; +} + +int8_t z_bytes_decode_into_uint32(const z_loaned_bytes_t *bytes, uint32_t *dst) { + *dst = _z_bytes_to_uint32(bytes); + return _Z_RES_OK; +} + +int8_t z_bytes_decode_into_uint64(const z_loaned_bytes_t *bytes, uint64_t *dst) { + *dst = _z_bytes_to_uint64(bytes); + return _Z_RES_OK; +} + +int8_t z_bytes_decode_into_float(const z_loaned_bytes_t *bytes, float *dst) { + *dst = _z_bytes_to_float(bytes); + return _Z_RES_OK; +} + +int8_t z_bytes_decode_into_double(const z_loaned_bytes_t *bytes, double *dst) { + *dst = _z_bytes_to_double(bytes); + return _Z_RES_OK; +} + +int8_t z_bytes_decode_into_slice(const z_loaned_bytes_t *bytes, z_owned_slice_t *dst) { + // Init owned slice + z_slice_null(dst); + dst->_val = (_z_slice_t *)z_malloc(sizeof(_z_slice_t)); + if (dst->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Convert bytes to slice + *dst->_val = _z_bytes_to_slice(bytes); + return _Z_RES_OK; +} int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *s) { // Init owned string @@ -270,19 +333,173 @@ int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_ return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Convert bytes to string - *s->_val = _z_string_from_bytes(bytes); + *s->_val = _z_string_from_bytes(&bytes->_slice); + return _Z_RES_OK; +} + +int8_t z_bytes_encode_from_int8(z_owned_bytes_t *bytes, int8_t val) { + return z_bytes_encode_from_uint8(bytes, (uint8_t)val); +} + +int8_t z_bytes_encode_from_int16(z_owned_bytes_t *bytes, int16_t val) { + return z_bytes_encode_from_uint16(bytes, (uint16_t)val); +} + +int8_t z_bytes_encode_from_int32(z_owned_bytes_t *bytes, int32_t val) { + return z_bytes_encode_from_uint32(bytes, (uint32_t)val); +} + +int8_t z_bytes_encode_from_int64(z_owned_bytes_t *bytes, int64_t val) { + return z_bytes_encode_from_uint64(bytes, (uint64_t)val); +} + +int8_t z_bytes_encode_from_uint8(z_owned_bytes_t *bytes, uint8_t val) { + // Init owned bytes + z_bytes_null(bytes); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Encode data + *bytes->_val = _z_bytes_from_uint8(val); + if (!_z_bytes_check(*bytes->_val)) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + return _Z_RES_OK; +} + +int8_t z_bytes_encode_from_uint16(z_owned_bytes_t *bytes, uint16_t val) { + // Init owned bytes + z_bytes_null(bytes); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Encode data + *bytes->_val = _z_bytes_from_uint16(val); + if (!_z_bytes_check(*bytes->_val)) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + return _Z_RES_OK; +} + +int8_t z_bytes_encode_from_uint32(z_owned_bytes_t *bytes, uint32_t val) { + // Init owned bytes + z_bytes_null(bytes); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Encode data + *bytes->_val = _z_bytes_from_uint32(val); + if (!_z_bytes_check(*bytes->_val)) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + return _Z_RES_OK; +} + +int8_t z_bytes_encode_from_uint64(z_owned_bytes_t *bytes, uint64_t val) { + // Init owned bytes + z_bytes_null(bytes); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Encode data + *bytes->_val = _z_bytes_from_uint64(val); + if (!_z_bytes_check(*bytes->_val)) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + return _Z_RES_OK; +} + +int8_t z_bytes_encode_from_float(z_owned_bytes_t *bytes, float val) { + // Init owned bytes + z_bytes_null(bytes); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Encode data + *bytes->_val = _z_bytes_from_float(val); + if (!_z_bytes_check(*bytes->_val)) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + return _Z_RES_OK; +} + +int8_t z_bytes_encode_from_double(z_owned_bytes_t *bytes, double val) { + // Init owned bytes + z_bytes_null(bytes); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Encode data + *bytes->_val = _z_bytes_from_double(val); + if (!_z_bytes_check(*bytes->_val)) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } return _Z_RES_OK; } -int8_t z_bytes_encode_from_string(z_owned_bytes_t *buffer, const z_loaned_string_t *s) { +int8_t z_bytes_encode_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { // Init owned bytes - z_bytes_null(buffer); - buffer->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (buffer->_val == NULL) { + z_bytes_null(bytes); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - *buffer->_val = _z_bytes_wrap((uint8_t *)s->val, s->len); + bytes->_val->_slice = _z_slice_wrap((uint8_t *)data, len); + return _Z_RES_OK; +} + +int8_t z_bytes_encode_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { + // Init owned bytes + z_bytes_null(bytes); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Allocate bytes + *bytes->_val = _z_bytes_make(len); + if (!_z_bytes_check(*bytes->_val)) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Copy data + memcpy((uint8_t *)bytes->_val->_slice.start, data, len); + return _Z_RES_OK; +} + +int8_t z_bytes_encode_from_string(z_owned_bytes_t *bytes, const char *s) { + // Init owned bytes + z_bytes_null(bytes); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Encode string without null terminator + size_t len = strlen(s); + bytes->_val->_slice = _z_slice_wrap((uint8_t *)s, len); + return _Z_RES_OK; +} + +int8_t z_bytes_encode_from_string_copy(z_owned_bytes_t *bytes, const char *s) { + // Init owned bytes + z_bytes_null(bytes); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Allocate bytes + size_t len = strlen(s); + *bytes->_val = _z_bytes_make(len); + if (!_z_bytes_check(*bytes->_val)) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Copy string without null terminator + memcpy((uint8_t *)bytes->_val->_slice.start, s, len); return _Z_RES_OK; } @@ -434,12 +651,13 @@ VIEW_FUNCTIONS_PTR(_z_string_t, string) OWNED_FUNCTIONS_PTR(_z_hello_t, hello, _z_owner_noop_copy, _z_hello_free) OWNED_FUNCTIONS_PTR(_z_string_vec_t, string_array, _z_owner_noop_copy, _z_string_vec_free) VIEW_FUNCTIONS_PTR(_z_string_vec_t, string_array) +OWNED_FUNCTIONS_PTR(_z_slice_t, slice, _z_slice_copy, _z_slice_free) 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(); + _z_bytes_t b = _z_bytes_null(); if ((bytes != NULL) && (bytes->_val != NULL)) { - b = _z_bytes_wrap(bytes->_val->start, bytes->_val->len); + b._slice = _z_slice_wrap(bytes->_val->_slice.start, bytes->_val->_slice.len); } return b; } @@ -626,6 +844,7 @@ z_attachment_t z_sample_attachment(const z_loaned_sample_t *sample) { return _Z_ #endif const char *z_string_data(const z_loaned_string_t *str) { return str->val; } +size_t z_string_len(const z_loaned_string_t *str) { return str->len; } #if Z_FEATURE_PUBLICATION == 1 int8_t _z_publisher_drop(_z_publisher_t **pub) { @@ -1188,9 +1407,9 @@ int8_t zp_send_join(const z_loaned_session_t *zs, const zp_send_join_options_t * 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_) { - _z_bytes_clear(&this_->key); - _z_bytes_clear(&this_->value); +void _z_slice_pair_clear(struct _z_slice_pair_t *this_) { + _z_slice_clear(&this_->key); + _z_slice_clear(&this_->value); } z_attachment_t z_bytes_map_as_attachment(const z_owned_bytes_map_t *this_) { @@ -1200,13 +1419,13 @@ z_attachment_t z_bytes_map_as_attachment(const z_owned_bytes_map_t *this_) { return (z_attachment_t){.data = this_, .iteration_driver = (z_attachment_iter_driver_t)z_bytes_map_iter}; } 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); } +void z_bytes_map_drop(z_owned_bytes_map_t *this_) { _z_slice_pair_list_free(&this_->_inner); } -int8_t _z_bytes_map_insert_by_alias(z_loaned_bytes_t *key, z_loaned_bytes_t *value, void *this_) { +int8_t _z_slice_map_insert_by_alias(z_loaned_slice_t *key, z_loaned_slice_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_loaned_bytes_t *key, z_loaned_bytes_t *value, void *this_) { +int8_t _z_slice_map_insert_by_copy(z_loaned_slice_t *key, z_loaned_slice_t *value, void *this_) { z_bytes_map_insert_by_copy((z_owned_bytes_map_t *)this_, key, value); return 0; } @@ -1215,7 +1434,7 @@ z_owned_bytes_map_t z_bytes_map_from_attachment(z_attachment_t this_) { return z_bytes_map_null(); } z_owned_bytes_map_t map = z_bytes_map_new(); - z_attachment_iterate(this_, _z_bytes_map_insert_by_copy, &map); + z_attachment_iterate(this_, _z_slice_map_insert_by_copy, &map); return map; } z_owned_bytes_map_t z_bytes_map_from_attachment_aliasing(z_attachment_t this_) { @@ -1223,81 +1442,81 @@ z_owned_bytes_map_t z_bytes_map_from_attachment_aliasing(z_attachment_t this_) { return z_bytes_map_null(); } z_owned_bytes_map_t map = z_bytes_map_new(); - z_attachment_iterate(this_, _z_bytes_map_insert_by_alias, &map); + z_attachment_iterate(this_, _z_slice_map_insert_by_alias, &map); return map; } -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; +z_loaned_slice_t *z_bytes_map_get(const z_owned_bytes_map_t *this_, z_loaned_slice_t *key) { + _z_slice_pair_list_t *current = this_->_inner; while (current) { - struct _z_bytes_pair_t *head = _z_bytes_pair_list_head(current); - if (_z_bytes_eq(&key, &head->key)) { - return _z_bytes_wrap(head->value.start, head->value.len); + struct _z_slice_pair_t *head = _z_slice_pair_list_head(current); + if (_z_slice_eq(&key, &head->key)) { + return _z_slice_wrap(head->value.start, head->value.len); } } - return z_bytes_null(); + return z_slice_null(); } -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; +void z_bytes_map_insert_by_alias(const z_owned_bytes_map_t *this_, z_loaned_slice_t *key, z_loaned_slice_t *value) { + _z_slice_pair_list_t *current = this_->_inner; while (current) { - struct _z_bytes_pair_t *head = _z_bytes_pair_list_head(current); - if (_z_bytes_eq(&key, &head->key)) { + struct _z_slice_pair_t *head = _z_slice_pair_list_head(current); + if (_z_slice_eq(&key, &head->key)) { break; } - current = _z_bytes_pair_list_tail(current); + current = _z_slice_pair_list_tail(current); } if (current) { - struct _z_bytes_pair_t *head = _z_bytes_pair_list_head(current); - _z_bytes_clear(&head->value); - head->value = _z_bytes_wrap(value.start, value.len); + struct _z_slice_pair_t *head = _z_slice_pair_list_head(current); + _z_slice_clear(&head->value); + head->value = _z_slice_wrap(value.start, value.len); } else { - struct _z_bytes_pair_t *insert = z_malloc(sizeof(struct _z_bytes_pair_t)); - memset(insert, 0, sizeof(struct _z_bytes_pair_t)); - insert->key = _z_bytes_wrap(key.start, key.len); - insert->value = _z_bytes_wrap(value.start, value.len); - ((z_owned_bytes_map_t *)this_)->_inner = _z_bytes_pair_list_push(this_->_inner, insert); + struct _z_slice_pair_t *insert = z_malloc(sizeof(struct _z_slice_pair_t)); + memset(insert, 0, sizeof(struct _z_slice_pair_t)); + insert->key = _z_slice_wrap(key.start, key.len); + insert->value = _z_slice_wrap(value.start, value.len); + ((z_owned_bytes_map_t *)this_)->_inner = _z_slice_pair_list_push(this_->_inner, insert); } } -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; +void z_bytes_map_insert_by_copy(const z_owned_bytes_map_t *this_, z_loaned_slice_t *key, z_loaned_slice_t *value) { + _z_slice_pair_list_t *current = this_->_inner; while (current) { - struct _z_bytes_pair_t *head = _z_bytes_pair_list_head(current); - if (_z_bytes_eq(&key, &head->key)) { + struct _z_slice_pair_t *head = _z_slice_pair_list_head(current); + if (_z_slice_eq(&key, &head->key)) { break; } - current = _z_bytes_pair_list_tail(current); + current = _z_slice_pair_list_tail(current); } if (current) { - struct _z_bytes_pair_t *head = _z_bytes_pair_list_head(current); - _z_bytes_clear(&head->value); - _z_bytes_copy(&head->value, &value); + struct _z_slice_pair_t *head = _z_slice_pair_list_head(current); + _z_slice_clear(&head->value); + _z_slice_copy(&head->value, &value); if (!head->key._is_alloc) { - _z_bytes_copy(&head->key, &key); + _z_slice_copy(&head->key, &key); } } else { - struct _z_bytes_pair_t *insert = z_malloc(sizeof(struct _z_bytes_pair_t)); - memset(insert, 0, sizeof(struct _z_bytes_pair_t)); - _z_bytes_copy(&insert->key, &key); - _z_bytes_copy(&insert->value, &value); - ((z_owned_bytes_map_t *)this_)->_inner = _z_bytes_pair_list_push(this_->_inner, insert); + struct _z_slice_pair_t *insert = z_malloc(sizeof(struct _z_slice_pair_t)); + memset(insert, 0, sizeof(struct _z_slice_pair_t)); + _z_slice_copy(&insert->key, &key); + _z_slice_copy(&insert->value, &value); + ((z_owned_bytes_map_t *)this_)->_inner = _z_slice_pair_list_push(this_->_inner, insert); } } int8_t z_bytes_map_iter(const z_owned_bytes_map_t *this_, z_attachment_iter_body_t body, void *ctx) { - _z_bytes_pair_list_t *current = this_->_inner; + _z_slice_pair_list_t *current = this_->_inner; while (current) { - struct _z_bytes_pair_t *head = _z_bytes_pair_list_head(current); + struct _z_slice_pair_t *head = _z_slice_pair_list_head(current); int8_t ret = body(head->key, head->value, ctx); if (ret) { return ret; } - current = _z_bytes_pair_list_tail(current); + current = _z_slice_pair_list_tail(current); } return 0; } -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_new(void) { return (z_owned_bytes_map_t){._inner = _z_slice_pair_list_new()}; } z_owned_bytes_map_t z_bytes_map_null(void) { return (z_owned_bytes_map_t){._inner = 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)); +int8_t z_bytes_from_str(z_owned_slice_t *bytes const char *str) { + bytes->_val = _z_slice_wrap((const uint8_t *)str, strlen(str)); return _Z_RES_OK; } #endif diff --git a/src/collections/bytes.c b/src/collections/bytes.c deleted file mode 100644 index 82ec1903e..000000000 --- a/src/collections/bytes.c +++ /dev/null @@ -1,110 +0,0 @@ -// -// Copyright (c) 2022 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -#include "zenoh-pico/collections/bytes.h" - -#include -#include - -#include "zenoh-pico/system/platform.h" -#include "zenoh-pico/utils/result.h" - -/*-------- bytes --------*/ -_z_bytes_t _z_bytes_empty(void) { return (_z_bytes_t){.start = NULL, .len = 0, ._is_alloc = false}; } - -int8_t _z_bytes_init(_z_bytes_t *bs, size_t capacity) { - int8_t ret = _Z_RES_OK; - - bs->start = (uint8_t *)z_malloc(capacity); - if (bs->start != NULL) { - bs->len = capacity; - bs->_is_alloc = true; - } else { - bs->len = 0; - bs->_is_alloc = false; - ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - - return ret; -} - -_z_bytes_t _z_bytes_make(size_t capacity) { - _z_bytes_t bs; - (void)_z_bytes_init(&bs, capacity); - return bs; -} - -_z_bytes_t _z_bytes_wrap(const uint8_t *p, size_t len) { - _z_bytes_t bs; - bs.start = p; - bs.len = len; - bs._is_alloc = false; - return bs; -} - -void _z_bytes_reset(_z_bytes_t *bs) { - bs->start = NULL; - bs->len = 0; - bs->_is_alloc = false; -} - -void _z_bytes_clear(_z_bytes_t *bs) { - if ((bs->_is_alloc == true) && (bs->start != NULL)) { - z_free((uint8_t *)bs->start); - } - _z_bytes_reset(bs); -} - -void _z_bytes_free(_z_bytes_t **bs) { - _z_bytes_t *ptr = *bs; - - if (ptr != NULL) { - _z_bytes_clear(ptr); - - z_free(ptr); - *bs = NULL; - } -} - -void _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src) { - int8_t ret = - _z_bytes_init(dst, src->len); // FIXME: it should check if dst is already initialized. Otherwise it will leak - if (ret == _Z_RES_OK) { - (void)memcpy((uint8_t *)dst->start, src->start, src->len); - } -} - -void _z_bytes_move(_z_bytes_t *dst, _z_bytes_t *src) { - dst->start = src->start; - dst->len = src->len; - dst->_is_alloc = src->_is_alloc; - - _z_bytes_reset(src); -} - -_z_bytes_t _z_bytes_duplicate(const _z_bytes_t *src) { - _z_bytes_t dst = _z_bytes_empty(); - _z_bytes_copy(&dst, src); - return dst; -} - -_Bool _z_bytes_is_empty(const _z_bytes_t *bs) { return bs->len == 0; } -_z_bytes_t _z_bytes_steal(_z_bytes_t *b) { - _z_bytes_t ret = *b; - *b = _z_bytes_empty(); - return ret; -} -_Bool _z_bytes_eq(const _z_bytes_t *left, const _z_bytes_t *right) { - return left->len == right->len && memcmp(left->start, right->start, left->len) == 0; -} diff --git a/src/collections/slice.c b/src/collections/slice.c new file mode 100644 index 000000000..e9e24dbc2 --- /dev/null +++ b/src/collections/slice.c @@ -0,0 +1,270 @@ +// +// Copyright (c) 2022 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#include "zenoh-pico/collections/slice.h" + +#include +#include + +#include "zenoh-pico/system/platform.h" +#include "zenoh-pico/utils/result.h" + +/*-------- Slice --------*/ +_z_slice_t _z_slice_empty(void) { return (_z_slice_t){.start = NULL, .len = 0, ._is_alloc = false}; } + +int8_t _z_slice_init(_z_slice_t *bs, size_t capacity) { + int8_t ret = _Z_RES_OK; + + bs->start = (uint8_t *)z_malloc(capacity); + if (bs->start != NULL) { + bs->len = capacity; + bs->_is_alloc = true; + } else { + bs->len = 0; + bs->_is_alloc = false; + ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + + return ret; +} + +_z_slice_t _z_slice_make(size_t capacity) { + _z_slice_t bs; + (void)_z_slice_init(&bs, capacity); + return bs; +} + +_z_slice_t _z_slice_wrap(const uint8_t *p, size_t len) { + _z_slice_t bs; + bs.start = p; + bs.len = len; + bs._is_alloc = false; + return bs; +} + +void _z_slice_reset(_z_slice_t *bs) { + bs->start = NULL; + bs->len = 0; + bs->_is_alloc = false; +} + +void _z_slice_clear(_z_slice_t *bs) { + if ((bs->_is_alloc == true) && (bs->start != NULL)) { + z_free((uint8_t *)bs->start); + } + _z_slice_reset(bs); +} + +void _z_slice_free(_z_slice_t **bs) { + _z_slice_t *ptr = *bs; + + if (ptr != NULL) { + _z_slice_clear(ptr); + + z_free(ptr); + *bs = NULL; + } +} + +void _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src) { + int8_t ret = + _z_slice_init(dst, src->len); // FIXME: it should check if dst is already initialized. Otherwise it will leak + if (ret == _Z_RES_OK) { + (void)memcpy((uint8_t *)dst->start, src->start, src->len); + } +} + +void _z_slice_move(_z_slice_t *dst, _z_slice_t *src) { + dst->start = src->start; + dst->len = src->len; + dst->_is_alloc = src->_is_alloc; + + _z_slice_reset(src); +} + +_z_slice_t _z_slice_duplicate(const _z_slice_t *src) { + _z_slice_t dst = _z_slice_empty(); + _z_slice_copy(&dst, src); + return dst; +} + +_Bool _z_slice_is_empty(const _z_slice_t *bs) { return bs->len == 0; } + +_z_slice_t _z_slice_steal(_z_slice_t *b) { + _z_slice_t ret = *b; + *b = _z_slice_empty(); + return ret; +} +_Bool _z_slice_eq(const _z_slice_t *left, const _z_slice_t *right) { + return left->len == right->len && memcmp(left->start, right->start, left->len) == 0; +} + +/*-------- Bytes --------*/ +_Bool _z_bytes_check(_z_bytes_t bytes) { return _z_slice_check(bytes._slice); } + +_z_bytes_t _z_bytes_null(void) { + return (_z_bytes_t){ + ._slice = _z_slice_empty(), + }; +} + +_z_bytes_t _z_bytes_make(size_t capacity) { + return (_z_bytes_t){ + ._slice = _z_slice_make(capacity), + }; +} + +void _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src) { + // Init only if needed + if (!_z_slice_check(dst->_slice)) { + if (_z_slice_init(&dst->_slice, src->_slice.len) != _Z_RES_OK) { + return; + } + } + (void)memcpy((uint8_t *)dst->_slice.start, src->_slice.start, src->_slice.len); +} + +_z_bytes_t _z_bytes_duplicate(const _z_bytes_t *src) { + _z_bytes_t dst = _z_bytes_null(); + _z_bytes_copy(&dst, src); + return dst; +} + +void _z_bytes_move(_z_bytes_t *dst, _z_bytes_t *src) { _z_slice_move(&dst->_slice, &src->_slice); } + +void _z_bytes_clear(_z_bytes_t *bytes) { _z_slice_clear(&bytes->_slice); } + +void _z_bytes_free(_z_bytes_t **bs) { + _z_bytes_t *ptr = *bs; + + if (ptr != NULL) { + _z_bytes_clear(ptr); + + z_free(ptr); + *bs = NULL; + } +} + +uint8_t _z_bytes_to_uint8(const _z_bytes_t *bs) { + uint8_t val = 0; + memcpy(&val, bs->_slice.start, sizeof(val)); + return val; +} + +uint16_t _z_bytes_to_uint16(const _z_bytes_t *bs) { + uint16_t val = 0; + memcpy(&val, bs->_slice.start, sizeof(val)); + return val; +} + +uint32_t _z_bytes_to_uint32(const _z_bytes_t *bs) { + uint32_t val = 0; + memcpy(&val, bs->_slice.start, sizeof(val)); + return val; +} + +uint64_t _z_bytes_to_uint64(const _z_bytes_t *bs) { + uint64_t val = 0; + memcpy(&val, bs->_slice.start, sizeof(val)); + return val; +} + +float _z_bytes_to_float(const _z_bytes_t *bs) { + float val = 0; + memcpy(&val, bs->_slice.start, sizeof(val)); + return val; +} + +double _z_bytes_to_double(const _z_bytes_t *bs) { + double val = 0; + memcpy(&val, bs->_slice.start, sizeof(val)); + return val; +} + +_z_slice_t _z_bytes_to_slice(const _z_bytes_t *bytes) { + // Allocate slice + _z_slice_t ret = _z_slice_make(bytes->_slice.len); + if (!_z_slice_check(ret)) { + return ret; + } + // Recopy data + memcpy((uint8_t *)ret.start, bytes->_slice.start, bytes->_slice.len); + return ret; +} + +_z_bytes_t _z_bytes_from_uint8(uint8_t val) { + _z_bytes_t ret = _z_bytes_null(); + // Init bytes array + if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { + return ret; + } + // Encode int + memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); + return ret; +} + +_z_bytes_t _z_bytes_from_uint16(uint16_t val) { + _z_bytes_t ret = _z_bytes_null(); + // Init bytes array + if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { + return ret; + } + // Encode int + memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); + return ret; +} + +_z_bytes_t _z_bytes_from_uint32(uint32_t val) { + _z_bytes_t ret = _z_bytes_null(); + // Init bytes array + if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { + return ret; + } + // Encode int + memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); + return ret; +} + +_z_bytes_t _z_bytes_from_uint64(uint64_t val) { + _z_bytes_t ret = _z_bytes_null(); + // Init bytes array + if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { + return ret; + } + // Encode int + memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); + return ret; +} + +_z_bytes_t _z_bytes_from_float(float val) { + _z_bytes_t ret = _z_bytes_null(); + // Init bytes array + if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { + return ret; + } + // Encode float + memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); + return ret; +} + +_z_bytes_t _z_bytes_from_double(double val) { + _z_bytes_t ret = _z_bytes_null(); + // Init bytes array + if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { + return ret; + } + // Encode double + memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); + return ret; +} \ No newline at end of file diff --git a/src/collections/string.c b/src/collections/string.c index 1f25c985a..47c3609ad 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -88,7 +88,7 @@ void _z_string_free(_z_string_t **str) { } } -_z_string_t _z_string_convert_bytes(const _z_bytes_t *bs) { +_z_string_t _z_string_convert_bytes(const _z_slice_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)); @@ -110,7 +110,7 @@ _z_string_t _z_string_convert_bytes(const _z_bytes_t *bs) { return s; } -_z_string_t _z_string_from_bytes(const _z_bytes_t *bs) { +_z_string_t _z_string_from_bytes(const _z_slice_t *bs) { _z_string_t s = _z_string_null(); // Allocate string s.len = bs->len + (size_t)1; // bytes data + null terminator diff --git a/src/link/link.c b/src/link/link.c index 91d484594..b00a20211 100644 --- a/src/link/link.c +++ b/src/link/link.c @@ -135,7 +135,7 @@ void _z_link_free(_z_link_t **l) { } } -size_t _z_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z_bytes_t *addr) { +size_t _z_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z_slice_t *addr) { size_t rb = link->_read_f(link, _z_zbuf_get_wptr(zbf), _z_zbuf_space_left(zbf), addr); if (rb != SIZE_MAX) { _z_zbuf_set_wpos(zbf, _z_zbuf_get_wpos(zbf) + rb); @@ -143,7 +143,7 @@ size_t _z_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z_bytes_t *addr return rb; } -size_t _z_link_recv_exact_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, size_t len, _z_bytes_t *addr) { +size_t _z_link_recv_exact_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, size_t len, _z_slice_t *addr) { size_t rb = link->_read_exact_f(link, _z_zbuf_get_wptr(zbf), len, addr); if (rb != SIZE_MAX) { _z_zbuf_set_wpos(zbf, _z_zbuf_get_wpos(zbf) + rb); @@ -165,7 +165,7 @@ int8_t _z_link_send_wbuf(const _z_link_t *link, const _z_wbuf_t *wbf) { break; } for (size_t i = 0; (i < _z_wbuf_len_iosli(wbf)) && (ret == _Z_RES_OK); i++) { - _z_bytes_t bs = _z_iosli_to_bytes(_z_wbuf_get_iosli(wbf, i)); + _z_slice_t bs = _z_iosli_to_bytes(_z_wbuf_get_iosli(wbf, i)); size_t n = bs.len; do { size_t wb = link->_write_f(link, bs.start, n); diff --git a/src/link/multicast/bt.c b/src/link/multicast/bt.c index 9c54b966c..4af9b148c 100644 --- a/src/link/multicast/bt.c +++ b/src/link/multicast/bt.c @@ -90,20 +90,20 @@ size_t _z_f_link_write_all_bt(const _z_link_t *self, const uint8_t *ptr, size_t return _z_send_bt(self->_socket._bt._sock, ptr, len); } -size_t _z_f_link_read_bt(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_bt(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) { size_t rb = _z_read_bt(self->_socket._bt._sock, ptr, len); if ((rb > (size_t)0) && (addr != NULL)) { - *addr = _z_bytes_make(strlen(self->_socket._bt._gname)); + *addr = _z_slice_make(strlen(self->_socket._bt._gname)); (void)memcpy((uint8_t *)addr->start, self->_socket._bt._gname, addr->len); } return rb; } -size_t _z_f_link_read_exact_bt(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_exact_bt(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) { size_t rb = _z_read_exact_bt(self->_socket._bt._sock, ptr, len); if ((rb == len) && (addr != NULL)) { - *addr = _z_bytes_make(strlen(self->_socket._bt._gname)); + *addr = _z_slice_make(strlen(self->_socket._bt._gname)); (void)memcpy((uint8_t *)addr->start, self->_socket._bt._gname, addr->len); } diff --git a/src/link/multicast/udp.c b/src/link/multicast/udp.c index 69d359ba1..0e08eff58 100644 --- a/src/link/multicast/udp.c +++ b/src/link/multicast/udp.c @@ -156,11 +156,11 @@ size_t _z_f_link_write_all_udp_multicast(const _z_link_t *self, const uint8_t *p return _z_send_udp_multicast(self->_socket._udp._msock, ptr, len, self->_socket._udp._rep); } -size_t _z_f_link_read_udp_multicast(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_udp_multicast(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) { return _z_read_udp_multicast(self->_socket._udp._sock, ptr, len, self->_socket._udp._lep, addr); } -size_t _z_f_link_read_exact_udp_multicast(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_exact_udp_multicast(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) { return _z_read_exact_udp_multicast(self->_socket._udp._sock, ptr, len, self->_socket._udp._lep, addr); } diff --git a/src/link/unicast/serial.c b/src/link/unicast/serial.c index db034f07c..2423c2804 100644 --- a/src/link/unicast/serial.c +++ b/src/link/unicast/serial.c @@ -101,12 +101,12 @@ size_t _z_f_link_write_all_serial(const _z_link_t *self, const uint8_t *ptr, siz return _z_send_serial(self->_socket._serial._sock, ptr, len); } -size_t _z_f_link_read_serial(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_serial(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) { (void)(addr); return _z_read_serial(self->_socket._serial._sock, ptr, len); } -size_t _z_f_link_read_exact_serial(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_exact_serial(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) { (void)(addr); return _z_read_exact_serial(self->_socket._serial._sock, ptr, len); } diff --git a/src/link/unicast/tcp.c b/src/link/unicast/tcp.c index a6cf3de7a..fe006cdc8 100644 --- a/src/link/unicast/tcp.c +++ b/src/link/unicast/tcp.c @@ -138,12 +138,12 @@ size_t _z_f_link_write_all_tcp(const _z_link_t *zl, const uint8_t *ptr, size_t l return _z_send_tcp(zl->_socket._tcp._sock, ptr, len); } -size_t _z_f_link_read_tcp(const _z_link_t *zl, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_tcp(const _z_link_t *zl, uint8_t *ptr, size_t len, _z_slice_t *addr) { (void)(addr); return _z_read_tcp(zl->_socket._tcp._sock, ptr, len); } -size_t _z_f_link_read_exact_tcp(const _z_link_t *zl, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_exact_tcp(const _z_link_t *zl, uint8_t *ptr, size_t len, _z_slice_t *addr) { (void)(addr); return _z_read_exact_tcp(zl->_socket._tcp._sock, ptr, len); } diff --git a/src/link/unicast/udp.c b/src/link/unicast/udp.c index cd93cdff7..b76b832e6 100644 --- a/src/link/unicast/udp.c +++ b/src/link/unicast/udp.c @@ -144,12 +144,12 @@ size_t _z_f_link_write_all_udp_unicast(const _z_link_t *self, const uint8_t *ptr return _z_send_udp_unicast(self->_socket._udp._sock, ptr, len, self->_socket._udp._rep); } -size_t _z_f_link_read_udp_unicast(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_udp_unicast(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) { (void)(addr); return _z_read_udp_unicast(self->_socket._udp._sock, ptr, len); } -size_t _z_f_link_read_exact_udp_unicast(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_exact_udp_unicast(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) { (void)(addr); return _z_read_exact_udp_unicast(self->_socket._udp._sock, ptr, len); } diff --git a/src/link/unicast/ws.c b/src/link/unicast/ws.c index f59fde872..5de468a68 100644 --- a/src/link/unicast/ws.c +++ b/src/link/unicast/ws.c @@ -139,12 +139,12 @@ size_t _z_f_link_write_all_ws(const _z_link_t *zl, const uint8_t *ptr, size_t le return _z_send_ws(zl->_socket._ws._sock, ptr, len); } -size_t _z_f_link_read_ws(const _z_link_t *zl, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_ws(const _z_link_t *zl, uint8_t *ptr, size_t len, _z_slice_t *addr) { (void)(addr); return _z_read_ws(zl->_socket._ws._sock, ptr, len); } -size_t _z_f_link_read_exact_ws(const _z_link_t *zl, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +size_t _z_f_link_read_exact_ws(const _z_link_t *zl, uint8_t *ptr, size_t len, _z_slice_t *addr) { (void)(addr); return _z_read_exact_ws(zl->_socket._ws._sock, ptr, len); } diff --git a/src/net/encoding.c b/src/net/encoding.c index c41e15bd6..782d3dc3a 100644 --- a/src/net/encoding.c +++ b/src/net/encoding.c @@ -23,37 +23,37 @@ int8_t _z_encoding_make(_z_encoding_t *encoding, z_encoding_id_t id, const char encoding->id = id; // Clone schema if (schema != NULL) { - encoding->schema = _z_bytes_make(strlen(schema) + 1); + encoding->schema = _z_slice_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(); + encoding->schema = _z_slice_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))}; + .id = id, .schema = _z_slice_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); }; +void _z_encoding_clear(_z_encoding_t *encoding) { _z_slice_clear(&encoding->schema); }; _Bool _z_encoding_check(const _z_encoding_t *encoding) { - return ((encoding->id != Z_ENCODING_ID_DEFAULT) || _z_bytes_check(encoding->schema)); + return ((encoding->id != Z_ENCODING_ID_DEFAULT) || _z_slice_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); + _z_slice_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); + _z_slice_move(&dst->schema, &src->schema); } diff --git a/src/net/primitives.c b/src/net/primitives.c index 43bc39ab9..c19ae7824 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -18,7 +18,7 @@ #include #include "zenoh-pico/api/constants.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/config.h" #include "zenoh-pico/net/filtering.h" #include "zenoh-pico/net/logger.h" @@ -152,7 +152,7 @@ int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *pay ._body._body._put = { ._commons = {._timestamp = _z_timestamp_null(), ._source_info = _z_source_info_null()}, - ._payload = _z_bytes_wrap(payload, len), + ._payload = _z_slice_wrap(payload, len), ._encoding = encoding, #if Z_FEATURE_ATTACHMENT == 1 ._attachment = {.is_encoded = false, .body.decoded = attachment}, @@ -359,7 +359,7 @@ int8_t _z_send_reply(const _z_query_t *query, _z_keyexpr_t keyexpr, const _z_val z_msg._body._response._tag = _Z_RESPONSE_BODY_REPLY; z_msg._body._response._body._reply._consolidation = Z_CONSOLIDATION_MODE_DEFAULT; z_msg._body._response._body._reply._body._is_put = true; - z_msg._body._response._body._reply._body._body._put._payload = payload.payload; + z_msg._body._response._body._reply._body._body._put._payload = payload.payload._slice; z_msg._body._response._body._reply._body._body._put._encoding = payload.encoding; z_msg._body._response._body._reply._body._body._put._commons._timestamp = _z_timestamp_null(); z_msg._body._response._body._reply._body._body._put._commons._source_info = _z_source_info_null(); @@ -408,7 +408,7 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, ret = _z_register_pending_query(zn, pq); // Add the pending query to the current session if (ret == _Z_RES_OK) { - _z_bytes_t params = _z_bytes_wrap((uint8_t *)pq->_parameters, strlen(pq->_parameters)); + _z_slice_t params = _z_slice_wrap((uint8_t *)pq->_parameters, strlen(pq->_parameters)); _z_zenoh_message_t z_msg = _z_msg_make_query(&keyexpr, ¶ms, pq->_id, pq->_consolidation, &value, timeout_ms #if Z_FEATURE_ATTACHMENT == 1 diff --git a/src/net/query.c b/src/net/query.c index e2e73c7b4..cb58f8f84 100644 --- a/src/net/query.c +++ b/src/net/query.c @@ -16,6 +16,17 @@ #include "zenoh-pico/session/utils.h" #include "zenoh-pico/utils/logging.h" +_z_query_t _z_query_null(void) { + return (_z_query_t){ + ._anyke = false, + ._key = _z_keyexpr_null(), + ._parameters = NULL, + ._request_id = 0, + ._value = _z_value_null(), + ._zn = NULL, + }; +} + void _z_query_clear(_z_query_t *q) { // Send REPLY_FINAL message _z_zenoh_message_t z_msg = _z_n_msg_make_response_final(q->_request_id); @@ -33,9 +44,9 @@ void _z_query_clear(_z_query_t *q) { } #if Z_FEATURE_QUERYABLE == 1 -_z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, const _z_bytes_t *parameters, +_z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, const _z_slice_t *parameters, _z_session_t *zn, uint32_t request_id, z_attachment_t att) { - _z_query_t q; + _z_query_t q = _z_query_null(); q._request_id = request_id; q._zn = zn; // Ideally would have been an rc q._parameters = (char *)z_malloc(parameters->len + 1); diff --git a/src/net/reply.c b/src/net/reply.c index 2ad0b3701..36866ff90 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -84,7 +84,7 @@ void _z_pending_reply_clear(_z_pending_reply_t *pr) { _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, +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_slice_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(); @@ -95,7 +95,7 @@ _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, _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); + _z_slice_copy(&sample.payload._slice, payload); sample.kind = kind; sample.timestamp = _z_timestamp_duplicate(timestamp); #if Z_FEATURE_ATTACHMENT == 1 @@ -107,7 +107,7 @@ _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(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_bytes_t *payload, +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_slice_t *payload, const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, z_attachment_t att) { _ZP_UNUSED(keyexpr); diff --git a/src/net/sample.c b/src/net/sample.c index c27bb8775..7d8ec345f 100644 --- a/src/net/sample.c +++ b/src/net/sample.c @@ -19,7 +19,7 @@ _z_sample_t _z_sample_null(void) { _z_sample_t s = { .keyexpr = _z_keyexpr_null(), - .payload = _z_bytes_empty(), + .payload = _z_bytes_null(), .encoding = _z_encoding_null(), .timestamp = _z_timestamp_null(), .kind = 0, @@ -85,12 +85,12 @@ _z_sample_t _z_sample_duplicate(const _z_sample_t *src) { } #if Z_FEATURE_SUBSCRIPTION == 1 -_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t *payload, const _z_timestamp_t timestamp, +_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_slice_t *payload, const _z_timestamp_t timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, const _z_qos_t qos, const z_attachment_t att) { _z_sample_t s = _z_sample_null(); _z_keyexpr_copy(&s.keyexpr, key); - _z_bytes_copy(&s.payload, payload); + _z_slice_copy(&s.payload._slice, payload); _z_encoding_copy(&s.encoding, &encoding); s.kind = kind; s.timestamp = timestamp; @@ -103,7 +103,7 @@ _z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t *payload, return s; } #else -_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t *payload, const _z_timestamp_t timestamp, +_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_slice_t *payload, const _z_timestamp_t timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, const _z_qos_t qos, const z_attachment_t att) { _ZP_UNUSED(key); diff --git a/src/net/session.c b/src/net/session.c index 509cb294b..0300584d2 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -18,7 +18,7 @@ #include #include "zenoh-pico/api/primitives.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" #include "zenoh-pico/net/sample.h" @@ -154,7 +154,7 @@ _z_config_t *_z_info(const _z_session_t *zn) { _z_config_t *ps = (_z_config_t *)z_malloc(sizeof(_z_config_t)); if (ps != NULL) { _z_config_init(ps); - _z_bytes_t local_zid = _z_bytes_wrap(zn->_local_zid.id, _z_id_len(zn->_local_zid)); + _z_slice_t local_zid = _z_slice_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_convert_bytes(&local_zid).val); diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 0afe9bf32..9ad0b9f2c 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -195,7 +195,7 @@ int8_t _z_zsize_decode(_z_zint_t *zint, _z_zbuf_t *zbf) { } /*------------------ uint8_array ------------------*/ -int8_t _z_bytes_val_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { +int8_t _z_slice_val_encode(_z_wbuf_t *wbf, const _z_slice_t *bs) { int8_t ret = _Z_RES_OK; if ((wbf->_expansion_step != 0) && (bs->len > Z_TSID_LENGTH)) { @@ -207,22 +207,22 @@ int8_t _z_bytes_val_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { return ret; } -int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { +int8_t _z_slice_encode(_z_wbuf_t *wbf, const _z_slice_t *bs) { int8_t ret = _Z_RES_OK; _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, bs->len)) - _Z_RETURN_IF_ERR(_z_bytes_val_encode(wbf, bs)) + _Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, bs)) return ret; } -size_t _z_bytes_encode_len(const _z_bytes_t *bs) { return _z_zint_len(bs->len) + bs->len; } +size_t _z_slice_encode_len(const _z_slice_t *bs) { return _z_zint_len(bs->len) + bs->len; } -int8_t _z_bytes_val_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf) { +int8_t _z_slice_val_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; if (ret == _Z_RES_OK) { if (_z_zbuf_len(zbf) >= bs->len) { // Check if we have enough bytes to read - *bs = _z_bytes_wrap(_z_zbuf_get_rptr(zbf), bs->len); // Decode without allocating + *bs = _z_slice_wrap(_z_zbuf_get_rptr(zbf), bs->len); // Decode without allocating _z_zbuf_set_rpos(zbf, _z_zbuf_get_rpos(zbf) + bs->len); // Move the read position } else { _Z_DEBUG("WARNING: Not enough bytes to read"); @@ -238,18 +238,22 @@ int8_t _z_bytes_val_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf) { return ret; } -int8_t _z_bytes_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf) { +int8_t _z_slice_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; ret |= _z_zsize_decode(&bs->len, zbf); - ret |= _z_bytes_val_decode_na(bs, zbf); + ret |= _z_slice_val_decode_na(bs, zbf); return ret; } -int8_t _z_bytes_val_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) { return _z_bytes_val_decode_na(bs, zbf); } +int8_t _z_slice_val_decode(_z_slice_t *bs, _z_zbuf_t *zbf) { return _z_slice_val_decode_na(bs, zbf); } -int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) { return _z_bytes_decode_na(bs, zbf); } +int8_t _z_slice_decode(_z_slice_t *bs, _z_zbuf_t *zbf) { return _z_slice_decode_na(bs, zbf); } + +int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) { return _z_slice_decode_na(&bs->_slice, zbf); } + +int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { return _z_slice_encode(wbf, &bs->_slice); } /*------------------ string with null terminator ------------------*/ int8_t _z_str_encode(_z_wbuf_t *wbf, const char *s) { @@ -299,21 +303,21 @@ int8_t _z_str_decode(char **str, _z_zbuf_t *zbf) { size_t _z_encoding_len(const _z_encoding_t *en) { size_t en_len = _z_zint_len((uint32_t)(en->id) << 1); - if (_z_bytes_check(en->schema)) { - en_len += _z_bytes_encode_len(&en->schema); + if (_z_slice_check(en->schema)) { + en_len += _z_slice_encode_len(&en->schema); } return en_len; } int8_t _z_encoding_encode(_z_wbuf_t *wbf, const _z_encoding_t *en) { - _Bool has_schema = _z_bytes_check(en->schema); + _Bool has_schema = _z_slice_check(en->schema); uint32_t id = (uint32_t)(en->id) << 1; if (has_schema) { id |= _Z_ENCODING_FLAG_S; } _Z_RETURN_IF_ERR(_z_zint32_encode(wbf, id)); if (has_schema) { - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &en->schema)); + _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &en->schema)); } return _Z_RES_OK; } @@ -327,7 +331,7 @@ int8_t _z_encoding_decode(_z_encoding_t *en, _z_zbuf_t *zbf) { } en->id = (uint16_t)(id >> 1); if (has_schema) { - _Z_RETURN_IF_ERR(_z_bytes_decode(&en->schema, zbf)); + _Z_RETURN_IF_ERR(_z_slice_decode(&en->schema, zbf)); } return _Z_RES_OK; } diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index 818e8373e..aec401587 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -196,7 +196,7 @@ int8_t _z_undecl_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _z_keyexpr_t *ke = (_z_keyexpr_t *)ctx; switch (extension->_header) { case _Z_MSG_EXT_ENC_ZBUF | _Z_MSG_EXT_FLAG_M | 0x0f: { - _z_zbuf_t _zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t _zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); _z_zbuf_t *zbf = &_zbf; uint8_t header; _Z_RETURN_IF_ERR(_z_uint8_decode(&header, zbf)); diff --git a/src/protocol/codec/ext.c b/src/protocol/codec/ext.c index 6ca7ecaee..c1188431e 100644 --- a/src/protocol/codec/ext.c +++ b/src/protocol/codec/ext.c @@ -18,7 +18,7 @@ #include #include -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/protocol/codec.h" #include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/system/platform.h" @@ -57,13 +57,13 @@ int8_t _z_msg_ext_decode_zint_na(_z_msg_ext_zint_t *ext, _z_zbuf_t *zbf) { retur int8_t _z_msg_ext_encode_zbuf(_z_wbuf_t *wbf, const _z_msg_ext_zbuf_t *ext) { int8_t ret = _Z_RES_OK; - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &ext->_val)) + _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &ext->_val)) return ret; } int8_t _z_msg_ext_decode_zbuf(_z_msg_ext_zbuf_t *ext, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - ret |= _z_bytes_decode(&ext->_val, zbf); + ret |= _z_slice_decode(&ext->_val, zbf); return ret; } @@ -180,7 +180,7 @@ int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id) { break; } case _Z_MSG_EXT_ENC_ZBUF: { - _z_bytes_t buf = extension->_body._zbuf._val; + _z_slice_t buf = extension->_body._zbuf._val; char *hex = z_malloc(buf.len * 2 + 1); for (size_t i = 0; i < buf.len; ++i) { snprintf(hex + 2 * i, 3, "%02x", buf.start[i]); diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 8c4c27cc7..f52362677 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -22,7 +22,7 @@ #include "zenoh-pico/api/constants.h" #include "zenoh-pico/api/types.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/link/endpoint.h" #include "zenoh-pico/protocol/codec.h" #include "zenoh-pico/protocol/codec/core.h" @@ -40,29 +40,29 @@ /* Fields */ /*=============================*/ /*------------------ Payload field ------------------*/ -int8_t _z_payload_encode(_z_wbuf_t *wbf, const _z_bytes_t *pld) { +int8_t _z_payload_encode(_z_wbuf_t *wbf, const _z_slice_t *pld) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _PAYLOAD"); - ret |= _z_bytes_encode(wbf, pld); + ret |= _z_slice_encode(wbf, pld); return ret; } -int8_t _z_payload_decode_na(_z_bytes_t *pld, _z_zbuf_t *zbf) { +int8_t _z_payload_decode_na(_z_slice_t *pld, _z_zbuf_t *zbf) { _Z_DEBUG("Decoding _PAYLOAD"); - return _z_bytes_decode(pld, zbf); + return _z_slice_decode(pld, zbf); } -int8_t _z_payload_decode(_z_bytes_t *pld, _z_zbuf_t *zbf) { return _z_payload_decode_na(pld, zbf); } +int8_t _z_payload_decode(_z_slice_t *pld, _z_zbuf_t *zbf) { return _z_payload_decode_na(pld, zbf); } -int8_t _z_id_encode_as_zbytes(_z_wbuf_t *wbf, const _z_id_t *id) { +int8_t _z_id_encode_as_slice(_z_wbuf_t *wbf, const _z_id_t *id) { int8_t ret = _Z_RES_OK; uint8_t len = _z_id_len(*id); if (len != 0) { printf("ZIDLEN: %d\n", len); - _z_bytes_t buf = _z_bytes_wrap(id->id, len); - ret = _z_bytes_encode(wbf, &buf); + _z_slice_t buf = _z_slice_wrap(id->id, len); + ret = _z_slice_encode(wbf, &buf); } else { _Z_DEBUG("Attempted to encode invalid ID 0"); ret = _Z_ERR_MESSAGE_ZENOH_UNKNOWN; @@ -74,7 +74,7 @@ int8_t _z_id_encode_as_zbytes(_z_wbuf_t *wbf, const _z_id_t *id) { /// /// Note that while `_z_id_t` has an error state (full 0s), this function doesn't /// guarantee that this state will be set in case of errors. -int8_t _z_id_decode_as_zbytes(_z_id_t *id, _z_zbuf_t *zbf) { +int8_t _z_id_decode_as_slice(_z_id_t *id, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; uint8_t len = _z_zbuf_read(zbf); _z_zbuf_read_bytes(zbf, id->id, 0, len); @@ -88,7 +88,7 @@ int8_t _z_timestamp_encode(_z_wbuf_t *wbf, const _z_timestamp_t *ts) { _Z_DEBUG("Encoding _TIMESTAMP"); _Z_RETURN_IF_ERR(_z_zint64_encode(wbf, ts->time)) - ret |= _z_id_encode_as_zbytes(wbf, &ts->id); + ret |= _z_id_encode_as_slice(wbf, &ts->id); return ret; } @@ -102,7 +102,7 @@ int8_t _z_timestamp_decode(_z_timestamp_t *ts, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; ret |= _z_zint64_decode(&ts->time, zbf); - ret |= _z_id_decode_as_zbytes(&ts->id, zbf); + ret |= _z_id_decode_as_slice(&ts->id, zbf); return ret; } @@ -224,8 +224,8 @@ int8_t _z_source_info_encode(_z_wbuf_t *wbf, const _z_source_info_t *info) { int8_t ret = 0; uint8_t zidlen = _z_id_len(info->_id); ret |= _z_uint8_encode(wbf, zidlen << 4); - _z_bytes_t zid = _z_bytes_wrap(info->_id.id, zidlen); - ret |= _z_bytes_val_encode(wbf, &zid); + _z_slice_t zid = _z_slice_wrap(info->_id.id, zidlen); + ret |= _z_slice_val_encode(wbf, &zid); ret |= _z_zsize_encode(wbf, info->_entity_id); ret |= _z_zsize_encode(wbf, info->_source_sn); return ret; @@ -236,17 +236,17 @@ int8_t _z_source_info_encode_ext(_z_wbuf_t *wbf, const _z_source_info_t *info) { uint16_t len = 1 + zidlen + _z_zint_len(info->_entity_id) + _z_zint_len(info->_source_sn); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, len)); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, zidlen << 4)); - _z_bytes_t zid = _z_bytes_wrap(info->_id.id, zidlen); - _Z_RETURN_IF_ERR(_z_bytes_val_encode(wbf, &zid)); + _z_slice_t zid = _z_slice_wrap(info->_id.id, zidlen); + _Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, &zid)); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, info->_entity_id)); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, info->_source_sn)); return ret; } #if Z_FEATURE_ATTACHMENT == 1 -int8_t _z_attachment_encode_ext_kv(_z_bytes_t key, _z_bytes_t value, void *ctx) { +int8_t _z_attachment_encode_ext_kv(_z_slice_t key, _z_slice_t value, void *ctx) { _z_wbuf_t *wbf = (_z_wbuf_t *)ctx; - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &key)); - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &value)); + _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &key)); + _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &value)); return 0; } int8_t _z_attachment_encode_ext(_z_wbuf_t *wbf, z_attachment_t att) { @@ -308,7 +308,7 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { } #endif if (pshb->_is_put) { - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &pshb->_body._put._payload)); + _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &pshb->_body._put._payload)); } return 0; @@ -318,7 +318,7 @@ int8_t _z_push_body_decode_extensions(_z_msg_ext_t *extension, void *ctx) { int8_t ret = _Z_RES_OK; switch (_Z_EXT_FULL_ID(extension->_header)) { case _Z_MSG_EXT_ENC_ZBUF | 0x01: { - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); ret = _z_source_info_decode(&pshb->_body._put._commons._source_info, &zbf); break; } @@ -326,8 +326,8 @@ int8_t _z_push_body_decode_extensions(_z_msg_ext_t *extension, void *ctx) { case _Z_MSG_EXT_ENC_ZBUF | 0x03: { pshb->_body._put._attachment.is_encoded = true; pshb->_body._put._attachment.body.encoded = extension->_body._zbuf._val._is_alloc - ? _z_bytes_steal(&extension->_body._zbuf._val) - : _z_bytes_duplicate(&extension->_body._zbuf._val); + ? _z_slice_steal(&extension->_body._zbuf._val) + : _z_slice_duplicate(&extension->_body._zbuf._val); break; } #endif @@ -356,7 +356,7 @@ int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf, uint8_t header) _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb)); } if (ret == _Z_RES_OK) { - _Z_RETURN_IF_ERR(_z_bytes_decode(&pshb->_body._put._payload, zbf)); + _Z_RETURN_IF_ERR(_z_slice_decode(&pshb->_body._put._payload, zbf)); } break; } @@ -409,7 +409,7 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) { int8_t ret = _Z_RES_OK; uint8_t header = _Z_MID_Z_QUERY; - _Bool has_params = _z_bytes_check(msg->_parameters); + _Bool has_params = _z_slice_check(msg->_parameters); if (has_params) { _Z_SET_FLAG(header, _Z_FLAG_Z_Q_P); } @@ -426,7 +426,7 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, msg->_consolidation)); } if (has_params) { - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_parameters)); + _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &msg->_parameters)); } if (required_exts.body) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x03; @@ -435,9 +435,9 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) { } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); _Z_RETURN_IF_ERR( - _z_zsize_encode(wbf, _z_encoding_len(&msg->_ext_value.encoding) + msg->_ext_value.payload.len)); + _z_zsize_encode(wbf, _z_encoding_len(&msg->_ext_value.encoding) + msg->_ext_value.payload._slice.len)); _Z_RETURN_IF_ERR(_z_encoding_encode(wbf, &msg->_ext_value.encoding)); - _Z_RETURN_IF_ERR(_z_bytes_val_encode(wbf, &msg->_ext_value.payload)); + _Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, &msg->_ext_value.payload._slice)); } if (required_exts.info) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; @@ -463,23 +463,23 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { int8_t ret = _Z_RES_OK; switch (_Z_EXT_FULL_ID(extension->_header)) { case _Z_MSG_EXT_ENC_ZBUF | 0x01: { // Source Info - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); ret = _z_source_info_decode(&msg->_ext_info, &zbf); break; } case _Z_MSG_EXT_ENC_ZBUF | 0x03: { // Payload - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); ret = _z_encoding_decode(&msg->_ext_value.encoding, &zbf); - _z_bytes_t bytes = _z_bytes_wrap((uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf)); - _z_bytes_copy(&msg->_ext_value.payload, &bytes); + _z_slice_t bytes = _z_slice_wrap((uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf)); + _z_slice_copy(&msg->_ext_value.payload._slice, &bytes); break; } #if Z_FEATURE_ATTACHMENT == 1 case _Z_MSG_EXT_ENC_ZBUF | 0x05: { msg->_ext_attachment.is_encoded = true; msg->_ext_attachment.body.encoded = extension->_body._zbuf._val._is_alloc - ? _z_bytes_steal(&extension->_body._zbuf._val) - : _z_bytes_duplicate(&extension->_body._zbuf._val); + ? _z_slice_steal(&extension->_body._zbuf._val) + : _z_slice_duplicate(&extension->_body._zbuf._val); break; } #endif @@ -502,9 +502,9 @@ int8_t _z_query_decode(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_consolidation = Z_CONSOLIDATION_MODE_DEFAULT; } if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Q_P)) { - _Z_RETURN_IF_ERR(_z_bytes_decode(&msg->_parameters, zbf)); + _Z_RETURN_IF_ERR(_z_slice_decode(&msg->_parameters, zbf)); } else { - _z_bytes_clear(&msg->_parameters); + _z_slice_clear(&msg->_parameters); } if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_query_decode_extensions, msg)); @@ -576,7 +576,7 @@ int8_t _z_err_encode(_z_wbuf_t *wbf, const _z_msg_err_t *err) { _Z_RETURN_IF_ERR(_z_source_info_encode_ext(wbf, &err->_ext_source_info)); } // Encode payload - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &err->_payload)); + _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &err->_payload)); return ret; } int8_t _z_err_decode_extension(_z_msg_ext_t *extension, void *ctx) { @@ -584,7 +584,7 @@ int8_t _z_err_decode_extension(_z_msg_ext_t *extension, void *ctx) { _z_msg_err_t *reply = (_z_msg_err_t *)ctx; switch (_Z_EXT_FULL_ID(extension->_header)) { case _Z_MSG_EXT_ENC_ZBUF | 0x01: { - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); ret = _z_source_info_decode(&reply->_ext_source_info, &zbf); break; } @@ -604,7 +604,7 @@ int8_t _z_err_decode(_z_msg_err_t *err, _z_zbuf_t *zbf, uint8_t header) { if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_err_decode_extension, err)); } - _Z_RETURN_IF_ERR(_z_bytes_decode(&err->_payload, zbf)); + _Z_RETURN_IF_ERR(_z_slice_decode(&err->_payload, zbf)); return _Z_RES_OK; } @@ -664,7 +664,7 @@ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *m cbyte |= (msg->_whatami & 0x03); cbyte |= ((zidlen - 1) & 0x0F) << 4; _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)) - _Z_RETURN_IF_ERR(_z_bytes_val_encode(wbf, &(_z_bytes_t){.start = msg->_zid.id, .len = zidlen, ._is_alloc = false})); + _Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, &(_z_slice_t){.start = msg->_zid.id, .len = zidlen, ._is_alloc = false})); if (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true) { _Z_RETURN_IF_ERR(_z_locators_encode(wbf, &msg->_locators)) diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 2243b2d32..a676cb595 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -20,7 +20,7 @@ #include "zenoh-pico/api/constants.h" #include "zenoh-pico/api/types.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/protocol/codec.h" #include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/protocol/codec/declarations.h" @@ -79,7 +79,7 @@ int8_t _z_push_decode_ext_cb(_z_msg_ext_t *extension, void *ctx) { break; } case _Z_MSG_EXT_ENC_ZBUF | 0x02: { // Timestamp ext - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); ret = _z_timestamp_decode(&msg->_timestamp, &zbf); break; } @@ -178,7 +178,7 @@ int8_t _z_request_decode_extensions(_z_msg_ext_t *extension, void *ctx) { break; } case 0x02 | _Z_MSG_EXT_ENC_ZBUF: { // Timestamp ext - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); _Z_RETURN_IF_ERR(_z_timestamp_decode(&msg->_ext_timestamp, &zbf)); break; } @@ -313,12 +313,12 @@ int8_t _z_response_decode_extension(_z_msg_ext_t *extension, void *ctx) { break; } case _Z_MSG_EXT_ENC_ZBUF | 0x02: { - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); ret = _z_timestamp_decode(&msg->_ext_timestamp, &zbf); break; } case _Z_MSG_EXT_ENC_ZBUF | 0x03: { - _z_zbuf_t _zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t _zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); _z_zbuf_t *zbf = &_zbf; uint8_t header; _Z_RETURN_IF_ERR(_z_uint8_decode(&header, zbf)); @@ -432,7 +432,7 @@ int8_t _z_declare_decode_extensions(_z_msg_ext_t *extension, void *ctx) { break; } case _Z_MSG_EXT_ENC_ZBUF | 0x02: { - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); return _z_timestamp_decode(&decl->_ext_timestamp, &zbf); } default: diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 87eba5ae5..00cacad9c 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -17,7 +17,7 @@ #include #include -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/protocol/codec/network.h" @@ -83,7 +83,7 @@ int8_t _z_join_decode_ext(_z_msg_ext_t *extension, void *ctx) { if (_Z_EXT_FULL_ID(extension->_header) == (_Z_MSG_EXT_ENC_ZBUF | _Z_MSG_EXT_FLAG_M | 1)) { // QOS: (enc=zbuf)(mandatory=true)(id=1) msg->_next_sn._is_qos = true; - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); for (int i = 0; (ret == _Z_RES_OK) && (i < Z_PRIORITIES_NUM); ++i) { ret |= _z_zsize_decode(&msg->_next_sn._val._qos[i]._reliable, &zbf); ret |= _z_zsize_decode(&msg->_next_sn._val._qos[i]._best_effort, &zbf); @@ -168,7 +168,7 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg } if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true) { - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_cookie)) + _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &msg->_cookie)) } return ret; @@ -208,9 +208,9 @@ int8_t _z_init_decode(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { } if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true)) { - ret |= _z_bytes_decode(&msg->_cookie, zbf); + ret |= _z_slice_decode(&msg->_cookie, zbf); } else { - msg->_cookie = _z_bytes_empty(); + msg->_cookie = _z_slice_empty(); } if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { @@ -234,7 +234,7 @@ int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, msg->_initial_sn)) if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_A) == false) { - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_cookie)) + _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &msg->_cookie)) } return ret; @@ -253,12 +253,12 @@ int8_t _z_open_decode(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { ret |= _z_zsize_decode(&msg->_initial_sn, zbf); if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_A) == false)) { - ret |= _z_bytes_decode(&msg->_cookie, zbf); + ret |= _z_slice_decode(&msg->_cookie, zbf); if (ret != _Z_RES_OK) { - msg->_cookie = _z_bytes_empty(); + msg->_cookie = _z_slice_empty(); } } else { - msg->_cookie = _z_bytes_empty(); + msg->_cookie = _z_slice_empty(); } if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x02); @@ -382,7 +382,7 @@ int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragmen if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { ret = _Z_ERR_MESSAGE_SERIALIZATION_FAILED; } - if (ret == _Z_RES_OK && _z_bytes_check(msg->_payload)) { + if (ret == _Z_RES_OK && _z_slice_check(msg->_payload)) { _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, msg->_payload.start, 0, msg->_payload.len)); } @@ -400,8 +400,8 @@ int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t head ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x05); } - _z_bytes_t bytes = _z_bytes_wrap((uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf)); - _z_bytes_copy(&msg->_payload, &bytes); + _z_slice_t slice = _z_slice_wrap((uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf)); + _z_slice_copy(&msg->_payload, &slice); zbf->_ios._r_pos = zbf->_ios._w_pos; return ret; diff --git a/src/protocol/core.c b/src/protocol/core.c index 6989f45c9..cb1617481 100644 --- a/src/protocol/core.c +++ b/src/protocol/core.c @@ -19,7 +19,7 @@ #include "zenoh-pico/api/primitives.h" #include "zenoh-pico/api/types.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/utils/logging.h" @@ -66,7 +66,7 @@ _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_null()}; } +_z_value_t _z_value_null(void) { return (_z_value_t){.payload = _z_bytes_null(), .encoding = _z_encoding_null()}; } _z_value_t _z_value_steal(_z_value_t *value) { _z_value_t ret = *value; *value = _z_value_null(); @@ -81,24 +81,24 @@ z_attachment_t z_attachment_null(void) { return (z_attachment_t){.data = NULL, . #if Z_FEATURE_ATTACHMENT == 1 struct _z_seeker_t { - _z_bytes_t key; - _z_bytes_t value; + _z_slice_t key; + _z_slice_t value; }; -int8_t _z_attachment_get_seeker(_z_bytes_t key, _z_bytes_t value, void *ctx) { +int8_t _z_attachment_get_seeker(_z_slice_t key, _z_slice_t value, void *ctx) { struct _z_seeker_t *seeker = (struct _z_seeker_t *)ctx; - _z_bytes_t seeked = seeker->key; + _z_slice_t seeked = seeker->key; if (key.len == seeked.len && memcmp(key.start, seeked.start, seeked.len)) { - seeker->value = (_z_bytes_t){.start = value.start, .len = value.len, ._is_alloc = false}; + seeker->value = (_z_slice_t){.start = value.start, .len = value.len, ._is_alloc = false}; return 1; } return 0; } -_z_bytes_t z_attachment_get(z_attachment_t this_, _z_bytes_t key) { +_z_slice_t z_attachment_get(z_attachment_t this_, _z_slice_t key) { struct _z_seeker_t seeker = {.value = {0}, .key = key}; z_attachment_iterate(this_, _z_attachment_get_seeker, (void *)&seeker); return seeker.value; } -int8_t _z_attachment_estimate_length_body(_z_bytes_t key, _z_bytes_t value, void *ctx) { +int8_t _z_attachment_estimate_length_body(_z_slice_t key, _z_slice_t value, void *ctx) { size_t *len = (size_t *)ctx; *len += _z_zint_len(key.len) + key.len + _z_zint_len(value.len) + value.len; return 0; @@ -110,12 +110,12 @@ size_t _z_attachment_estimate_length(z_attachment_t att) { } int8_t _z_encoded_attachment_iteration_driver(const void *this_, z_attachment_iter_body_t body, void *ctx) { - _z_zbuf_t data = _z_zbytes_as_zbuf(*(_z_bytes_t *)this_); + _z_zbuf_t data = _z_slice_as_zbuf(*(_z_slice_t *)this_); while (_z_zbuf_can_read(&data)) { - _z_bytes_t key = _z_bytes_empty(); - _z_bytes_t value = _z_bytes_empty(); - _z_bytes_decode(&key, &data); - _z_bytes_decode(&value, &data); + _z_slice_t key = _z_slice_empty(); + _z_slice_t value = _z_slice_empty(); + _z_slice_decode(&key, &data); + _z_slice_decode(&value, &data); int8_t ret = body(key, value, ctx); if (ret != 0) { return ret; @@ -127,7 +127,7 @@ int8_t _z_encoded_attachment_iteration_driver(const void *this_, z_attachment_it z_attachment_t _z_encoded_as_attachment(const _z_owned_encoded_attachment_t *att) { if (att->is_encoded) { // Recopy z_bytes data in allocated memory to avoid it going out of scope - z_bytes_t *att_data = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + z_bytes_t *att_data = (_z_slice_t *)z_malloc(sizeof(_z_slice_t)); if (att_data == NULL) { return att->body.decoded; } @@ -146,7 +146,7 @@ int8_t z_attachment_iterate(z_attachment_t this_, z_attachment_iter_body_t body, void _z_attachment_drop(z_attachment_t *att) { if (att->iteration_driver == _z_encoded_attachment_iteration_driver) { - _z_bytes_clear((z_bytes_t *)att->data); + _z_slice_clear((z_bytes_t *)att->data); z_free((z_bytes_t *)att->data); } } diff --git a/src/protocol/definitions/message.c b/src/protocol/definitions/message.c index 8daa82210..b9bb3ecb1 100644 --- a/src/protocol/definitions/message.c +++ b/src/protocol/definitions/message.c @@ -16,7 +16,7 @@ #include -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/network.h" @@ -24,7 +24,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_encoding_clear(&msg->_encoding); - _z_bytes_clear(&msg->_payload); + _z_slice_clear(&msg->_payload); _z_timestamp_clear(&msg->_commons._timestamp); } @@ -33,7 +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 || _z_encoding_check(&msg->_ext_value.encoding), + .body = msg->_ext_value.payload._slice.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) @@ -44,10 +44,10 @@ _z_msg_query_reqexts_t _z_msg_query_required_extensions(const _z_msg_query_t *ms } void _z_msg_query_clear(_z_msg_query_t *msg) { - _z_bytes_clear(&msg->_parameters); + _z_slice_clear(&msg->_parameters); _z_value_clear(&msg->_ext_value); } void _z_msg_err_clear(_z_msg_err_t *err) { _z_encoding_clear(&err->encoding); - _z_bytes_clear(&err->_payload); + _z_slice_clear(&err->_payload); } diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index 4d88903fd..57322e339 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -136,7 +136,7 @@ void _z_n_msg_free(_z_network_message_t **msg) { } } -_z_zenoh_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes_t) parameters, _z_zint_t qid, +_z_zenoh_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_slice_t) parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value, uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 @@ -151,7 +151,7 @@ _z_zenoh_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes ._rid = qid, ._key = _z_keyexpr_steal(key), ._tag = _Z_REQUEST_QUERY, - ._body._query = {._parameters = _z_bytes_steal(parameters), + ._body._query = {._parameters = _z_slice_steal(parameters), ._consolidation = consolidation, ._ext_value = _z_value_steal(value), ._ext_info = _z_source_info_null(), diff --git a/src/protocol/definitions/transport.c b/src/protocol/definitions/transport.c index 44efd5837..2a774f758 100644 --- a/src/protocol/definitions/transport.c +++ b/src/protocol/definitions/transport.c @@ -14,7 +14,7 @@ #include "zenoh-pico/protocol/definitions/transport.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/utils/logging.h" void _z_s_msg_scout_clear(_z_s_msg_scout_t *msg) {} @@ -26,9 +26,9 @@ void _z_s_msg_hello_clear(_z_s_msg_hello_t *msg) { _z_locators_clear(&msg->_loca void _z_t_msg_join_clear(_z_t_msg_join_t *msg) {} -void _z_t_msg_init_clear(_z_t_msg_init_t *msg) { _z_bytes_clear(&msg->_cookie); } +void _z_t_msg_init_clear(_z_t_msg_init_t *msg) { _z_slice_clear(&msg->_cookie); } -void _z_t_msg_open_clear(_z_t_msg_open_t *msg) { _z_bytes_clear(&msg->_cookie); } +void _z_t_msg_open_clear(_z_t_msg_open_t *msg) { _z_slice_clear(&msg->_cookie); } void _z_t_msg_close_clear(_z_t_msg_close_t *msg) { (void)(msg); } @@ -36,7 +36,7 @@ void _z_t_msg_keep_alive_clear(_z_t_msg_keep_alive_t *msg) { (void)(msg); } void _z_t_msg_frame_clear(_z_t_msg_frame_t *msg) { _z_network_message_vec_clear(&msg->_messages); } -void _z_t_msg_fragment_clear(_z_t_msg_fragment_t *msg) { _z_bytes_clear(&msg->_payload); } +void _z_t_msg_fragment_clear(_z_t_msg_fragment_t *msg) { _z_slice_clear(&msg->_payload); } void _z_t_msg_clear(_z_transport_message_t *msg) { uint8_t mid = _Z_MID(msg->_header); @@ -117,7 +117,7 @@ _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid) msg._body._init._seq_num_res = Z_SN_RESOLUTION; msg._body._init._req_id_res = Z_REQ_RESOLUTION; msg._body._init._batch_size = Z_BATCH_UNICAST_SIZE; - _z_bytes_reset(&msg._body._init._cookie); + _z_slice_reset(&msg._body._init._cookie); if ((msg._body._init._batch_size != _Z_DEFAULT_UNICAST_BATCH_SIZE) || (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || @@ -128,7 +128,7 @@ _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid) return msg; } -_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, _z_bytes_t cookie) { +_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, _z_slice_t cookie) { _z_transport_message_t msg; msg._header = _Z_MID_T_INIT; _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_A); @@ -151,7 +151,7 @@ _z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, } /*------------------ Open Message ------------------*/ -_z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie) { +_z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_slice_t cookie) { _z_transport_message_t msg; msg._header = _Z_MID_T_OPEN; @@ -173,7 +173,7 @@ _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial msg._body._open._lease = lease; msg._body._open._initial_sn = initial_sn; - _z_bytes_reset(&msg._body._open._cookie); + _z_slice_reset(&msg._body._open._cookie); if ((lease % 1000) == 0) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_T); @@ -234,9 +234,9 @@ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliabl /*------------------ Fragment Message ------------------*/ _z_transport_message_t _z_t_msg_make_fragment_header(_z_zint_t sn, _Bool is_reliable, _Bool is_last) { - return _z_t_msg_make_fragment(sn, _z_bytes_empty(), is_reliable, is_last); + return _z_t_msg_make_fragment(sn, _z_slice_empty(), is_reliable, is_last); } -_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_bytes_t payload, _Bool is_reliable, _Bool is_last) { +_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_slice_t payload, _Bool is_reliable, _Bool is_last) { _z_transport_message_t msg; msg._header = _Z_MID_T_FRAGMENT; if (is_last == false) { @@ -253,7 +253,7 @@ _z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_bytes_t payload, } void _z_t_msg_copy_fragment(_z_t_msg_fragment_t *clone, _z_t_msg_fragment_t *msg) { - _z_bytes_copy(&clone->_payload, &msg->_payload); + _z_slice_copy(&clone->_payload, &msg->_payload); } void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg) { @@ -274,13 +274,13 @@ void _z_t_msg_copy_init(_z_t_msg_init_t *clone, _z_t_msg_init_t *msg) { clone->_req_id_res = msg->_req_id_res; clone->_batch_size = msg->_batch_size; memcpy(clone->_zid.id, msg->_zid.id, 16); - _z_bytes_copy(&clone->_cookie, &msg->_cookie); + _z_slice_copy(&clone->_cookie, &msg->_cookie); } void _z_t_msg_copy_open(_z_t_msg_open_t *clone, _z_t_msg_open_t *msg) { clone->_lease = msg->_lease; clone->_initial_sn = msg->_initial_sn; - _z_bytes_copy(&clone->_cookie, &msg->_cookie); + _z_slice_copy(&clone->_cookie, &msg->_cookie); } void _z_t_msg_copy_close(_z_t_msg_close_t *clone, _z_t_msg_close_t *msg) { clone->_reason = msg->_reason; } diff --git a/src/protocol/ext.c b/src/protocol/ext.c index 0f3c3a56c..c40f2d75c 100644 --- a/src/protocol/ext.c +++ b/src/protocol/ext.c @@ -16,7 +16,7 @@ #include -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/utils/logging.h" _z_msg_ext_t _z_msg_ext_make_unit(uint8_t id) { @@ -52,22 +52,22 @@ void _z_msg_ext_clear_zint(_z_msg_ext_zint_t *ext) { (void)(ext); } void _z_msg_ext_copy_zint(_z_msg_ext_zint_t *clone, const _z_msg_ext_zint_t *ext) { clone->_val = ext->_val; } -_z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, _z_bytes_t zbuf) { +_z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, _z_slice_t zbuf) { _z_msg_ext_t ext; ext._header = 0; ext._header |= (id & _Z_EXT_ID_MASK); ext._header |= _Z_MSG_EXT_ENC_ZBUF; - _z_bytes_move(&ext._body._zbuf._val, &zbuf); + _z_slice_move(&ext._body._zbuf._val, &zbuf); return ext; } -void _z_msg_ext_clear_zbuf(_z_msg_ext_zbuf_t *ext) { _z_bytes_clear(&ext->_val); } +void _z_msg_ext_clear_zbuf(_z_msg_ext_zbuf_t *ext) { _z_slice_clear(&ext->_val); } void _z_msg_ext_copy_zbuf(_z_msg_ext_zbuf_t *clone, const _z_msg_ext_zbuf_t *ext) { - _z_bytes_copy(&clone->_val, &ext->_val); + _z_slice_copy(&clone->_val, &ext->_val); } void _z_msg_ext_copy(_z_msg_ext_t *clone, const _z_msg_ext_t *ext) { diff --git a/src/protocol/iobuf.c b/src/protocol/iobuf.c index c5700886f..f31511c4c 100644 --- a/src/protocol/iobuf.c +++ b/src/protocol/iobuf.c @@ -100,8 +100,8 @@ void _z_iosli_put(_z_iosli_t *ios, uint8_t b, size_t pos) { ios->_buf[pos] = b; } -_z_bytes_t _z_iosli_to_bytes(const _z_iosli_t *ios) { - _z_bytes_t a; +_z_slice_t _z_iosli_to_bytes(const _z_iosli_t *ios) { + _z_slice_t a; a.len = _z_iosli_readable(ios); a.start = _z_cptr_u8_offset(ios->_buf, ios->_r_pos); return a; @@ -171,7 +171,7 @@ _z_zbuf_t _z_zbuf_view(_z_zbuf_t *zbf, size_t length) { v._ios = _z_iosli_wrap(_z_zbuf_get_rptr(zbf), length, 0, length); return v; } -_z_zbuf_t _z_zbytes_as_zbuf(_z_bytes_t slice) { +_z_zbuf_t _z_slice_as_zbuf(_z_slice_t slice) { return (_z_zbuf_t){._ios = {._buf = (uint8_t *)slice.start, // Safety: `_z_zbuf_t` is an immutable buffer ._is_alloc = false, ._capacity = slice.len, diff --git a/src/session/push.c b/src/session/push.c index 1d791cb83..df197244c 100644 --- a/src/session/push.c +++ b/src/session/push.c @@ -16,7 +16,7 @@ #include "zenoh-pico/api/constants.h" #include "zenoh-pico/api/primitives.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/config.h" #include "zenoh-pico/session/subscription.h" #include "zenoh-pico/utils/logging.h" @@ -25,7 +25,7 @@ int8_t _z_trigger_push(_z_session_t *zn, _z_n_msg_push_t *push) { int8_t ret = _Z_RES_OK; // 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_slice_t payload = push->_body._is_put ? push->_body._body._put._payload : _z_slice_empty(); _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 diff --git a/src/session/rx.c b/src/session/rx.c index f70f33b2f..d5d5d8e33 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -18,7 +18,7 @@ #include "zenoh-pico/api/constants.h" #include "zenoh-pico/api/primitives.h" #include "zenoh-pico/api/types.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/declarations.h" #include "zenoh-pico/protocol/definitions/message.h" @@ -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_null(), + ret = _z_trigger_subscriptions(zn, req._key, _z_slice_empty(), _z_encoding_null(), Z_SAMPLE_KIND_DELETE, del._commons._timestamp, req._ext_qos, z_attachment_null()); #endif @@ -138,7 +138,7 @@ int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint case _Z_RESPONSE_BODY_ERR: { // @TODO: expose zenoh errors to the user _z_msg_err_t error = response._body._err; - _z_bytes_t payload = error._payload; + _z_slice_t payload = error._payload; _ZP_UNUSED(payload); // Unused when logs are deactivated _Z_ERROR("Received Err for query %zu: message=%.*s", response._request_id, (int)payload.len, payload.start); diff --git a/src/session/subscription.c b/src/session/subscription.c index 31e28b0e2..7444719ac 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -141,12 +141,12 @@ _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 = _z_encoding_null(); - int8_t ret = _z_trigger_subscriptions(zn, keyexpr, _z_bytes_wrap(payload, payload_len), encoding, Z_SAMPLE_KIND_PUT, + int8_t ret = _z_trigger_subscriptions(zn, keyexpr, _z_slice_wrap(payload, payload_len), encoding, Z_SAMPLE_KIND_PUT, _z_timestamp_null(), qos, att); (void)ret; } -int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t payload, +int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_slice_t payload, const _z_encoding_t encoding, const _z_zint_t kind, const _z_timestamp_t timestamp, const _z_n_qos_t qos, const z_attachment_t att) { int8_t ret = _Z_RES_OK; diff --git a/src/system/arduino/esp32/network.cpp b/src/system/arduino/esp32/network.cpp index ba4e65206..cbfe0360c 100644 --- a/src/system/arduino/esp32/network.cpp +++ b/src/system/arduino/esp32/network.cpp @@ -21,7 +21,7 @@ extern "C" { #include #include -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" #include "zenoh-pico/system/link/bt.h" @@ -475,7 +475,7 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t * } size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep, - _z_bytes_t *addr) { + _z_slice_t *addr) { struct sockaddr_storage raddr; unsigned int raddrlen = sizeof(struct sockaddr_storage); @@ -493,7 +493,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ if (!((a->sin_port == b->sin_port) && (a->sin_addr.s_addr == b->sin_addr.s_addr))) { // If addr is not NULL, it means that the raddr was requested by the upper-layers if (addr != NULL) { - *addr = _z_bytes_make(sizeof(in_addr_t) + sizeof(in_port_t)); + *addr = _z_slice_make(sizeof(in_addr_t) + sizeof(in_port_t)); (void)memcpy((uint8_t *)addr->start, &b->sin_addr.s_addr, sizeof(in_addr_t)); (void)memcpy((uint8_t *)(addr->start + sizeof(in_addr_t)), &b->sin_port, sizeof(in_port_t)); } @@ -506,7 +506,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ (memcmp(&a->sin6_addr, &b->sin6_addr, sizeof(struct in6_addr)) != 0)) { // If addr is not NULL, it means that the raddr was requested by the upper-layers if (addr != NULL) { - *addr = _z_bytes_make(sizeof(struct in6_addr) + sizeof(in_port_t)); + *addr = _z_slice_make(sizeof(struct in6_addr) + sizeof(in_port_t)); (void)memcpy((uint8_t *)addr->start, &b->sin6_addr.s6_addr, sizeof(struct in6_addr)); (void)memcpy((uint8_t *)(addr->start + sizeof(struct in6_addr)), &b->sin6_port, sizeof(in_port_t)); } @@ -521,7 +521,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ } size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, - const _z_sys_net_endpoint_t lep, _z_bytes_t *addr) { + const _z_sys_net_endpoint_t lep, _z_slice_t *addr) { size_t n = 0; uint8_t *pos = &ptr[0]; diff --git a/src/system/arduino/opencr/network.cpp b/src/system/arduino/opencr/network.cpp index 247ba82f6..0d75a298d 100644 --- a/src/system/arduino/opencr/network.cpp +++ b/src/system/arduino/opencr/network.cpp @@ -281,7 +281,7 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t * } size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep, - _z_bytes_t *addr) { + _z_slice_t *addr) { // Block until something to read // FIXME: provide some kind of timeout functionality ssize_t rb = 0; @@ -295,7 +295,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ IPAddress rip = sock._udp->remoteIP(); uint16_t rport = sock._udp->remotePort(); - *addr = _z_bytes_make(strlen((const char *)&rip[0]) + strlen((const char *)&rip[1]) + + *addr = _z_slice_make(strlen((const char *)&rip[0]) + strlen((const char *)&rip[1]) + strlen((const char *)&rip[2]) + strlen((const char *)&rip[3]) + sizeof(uint16_t)); uint8_t offset = 0; for (uint8_t i = 0; i < (uint8_t)4; i++) { @@ -313,7 +313,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ } size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, - const _z_sys_net_endpoint_t lep, _z_bytes_t *addr) { + const _z_sys_net_endpoint_t lep, _z_slice_t *addr) { size_t n = 0; uint8_t *pos = &ptr[0]; diff --git a/src/system/espidf/network.c b/src/system/espidf/network.c index 5ae14373f..d60a9a4f4 100644 --- a/src/system/espidf/network.c +++ b/src/system/espidf/network.c @@ -17,7 +17,7 @@ #include #include -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" #include "zenoh-pico/system/link/bt.h" @@ -471,7 +471,7 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t * } size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep, - _z_bytes_t *addr) { + _z_slice_t *addr) { struct sockaddr_storage raddr; socklen_t raddrlen = sizeof(struct sockaddr_storage); @@ -489,7 +489,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ if (!((a->sin_port == b->sin_port) && (a->sin_addr.s_addr == b->sin_addr.s_addr))) { // If addr is not NULL, it means that the raddr was requested by the upper-layers if (addr != NULL) { - *addr = _z_bytes_make(sizeof(in_addr_t) + sizeof(in_port_t)); + *addr = _z_slice_make(sizeof(in_addr_t) + sizeof(in_port_t)); (void)memcpy((uint8_t *)addr->start, &b->sin_addr.s_addr, sizeof(in_addr_t)); (void)memcpy((uint8_t *)(addr->start + sizeof(in_addr_t)), &b->sin_port, sizeof(in_port_t)); } @@ -502,7 +502,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ (memcmp(&a->sin6_addr, &b->sin6_addr, sizeof(struct in6_addr)) != 0)) { // If addr is not NULL, it means that the raddr was requested by the upper-layers if (addr != NULL) { - *addr = _z_bytes_make(sizeof(struct in6_addr) + sizeof(in_port_t)); + *addr = _z_slice_make(sizeof(struct in6_addr) + sizeof(in_port_t)); (void)memcpy((uint8_t *)addr->start, &b->sin6_addr.s6_addr, sizeof(struct in6_addr)); (void)memcpy((uint8_t *)(addr->start + sizeof(struct in6_addr)), &b->sin6_port, sizeof(in_port_t)); } @@ -517,7 +517,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ } size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, - const _z_sys_net_endpoint_t lep, _z_bytes_t *addr) { + const _z_sys_net_endpoint_t lep, _z_slice_t *addr) { size_t n = 0; uint8_t *pos = &ptr[0]; diff --git a/src/system/mbed/network.cpp b/src/system/mbed/network.cpp index 7ab41e452..8b96a86e9 100644 --- a/src/system/mbed/network.cpp +++ b/src/system/mbed/network.cpp @@ -262,7 +262,7 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t * } size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep, - _z_bytes_t *addr) { + _z_slice_t *addr) { SocketAddress raddr; nsapi_size_or_error_t rb = 0; @@ -274,13 +274,13 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ } if (raddr.get_ip_version() == NSAPI_IPv4) { - *addr = _z_bytes_make(NSAPI_IPv4_BYTES + sizeof(uint16_t)); + *addr = _z_slice_make(NSAPI_IPv4_BYTES + sizeof(uint16_t)); (void)memcpy(const_cast(addr->start), raddr.get_ip_bytes(), NSAPI_IPv4_BYTES); uint16_t port = raddr.get_port(); (void)memcpy(const_cast(addr->start + NSAPI_IPv4_BYTES), &port, sizeof(uint16_t)); break; } else if (raddr.get_ip_version() == NSAPI_IPv6) { - *addr = _z_bytes_make(NSAPI_IPv6_BYTES + sizeof(uint16_t)); + *addr = _z_slice_make(NSAPI_IPv6_BYTES + sizeof(uint16_t)); (void)memcpy(const_cast(addr->start), raddr.get_ip_bytes(), NSAPI_IPv6_BYTES); uint16_t port = raddr.get_port(); (void)memcpy(const_cast(addr->start + NSAPI_IPv6_BYTES), &port, sizeof(uint16_t)); @@ -294,7 +294,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ } size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, - const _z_sys_net_endpoint_t lep, _z_bytes_t *addr) { + const _z_sys_net_endpoint_t lep, _z_slice_t *addr) { size_t n = 0; uint8_t *pos = &ptr[0]; diff --git a/src/system/unix/link/raweth.c b/src/system/unix/link/raweth.c index 10e35c565..0b3131e35 100644 --- a/src/system/unix/link/raweth.c +++ b/src/system/unix/link/raweth.c @@ -92,7 +92,7 @@ size_t _z_send_raweth(const _z_sys_net_socket_t *sock, const void *buff, size_t return (size_t)wb; } -size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr, +size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_slice_t *addr, const _zp_raweth_whitelist_array_t *whitelist) { // Read from socket ssize_t bytesRead = recvfrom(sock->_fd, buff, buff_len, 0, NULL, NULL); @@ -118,7 +118,7 @@ size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buf } // Copy sender mac if needed if (addr != NULL) { - *addr = _z_bytes_make(sizeof(ETH_ALEN)); + *addr = _z_slice_make(sizeof(ETH_ALEN)); (void)memcpy((uint8_t *)addr->start, (buff + ETH_ALEN), sizeof(ETH_ALEN)); } return bytesRead; diff --git a/src/system/unix/network.c b/src/system/unix/network.c index d5ec40393..e4a7a2af9 100644 --- a/src/system/unix/network.c +++ b/src/system/unix/network.c @@ -509,7 +509,7 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t * } size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep, - _z_bytes_t *addr) { + _z_slice_t *addr) { struct sockaddr_storage raddr; unsigned int replen = sizeof(struct sockaddr_storage); @@ -527,7 +527,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ if (!((a->sin_port == b->sin_port) && (a->sin_addr.s_addr == b->sin_addr.s_addr))) { // If addr is not NULL, it means that the rep was requested by the upper-layers if (addr != NULL) { - *addr = _z_bytes_make(sizeof(in_addr_t) + sizeof(in_port_t)); + *addr = _z_slice_make(sizeof(in_addr_t) + sizeof(in_port_t)); (void)memcpy((uint8_t *)addr->start, &b->sin_addr.s_addr, sizeof(in_addr_t)); (void)memcpy((uint8_t *)(addr->start + sizeof(in_addr_t)), &b->sin_port, sizeof(in_port_t)); } @@ -540,7 +540,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ (memcmp(a->sin6_addr.s6_addr, b->sin6_addr.s6_addr, sizeof(struct in6_addr)) == 0))) { // If addr is not NULL, it means that the rep was requested by the upper-layers if (addr != NULL) { - *addr = _z_bytes_make(sizeof(struct in6_addr) + sizeof(in_port_t)); + *addr = _z_slice_make(sizeof(struct in6_addr) + sizeof(in_port_t)); (void)memcpy((uint8_t *)addr->start, &b->sin6_addr.s6_addr, sizeof(struct in6_addr)); (void)memcpy((uint8_t *)(addr->start + sizeof(struct in6_addr)), &b->sin6_port, sizeof(in_port_t)); } @@ -555,7 +555,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ } size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, - const _z_sys_net_endpoint_t lep, _z_bytes_t *addr) { + const _z_sys_net_endpoint_t lep, _z_slice_t *addr) { size_t n = 0; uint8_t *pos = &ptr[0]; diff --git a/src/system/windows/network.c b/src/system/windows/network.c index 833e43a97..821a2883f 100644 --- a/src/system/windows/network.c +++ b/src/system/windows/network.c @@ -532,7 +532,7 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t * } size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep, - _z_bytes_t *addr) { + _z_slice_t *addr) { SOCKADDR_STORAGE raddr; unsigned int replen = sizeof(SOCKADDR_STORAGE); @@ -550,7 +550,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ if (!((a->sin_port == b->sin_port) && (a->sin_addr.s_addr == b->sin_addr.s_addr))) { // If addr is not NULL, it means that the rep was requested by the upper-layers if (addr != NULL) { - *addr = _z_bytes_make(sizeof(IN_ADDR) + sizeof(USHORT)); + *addr = _z_slice_make(sizeof(IN_ADDR) + sizeof(USHORT)); (void)memcpy((uint8_t *)addr->start, &b->sin_addr.s_addr, sizeof(IN_ADDR)); (void)memcpy((uint8_t *)(addr->start + sizeof(IN_ADDR)), &b->sin_port, sizeof(USHORT)); } @@ -563,7 +563,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ (memcmp(a->sin6_addr.s6_addr, b->sin6_addr.s6_addr, sizeof(struct in6_addr)) == 0))) { // If addr is not NULL, it means that the rep was requested by the upper-layers if (addr != NULL) { - *addr = _z_bytes_make(sizeof(struct in6_addr) + sizeof(USHORT)); + *addr = _z_slice_make(sizeof(struct in6_addr) + sizeof(USHORT)); (void)memcpy((uint8_t *)addr->start, &b->sin6_addr.s6_addr, sizeof(struct in6_addr)); (void)memcpy((uint8_t *)(addr->start + sizeof(struct in6_addr)), &b->sin6_port, sizeof(USHORT)); } @@ -578,7 +578,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ } size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, - const _z_sys_net_endpoint_t lep, _z_bytes_t *addr) { + const _z_sys_net_endpoint_t lep, _z_slice_t *addr) { size_t n = 0; uint8_t *pos = &ptr[0]; diff --git a/src/system/zephyr/network.c b/src/system/zephyr/network.c index 00d3ec1ce..c115d95fb 100644 --- a/src/system/zephyr/network.c +++ b/src/system/zephyr/network.c @@ -483,7 +483,7 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t * } size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep, - _z_bytes_t *addr) { + _z_slice_t *addr) { struct sockaddr_storage raddr; unsigned int raddrlen = sizeof(struct sockaddr_storage); @@ -501,7 +501,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ if (!((a->sin_port == b->sin_port) && (a->sin_addr.s_addr == b->sin_addr.s_addr))) { // If addr is not NULL, it means that the raddr was requested by the upper-layers if (addr != NULL) { - *addr = _z_bytes_make(sizeof(uint32_t) + sizeof(uint16_t)); + *addr = _z_slice_make(sizeof(uint32_t) + sizeof(uint16_t)); (void)memcpy((uint8_t *)addr->start, &b->sin_addr.s_addr, sizeof(uint32_t)); (void)memcpy((uint8_t *)(addr->start + sizeof(uint32_t)), &b->sin_port, sizeof(uint16_t)); } @@ -514,7 +514,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ (memcmp(a->sin6_addr.s6_addr, b->sin6_addr.s6_addr, sizeof(uint32_t) * 4UL) == 0))) { // If addr is not NULL, it means that the raddr was requested by the upper-layers if (addr != NULL) { - *addr = _z_bytes_make((sizeof(uint32_t) * 4UL) + sizeof(uint16_t)); + *addr = _z_slice_make((sizeof(uint32_t) * 4UL) + sizeof(uint16_t)); (void)memcpy((uint8_t *)addr->start, &b->sin6_addr.s6_addr, sizeof(uint32_t) * 4UL); (void)memcpy((uint8_t *)(addr->start + (sizeof(uint32_t) * 4UL)), &b->sin6_port, sizeof(uint16_t)); } @@ -529,7 +529,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ } size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, - const _z_sys_net_endpoint_t lep, _z_bytes_t *addr) { + const _z_sys_net_endpoint_t lep, _z_slice_t *addr) { size_t n = 0; uint8_t *pos = &ptr[0]; diff --git a/src/transport/multicast.c b/src/transport/multicast.c index db76bb01a..de047590e 100644 --- a/src/transport/multicast.c +++ b/src/transport/multicast.c @@ -45,7 +45,7 @@ void _zp_multicast_info_session(const _z_transport_t *zt, _z_config_t *ps) { _z_transport_peer_entry_list_t *xs = zt->_transport._multicast._peers; 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_slice_t remote_zid = _z_slice_wrap(peer->_remote_zid.id, _z_id_len(peer->_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/multicast/read.c b/src/transport/multicast/read.c index 9e293f990..1082a579a 100644 --- a/src/transport/multicast/read.c +++ b/src/transport/multicast/read.c @@ -28,7 +28,7 @@ int8_t _zp_multicast_read(_z_transport_multicast_t *ztm) { int8_t ret = _Z_RES_OK; - _z_bytes_t addr; + _z_slice_t addr; _z_transport_message_t t_msg; ret = _z_multicast_recv_t_msg(ztm, &t_msg, &addr); if (ret == _Z_RES_OK) { @@ -56,7 +56,7 @@ void *_zp_multicast_read_task(void *ztm_arg) { // Prepare the buffer _z_zbuf_reset(&ztm->_zbuf); - _z_bytes_t addr = _z_bytes_wrap(NULL, 0); + _z_slice_t addr = _z_slice_wrap(NULL, 0); while (ztm->_read_task_running == true) { // Read bytes from socket to the main buffer size_t to_read = 0; @@ -66,7 +66,7 @@ void *_zp_multicast_read_task(void *ztm_arg) { if (_z_zbuf_len(&ztm->_zbuf) < _Z_MSG_LEN_ENC_SIZE) { _z_link_recv_zbuf(&ztm->_link, &ztm->_zbuf, &addr); if (_z_zbuf_len(&ztm->_zbuf) < _Z_MSG_LEN_ENC_SIZE) { - _z_bytes_clear(&addr); + _z_slice_clear(&addr); _z_zbuf_compact(&ztm->_zbuf); continue; } @@ -109,7 +109,7 @@ void *_zp_multicast_read_task(void *ztm_arg) { if (ret == _Z_RES_OK) { _z_t_msg_clear(&t_msg); - _z_bytes_clear(&addr); + _z_slice_clear(&addr); } else { ztm->_read_task_running = false; continue; diff --git a/src/transport/multicast/rx.c b/src/transport/multicast/rx.c index 048a6563f..ffcbf0701 100644 --- a/src/transport/multicast/rx.c +++ b/src/transport/multicast/rx.c @@ -29,7 +29,7 @@ #if Z_FEATURE_MULTICAST_TRANSPORT == 1 static int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, - _z_bytes_t *addr) { + _z_slice_t *addr) { _Z_DEBUG(">> recv session msg"); int8_t ret = _Z_RES_OK; @@ -88,11 +88,11 @@ static int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_trans return ret; } -int8_t _z_multicast_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_bytes_t *addr) { +int8_t _z_multicast_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_slice_t *addr) { return _z_multicast_recv_t_msg_na(ztm, t_msg, addr); } #else -int8_t _z_multicast_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_bytes_t *addr) { +int8_t _z_multicast_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_slice_t *addr) { _ZP_UNUSED(ztm); _ZP_UNUSED(t_msg); _ZP_UNUSED(addr); @@ -102,7 +102,7 @@ int8_t _z_multicast_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_messa #if Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1 -static _z_transport_peer_entry_t *_z_find_peer_entry(_z_transport_peer_entry_list_t *l, _z_bytes_t *remote_addr) { +static _z_transport_peer_entry_t *_z_find_peer_entry(_z_transport_peer_entry_list_t *l, _z_slice_t *remote_addr) { _z_transport_peer_entry_t *ret = NULL; _z_transport_peer_entry_list_t *xs = l; @@ -121,7 +121,7 @@ static _z_transport_peer_entry_t *_z_find_peer_entry(_z_transport_peer_entry_lis } int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, - _z_bytes_t *addr) { + _z_slice_t *addr) { int8_t ret = _Z_RES_OK; #if Z_FEATURE_MULTI_THREAD == 1 // Acquire and keep the lock @@ -269,7 +269,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t } if (ret == _Z_RES_OK) { - entry->_remote_addr = _z_bytes_duplicate(addr); + entry->_remote_addr = _z_slice_duplicate(addr); entry->_remote_zid = t_msg->_body._join._zid; _z_conduit_sn_list_copy(&entry->_sn_rx_sns, &t_msg->_body._join._next_sn); @@ -348,7 +348,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t } #else int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, - _z_bytes_t *addr) { + _z_slice_t *addr) { _ZP_UNUSED(ztm); _ZP_UNUSED(t_msg); _ZP_UNUSED(addr); diff --git a/src/transport/peer_entry.c b/src/transport/peer_entry.c index 599660fcc..013c8c6de 100644 --- a/src/transport/peer_entry.c +++ b/src/transport/peer_entry.c @@ -23,7 +23,7 @@ void _z_transport_peer_entry_clear(_z_transport_peer_entry_t *src) { #endif src->_remote_zid = _z_id_empty(); - _z_bytes_clear(&src->_remote_addr); + _z_slice_clear(&src->_remote_addr); } void _z_transport_peer_entry_copy(_z_transport_peer_entry_t *dst, const _z_transport_peer_entry_t *src) { @@ -40,7 +40,7 @@ void _z_transport_peer_entry_copy(_z_transport_peer_entry_t *dst, const _z_trans dst->_received = src->_received; dst->_remote_zid = src->_remote_zid; - _z_bytes_copy(&dst->_remote_addr, &src->_remote_addr); + _z_slice_copy(&dst->_remote_addr, &src->_remote_addr); } size_t _z_transport_peer_entry_size(const _z_transport_peer_entry_t *src) { diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index a6430bc60..1970a3d34 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -80,8 +80,8 @@ static void _z_f_link_close_raweth(_z_link_t *self); static void _z_f_link_free_raweth(_z_link_t *self); static size_t _z_f_link_write_raweth(const _z_link_t *self, const uint8_t *ptr, size_t len); static size_t _z_f_link_write_all_raweth(const _z_link_t *self, const uint8_t *ptr, size_t len); -static size_t _z_f_link_read_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr); -static size_t _z_f_link_read_exact_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr); +static size_t _z_f_link_read_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr); +static size_t _z_f_link_read_exact_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr); static uint16_t _z_get_link_mtu_raweth(void); static _Bool _z_valid_iface_raweth(_z_str_intmap_t *config) { @@ -428,7 +428,7 @@ static size_t _z_f_link_write_all_raweth(const _z_link_t *self, const uint8_t *p return SIZE_MAX; } -static size_t _z_f_link_read_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +static size_t _z_f_link_read_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) { _ZP_UNUSED(self); _ZP_UNUSED(ptr); _ZP_UNUSED(len); @@ -436,7 +436,7 @@ static size_t _z_f_link_read_raweth(const _z_link_t *self, uint8_t *ptr, size_t return SIZE_MAX; } -static size_t _z_f_link_read_exact_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr) { +static size_t _z_f_link_read_exact_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_slice_t *addr) { _ZP_UNUSED(self); _ZP_UNUSED(ptr); _ZP_UNUSED(len); diff --git a/src/transport/raweth/read.c b/src/transport/raweth/read.c index 227f8cc55..dfb540700 100644 --- a/src/transport/raweth/read.c +++ b/src/transport/raweth/read.c @@ -29,14 +29,14 @@ int8_t _zp_raweth_read(_z_transport_multicast_t *ztm) { int8_t ret = _Z_RES_OK; - _z_bytes_t addr; + _z_slice_t addr; _z_transport_message_t t_msg; ret = _z_raweth_recv_t_msg(ztm, &t_msg, &addr); if (ret == _Z_RES_OK) { ret = _z_multicast_handle_transport_message(ztm, &t_msg, &addr); _z_t_msg_clear(&t_msg); } - _z_bytes_clear(&addr); + _z_slice_clear(&addr); return ret; } #else @@ -52,7 +52,7 @@ int8_t _zp_raweth_read(_z_transport_multicast_t *ztm) { void *_zp_raweth_read_task(void *ztm_arg) { _z_transport_multicast_t *ztm = (_z_transport_multicast_t *)ztm_arg; _z_transport_message_t t_msg; - _z_bytes_t addr = _z_bytes_wrap(NULL, 0); + _z_slice_t addr = _z_slice_wrap(NULL, 0); // Task loop while (ztm->_read_task_running == true) { @@ -64,25 +64,25 @@ void *_zp_raweth_read_task(void *ztm_arg) { break; case _Z_ERR_TRANSPORT_RX_FAILED: // Drop message - _z_bytes_clear(&addr); + _z_slice_clear(&addr); continue; break; default: // Drop message & stop task _Z_ERROR("Connection closed due to malformed message"); ztm->_read_task_running = false; - _z_bytes_clear(&addr); + _z_slice_clear(&addr); continue; break; } // Process message if (_z_multicast_handle_transport_message(ztm, &t_msg, &addr) != _Z_RES_OK) { ztm->_read_task_running = false; - _z_bytes_clear(&addr); + _z_slice_clear(&addr); continue; } _z_t_msg_clear(&t_msg); - _z_bytes_clear(&addr); + _z_slice_clear(&addr); } return NULL; } diff --git a/src/transport/raweth/rx.c b/src/transport/raweth/rx.c index 6732532ab..db135c9cb 100644 --- a/src/transport/raweth/rx.c +++ b/src/transport/raweth/rx.c @@ -27,7 +27,7 @@ #if Z_FEATURE_RAWETH_TRANSPORT == 1 -static size_t _z_raweth_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z_bytes_t *addr) { +static size_t _z_raweth_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z_slice_t *addr) { uint8_t *buff = _z_zbuf_get_wptr(zbf); size_t rb = _z_receive_raweth(&link->_socket._raweth._sock, buff, _z_zbuf_space_left(zbf), addr, &link->_socket._raweth._whitelist); @@ -73,7 +73,7 @@ static size_t _z_raweth_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z } /*------------------ Reception helper ------------------*/ -int8_t _z_raweth_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_bytes_t *addr) { +int8_t _z_raweth_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_slice_t *addr) { _Z_DEBUG(">> recv session msg"); int8_t ret = _Z_RES_OK; @@ -113,12 +113,12 @@ int8_t _z_raweth_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_messa return ret; } -int8_t _z_raweth_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_bytes_t *addr) { +int8_t _z_raweth_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_slice_t *addr) { return _z_raweth_recv_t_msg_na(ztm, t_msg, addr); } #else -int8_t _z_raweth_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_bytes_t *addr) { +int8_t _z_raweth_recv_t_msg(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_slice_t *addr) { _ZP_UNUSED(ztm); _ZP_UNUSED(t_msg); _ZP_UNUSED(addr); diff --git a/src/transport/raweth/tx.c b/src/transport/raweth/tx.c index faeada46e..9e75d3163 100644 --- a/src/transport/raweth/tx.c +++ b/src/transport/raweth/tx.c @@ -151,7 +151,7 @@ static int8_t __unsafe_z_raweth_write_header(_z_link_t *zl, _z_wbuf_t *wbf) { static int8_t _z_raweth_link_send_wbuf(const _z_link_t *zl, const _z_wbuf_t *wbf) { int8_t ret = _Z_RES_OK; for (size_t i = 0; (i < _z_wbuf_len_iosli(wbf)) && (ret == _Z_RES_OK); i++) { - _z_bytes_t bs = _z_iosli_to_bytes(_z_wbuf_get_iosli(wbf, i)); + _z_slice_t bs = _z_iosli_to_bytes(_z_wbuf_get_iosli(wbf, i)); size_t n = bs.len; do { diff --git a/src/transport/unicast.c b/src/transport/unicast.c index 650a99615..db869a0b5 100644 --- a/src/transport/unicast.c +++ b/src/transport/unicast.c @@ -39,7 +39,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_slice_t remote_zid_bytes = _z_slice_wrap(remote_zid.id, _z_id_len(remote_zid)); _z_string_t remote_zid_str = _z_string_convert_bytes(&remote_zid_bytes); _zp_config_insert(ps, Z_INFO_ROUTER_PID_KEY, remote_zid_str.val); _z_string_clear(&remote_zid_str); diff --git a/src/transport/unicast/transport.c b/src/transport/unicast/transport.c index 11e7467e6..1cbce4700 100644 --- a/src/transport/unicast/transport.c +++ b/src/transport/unicast/transport.c @@ -224,8 +224,8 @@ int8_t _z_unicast_open_client(_z_transport_unicast_establish_param_t *param, con // Create the OpenSyn message _z_zint_t lease = Z_TRANSPORT_LEASE; _z_zint_t initial_sn = param->_initial_sn_tx; - _z_bytes_t cookie; - _z_bytes_copy(&cookie, &iam._body._init._cookie); + _z_slice_t cookie; + _z_slice_copy(&cookie, &iam._body._init._cookie); _z_transport_message_t osm = _z_t_msg_make_open_syn(lease, initial_sn, cookie); diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 1204092f4..9c7a36939 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -83,10 +83,8 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_query_reply_options_default(&_ret_qreply_opt); // Reply value encoding - z_view_string_t reply_str; - z_view_string_wrap(&reply_str, value); z_owned_bytes_t reply_payload; - z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + z_bytes_encode_from_string(&reply_payload, value); z_query_reply(query, query_ke, z_move(reply_payload), &_ret_qreply_opt); diff --git a/tests/z_channels_test.c b/tests/z_channels_test.c index 7ca5d3358..da794fd9f 100644 --- a/tests/z_channels_test.c +++ b/tests/z_channels_test.c @@ -23,31 +23,34 @@ #undef NDEBUG #include -#define SEND(channel, v) \ - do { \ - _z_sample_t s = {.keyexpr = _z_rname("key"), \ - .payload = {.start = (const uint8_t *)v, .len = strlen(v)}, \ - .timestamp = _z_timestamp_null(), \ - .encoding = _z_encoding_null(), \ - .kind = 0, \ - .qos = {0}}; \ - z_loaned_sample_t sample = _z_sample_rc_new_from_val(s); \ - z_call(channel.send, &sample); \ +#define SEND(channel, v) \ + do { \ + _z_sample_t s = {.keyexpr = _z_rname("key"), \ + .payload = {._slice = {.start = (const uint8_t *)v, .len = strlen(v)}}, \ + .timestamp = _z_timestamp_null(), \ + .encoding = _z_encoding_null(), \ + .kind = 0, \ + .qos = {0}}; \ + 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(&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'; \ - } \ +#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 087ff18f7..c82bd25c4 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -62,10 +62,8 @@ void query_handler(const z_loaned_query_t *query, void *arg) { assert(strncmp((const char *)z_loan(pred)->val, "", strlen("")) == 0); // Reply value encoding - z_view_string_t reply_str; - z_view_string_wrap(&reply_str, res); z_owned_bytes_t reply_payload; - z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + z_bytes_encode_from_string(&reply_payload, res); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); queries++; @@ -83,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(payload->len == strlen(res)); - assert(strncmp(res, (const char *)payload->start, 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"); } @@ -104,17 +104,20 @@ 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((payload->len == MSG_LEN) || (payload->len == FRAGMENT_MSG_LEN)); + 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); } _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)); + _z_slice_t id_as_bytes = _z_slice_wrap(id->id, _z_id_len(*id)); return _z_string_convert_bytes(&id_as_bytes); } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 7f515198d..0937f42ac 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -26,7 +26,7 @@ #include #include -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/protocol/codec/declarations.h" @@ -80,7 +80,7 @@ void print_wbuf(_z_wbuf_t *wbf) { void print_zbuf(_z_zbuf_t *zbf) { print_iosli(&zbf->_ios); } -void print_uint8_array(_z_bytes_t *arr) { +void print_uint8_array(_z_slice_t *arr) { printf("Length: %zu, Buffer: [", arr->len); for (size_t i = 0; i < arr->len; i++) { printf("%02x", arr->start[i]); @@ -170,8 +170,8 @@ _z_wbuf_t gen_wbuf(size_t len) { return _z_wbuf_make(len, is_expandable); } -_z_bytes_t gen_payload(size_t len) { - _z_bytes_t pld; +_z_slice_t gen_payload(size_t len) { + _z_slice_t pld; pld._is_alloc = true; pld.len = len; pld.start = (uint8_t *)z_malloc(len); @@ -180,8 +180,8 @@ _z_bytes_t gen_payload(size_t len) { return pld; } -_z_bytes_t gen_bytes(size_t len) { - _z_bytes_t arr; +_z_slice_t gen_slice(size_t len) { + _z_slice_t arr; arr._is_alloc = true; arr.len = len; arr.start = NULL; @@ -195,6 +195,12 @@ _z_bytes_t gen_bytes(size_t len) { return arr; } +_z_bytes_t gen_bytes(size_t len) { + return (_z_bytes_t){ + ._slice = gen_slice(len), + }; +} + _z_id_t gen_zid(void) { _z_id_t id = _z_id_empty(); uint8_t hash = 55; @@ -244,9 +250,9 @@ _z_encoding_t gen_encoding(void) { _z_encoding_t en; en.id = gen_uint16(); if (gen_bool()) { - en.schema = gen_bytes(16); + en.schema = gen_slice(16); } else { - en.schema = _z_bytes_empty(); + en.schema = _z_slice_empty(); } return en; } @@ -255,7 +261,7 @@ _z_value_t gen_value(void) { _z_value_t val; val.encoding = gen_encoding(); if (gen_bool()) { - val.payload = _z_bytes_empty(); + val.payload = _z_bytes_null(); } else { val.payload = gen_bytes(16); } @@ -285,7 +291,7 @@ void assert_eq_iosli(_z_iosli_t *left, _z_iosli_t *right) { printf(")"); } -void assert_eq_uint8_array(const _z_bytes_t *left, const _z_bytes_t *right) { +void assert_eq_uint8_array(const _z_slice_t *left, const _z_slice_t *right) { printf("Array -> "); printf("Length (%zu:%zu), ", left->len, right->len); @@ -456,7 +462,7 @@ void zint_extension(void) { /*------------------ Unit extension ------------------*/ _z_msg_ext_t gen_zbuf_extension(void) { - _z_bytes_t val = gen_bytes(gen_uint8()); + _z_slice_t val = gen_slice(gen_uint8()); return _z_msg_ext_make_zbuf(_Z_MOCK_EXTENSION_ZBUF, val); } @@ -500,33 +506,37 @@ void zbuf_extension(void) { /* Message Fields */ /*=============================*/ /*------------------ Payload field ------------------*/ -void assert_eq_bytes(const _z_bytes_t *left, const _z_bytes_t *right) { assert_eq_uint8_array(left, right); } +void assert_eq_slice(const _z_slice_t *left, const _z_slice_t *right) { assert_eq_uint8_array(left, right); } + +void assert_eq_bytes(const _z_bytes_t *left, const _z_bytes_t *right) { + assert_eq_slice(&left->_slice, &right->_slice); +} void payload_field(void) { printf("\n>> Payload field\n"); _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); // Initialize - _z_bytes_t e_pld = gen_payload(64); + _z_slice_t e_pld = gen_payload(64); // Encode - int8_t res = _z_bytes_encode(&wbf, &e_pld); + int8_t res = _z_slice_encode(&wbf, &e_pld); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_bytes_t d_pld; - res = _z_bytes_decode(&d_pld, &zbf); + _z_slice_t d_pld; + res = _z_slice_decode(&d_pld, &zbf); assert(res == _Z_RES_OK); printf(" "); - assert_eq_bytes(&e_pld, &d_pld); + assert_eq_slice(&e_pld, &d_pld); printf("\n"); // Free - _z_bytes_clear(&e_pld); - _z_bytes_clear(&d_pld); + _z_slice_clear(&e_pld); + _z_slice_clear(&d_pld); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -543,7 +553,7 @@ void assert_eq_source_info(const _z_source_info_t *left, const _z_source_info_t } void assert_eq_encoding(const _z_encoding_t *left, const _z_encoding_t *right) { assert(left->id == right->id); - assert_eq_bytes(&left->schema, &right->schema); + assert_eq_slice(&left->schema, &right->schema); } void assert_eq_value(const _z_value_t *left, const _z_value_t *right) { assert_eq_encoding(&left->encoding, &right->encoding); @@ -1143,7 +1153,7 @@ _z_push_body_t gen_push_body(void) { return (_z_push_body_t){._is_put = true, ._body._put = { ._commons = commons, - ._payload = gen_bytes(64), + ._payload = gen_slice(64), ._encoding = gen_encoding(), }}; } else { @@ -1154,7 +1164,7 @@ _z_push_body_t gen_push_body(void) { void assert_eq_push_body(const _z_push_body_t *left, const _z_push_body_t *right) { assert(left->_is_put == right->_is_put); if (left->_is_put) { - assert_eq_bytes(&left->_body._put._payload, &right->_body._put._payload); + assert_eq_slice(&left->_body._put._payload, &right->_body._put._payload); assert_eq_encoding(&left->_body._put._encoding, &right->_body._put._encoding); assert_eq_timestamp(&left->_body._put._commons._timestamp, &right->_body._put._commons._timestamp); assert_eq_source_info(&left->_body._put._commons._source_info, &right->_body._put._commons._source_info); @@ -1196,7 +1206,7 @@ _z_msg_query_t gen_query(void) { return (_z_msg_query_t){ ._consolidation = (gen_uint8() % 4) - 1, ._ext_info = gen_source_info(), - ._parameters = gen_bytes(16), + ._parameters = gen_slice(16), ._ext_value = gen_bool() ? gen_value() : _z_value_null(), }; } @@ -1204,7 +1214,7 @@ _z_msg_query_t gen_query(void) { void assert_eq_query(const _z_msg_query_t *left, const _z_msg_query_t *right) { assert(left->_consolidation == right->_consolidation); assert_eq_source_info(&left->_ext_info, &right->_ext_info); - assert_eq_bytes(&left->_parameters, &right->_parameters); + assert_eq_slice(&left->_parameters, &right->_parameters); assert_eq_value(&left->_ext_value, &right->_ext_value); } @@ -1237,7 +1247,7 @@ _z_msg_err_t gen_err(void) { void assert_eq_err(const _z_msg_err_t *left, const _z_msg_err_t *right) { assert_eq_encoding(&left->encoding, &right->encoding); assert_eq_source_info(&left->_ext_source_info, &right->_ext_source_info); - assert_eq_bytes(&left->_payload, &right->_payload); + assert_eq_slice(&left->_payload, &right->_payload); } void err_message(void) { @@ -1518,14 +1528,14 @@ _z_transport_message_t gen_init(void) { if (gen_bool()) { return _z_t_msg_make_init_syn(gen_uint8() % 3, gen_zid()); } else { - return _z_t_msg_make_init_ack(gen_uint8() % 3, gen_zid(), gen_bytes(16)); + return _z_t_msg_make_init_ack(gen_uint8() % 3, gen_zid(), gen_slice(16)); } } void assert_eq_init(const _z_t_msg_init_t *left, const _z_t_msg_init_t *right) { assert(left->_batch_size == right->_batch_size); assert(left->_req_id_res == right->_req_id_res); assert(left->_seq_num_res == right->_seq_num_res); - assert_eq_bytes(&left->_cookie, &right->_cookie); + assert_eq_slice(&left->_cookie, &right->_cookie); assert(memcmp(left->_zid.id, right->_zid.id, 16) == 0); assert(left->_version == right->_version); assert(left->_whatami == right->_whatami); @@ -1548,13 +1558,13 @@ void init_message(void) { _z_transport_message_t gen_open(void) { if (gen_bool()) { - return _z_t_msg_make_open_syn(gen_uint32(), gen_uint32(), gen_bytes(16)); + return _z_t_msg_make_open_syn(gen_uint32(), gen_uint32(), gen_slice(16)); } else { return _z_t_msg_make_open_ack(gen_uint32(), gen_uint32()); } } void assert_eq_open(const _z_t_msg_open_t *left, const _z_t_msg_open_t *right) { - assert_eq_bytes(&left->_cookie, &right->_cookie); + assert_eq_slice(&left->_cookie, &right->_cookie); assert(left->_initial_sn == right->_initial_sn); assert(left->_lease == right->_lease); } @@ -1703,11 +1713,11 @@ void frame_message(void) { } _z_transport_message_t gen_fragment(void) { - return _z_t_msg_make_fragment(gen_uint32(), gen_bytes(gen_uint8()), gen_bool(), gen_bool()); + return _z_t_msg_make_fragment(gen_uint32(), gen_slice(gen_uint8()), gen_bool(), gen_bool()); } void assert_eq_fragment(const _z_t_msg_fragment_t *left, const _z_t_msg_fragment_t *right) { assert(left->_sn == right->_sn); - assert_eq_bytes(&left->_payload, &right->_payload); + assert_eq_slice(&left->_payload, &right->_payload); } void fragment_message(void) { printf("\n>> fragment message\n"); diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 898eeb508..93a17c10a 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -18,7 +18,7 @@ #include #include "zenoh-pico.h" -#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/collections/slice.h" #include "zenoh-pico/protocol/core.h" #undef NDEBUG @@ -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(payload->len == 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); } @@ -77,8 +79,8 @@ int main(int argc, char **argv) { 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_RC_IN_VAL(z_loan(s1))._local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s1))._local_zid)); + _z_slice_t id_as_bytes = + _z_slice_wrap(_Z_RC_IN_VAL(z_loan(s1))._local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s1))._local_zid)); _z_string_t zid1 = _z_string_convert_bytes(&id_as_bytes); printf("Session 1 with PID: %s\n", zid1.val); _z_string_clear(&zid1); @@ -96,7 +98,7 @@ int main(int argc, char **argv) { z_owned_session_t s2; 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)); + id_as_bytes = _z_slice_wrap(_Z_RC_IN_VAL(z_loan(s2))._local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s2))._local_zid)); _z_string_t zid2 = _z_string_convert_bytes(&id_as_bytes); printf("Session 2 with PID: %s\n", zid2.val); _z_string_clear(&zid2); diff --git a/tests/z_perf_rx.c b/tests/z_perf_rx.c index 331bebb1a..65511a4b4 100644 --- a/tests/z_perf_rx.c +++ b/tests/z_perf_rx.c @@ -41,14 +41,16 @@ 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); + 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 != payload->len) { + if (stats->curr_len != data_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 = data_len; + if (data_len == 1) { test_end = true; return; } @@ -56,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 8f1c8b95f..06fafd4ae 100644 --- a/tests/z_test_fragment_rx.c +++ b/tests/z_test_fragment_rx.c @@ -23,17 +23,20 @@ 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 = payload->start; - for (size_t i = 0; i < payload->len; i++) { + 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; break; } } - 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); + 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) {