Skip to content

Commit

Permalink
Dev/fix hello closure (#432)
Browse files Browse the repository at this point in the history
* fix z_owned_closure_hello_t to accept z_loaned_hello_t

* remove unneeded NULLs

* fix example

* void unused arg
  • Loading branch information
DenisBiryukov91 authored Jun 18, 2024
1 parent 2b4e465 commit cd525c4
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 31 deletions.
4 changes: 2 additions & 2 deletions examples/arduino/z_scout.ino
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ void fprinthello(const z_loaned_hello_t *hello) {
Serial.println(" }");
}

void callback(z_owned_hello_t *hello, void *context) {
fprinthello(z_hello_loan(hello));
void callback(const z_loaned_hello_t *hello, void *context) {
fprinthello(hello);
Serial.println("");
(*(int *)context)++;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/espidf/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, " }");
}

void callback(z_owned_hello_t *hello, void *context) {
fprinthello(stdout, z_hello_loan(hello));
void callback(const z_loaned_hello_t *hello, void *context) {
fprinthello(stdout, hello);
fprintf(stdout, "\n");
(*(int *)context)++;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/freertos_plus_tcp/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, " }");
}

void callback(z_owned_hello_t *hello, void *context) {
fprinthello(stdout, z_loan(*hello));
void callback(const z_loaned_hello_t *hello, void *context) {
fprinthello(stdout, hello);
fprintf(stdout, "\n");
(*(int *)context)++;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/mbed/z_scout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, " }");
}

void callback(z_owned_hello_t *hello, void *context) {
fprinthello(stdout, z_hello_loan(hello));
void callback(const z_loaned_hello_t *hello, void *context) {
fprinthello(stdout, hello);
fprintf(stdout, "\n");
(*(int *)context)++;
}
Expand Down
5 changes: 2 additions & 3 deletions examples/unix/c11/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, " }");
}

void callback(z_owned_hello_t *hello, void *context) {
fprinthello(stdout, z_loan(*hello));
void callback(const z_loaned_hello_t *hello, void *context) {
fprinthello(stdout, hello);
fprintf(stdout, "\n");
(*(int *)context)++;
z_drop(hello);
}

void drop(void *context) {
Expand Down
5 changes: 2 additions & 3 deletions examples/unix/c99/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, " }");
}

void callback(z_owned_hello_t *hello, void *context) {
fprinthello(stdout, z_hello_loan(hello));
void callback(const z_loaned_hello_t *hello, void *context) {
fprinthello(stdout, hello);
fprintf(stdout, "\n");
(*(int *)context)++;
z_hello_drop(hello);
}

void drop(void *context) {
Expand Down
4 changes: 2 additions & 2 deletions examples/windows/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, " }");
}

void callback(z_owned_hello_t *hello, void *context) {
fprinthello(stdout, z_loan(*hello));
void callback(const z_loaned_hello_t *hello, void *context) {
fprinthello(stdout, hello);
fprintf(stdout, "\n");
(*(int *)context)++;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/zephyr/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, " }");
}

