From 46f00205db483a971254cda4531e96df28930136 Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Wed, 29 May 2024 12:39:02 +0200 Subject: [PATCH] Add decode bytes function (#6) * feat: add zenoh-c decode bytes function * fix: rename string_from_bytes to string_convert_bytes * feat: add _z_string_from_bytes function --- examples/arduino/z_get.ino | 6 ++++-- examples/arduino/z_queryable.ino | 18 ++++++++++++----- examples/espidf/z_get.c | 6 ++++-- examples/espidf/z_queryable.c | 8 ++++++++ examples/freertos_plus_tcp/z_get.c | 7 +++++-- examples/freertos_plus_tcp/z_queryable.c | 10 +++++++--- examples/mbed/z_get.cpp | 7 +++++-- examples/mbed/z_queryable.cpp | 9 ++++++++- examples/unix/c11/z_get.c | 7 +++++-- examples/unix/c11/z_get_channel.c | 7 +++++-- examples/unix/c11/z_queryable.c | 10 +++++++--- examples/unix/c11/z_queryable_channel.c | 9 ++++++--- examples/unix/c99/z_get.c | 7 +++++-- examples/unix/c99/z_queryable.c | 8 ++++++++ examples/windows/z_get.c | 7 +++++-- examples/windows/z_queryable.c | 11 +++++++---- examples/zephyr/z_get.c | 7 +++++-- examples/zephyr/z_queryable.c | 9 ++++++++- include/zenoh-pico/api/primitives.h | 18 +++++++++++++++++ include/zenoh-pico/collections/string.h | 2 ++ src/api/api.c | 16 +++++++++++++++ src/collections/string.c | 25 ++++++++++++++++++++++-- src/net/session.c | 2 +- src/transport/multicast.c | 2 +- src/transport/unicast.c | 2 +- tests/z_client_test.c | 2 +- tests/z_peer_multicast_test.c | 4 ++-- 27 files changed, 180 insertions(+), 46 deletions(-) diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 70cc7c328..3fc12d587 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -48,15 +48,17 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(oreply); z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - std::string val((const char *)z_sample_payload(sample)->start, z_sample_payload(sample)->len); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); Serial.print(" >> [Get listener] Received ("); Serial.print(z_str_data(z_string_loan(&keystr))); Serial.print(", "); - Serial.print(val.c_str()); + Serial.print(z_str_data(z_string_loan(&replystr))); Serial.println(")"); z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&replystr)); } else { Serial.println(" >> Received an error"); } diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index 5f7b69850..b7f652b7f 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -38,12 +38,20 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_owned_string_t keystr; z_keyexpr_to_string(z_query_keyexpr(query), &keystr); - - Serial.print(" >> [Queryable handler] Replying Data ('"); + Serial.print(" >> [Queryable handler] Received Query '"); Serial.print(z_str_data(z_string_loan(&keystr))); - Serial.print("': '"); - Serial.print(VALUE); - Serial.println("')"); + Serial.println("'"); + + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + Serial.print(" with value '"); + Serial.print(z_str_data(z_string_loan(&payload_string))); + Serial.println("'"); + z_string_drop(z_string_move(&payload_string)); + } z_view_keyexpr_t ke; z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index 9424c03d3..889e4e1a5 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -108,10 +108,12 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(oreply); z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); - printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, - z_sample_payload(sample)->start); + printf(" >> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(" >> Received an error\n"); } diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index 9eafadb57..dbf232594 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -109,6 +109,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, z_loan(params)->val); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_str_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); + } z_view_keyexpr_t ke; z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); // Reply value encoding diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index 49744ae6d..3c7ddbb3b 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -42,9 +42,12 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(reply); z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - const z_loaned_bytes_t *payload = z_sample_payload(sample); - printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 39fbabfad..be8e1672f 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -37,9 +37,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, z_loan(params)->val); - const z_loaned_value_t *payload_value = z_query_value(query); - if (payload_value->payload.len > 0) { - printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_str_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } z_query_reply_options_t options; z_query_reply_options_default(&options); diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index ded6f7e9e..d5f89b09d 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -38,9 +38,12 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(oreply); z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - const z_loaned_bytes_t *payload = z_sample_payload(sample); - printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, payload->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(" >> Received ('%s': '%s')\n", z_str_data(z_string_loan(&keystr)), z_str_data(z_string_loan(&replystr))); z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&replystr)); } else { printf(" >> Received an error\n"); } diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index 5828f920f..64f68e9fa 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -39,7 +39,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, &pred); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_string_loan(&keystr)), (int)z_view_string_loan(&pred)->len, z_view_string_loan(&pred)->val); - + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_str_data(z_string_loan(&payload_string))); + z_string_drop(z_string_move(&payload_string)); + } // Reply value encoding z_view_string_t reply_str; z_view_str_wrap(&reply_str, VALUE); diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index 14ab50d40..b650556ed 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -43,8 +43,10 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(reply); z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, - z_sample_payload(sample)->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); #if Z_FEATURE_ATTACHMENT == 1 if (z_attachment_check(&sample.attachment)) { printf("Attachement found\n"); @@ -52,6 +54,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { } #endif z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index e5690e13f..daefcd25c 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -112,9 +112,12 @@ int main(int argc, char **argv) { const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply)); z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, - z_sample_payload(sample)->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 5cc32f236..b480c3636 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -39,9 +39,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, z_loan(params)->val); - const z_loaned_value_t *payload_value = z_query_value(query); - if (payload_value->payload.len > 0) { - printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_str_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t attachment = z_query_attachment(query); diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index d5df62bf7..562e6cd66 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -106,9 +106,12 @@ int main(int argc, char **argv) { z_query_parameters(q, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, z_loan(params)->val); - const z_loaned_value_t *payload_value = z_query_value(q); - if (payload_value->payload.len > 0) { - printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(z_loan(query))); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_str_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } z_query_reply_options_t options; z_query_reply_options_default(&options); diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 08d51e440..78a9a6ad4 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -35,9 +35,12 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(reply); z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - const z_loaned_bytes_t *payload = z_sample_payload(sample); - printf(">> Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, payload->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_str_data(z_string_loan(&keystr)), z_str_data(z_string_loan(&replystr))); z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&replystr)); } else { printf(">> Received an error\n"); } diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index 8f69c8387..de03d1c49 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -30,6 +30,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_string_loan(&keystr)), (int)z_view_string_loan(¶ms)->len, z_view_string_loan(¶ms)->val); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_str_data(z_string_loan(&payload_string))); + z_string_drop(z_string_move(&payload_string)); + } z_query_reply_options_t options; z_query_reply_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 7d77faf1e..8e0192618 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -34,9 +34,12 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(reply); z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - const z_loaned_bytes_t *payload = z_sample_payload(sample); - printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index d4540ec64..d5c06db56 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -30,11 +30,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, z_loan(params)->val); - const z_loaned_value_t *payload_value = z_query_value(query); - if (payload_value->payload.len > 0) { - printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_str_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } - z_query_reply_options_t options; z_query_reply_options_default(&options); // Reply value encoding diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index fe478deef..f4213b96c 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -39,9 +39,12 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(oreply); z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - const z_loaned_bytes_t *payload = z_sample_payload(sample); - printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(" >> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(" >> Received an error\n"); } diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index 173191bb5..3cbb4c3bb 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -39,7 +39,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), z_loan(params)->len, z_loan(params)->val); - + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_str_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); + } // Reply value encoding z_view_string_t reply_str; z_view_str_wrap(&reply_str, VALUE); diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 2979a9f84..6a782b3a4 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -413,6 +413,24 @@ z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix); */ z_encoding_t z_encoding_default(void); +/** + * Returns value payload. + */ +const z_loaned_bytes_t *z_value_payload(const z_loaned_value_t *value); + +/** + * Returns total number of bytes in the payload. + */ +size_t z_bytes_len(const z_loaned_bytes_t *bytes); + +/** + * Decodes data into an owned null-terminated string. + * + * @param this_: Data to decode. + * @param dst: An unitialized memory location where to construct a decoded string. + */ +int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *s); + /** * Encodes string by aliasing. */ diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index bd0b599b4..2c7f95bdd 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -69,6 +69,7 @@ typedef struct { char *val; } _z_string_t; +_z_string_t _z_string_null(void); _z_string_t _z_string_make(const char *value); _z_string_t _z_string_wrap(char *value); _z_string_t *_z_string_make_as_ptr(const char *value); @@ -80,6 +81,7 @@ void _z_string_move_str(_z_string_t *dst, char *src); void _z_string_clear(_z_string_t *s); void _z_string_free(_z_string_t **s); void _z_string_reset(_z_string_t *s); +_z_string_t _z_string_convert_bytes(const _z_bytes_t *bs); _z_string_t _z_string_from_bytes(const _z_bytes_t *bs); _Z_ELEM_DEFINE(_z_string, _z_string_t, _z_string_size, _z_string_clear, _z_string_copy) diff --git a/src/api/api.c b/src/api/api.c index 04e19ea18..64a517adb 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -226,6 +226,22 @@ z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix) { z_encoding_t z_encoding_default(void) { return z_encoding(Z_ENCODING_PREFIX_DEFAULT, NULL); } +const z_loaned_bytes_t *z_value_payload(const z_loaned_value_t *value) { return &value->payload; } + +size_t z_bytes_len(const z_loaned_bytes_t *bytes) { return bytes->len; } + +int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *s) { + // Init owned string + z_string_null(s); + s->_val = (_z_string_t *)z_malloc(sizeof(_z_string_t)); + if (s->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Convert bytes to string + *s->_val = _z_string_from_bytes(bytes); + return _Z_RES_OK; +} + int8_t z_bytes_encode_from_string(z_owned_bytes_t *buffer, const z_loaned_string_t *s) { // Init owned bytes z_bytes_null(buffer); diff --git a/src/collections/string.c b/src/collections/string.c index 927dc7dab..1f25c985a 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -18,6 +18,11 @@ #include /*-------- string --------*/ +_z_string_t _z_string_null(void) { + _z_string_t s = {.len = 0, .val = NULL}; + return s; +} + _z_string_t _z_string_make(const char *value) { _z_string_t s; s.val = _z_str_clone(value); @@ -83,8 +88,8 @@ void _z_string_free(_z_string_t **str) { } } -_z_string_t _z_string_from_bytes(const _z_bytes_t *bs) { - _z_string_t s; +_z_string_t _z_string_convert_bytes(const _z_bytes_t *bs) { + _z_string_t s = _z_string_null(); size_t len = bs->len * (size_t)2; char *s_val = (char *)z_malloc((len + (size_t)1) * sizeof(char)); @@ -105,6 +110,22 @@ _z_string_t _z_string_from_bytes(const _z_bytes_t *bs) { return s; } +_z_string_t _z_string_from_bytes(const _z_bytes_t *bs) { + _z_string_t s = _z_string_null(); + // Allocate string + s.len = bs->len + (size_t)1; // bytes data + null terminator + char *str_val = (char *)z_malloc(s.len * sizeof(char)); + if (str_val == NULL) { + return s; + } + // Recopy data + s.val = str_val; + memcpy(s.val, bs->start, bs->len); + // Set null terminator + s.val[bs->len] = '\0'; + return s; +} + /*-------- str --------*/ size_t _z_str_size(const char *src) { return strlen(src) + (size_t)1; } diff --git a/src/net/session.c b/src/net/session.c index 3419cabda..509cb294b 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -157,7 +157,7 @@ _z_config_t *_z_info(const _z_session_t *zn) { _z_bytes_t local_zid = _z_bytes_wrap(zn->_local_zid.id, _z_id_len(zn->_local_zid)); // TODO(sasahcmc): is it zero terminated??? // rework it!!! - _zp_config_insert(ps, Z_INFO_PID_KEY, _z_string_from_bytes(&local_zid).val); + _zp_config_insert(ps, Z_INFO_PID_KEY, _z_string_convert_bytes(&local_zid).val); switch (zn->_tp._type) { case _Z_TRANSPORT_UNICAST_TYPE: diff --git a/src/transport/multicast.c b/src/transport/multicast.c index 14d0fe4df..db76bb01a 100644 --- a/src/transport/multicast.c +++ b/src/transport/multicast.c @@ -46,7 +46,7 @@ void _zp_multicast_info_session(const _z_transport_t *zt, _z_config_t *ps) { while (xs != NULL) { _z_transport_peer_entry_t *peer = _z_transport_peer_entry_list_head(xs); _z_bytes_t remote_zid = _z_bytes_wrap(peer->_remote_zid.id, _z_id_len(peer->_remote_zid)); - _z_string_t remote_zid_str = _z_string_from_bytes(&remote_zid); + _z_string_t remote_zid_str = _z_string_convert_bytes(&remote_zid); _zp_config_insert(ps, Z_INFO_PEER_PID_KEY, remote_zid_str.val); _z_string_clear(&remote_zid_str); diff --git a/src/transport/unicast.c b/src/transport/unicast.c index f7d066bb8..650a99615 100644 --- a/src/transport/unicast.c +++ b/src/transport/unicast.c @@ -40,7 +40,7 @@ void _zp_unicast_fetch_zid(const _z_transport_t *zt, z_owned_closure_zid_t *call void _zp_unicast_info_session(const _z_transport_t *zt, _z_config_t *ps) { _z_id_t remote_zid = zt->_transport._unicast._remote_zid; _z_bytes_t remote_zid_bytes = _z_bytes_wrap(remote_zid.id, _z_id_len(remote_zid)); - _z_string_t remote_zid_str = _z_string_from_bytes(&remote_zid_bytes); + _z_string_t remote_zid_str = _z_string_convert_bytes(&remote_zid_bytes); _zp_config_insert(ps, Z_INFO_ROUTER_PID_KEY, remote_zid_str.val); _z_string_clear(&remote_zid_str); } diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 0e96d9d26..f23d1f8ea 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -115,7 +115,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { _z_string_t format_id(const z_id_t *id) { _z_bytes_t id_as_bytes = _z_bytes_wrap(id->id, _z_id_len(*id)); - return _z_string_from_bytes(&id_as_bytes); + return _z_string_convert_bytes(&id_as_bytes); } int main(int argc, char **argv) { diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 6808d8f23..898eeb508 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -79,7 +79,7 @@ int main(int argc, char **argv) { assert(z_check(s1)); _z_bytes_t id_as_bytes = _z_bytes_wrap(_Z_RC_IN_VAL(z_loan(s1))._local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s1))._local_zid)); - _z_string_t zid1 = _z_string_from_bytes(&id_as_bytes); + _z_string_t zid1 = _z_string_convert_bytes(&id_as_bytes); printf("Session 1 with PID: %s\n", zid1.val); _z_string_clear(&zid1); @@ -97,7 +97,7 @@ int main(int argc, char **argv) { z_open(&s2, z_move(config)); assert(z_check(s2)); id_as_bytes = _z_bytes_wrap(_Z_RC_IN_VAL(z_loan(s2))._local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s2))._local_zid)); - _z_string_t zid2 = _z_string_from_bytes(&id_as_bytes); + _z_string_t zid2 = _z_string_convert_bytes(&id_as_bytes); printf("Session 2 with PID: %s\n", zid2.val); _z_string_clear(&zid2);