Skip to content

Commit

Permalink
Add decode bytes function (#6)
Browse files Browse the repository at this point in the history
* feat: add zenoh-c decode bytes function

* fix: rename string_from_bytes to string_convert_bytes

* feat: add _z_string_from_bytes function
  • Loading branch information
jean-roland authored May 29, 2024
1 parent c1d56d9 commit 46f0020
Show file tree
Hide file tree
Showing 27 changed files with 180 additions and 46 deletions.
6 changes: 4 additions & 2 deletions examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
18 changes: 13 additions & 5 deletions examples/arduino/z_queryable.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions examples/espidf/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
8 changes: 8 additions & 0 deletions examples/espidf/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
z_query_parameters(query, &params);
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
Expand Down
7 changes: 5 additions & 2 deletions examples/freertos_plus_tcp/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
10 changes: 7 additions & 3 deletions examples/freertos_plus_tcp/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
z_query_parameters(query, &params);
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);
Expand Down
7 changes: 5 additions & 2 deletions examples/mbed/z_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
9 changes: 8 additions & 1 deletion examples/mbed/z_queryable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions examples/unix/c11/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ 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");
z_attachment_iterate(sample.attachment, attachment_handler, NULL);
}
#endif
z_drop(z_move(keystr));
z_drop(z_move(replystr));
} else {
printf(">> Received an error\n");
}
Expand Down
7 changes: 5 additions & 2 deletions examples/unix/c11/z_get_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
10 changes: 7 additions & 3 deletions examples/unix/c11/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
z_query_parameters(query, &params);
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);
Expand Down
9 changes: 6 additions & 3 deletions examples/unix/c11/z_queryable_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ int main(int argc, char **argv) {
z_query_parameters(q, &params);
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);
Expand Down
7 changes: 5 additions & 2 deletions examples/unix/c99/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
8 changes: 8 additions & 0 deletions examples/unix/c99/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
z_query_parameters(query, &params);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_string_loan(&keystr)),
(int)z_view_string_loan(&params)->len, z_view_string_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_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);
Expand Down
7 changes: 5 additions & 2 deletions examples/windows/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
11 changes: 7 additions & 4 deletions examples/windows/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
z_query_parameters(query, &params);
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
Expand Down
7 changes: 5 additions & 2 deletions examples/zephyr/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
9 changes: 8 additions & 1 deletion examples/zephyr/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
z_query_parameters(query, &params);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), z_loan(params)->len,
z_loan(params)->val);

// 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);
Expand Down
18 changes: 18 additions & 0 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
2 changes: 2 additions & 0 deletions include/zenoh-pico/collections/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 46f0020

Please sign in to comment.