From 411e320b0c9a8a1268691230503be01f81fe0cbf Mon Sep 17 00:00:00 2001 From: "Martine S. Lenders" Date: Wed, 11 Mar 2020 12:27:01 +0100 Subject: [PATCH] sock_async_event: update for async callback argument support --- sys/include/net/sock/async/event.h | 59 +++++++++++---------- sys/net/application_layer/gcoap/gcoap.c | 2 +- sys/net/sock/async/event/sock_async_ctx.h | 1 + sys/net/sock/async/event/sock_async_event.c | 43 +++++++-------- tests/gnrc_sock_async_event/main.c | 4 +- tests/lwip/ip.c | 2 +- tests/lwip/tcp.c | 4 +- tests/lwip/udp.c | 2 +- 8 files changed, 59 insertions(+), 58 deletions(-) diff --git a/sys/include/net/sock/async/event.h b/sys/include/net/sock/async/event.h index c1eb1d7108..17cac78e64 100644 --- a/sys/include/net/sock/async/event.h +++ b/sys/include/net/sock/async/event.h @@ -66,7 +66,7 @@ * } * * event_queue_init(&queue); - * sock_udp_event_init(&sock, &queue, handler); + * sock_udp_event_init(&sock, &queue, handler, NULL); * event_loop(&queue); * return 0; * } @@ -146,7 +146,7 @@ * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.c} * event_queue_init(&queue); - * sock_udp_event_init(&sock, &queue, handler); + * sock_udp_event_init(&sock, &queue, handler, NULL); * event_loop(&queue); * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * @@ -181,15 +181,16 @@ extern "C" { * @brief Makes a DTLS sock able to handle asynchronous events using * @ref sys_event. * - * @param[in] sock A DTLS sock object. - * @param[in] ev_queue The queue the events on @p sock will be added to. - * @param[in] handler The event handler function to call on an event on - * @p sock. + * @param[in] sock A DTLS sock object. + * @param[in] ev_queue The queue the events on @p sock will be added to. + * @param[in] handler The event handler function to call on an event on + * @p sock. + * @param[in] handler_arg Argument to provided to @p handler. * * @note Only available with module `sock_dtls`. */ void sock_dtls_event_init(sock_dtls_t *sock, event_queue_t *ev_queue, - sock_dtls_cb_t handler); + sock_dtls_cb_t handler, void *handler_arg); #endif /* defined(MODULE_SOCK_DTLS) || defined(DOXYGEN) */ #if defined(MODULE_SOCK_IP) || defined(DOXYGEN) @@ -197,15 +198,16 @@ void sock_dtls_event_init(sock_dtls_t *sock, event_queue_t *ev_queue, * @brief Makes a raw IPv4/IPv6 sock able to handle asynchronous events using * @ref sys_event. * - * @param[in] sock A raw IPv4/IPv6 sock object. - * @param[in] ev_queue The queue the events on @p sock will be added to. - * @param[in] handler The event handler function to call on an event on - * @p sock. + * @param[in] sock A raw IPv4/IPv6 sock object. + * @param[in] ev_queue The queue the events on @p sock will be added to. + * @param[in] handler The event handler function to call on an event on + * @p sock. + * @param[in] handler_arg Argument to provided to @p handler. * * @note Only available with module `sock_ip`. */ void sock_ip_event_init(sock_ip_t *sock, event_queue_t *ev_queue, - sock_ip_cb_t handler); + sock_ip_cb_t handler, void *handler_arg); #endif /* defined(MODULE_SOCK_IP) || defined(DOXYGEN) */ #if defined(MODULE_SOCK_TCP) || defined(DOXYGEN) @@ -213,29 +215,31 @@ void sock_ip_event_init(sock_ip_t *sock, event_queue_t *ev_queue, * @brief Makes a TCP sock able to handle asynchronous events using * @ref sys_event. * - * @param[in] sock A TCP sock object. - * @param[in] ev_queue The queue the events on @p sock will be added to. - * @param[in] handler The event handler function to call on an event on - * @p sock. + * @param[in] sock A TCP sock object. + * @param[in] ev_queue The queue the events on @p sock will be added to. + * @param[in] handler The event handler function to call on an event on + * @p sock. + * @param[in] handler_arg Argument to provided to @p handler. * * @note Only available with module `sock_tcp`. */ void sock_tcp_event_init(sock_tcp_t *sock, event_queue_t *ev_queue, - sock_tcp_cb_t handler); + sock_tcp_cb_t handler, void *handler_arg); /** * @brief Makes a TCP listening queue able to handle asynchronous events using * @ref sys_event. * - * @param[in] queue A TCP listening queue. - * @param[in] ev_queue The queue the events on @p sock will be added to. - * @param[in] handler The event handler function to call on an event on - * @p sock. + * @param[in] queue A TCP listening queue. + * @param[in] ev_queue The queue the events on @p sock will be added to. + * @param[in] handler The event handler function to call on an event on + * @p sock. + * @param[in] handler_arg Argument to provided to @p handler. * * @note Only available with module `sock_tcp`. */ void sock_tcp_queue_event_init(sock_tcp_queue_t *queue, event_queue_t *ev_queue, - sock_tcp_queue_cb_t handler); + sock_tcp_queue_cb_t handler, void *handler_arg); #endif /* defined(MODULE_SOCK_TCP) || defined(DOXYGEN) */ #if defined(MODULE_SOCK_UDP) || defined(DOXYGEN) @@ -243,15 +247,16 @@ void sock_tcp_queue_event_init(sock_tcp_queue_t *queue, event_queue_t *ev_queue, * @brief Makes a UDP sock able to handle asynchronous events using * @ref sys_event. * - * @param[in] sock A UDP sock object. - * @param[in] ev_queue The queue the events on @p sock will be added to. - * @param[in] handler The event handler function to call on an event on - * @p sock. + * @param[in] sock A UDP sock object. + * @param[in] ev_queue The queue the events on @p sock will be added to. + * @param[in] handler The event handler function to call on an event on + * @p sock. + * @param[in] handler_arg Argument to provided to @p handler. * * @note Only available with module `sock_udp`. */ void sock_udp_event_init(sock_udp_t *sock, event_queue_t *ev_queue, - sock_udp_cb_t handler); + sock_udp_cb_t handler, void *handler_arg); #endif /* defined(MODULE_SOCK_UDP) || defined(DOXYGEN) */ #ifdef __cplusplus diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index 24edc953c9..762f18a3d4 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -121,7 +121,7 @@ static void *_event_loop(void *arg) } event_queue_init(&_queue); - sock_udp_event_init(&_sock, &_queue, _on_sock_evt); + sock_udp_event_init(&_sock, &_queue, _on_sock_evt, NULL); event_loop(&_queue); return 0; diff --git a/sys/net/sock/async/event/sock_async_ctx.h b/sys/net/sock/async/event/sock_async_ctx.h index 48aa049ed1..44c607eea1 100644 --- a/sys/net/sock/async/event/sock_async_ctx.h +++ b/sys/net/sock/async/event/sock_async_ctx.h @@ -54,6 +54,7 @@ typedef struct { event_t super; /**< event structure that gets extended */ sock_event_cb_t cb; /**< callback */ void *sock; /**< generic pointer to a @ref net_sock object */ + void *cb_arg; /**< callback argument */ sock_async_flags_t type; /**< types of the event */ } sock_event_t; diff --git a/sys/net/sock/async/event/sock_async_event.c b/sys/net/sock/async/event/sock_async_event.c index 2606551384..edcc5c2623 100644 --- a/sys/net/sock/async/event/sock_async_event.c +++ b/sys/net/sock/async/event/sock_async_event.c @@ -25,14 +25,15 @@ static void _event_handler(event_t *ev) event->type = 0; irq_restore(state); if (_type) { - event->cb.generic(event->sock, _type, NULL); + event->cb.generic(event->sock, _type, event->cb_arg); } } -static inline void _cb(void *sock, sock_async_flags_t type, +static inline void _cb(void *sock, sock_async_flags_t type, void *arg, sock_async_ctx_t *ctx) { ctx->event.sock = sock; + ctx->event.cb_arg = arg; ctx->event.type |= type; event_post(ctx->queue, &ctx->event.super); } @@ -48,90 +49,84 @@ static void _set_ctx(sock_async_ctx_t *ctx, event_queue_t *ev_queue) #ifdef MODULE_SOCK_DTLS static void _dtls_cb(sock_dtls_t *sock, sock_async_flags_t type, void *arg) { - (void)arg; - _cb(sock, type, sock_dtls_get_async_ctx(sock)); + _cb(sock, type, arg, sock_dtls_get_async_ctx(sock)); } void sock_dtls_event_init(sock_dtls_t *sock, event_queue_t *ev_queue, - sock_dtls_cb_t handler) + sock_dtls_cb_t handler, void *handler_arg) { sock_async_ctx_t *ctx = sock_dtls_get_async_ctx(sock); _set_ctx(ctx, ev_queue); ctx->event.cb.dtls = handler; - sock_dtls_set_cb(sock, _dtls_cb, NULL); + sock_dtls_set_cb(sock, _dtls_cb, handler_arg); } #endif /* MODULE_SOCK_DTLS */ #ifdef MODULE_SOCK_IP static void _ip_cb(sock_ip_t *sock, sock_async_flags_t type, void *arg) { - (void)arg; - _cb(sock, type, sock_ip_get_async_ctx(sock)); + _cb(sock, type, arg, sock_ip_get_async_ctx(sock)); } void sock_ip_event_init(sock_ip_t *sock, event_queue_t *ev_queue, - sock_ip_cb_t handler) + sock_ip_cb_t handler, void *handler_arg) { sock_async_ctx_t *ctx = sock_ip_get_async_ctx(sock); _set_ctx(ctx, ev_queue); ctx->event.cb.ip = handler; - sock_ip_set_cb(sock, _ip_cb, NULL); + sock_ip_set_cb(sock, _ip_cb, handler_arg); } #endif /* MODULE_SOCK_IP */ #ifdef MODULE_SOCK_TCP static void _tcp_cb(sock_tcp_t *sock, sock_async_flags_t type, void *arg) { - (void)arg; - _cb(sock, type, sock_tcp_get_async_ctx(sock)); + _cb(sock, type, arg, sock_tcp_get_async_ctx(sock)); } void sock_tcp_event_init(sock_tcp_t *sock, event_queue_t *ev_queue, - sock_tcp_cb_t handler) + sock_tcp_cb_t handler, void *handler_arg) { sock_async_ctx_t *ctx = sock_tcp_get_async_ctx(sock); _set_ctx(ctx, ev_queue); ctx->event.cb.tcp = handler; - sock_tcp_set_cb(sock, _tcp_cb, NULL); + sock_tcp_set_cb(sock, _tcp_cb, handler_arg); } static void _tcp_queue_cb(sock_tcp_queue_t *queue, sock_async_flags_t type, void *arg) { - (void)arg; - _cb(queue, type, sock_tcp_queue_get_async_ctx(queue)); + _cb(queue, type, arg, sock_tcp_queue_get_async_ctx(queue)); } -void sock_tcp_queue_event_init(sock_tcp_queue_t *queue, - event_queue_t *ev_queue, - sock_tcp_queue_cb_t handler) +void sock_tcp_queue_event_init(sock_tcp_queue_t *queue, event_queue_t *ev_queue, + sock_tcp_queue_cb_t handler, void *handler_arg) { sock_async_ctx_t *ctx = sock_tcp_queue_get_async_ctx(queue); _set_ctx(ctx, ev_queue); ctx->event.cb.tcp_queue = handler; - sock_tcp_queue_set_cb(queue, _tcp_queue_cb, NULL); + sock_tcp_queue_set_cb(queue, _tcp_queue_cb, handler_arg); } #endif /* MODULE_SOCK_TCP */ #ifdef MODULE_SOCK_UDP static void _udp_cb(sock_udp_t *sock, sock_async_flags_t type, void *arg) { - (void)arg; - _cb(sock, type, sock_udp_get_async_ctx(sock)); + _cb(sock, type, arg, sock_udp_get_async_ctx(sock)); } void sock_udp_event_init(sock_udp_t *sock, event_queue_t *ev_queue, - sock_udp_cb_t handler) + sock_udp_cb_t handler, void *handler_arg) { sock_async_ctx_t *ctx = sock_udp_get_async_ctx(sock); _set_ctx(ctx, ev_queue); ctx->event.cb.udp = handler; - sock_udp_set_cb(sock, _udp_cb, NULL); + sock_udp_set_cb(sock, _udp_cb, handler_arg); } #endif /* MODULE_SOCK_UDP */ diff --git a/tests/gnrc_sock_async_event/main.c b/tests/gnrc_sock_async_event/main.c index 0492ee87c2..2092691a44 100644 --- a/tests/gnrc_sock_async_event/main.c +++ b/tests/gnrc_sock_async_event/main.c @@ -128,8 +128,8 @@ int main(void) sock_udp_create(&_udp_sock, &local, NULL, 0); sock_ip_create(&_ip_sock, (sock_ip_ep_t *)&local, NULL, PROTNUM_UDP, 0); - sock_udp_event_init(&_udp_sock, &_ev_queue, _recv_udp); - sock_ip_event_init(&_ip_sock, &_ev_queue, _recv_ip); + sock_udp_event_init(&_udp_sock, &_ev_queue, _recv_udp, NULL); + sock_ip_event_init(&_ip_sock, &_ev_queue, _recv_ip, NULL); memcpy(remote.addr.ipv6, _test_remote, sizeof(_test_remote)); remote.port = TEST_PORT - 1; diff --git a/tests/lwip/ip.c b/tests/lwip/ip.c index e2914d48ea..5b5bea7eaa 100644 --- a/tests/lwip/ip.c +++ b/tests/lwip/ip.c @@ -90,7 +90,7 @@ static void *_server_thread(void *args) server_running = true; printf("Success: started IP server on protocol %u\n", protocol); event_queue_init(&queue); - sock_ip_event_init(&server_sock, &queue, _ip_recv); + sock_ip_event_init(&server_sock, &queue, _ip_recv, NULL); event_loop(&queue); return NULL; } diff --git a/tests/lwip/tcp.c b/tests/lwip/tcp.c index 00bc356077..ac770e9a30 100644 --- a/tests/lwip/tcp.c +++ b/tests/lwip/tcp.c @@ -100,7 +100,7 @@ static void _tcp_accept(sock_tcp_queue_t *queue, sock_async_flags_t flags, else { sock_tcp_ep_t client; - sock_tcp_event_init(sock, &_ev_queue, _tcp_recv); + sock_tcp_event_init(sock, &_ev_queue, _tcp_recv, NULL); sock_tcp_get_remote(sock, &client); #ifdef MODULE_LWIP_IPV6 ipv6_addr_to_str(_addr_str, (ipv6_addr_t *)&client.addr.ipv6, @@ -132,7 +132,7 @@ static void *_server_thread(void *args) printf("Success: started TCP server on port %" PRIu16 "\n", server_addr.port); event_queue_init(&_ev_queue); - sock_tcp_queue_event_init(&server_queue, &_ev_queue, _tcp_accept); + sock_tcp_queue_event_init(&server_queue, &_ev_queue, _tcp_accept, NULL); event_loop(&_ev_queue); return NULL; } diff --git a/tests/lwip/udp.c b/tests/lwip/udp.c index c456f61376..39ec1fbe3c 100644 --- a/tests/lwip/udp.c +++ b/tests/lwip/udp.c @@ -93,7 +93,7 @@ static void *_server_thread(void *args) printf("Success: started UDP server on port %" PRIu16 "\n", server_addr.port); event_queue_init(&queue); - sock_udp_event_init(&server_sock, &queue, _udp_recv); + sock_udp_event_init(&server_sock, &queue, _udp_recv, NULL); event_loop(&queue); return NULL; }