Skip to content

Commit

Permalink
add z_hello_t accessors and z_whatami_to_view_string (#506)
Browse files Browse the repository at this point in the history
* add z_hello_t accessors;
make z_hello_t fields private;
add z_whatami_to_view_string;
change z_what, z_whatami flag values to correspond to what is exposed in zenoh / zenoh-c

* fix warning and add docs

* fix typo

* fix arduino scouting example

* fix mbed scouting example
  • Loading branch information
DenisBiryukov91 authored Jul 4, 2024
1 parent 05ccf02 commit 23919e2
Show file tree
Hide file tree
Showing 24 changed files with 189 additions and 124 deletions.
4 changes: 4 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ Primitives
.. autocfunction:: primitives.h::z_string_from_substr
.. autocfunction:: primitives.h::z_string_empty
.. autocfunction:: primitives.h::z_string_is_empty
.. autocfunction:: primitives.h::z_hello_zid
.. autocfunction:: primitives.h::z_hello_whatami
.. autocfunction:: primitives.h::z_hello_locators
.. autocfunction:: primitives.h::z_whatami_to_view_string
.. autocfunction:: primitives.h::z_scout
.. autocfunction:: primitives.h::z_open
.. autocfunction:: primitives.h::z_close
Expand Down
18 changes: 7 additions & 11 deletions examples/arduino/z_scout.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@ void fprintzid(z_id_t zid) {
}
}

