sock_async_event: update for async callback argument support

This commit is contained in:
Martine S. Lenders 2020-03-11 12:27:01 +01:00
parent 29651da8f4
commit 411e320b0c
No known key found for this signature in database
GPG Key ID: CCD317364F63286F
8 changed files with 59 additions and 58 deletions

View File

@ -66,7 +66,7 @@
* } * }
* *
* event_queue_init(&queue); * event_queue_init(&queue);
* sock_udp_event_init(&sock, &queue, handler); * sock_udp_event_init(&sock, &queue, handler, NULL);
* event_loop(&queue); * event_loop(&queue);
* return 0; * return 0;
* } * }
@ -146,7 +146,7 @@
* *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.c} * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.c}
* event_queue_init(&queue); * event_queue_init(&queue);
* sock_udp_event_init(&sock, &queue, handler); * sock_udp_event_init(&sock, &queue, handler, NULL);
* event_loop(&queue); * event_loop(&queue);
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* *
@ -181,15 +181,16 @@ extern "C" {
* @brief Makes a DTLS sock able to handle asynchronous events using * @brief Makes a DTLS sock able to handle asynchronous events using
* @ref sys_event. * @ref sys_event.
* *
* @param[in] sock A DTLS sock object. * @param[in] sock A DTLS sock object.
* @param[in] ev_queue The queue the events on @p sock will be added to. * @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 * @param[in] handler The event handler function to call on an event on
* @p sock. * @p sock.
* @param[in] handler_arg Argument to provided to @p handler.
* *
* @note Only available with module `sock_dtls`. * @note Only available with module `sock_dtls`.
*/ */
void sock_dtls_event_init(sock_dtls_t *sock, event_queue_t *ev_queue, 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) */ #endif /* defined(MODULE_SOCK_DTLS) || defined(DOXYGEN) */
#if defined(MODULE_SOCK_IP) || 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 * @brief Makes a raw IPv4/IPv6 sock able to handle asynchronous events using
* @ref sys_event. * @ref sys_event.
* *
* @param[in] sock A raw IPv4/IPv6 sock object. * @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] 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 * @param[in] handler The event handler function to call on an event on
* @p sock. * @p sock.
* @param[in] handler_arg Argument to provided to @p handler.
* *
* @note Only available with module `sock_ip`. * @note Only available with module `sock_ip`.
*/ */
void sock_ip_event_init(sock_ip_t *sock, event_queue_t *ev_queue, 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) */ #endif /* defined(MODULE_SOCK_IP) || defined(DOXYGEN) */
#if defined(MODULE_SOCK_TCP) || 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 * @brief Makes a TCP sock able to handle asynchronous events using
* @ref sys_event. * @ref sys_event.
* *
* @param[in] sock A TCP sock object. * @param[in] sock A TCP sock object.
* @param[in] ev_queue The queue the events on @p sock will be added to. * @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 * @param[in] handler The event handler function to call on an event on
* @p sock. * @p sock.
* @param[in] handler_arg Argument to provided to @p handler.
* *
* @note Only available with module `sock_tcp`. * @note Only available with module `sock_tcp`.
*/ */
void sock_tcp_event_init(sock_tcp_t *sock, event_queue_t *ev_queue, 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 * @brief Makes a TCP listening queue able to handle asynchronous events using
* @ref sys_event. * @ref sys_event.
* *
* @param[in] queue A TCP listening queue. * @param[in] queue A TCP listening queue.
* @param[in] ev_queue The queue the events on @p sock will be added to. * @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 * @param[in] handler The event handler function to call on an event on
* @p sock. * @p sock.
* @param[in] handler_arg Argument to provided to @p handler.
* *
* @note Only available with module `sock_tcp`. * @note Only available with module `sock_tcp`.
*/ */
void sock_tcp_queue_event_init(sock_tcp_queue_t *queue, event_queue_t *ev_queue, 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) */ #endif /* defined(MODULE_SOCK_TCP) || defined(DOXYGEN) */
#if defined(MODULE_SOCK_UDP) || 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 * @brief Makes a UDP sock able to handle asynchronous events using
* @ref sys_event. * @ref sys_event.
* *
* @param[in] sock A UDP sock object. * @param[in] sock A UDP sock object.
* @param[in] ev_queue The queue the events on @p sock will be added to. * @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 * @param[in] handler The event handler function to call on an event on
* @p sock. * @p sock.
* @param[in] handler_arg Argument to provided to @p handler.
* *
* @note Only available with module `sock_udp`. * @note Only available with module `sock_udp`.
*/ */
void sock_udp_event_init(sock_udp_t *sock, event_queue_t *ev_queue, 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) */ #endif /* defined(MODULE_SOCK_UDP) || defined(DOXYGEN) */
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -121,7 +121,7 @@ static void *_event_loop(void *arg)
} }
event_queue_init(&_queue); 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); event_loop(&_queue);
return 0; return 0;

