Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _z_id_to_string function #608

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,18 @@ int8_t z_info_routers_zid(const z_loaned_session_t *zs, z_moved_closure_zid_t *c
*/
z_id_t z_info_zid(const z_loaned_session_t *zs);

/**
* Converts a Zenoh ID into a string for print purposes.
*
* Parameters:
* str: Pointer to uninitialized :c:type:`z_owned_string_t` to store the string.
* id: Pointer to the id to convert.
*
* Return:
* ``0`` if operation successful, ``negative value`` otherwise.
*/
z_result_t z_id_to_string(z_owned_string_t *str, z_id_t *id);

/**
* Gets the keyexpr from a sample by aliasing it.
*
Expand Down
9 changes: 9 additions & 0 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,15 @@ int8_t z_info_routers_zid(const z_loaned_session_t *zs, z_moved_closure_zid_t *c

z_id_t z_info_zid(const z_loaned_session_t *zs) { return _Z_RC_IN_VAL(zs)->_local_zid; }

z_result_t z_id_to_string(z_owned_string_t *str, z_id_t *id) {
_z_slice_t buf = _z_slice_alias_buf(id->id, sizeof(id->id));
str->_val = _z_string_convert_bytes(&buf);
if (!_z_string_check(&str->_val)) {
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
return _Z_RES_OK;
}

const z_loaned_keyexpr_t *z_sample_keyexpr(const z_loaned_sample_t *sample) { return &sample->keyexpr; }
z_sample_kind_t z_sample_kind(const z_loaned_sample_t *sample) { return sample->kind; }
const z_loaned_bytes_t *z_sample_payload(const z_loaned_sample_t *sample) { return &sample->payload; }
Expand Down
13 changes: 4 additions & 9 deletions src/collections/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,12 @@ _z_string_t _z_string_convert_bytes(const _z_slice_t *bs) {
return s;
}

if (s_val != NULL) {
const char c[] = "0123456789ABCDEF";
for (size_t i = 0; i < bs->len; i++) {
s_val[i * (size_t)2] = c[(bs->start[i] & (uint8_t)0xF0) >> (uint8_t)4];
s_val[(i * (size_t)2)] = c[bs->start[i] & (uint8_t)0x0F];
}
} else {
len = 0;
const char c[] = "0123456789ABCDEF";
for (size_t i = 0; i < bs->len; i++) {
s_val[i * (size_t)2] = c[(bs->start[i] & (uint8_t)0xF0) >> (uint8_t)4];
s_val[(i * (size_t)2) + 1] = c[bs->start[i] & (uint8_t)0x0F];
}
s._slice = _z_slice_from_buf_custom_deleter((const uint8_t *)s_val, len, _z_delete_context_default());

return s;
}

Expand Down
Loading