void callback(z_owned_hello_t *hello, void *context) {
fprinthello(stdout, z_hello_loan(hello));
void callback(const z_loaned_hello_t *hello, void *context) {
fprinthello(stdout, hello);
fprintf(stdout, "\n");
(*(int *)context)++;
}
Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ int8_t z_closure_owned_reply(z_owned_closure_owned_reply_t *closure, z_owned_rep
* Return:
* The hello closure.
*/
int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_owned_hello_handler_t call, z_dropper_handler_t drop,
int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_loaned_hello_handler_t call, z_dropper_handler_t drop,
void *context);

/**
Expand Down
8 changes: 4 additions & 4 deletions include/zenoh-pico/api/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ typedef struct {

void z_closure_owned_reply_call(const z_owned_closure_owned_reply_t *closure, z_owned_reply_t *reply);

typedef void (*z_owned_hello_handler_t)(z_owned_hello_t *hello, void *arg);
typedef void (*z_loaned_hello_handler_t)(const z_loaned_hello_t *hello, void *arg);

/**
* Represents the Zenoh ID callback closure.
Expand All @@ -597,18 +597,18 @@ typedef void (*z_owned_hello_handler_t)(z_owned_hello_t *hello, void *arg);
*
* Members:
* void *context: a pointer to an arbitrary state.
* z_owned_hello_handler_t call: `void (*z_owned_hello_handler_t)(const z_owned_hello_t *hello, void *arg)` is the
* z_loaned_hello_handler_t call: `void (*z_loaned_hello_handler_t)(const z_loaned_hello_t *hello, void *arg)` is the
* callback function.
* z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed.
* void *context: a pointer to an arbitrary state.
*/
typedef struct {
void *context;
z_owned_hello_handler_t call;
z_loaned_hello_handler_t call;
z_dropper_handler_t drop;
} z_owned_closure_hello_t;

void z_closure_hello_call(const z_owned_closure_hello_t *closure, z_owned_hello_t *hello);
void z_closure_hello_call(const z_owned_closure_hello_t *closure, const z_loaned_hello_t *hello);

typedef void (*z_id_handler_t)(const z_id_t *id, void *arg);

Expand Down
9 changes: 4 additions & 5 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ void z_closure_owned_reply_call(const z_owned_closure_owned_reply_t *closure, z_
}
}

void z_closure_hello_call(const z_owned_closure_hello_t *closure, z_owned_hello_t *hello) {
void z_closure_hello_call(const z_owned_closure_hello_t *closure, const z_loaned_hello_t *hello) {
if (closure->call != NULL) {
(closure->call)(hello, closure->context);
}
Expand Down Expand Up @@ -781,18 +781,17 @@ OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_query_t, closure_owned_query, z_ow
OWNED_FUNCTIONS_CLOSURE(z_owned_closure_reply_t, closure_reply, _z_reply_handler_t, z_dropper_handler_t)
OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_reply_t, closure_owned_reply, z_owned_reply_handler_t,
z_dropper_handler_t)
OWNED_FUNCTIONS_CLOSURE(z_owned_closure_hello_t, closure_hello, z_owned_hello_handler_t, z_dropper_handler_t)
OWNED_FUNCTIONS_CLOSURE(z_owned_closure_hello_t, closure_hello, z_loaned_hello_handler_t, z_dropper_handler_t)
OWNED_FUNCTIONS_CLOSURE(z_owned_closure_zid_t, closure_zid, z_id_handler_t, z_dropper_handler_t)

/************* Primitives **************/
typedef struct __z_hello_handler_wrapper_t {
z_owned_hello_handler_t user_call;
z_loaned_hello_handler_t user_call;
void *ctx;
} __z_hello_handler_wrapper_t;

void __z_hello_handler(_z_hello_t *hello, __z_hello_handler_wrapper_t *wrapped_ctx) {
z_owned_hello_t ohello = {._val = hello};
wrapped_ctx->user_call(&ohello, wrapped_ctx->ctx);
wrapped_ctx->user_call(hello, wrapped_ctx->ctx);
}

int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callback) {
Expand Down
3 changes: 2 additions & 1 deletion src/net/primitives.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ void _z_scout(const z_what_t what, const _z_id_t zid, const char *locator, const
while (hellos != NULL) {
_z_hello_t *hello = NULL;
hellos = _z_hello_list_pop(hellos, &hello);
(*callback)(hello, arg_call); // callback takes ownership of hello
(*callback)(hello, arg_call);
_z_hello_free(&hello);
}

if (dropper != NULL) {
Expand Down
4 changes: 2 additions & 2 deletions tests/z_api_alignment_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ void zid_handler(const z_id_t *id, void *arg) {
}

volatile unsigned int hellos = 0;
void hello_handler(z_owned_hello_t *hello, void *arg) {
void hello_handler(const z_loaned_hello_t *hello, void *arg) {
(void)hello;
(void)(arg);
printf("%s\n", __func__);
hellos++;
z_drop(hello);
}

volatile unsigned int queries = 0;
Expand Down

0 comments on commit cd525c4

Please sign in to comment.