View File

@ -54,6 +54,7 @@ typedef struct {
event_t super; /**< event structure that gets extended */ event_t super; /**< event structure that gets extended */
sock_event_cb_t cb; /**< callback */ sock_event_cb_t cb; /**< callback */
void *sock; /**< generic pointer to a @ref net_sock object */ 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_async_flags_t type; /**< types of the event */
} sock_event_t; } sock_event_t;

View File

@ -25,14 +25,15 @@ static void _event_handler(event_t *ev)
event->type = 0; event->type = 0;
irq_restore(state); irq_restore(state);
if (_type) { 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) sock_async_ctx_t *ctx)
{ {
ctx->event.sock = sock; ctx->event.sock = sock;
ctx->event.cb_arg = arg;
ctx->event.type |= type; ctx->event.type |= type;
event_post(ctx->queue, &ctx->event.super); 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 #ifdef MODULE_SOCK_DTLS
static void _dtls_cb(sock_dtls_t *sock, sock_async_flags_t type, void *arg) static void _dtls_cb(sock_dtls_t *sock, sock_async_flags_t type, void *arg)
{ {
(void)arg; _cb(sock, type, arg, sock_dtls_get_async_ctx(sock));
_cb(sock, type, sock_dtls_get_async_ctx(sock));
} }
void sock_dtls_event_init(sock_dtls_t *sock, event_queue_t *ev_queue, 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); sock_async_ctx_t *ctx = sock_dtls_get_async_ctx(sock);
_set_ctx(ctx, ev_queue); _set_ctx(ctx, ev_queue);
ctx->event.cb.dtls = handler; 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 */ #endif /* MODULE_SOCK_DTLS */
#ifdef MODULE_SOCK_IP #ifdef MODULE_SOCK_IP
static void _ip_cb(sock_ip_t *sock, sock_async_flags_t type, void *arg) static void _ip_cb(sock_ip_t *sock, sock_async_flags_t type, void *arg)
{ {
(void)arg; _cb(sock, type, arg, sock_ip_get_async_ctx(sock));
_cb(sock, type, sock_ip_get_async_ctx(sock));
} }
void sock_ip_event_init(sock_ip_t *sock, event_queue_t *ev_queue, 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); sock_async_ctx_t *ctx = sock_ip_get_async_ctx(sock);
_set_ctx(ctx, ev_queue); _set_ctx(ctx, ev_queue);
ctx->event.cb.ip = handler; 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 */ #endif /* MODULE_SOCK_IP */
#ifdef MODULE_SOCK_TCP #ifdef MODULE_SOCK_TCP
static void _tcp_cb(sock_tcp_t *sock, sock_async_flags_t type, void *arg) static void _tcp_cb(sock_tcp_t *sock, sock_async_flags_t type, void *arg)
{ {
(void)arg; _cb(sock, type, arg, sock_tcp_get_async_ctx(sock));
_cb(sock, type, sock_tcp_get_async_ctx(sock));
} }
void sock_tcp_event_init(sock_tcp_t *sock, event_queue_t *ev_queue, 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); sock_async_ctx_t *ctx = sock_tcp_get_async_ctx(sock);
_set_ctx(ctx, ev_queue); _set_ctx(ctx, ev_queue);
ctx->event.cb.tcp = handler; 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, static void _tcp_queue_cb(sock_tcp_queue_t *queue, sock_async_flags_t type,
void *arg) void *arg)
{ {
(void)arg; _cb(queue, type, arg, sock_tcp_queue_get_async_ctx(queue));
_cb(queue, type, sock_tcp_queue_get_async_ctx(queue));
} }
void sock_tcp_queue_event_init(sock_tcp_queue_t *queue, void sock_tcp_queue_event_init(sock_tcp_queue_t *queue, event_queue_t *ev_queue,
event_queue_t *ev_queue, sock_tcp_queue_cb_t handler, void *handler_arg)
sock_tcp_queue_cb_t handler)
{ {
sock_async_ctx_t *ctx = sock_tcp_queue_get_async_ctx(queue); sock_async_ctx_t *ctx = sock_tcp_queue_get_async_ctx(queue);
_set_ctx(ctx, ev_queue); _set_ctx(ctx, ev_queue);
ctx->event.cb.tcp_queue = handler; 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 */ #endif /* MODULE_SOCK_TCP */
#ifdef MODULE_SOCK_UDP #ifdef MODULE_SOCK_UDP
static void _udp_cb(sock_udp_t *sock, sock_async_flags_t type, void *arg) static void _udp_cb(sock_udp_t *sock, sock_async_flags_t type, void *arg)
{ {
(void)arg; _cb(sock, type, arg, sock_udp_get_async_ctx(sock));
_cb(sock, type, sock_udp_get_async_ctx(sock));
} }
void sock_udp_event_init(sock_udp_t *sock, event_queue_t *ev_queue, 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); sock_async_ctx_t *ctx = sock_udp_get_async_ctx(sock);
_set_ctx(ctx, ev_queue); _set_ctx(ctx, ev_queue);
ctx->event.cb.udp = handler; 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 */ #endif /* MODULE_SOCK_UDP */

View File

@ -128,8 +128,8 @@ int main(void)
sock_udp_create(&_udp_sock, &local, NULL, 0); sock_udp_create(&_udp_sock, &local, NULL, 0);
sock_ip_create(&_ip_sock, (sock_ip_ep_t *)&local, NULL, PROTNUM_UDP, 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_udp_event_init(&_udp_sock, &_ev_queue, _recv_udp, NULL);
sock_ip_event_init(&_ip_sock, &_ev_queue, _recv_ip); sock_ip_event_init(&_ip_sock, &_ev_queue, _recv_ip, NULL);
memcpy(remote.addr.ipv6, _test_remote, sizeof(_test_remote)); memcpy(remote.addr.ipv6, _test_remote, sizeof(_test_remote));
remote.port = TEST_PORT - 1; remote.port = TEST_PORT - 1;

View File

@ -90,7 +90,7 @@ static void *_server_thread(void *args)
server_running = true; server_running = true;
printf("Success: started IP server on protocol %u\n", protocol); printf("Success: started IP server on protocol %u\n", protocol);
event_queue_init(&queue); 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); event_loop(&queue);
return NULL; return NULL;
} }

View File

@ -100,7 +100,7 @@ static void _tcp_accept(sock_tcp_queue_t *queue, sock_async_flags_t flags,
else { else {
sock_tcp_ep_t client; 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); sock_tcp_get_remote(sock, &client);
#ifdef MODULE_LWIP_IPV6 #ifdef MODULE_LWIP_IPV6
ipv6_addr_to_str(_addr_str, (ipv6_addr_t *)&client.addr.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", printf("Success: started TCP server on port %" PRIu16 "\n",
server_addr.port); server_addr.port);
event_queue_init(&_ev_queue); 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); event_loop(&_ev_queue);
return NULL; return NULL;
} }

View File

@ -93,7 +93,7 @@ static void *_server_thread(void *args)
printf("Success: started UDP server on port %" PRIu16 "\n", printf("Success: started UDP server on port %" PRIu16 "\n",
server_addr.port); server_addr.port);
event_queue_init(&queue); 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); event_loop(&queue);
return NULL; return NULL;
} }