void fprintwhatami(unsigned int whatami) {
if (whatami == Z_WHATAMI_ROUTER) {
Serial.print("'Router'");
} else if (whatami == Z_WHATAMI_PEER) {
Serial.print("'Peer'");
} else {
Serial.print("'Other'");
}
void fprintwhatami(z_whatami_t whatami) {
z_view_string_t s;
z_whatami_to_view_string(whatami, &s);
Serial.write(z_string_data(z_view_string_loan(&s)), z_string_len(z_view_string_loan(&s)));
}

void fprintlocators(const z_loaned_string_array_t *locs) {
Expand All @@ -71,11 +67,11 @@ void fprintlocators(const z_loaned_string_array_t *locs) {

void fprinthello(const z_loaned_hello_t *hello) {
Serial.print(" >> Hello { zid: ");
fprintzid(hello->zid);
fprintzid(z_hello_zid(hello));
Serial.print(", whatami: ");
fprintwhatami(hello->whatami);
fprintwhatami(z_hello_whatami(hello));
Serial.print(", locators: ");
fprintlocators(&hello->locators);
fprintlocators(z_hello_locators(hello));
Serial.println(" }");
}

Expand Down
18 changes: 7 additions & 11 deletions examples/espidf/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,10 @@ void fprintzid(FILE *stream, z_id_t zid) {
}
}

void fprintwhatami(FILE *stream, unsigned int whatami) {
if (whatami == Z_WHATAMI_ROUTER) {
fprintf(stream, "\"Router\"");
} else if (whatami == Z_WHATAMI_PEER) {
fprintf(stream, "\"Peer\"");
} else {
fprintf(stream, "\"Other\"");
}
void fprintwhatami(FILE *stream, z_whatami_t whatami) {
z_view_string_t s;
z_whatami_to_view_string(whatami, &s);
fprintf(stream, "\"%.*s\"", (int)z_string_len(z_loan(s)), z_string_data(z_loan(s)));
}

void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {
Expand All @@ -125,11 +121,11 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {

void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, "Hello { zid: ");
fprintzid(stream, hello->zid);
fprintzid(stream, z_hello_zid(hello));
fprintf(stream, ", whatami: ");
fprintwhatami(stream, hello->whatami);
fprintwhatami(stream, z_hello_whatami(hello));
fprintf(stream, ", locators: ");
fprintlocators(stream, &hello->locators);
fprintlocators(stream, z_hello_locators(hello));
fprintf(stream, " }");
}

Expand Down
18 changes: 7 additions & 11 deletions examples/freertos_plus_tcp/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ void fprintzid(FILE *stream, z_id_t zid) {
}
}

void fprintwhatami(FILE *stream, unsigned int whatami) {
if (whatami == Z_WHATAMI_ROUTER) {
fprintf(stream, "\"Router\"");
} else if (whatami == Z_WHATAMI_PEER) {
fprintf(stream, "\"Peer\"");
} else {
fprintf(stream, "\"Other\"");
}
void fprintwhatami(FILE *stream, z_whatami_t whatami) {
z_view_string_t s;
z_whatami_to_view_string(whatami, &s);
fprintf(stream, "\"%.*s\"", (int)z_string_len(z_loan(s)), z_string_data(z_loan(s)));
}

void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {
Expand All @@ -59,11 +55,11 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {

void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, "Hello { zid: ");
fprintzid(stream, hello->zid);
fprintzid(stream, z_hello_zid(hello));
fprintf(stream, ", whatami: ");
fprintwhatami(stream, hello->whatami);
fprintwhatami(stream, z_hello_whatami(hello));
fprintf(stream, ", locators: ");
fprintlocators(stream, &hello->locators);
fprintlocators(stream, z_hello_locators(hello));
fprintf(stream, " }");
}

Expand Down
18 changes: 7 additions & 11 deletions examples/mbed/z_scout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,10 @@ void fprintzid(FILE *stream, z_id_t zid) {
}
}

void fprintwhatami(FILE *stream, unsigned int whatami) {
if (whatami == Z_WHATAMI_ROUTER) {
fprintf(stream, "\"Router\"");
} else if (whatami == Z_WHATAMI_PEER) {
fprintf(stream, "\"Peer\"");
} else {
fprintf(stream, "\"Other\"");
}
void fprintwhatami(FILE *stream, z_whatami_t whatami) {
z_view_string_t s;
z_whatami_to_view_string(whatami, &s);
fprintf(stream, "\"%.*s\"", (int)z_string_len(z_view_string_loan(&s)), z_string_data(z_view_string_loan(&s)));
}

void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {
Expand All @@ -67,11 +63,11 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {

void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, "Hello { zid: ");
fprintzid(stream, hello->zid);
fprintzid(stream, z_hello_zid(hello));
fprintf(stream, ", whatami: ");
fprintwhatami(stream, hello->whatami);
fprintwhatami(stream, z_hello_whatami(hello));
fprintf(stream, ", locators: ");
fprintlocators(stream, &hello->locators);
fprintlocators(stream, z_hello_locators(hello));
fprintf(stream, " }");
}

Expand Down
18 changes: 7 additions & 11 deletions examples/unix/c11/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,10 @@ void fprintzid(FILE *stream, z_id_t zid) {
}
}

void fprintwhatami(FILE *stream, unsigned int whatami) {
if (whatami == Z_WHATAMI_ROUTER) {
fprintf(stream, "\"Router\"");
} else if (whatami == Z_WHATAMI_PEER) {
fprintf(stream, "\"Peer\"");
} else {
fprintf(stream, "\"Other\"");
}
void fprintwhatami(FILE *stream, z_whatami_t whatami) {
z_view_string_t s;
z_whatami_to_view_string(whatami, &s);
fprintf(stream, "\"%.*s\"", (int)z_string_len(z_loan(s)), z_string_data(z_loan(s)));
}

void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {
Expand All @@ -57,11 +53,11 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {

void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, "Hello { zid: ");
fprintzid(stream, hello->zid);
fprintzid(stream, z_hello_zid(hello));
fprintf(stream, ", whatami: ");
fprintwhatami(stream, hello->whatami);
fprintwhatami(stream, z_hello_whatami(hello));
fprintf(stream, ", locators: ");
fprintlocators(stream, &hello->locators);
fprintlocators(stream, z_hello_locators(hello));
fprintf(stream, " }");
}

Expand Down
18 changes: 7 additions & 11 deletions examples/unix/c99/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ void fprintzid(FILE *stream, z_id_t zid) {
}
}

void fprintwhatami(FILE *stream, unsigned int whatami) {
if (whatami == Z_WHATAMI_ROUTER) {
fprintf(stream, "\"Router\"");
} else if (whatami == Z_WHATAMI_PEER) {
fprintf(stream, "\"Peer\"");
} else {
fprintf(stream, "\"Other\"");
}
void fprintwhatami(FILE *stream, z_whatami_t whatami) {
z_view_string_t s;
z_whatami_to_view_string(whatami, &s);
fprintf(stream, "\"%.*s\"", (int)z_string_len(z_view_string_loan(&s)), z_string_data(z_view_string_loan(&s)));
}

void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {
Expand All @@ -56,11 +52,11 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {

void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, "Hello { zid: ");
fprintzid(stream, hello->zid);
fprintzid(stream, z_hello_zid(hello));
fprintf(stream, ", whatami: ");
fprintwhatami(stream, hello->whatami);
fprintwhatami(stream, z_hello_whatami(hello));
fprintf(stream, ", locators: ");
fprintlocators(stream, &hello->locators);
fprintlocators(stream, z_hello_locators(hello));
fprintf(stream, " }");
}

Expand Down
18 changes: 7 additions & 11 deletions examples/windows/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ void fprintzid(FILE *stream, z_id_t zid) {
}
}

void fprintwhatami(FILE *stream, unsigned int whatami) {
if (whatami == Z_WHATAMI_ROUTER) {
fprintf(stream, "\"Router\"");
} else if (whatami == Z_WHATAMI_PEER) {
fprintf(stream, "\"Peer\"");
} else {
fprintf(stream, "\"Other\"");
}
void fprintwhatami(FILE *stream, z_whatami_t whatami) {
z_view_string_t s;
z_whatami_to_view_string(whatami, &s);
fprintf(stream, "\"%.*s\"", (int)z_string_len(z_loan(s)), z_string_data(z_loan(s)));
}

void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {
Expand All @@ -56,11 +52,11 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {

void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, "Hello { zid: ");
fprintzid(stream, hello->zid);
fprintzid(stream, z_hello_zid(hello));
fprintf(stream, ", whatami: ");
fprintwhatami(stream, hello->whatami);
fprintwhatami(stream, z_hello_whatami(hello));
fprintf(stream, ", locators: ");
fprintlocators(stream, &hello->locators);
fprintlocators(stream, z_hello_locators(hello));
fprintf(stream, " }");
}

Expand Down
18 changes: 7 additions & 11 deletions examples/zephyr/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ void fprintzid(FILE *stream, z_id_t zid) {
}
}

void fprintwhatami(FILE *stream, unsigned int whatami) {
if (whatami == Z_WHATAMI_ROUTER) {
fprintf(stream, "\"Router\"");
} else if (whatami == Z_WHATAMI_PEER) {
fprintf(stream, "\"Peer\"");
} else {
fprintf(stream, "\"Other\"");
}
void fprintwhatami(FILE *stream, z_whatami_t whatami) {
z_view_string_t s;
z_whatami_to_view_string(whatami, &s);
fprintf(stream, "\"%.*s\"", (int)z_string_len(z_loan(s)), z_string_data(z_loan(s)));
}

void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {
Expand All @@ -53,11 +49,11 @@ void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {

void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, "Hello { zid: ");
fprintzid(stream, hello->zid);
fprintzid(stream, z_hello_zid(hello));
fprintf(stream, ", whatami: ");
fprintwhatami(stream, hello->whatami);
fprintwhatami(stream, z_hello_whatami(hello));
fprintf(stream, ", locators: ");
fprintlocators(stream, &hello->locators);
fprintlocators(stream, z_hello_locators(hello));
fprintf(stream, " }");
}

Expand Down
16 changes: 12 additions & 4 deletions include/zenoh-pico/api/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern "C" {
#endif

/**
* What values.
* What bitmask for scouting.
*
* Enumerators:
* Z_WHAT_ROUTER: Router.
Expand All @@ -32,7 +32,11 @@ extern "C" {
typedef enum {
Z_WHAT_ROUTER = 0x01, // Router
Z_WHAT_PEER = 0x02, // Peer
Z_WHAT_CLIENT = 0x03 // Client
Z_WHAT_CLIENT = 0x04, // Client
Z_WHAT_ROUTER_PEER = (0x01 | 0x02),
Z_WHAT_ROUTER_CLIENT = (0x01 | 0x04),
Z_WHAT_PEER_CLIENT = (0x02 | 0x04),
Z_WHAT_ROUTER_PEER_CLIENT = ((0x01 | 0x02) | 0x04),
} z_what_t;

/**
Expand All @@ -43,8 +47,12 @@ typedef enum {
* Z_WHATAMI_PEER: Bitmask to filter for Zenoh peers.
* Z_WHATAMI_CLIENT: Bitmask to filter for Zenoh clients.
*/
typedef enum { Z_WHATAMI_ROUTER = 0x00, Z_WHATAMI_PEER = 0x01, Z_WHATAMI_CLIENT = 0x02 } z_whatami_t;
#define Z_WHATAMI_DEFAULT Z_WHATAMI_ROUTER
typedef enum z_whatami_t {
Z_WHATAMI_ROUTER = 0x01,
Z_WHATAMI_PEER = 0x02,
Z_WHATAMI_CLIENT = 0x04,
} z_whatami_t;
#define Z_WHATAMI_DEFAULT Z_WHATAMI_ROUTER;

/**
* Status values for keyexpr canonization operation.
Expand Down
43 changes: 43 additions & 0 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,49 @@ int8_t z_string_from_substr(z_owned_string_t *str, const char *value, size_t len
*/
bool z_string_is_empty(const z_loaned_string_t *str);

/**
* Returns id of Zenoh entity that transmitted hello message.
*
* Parameters:
* hello: Pointer to a :c:type:`z_loaned_hello_t` message.
* Return:
* Id of the Zenoh entity that transmitted hello message.
*/
z_id_t z_hello_zid(const z_loaned_hello_t *hello);

/**
* Returns type of Zenoh entity that transmitted hello message.
*
* Parameters:
* hello: Pointer to a :c:type:`z_loaned_hello_t` message.
* Return:
* Type of the Zenoh entity that transmitted hello message.
*/
z_whatami_t z_hello_whatami(const z_loaned_hello_t *hello);

/**
* Constructs an array of locators of Zenoh entity that sent hello message.
*
* Parameters:
* hello: Pointer to a :c:type:`z_loaned_hello_t` message.
* Return:
* :c:type:`z_loaned_string_array_t` containing locators.
*/
const z_loaned_string_array_t *z_hello_locators(const z_loaned_hello_t *hello);

/**
* Constructs a non-owned non-null-terminated string from the kind of zenoh entity.
*
* The string has static storage (i.e. valid until the end of the program).
* Parameters:
* whatami: A whatami bitmask of zenoh entity kind.
* str_out: An uninitialized memory location where strring will be constructed.
*
* Return:
* ``0`` in case of success, ``negative value`` otherwise.
*/
int8_t z_whatami_to_view_string(z_whatami_t whatami, z_view_string_t *str_out);

/************* Primitives **************/
/**
* Scouts for other Zenoh entities like routers and/or peers.
Expand Down
3 changes: 3 additions & 0 deletions include/zenoh-pico/protocol/codec/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ int8_t _z_query_target_decode(z_query_target_t *en, _z_zbuf_t *zbf);
int8_t _z_whatami_encode(_z_wbuf_t *wbf, z_whatami_t en);
int8_t _z_whatami_decode(z_whatami_t *en, _z_zbuf_t *zbf);

uint8_t _z_whatami_to_uint8(z_whatami_t whatami);
z_whatami_t _z_whatami_from_uint8(uint8_t b);

int8_t _z_uint8_encode(_z_wbuf_t *buf, uint8_t v);
int8_t _z_uint8_decode(uint8_t *u8, _z_zbuf_t *buf);

Expand Down
8 changes: 4 additions & 4 deletions include/zenoh-pico/protocol/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ void _z_value_free(_z_value_t **hello);
* z_whatami_t whatami: The kind of zenoh entity.
*/
typedef struct {
_z_id_t zid;
_z_string_svec_t locators;
z_whatami_t whatami;
uint8_t version;
_z_id_t _zid;
_z_string_svec_t _locators;
z_whatami_t _whatami;
uint8_t _version;
} _z_hello_t;
void _z_hello_clear(_z_hello_t *src);
void _z_hello_free(_z_hello_t **hello);
Expand Down
Loading

0 comments on commit 23919e2

Please sign in to comment.