Merge pull request #13615 from miri64/sock_async/api/callback-arg

sock_async: add optional callback argument
This commit is contained in:
Martine Lenders 2020-03-11 20:07:18 +01:00 committed by GitHub
commit 72d28d54e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 153 additions and 99 deletions

View File

@ -181,8 +181,9 @@ ssize_t sock_ip_send(sock_ip_t *sock, const void *data, size_t len,
} }
#ifdef SOCK_HAS_ASYNC #ifdef SOCK_HAS_ASYNC
void sock_ip_set_cb(sock_ip_t *sock, sock_ip_cb_t cb) void sock_ip_set_cb(sock_ip_t *sock, sock_ip_cb_t cb, void *arg)
{ {
sock->base.async_cb_arg = arg;
sock->base.async_cb.ip = cb; sock->base.async_cb.ip = cb;
} }

View File

@ -312,7 +312,7 @@ static void _netconn_cb(struct netconn *conn, enum netconn_evt evt,
break; break;
} }
if (flags && sock->async_cb.gen) { if (flags && sock->async_cb.gen) {
sock->async_cb.gen(sock, flags); sock->async_cb.gen(sock, flags, sock->async_cb_arg);
} }
} }
#else #else

View File

@ -380,13 +380,16 @@ ssize_t sock_tcp_write(sock_tcp_t *sock, const void *data, size_t len)
} }
#ifdef SOCK_HAS_ASYNC #ifdef SOCK_HAS_ASYNC
void sock_tcp_set_cb(sock_tcp_t *sock, sock_tcp_cb_t cb) void sock_tcp_set_cb(sock_tcp_t *sock, sock_tcp_cb_t cb, void *arg)
{ {
sock->base.async_cb_arg = arg;
sock->base.async_cb.tcp = cb; sock->base.async_cb.tcp = cb;
} }
void sock_tcp_queue_set_cb(sock_tcp_queue_t *queue, sock_tcp_queue_cb_t cb) void sock_tcp_queue_set_cb(sock_tcp_queue_t *queue, sock_tcp_queue_cb_t cb,
void *arg)
{ {
queue->base.async_cb_arg = arg;
queue->base.async_cb.tcp_queue = cb; queue->base.async_cb.tcp_queue = cb;
} }

View File

@ -136,8 +136,9 @@ ssize_t sock_udp_send(sock_udp_t *sock, const void *data, size_t len,
} }
#ifdef SOCK_HAS_ASYNC #ifdef SOCK_HAS_ASYNC
void sock_udp_set_cb(sock_udp_t *sock, sock_udp_cb_t cb) void sock_udp_set_cb(sock_udp_t *sock, sock_udp_cb_t cb, void *arg)
{ {
sock->base.async_cb_arg = arg;
sock->base.async_cb.udp = cb; sock->base.async_cb.udp = cb;
} }

View File

@ -40,7 +40,8 @@ typedef struct lwip_sock_base lwip_sock_base_t;
* @internal * @internal
*/ */
typedef void (*lwip_sock_cb_t)(lwip_sock_base_t *sock, typedef void (*lwip_sock_cb_t)(lwip_sock_base_t *sock,
sock_async_flags_t flags); sock_async_flags_t flags,
void *arg);
#endif /* SOCK_HAS_ASYNC */ #endif /* SOCK_HAS_ASYNC */
/** /**
@ -74,6 +75,7 @@ struct lwip_sock_base {
sock_udp_cb_t udp; /**< UDP version */ sock_udp_cb_t udp; /**< UDP version */
#endif #endif
} async_cb; } async_cb;
void *async_cb_arg; /**< asynchronous callback argument */
#ifdef SOCK_HAS_ASYNC_CTX #ifdef SOCK_HAS_ASYNC_CTX
sock_async_ctx_t async_ctx; /**< asynchronous event context */ sock_async_ctx_t async_ctx; /**< asynchronous event context */
#endif /* SOCK_HAS_ASYNC_CTX */ #endif /* SOCK_HAS_ASYNC_CTX */

View File

@ -42,10 +42,11 @@ extern "C" {
* *
* @note Only available with @ref SOCK_HAS_ASYNC defined. * @note Only available with @ref SOCK_HAS_ASYNC defined.
* *
* @param[in] sock A DTLS sock object. * @param[in] sock A DTLS sock object.
* @param[in] cb An event callback. May be NULL to unset event callback. * @param[in] cb An event callback. May be NULL to unset event callback.
* @param[in] cb_arg Argument to provide to @p cb. May be NULL.
*/ */
void sock_dtls_set_cb(sock_dtls_t *sock, sock_dtls_cb_t cb); void sock_dtls_set_cb(sock_dtls_t *sock, sock_dtls_cb_t cb, void *cb_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)
@ -61,8 +62,9 @@ void sock_dtls_set_cb(sock_dtls_t *sock, sock_dtls_cb_t cb);
* *
* @param[in] sock A raw IPv4/IPv6 sock object. * @param[in] sock A raw IPv4/IPv6 sock object.
* @param[in] cb An event callback. May be NULL to unset event callback. * @param[in] cb An event callback. May be NULL to unset event callback.
* @param[in] cb_arg Argument to provide to @p cb. May be NULL.
*/ */
void sock_ip_set_cb(sock_ip_t *sock, sock_ip_cb_t cb); void sock_ip_set_cb(sock_ip_t *sock, sock_ip_cb_t cb, void *cb_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)
@ -76,10 +78,11 @@ void sock_ip_set_cb(sock_ip_t *sock, sock_ip_cb_t cb);
* *
* @note Only available with @ref SOCK_HAS_ASYNC defined. * @note Only available with @ref SOCK_HAS_ASYNC defined.
* *
* @param[in] sock A TCP sock object. * @param[in] sock A TCP sock object.
* @param[in] cb An event callback. May be NULL to unset event callback. * @param[in] cb An event callback. May be NULL to unset event callback.
* @param[in] cb_arg Argument to provide to @p cb. May be NULL.
*/ */
void sock_tcp_set_cb(sock_tcp_t *sock, sock_tcp_cb_t cb); void sock_tcp_set_cb(sock_tcp_t *sock, sock_tcp_cb_t cb, void *cb_arg);
/** /**
* @brief Sets event callback for @ref sock_tcp_queue_t * @brief Sets event callback for @ref sock_tcp_queue_t
@ -91,10 +94,12 @@ void sock_tcp_set_cb(sock_tcp_t *sock, sock_tcp_cb_t cb);
* *
* @note Only available with @ref SOCK_HAS_ASYNC defined. * @note Only available with @ref SOCK_HAS_ASYNC defined.
* *
* @param[in] queue A TCP listening queue. * @param[in] queue A TCP listening queue.
* @param[in] cb An event callback. May be NULL to unset event callback. * @param[in] cb An event callback. May be NULL to unset event callback.
* @param[in] cb_arg Argument to provide to @p cb. May be NULL.
*/ */
void sock_tcp_queue_set_cb(sock_tcp_queue_t *queue, sock_tcp_queue_cb_t cb); void sock_tcp_queue_set_cb(sock_tcp_queue_t *queue, sock_tcp_queue_cb_t cb,
void *cb_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)
@ -108,10 +113,11 @@ void sock_tcp_queue_set_cb(sock_tcp_queue_t *queue, sock_tcp_queue_cb_t cb);
* *
* @note Only available with @ref SOCK_HAS_ASYNC defined. * @note Only available with @ref SOCK_HAS_ASYNC defined.
* *
* @param[in] sock A UDP sock object. * @param[in] sock A UDP sock object.
* @param[in] cb An event callback. May be NULL to unset event callback. * @param[in] cb An event callback. May be NULL to unset event callback.
* @param[in] cb_arg Argument to provide to @p cb
*/ */
void sock_udp_set_cb(sock_udp_t *sock, sock_udp_cb_t cb); void sock_udp_set_cb(sock_udp_t *sock, sock_udp_cb_t cb, void *cb_arg);
#endif /* defined(MODULE_SOCK_UDP) || defined(DOXYGEN) */ #endif /* defined(MODULE_SOCK_UDP) || defined(DOXYGEN) */
#if defined(SOCK_HAS_ASYNC_CTX) || defined(DOXYGEN) #if defined(SOCK_HAS_ASYNC_CTX) || defined(DOXYGEN)

View File

@ -36,8 +36,9 @@
* event_queue_t queue; * event_queue_t queue;
* uint8_t buf[128]; * uint8_t buf[128];
* *
* void handler(sock_udp_t *sock, sock_async_flags_t type) * void handler(sock_udp_t *sock, sock_async_flags_t type, void *arg)
* { * {
* (void)arg;
* if (type & SOCK_ASYNC_MSG_RECV) { * if (type & SOCK_ASYNC_MSG_RECV) {
* sock_udp_ep_t remote; * sock_udp_ep_t remote;
* ssize_t res; * ssize_t res;
@ -65,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;
* } * }
@ -101,8 +102,9 @@
* send, we print an according message: * send, we print an according message:
* *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.c} * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.c}
* void handler(sock_udp_t *sock, sock_async_flags_t type) * void handler(sock_udp_t *sock, sock_async_flags_t type, void *arg)
* { * {
* (void)arg;
* if (type & SOCK_ASYNC_MSG_RECV) { * if (type & SOCK_ASYNC_MSG_RECV) {
* sock_udp_ep_t remote; * sock_udp_ep_t remote;
* ssize_t res; * ssize_t res;
@ -144,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);
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* *
@ -179,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)
@ -195,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)
@ -211,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)
@ -241,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

@ -54,8 +54,11 @@ typedef struct sock_dtls sock_dtls_t; /**< forward declare for async */
* - @ref SOCK_ASYNC_MSG_SENT, * - @ref SOCK_ASYNC_MSG_SENT,
* - @ref SOCK_ASYNC_PATH_PROP, or * - @ref SOCK_ASYNC_PATH_PROP, or
* - a combination of them. * - a combination of them.
* @param[in] arg Argument provided when setting the callback using
* @ref sock_dtls_set_cb(). May be NULL.
*/ */
typedef void (*sock_dtls_cb_t)(sock_dtls_t *sock, sock_async_flags_t flags); typedef void (*sock_dtls_cb_t)(sock_dtls_t *sock, sock_async_flags_t flags,
void *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)
@ -74,8 +77,11 @@ typedef struct sock_ip sock_ip_t; /**< forward declare for async */
* - @ref SOCK_ASYNC_MSG_SENT, * - @ref SOCK_ASYNC_MSG_SENT,
* - @ref SOCK_ASYNC_PATH_PROP, or * - @ref SOCK_ASYNC_PATH_PROP, or
* - a combination of them. * - a combination of them.
* @param[in] arg Argument provided when setting the callback using
* @ref sock_ip_set_cb(). May be NULL.
*/ */
typedef void (*sock_ip_cb_t)(sock_ip_t *sock, sock_async_flags_t flags); typedef void (*sock_ip_cb_t)(sock_ip_t *sock, sock_async_flags_t flags,
void *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)
@ -97,8 +103,11 @@ typedef struct sock_tcp_queue sock_tcp_queue_t;/**< forward declare for async */
* - @ref SOCK_ASYNC_MSG_SENT, * - @ref SOCK_ASYNC_MSG_SENT,
* - @ref SOCK_ASYNC_PATH_PROP, or * - @ref SOCK_ASYNC_PATH_PROP, or
* - a combination of them. * - a combination of them.
* @param[in] arg Argument provided when setting the callback using
* @ref sock_tcp_set_cb(). May be NULL.
*/ */
typedef void (*sock_tcp_cb_t)(sock_tcp_t *sock, sock_async_flags_t flags); typedef void (*sock_tcp_cb_t)(sock_tcp_t *sock, sock_async_flags_t flags,
void *arg);
/** /**
* @brief Event callback for @ref sock_tcp_queue_t * @brief Event callback for @ref sock_tcp_queue_t
@ -110,9 +119,12 @@ typedef void (*sock_tcp_cb_t)(sock_tcp_t *sock, sock_async_flags_t flags);
* @param[in] queue The TCP listening queue the event happened on * @param[in] queue The TCP listening queue the event happened on
* @param[in] flags The event flags. The only expected value is @ref * @param[in] flags The event flags. The only expected value is @ref
* SOCK_ASYNC_CONN_RECV. * SOCK_ASYNC_CONN_RECV.
* @param[in] arg Argument provided when setting the callback using
* @ref sock_tcp_queue_set_cb(). May be NULL.
*/ */
typedef void (*sock_tcp_queue_cb_t)(sock_tcp_queue_t *queue, typedef void (*sock_tcp_queue_cb_t)(sock_tcp_queue_t *queue,
sock_async_flags_t flags); sock_async_flags_t flags,
void *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)
@ -131,8 +143,11 @@ typedef struct sock_udp sock_udp_t; /**< forward declare for async */
* - @ref SOCK_ASYNC_MSG_SENT, * - @ref SOCK_ASYNC_MSG_SENT,
* - @ref SOCK_ASYNC_PATH_PROP, or * - @ref SOCK_ASYNC_PATH_PROP, or
* - a combination of them. * - a combination of them.
* @param[in] arg Argument provided when setting the callback using
* @ref sock_udp_set_cb(). May be NULL.
*/ */
typedef void (*sock_udp_cb_t)(sock_udp_t *sock, sock_async_flags_t type); typedef void (*sock_udp_cb_t)(sock_udp_t *sock, sock_async_flags_t type,
void *arg);
#endif /* defined(MODULE_SOCK_UDP) || defined(DOXYGEN) */ #endif /* defined(MODULE_SOCK_UDP) || defined(DOXYGEN) */
#ifdef SOCK_HAS_ASYNC_CTX #ifdef SOCK_HAS_ASYNC_CTX

View File

@ -46,7 +46,7 @@
/* Internal functions */ /* Internal functions */
static void *_event_loop(void *arg); static void *_event_loop(void *arg);
static void _on_sock_evt(sock_udp_t *sock, sock_async_flags_t type); static void _on_sock_evt(sock_udp_t *sock, sock_async_flags_t type, void *arg);
static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx); static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len, static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len,
sock_udp_ep_t *remote); sock_udp_ep_t *remote);
@ -121,19 +121,20 @@ 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;
} }
/* Handles sock events from the event queue. */ /* Handles sock events from the event queue. */
static void _on_sock_evt(sock_udp_t *sock, sock_async_flags_t type) static void _on_sock_evt(sock_udp_t *sock, sock_async_flags_t type, void *arg)
{ {
coap_pkt_t pdu; coap_pkt_t pdu;
sock_udp_ep_t remote; sock_udp_ep_t remote;
gcoap_request_memo_t *memo = NULL; gcoap_request_memo_t *memo = NULL;
(void)arg;
if (type & SOCK_ASYNC_MSG_RECV) { if (type & SOCK_ASYNC_MSG_RECV) {
ssize_t res = sock_udp_recv(sock, _listen_buf, sizeof(_listen_buf), ssize_t res = sock_udp_recv(sock, _listen_buf, sizeof(_listen_buf),
0, &remote); 0, &remote);

View File

@ -58,7 +58,7 @@ static void _netapi_cb(uint16_t cmd, gnrc_pktsnip_t *pkt, void *ctx)
(void *)&reg->mbox); (void *)&reg->mbox);
} }
if (reg->async_cb.generic) { if (reg->async_cb.generic) {
reg->async_cb.generic(reg, SOCK_ASYNC_MSG_RECV); reg->async_cb.generic(reg, SOCK_ASYNC_MSG_RECV, reg->async_cb_arg);
} }
} }
} }

View File

@ -55,7 +55,8 @@ typedef struct gnrc_sock_reg gnrc_sock_reg_t;
* @internal * @internal
*/ */
typedef void (*gnrc_sock_reg_cb_t)(gnrc_sock_reg_t *sock, typedef void (*gnrc_sock_reg_cb_t)(gnrc_sock_reg_t *sock,
sock_async_flags_t flags); sock_async_flags_t flags,
void *arg);
#endif /* SOCK_HAS_ASYNC */ #endif /* SOCK_HAS_ASYNC */
/** /**
@ -86,6 +87,7 @@ struct gnrc_sock_reg {
sock_udp_cb_t udp; /**< UDP version */ sock_udp_cb_t udp; /**< UDP version */
#endif #endif
} async_cb; } async_cb;
void *async_cb_arg; /**< asynchronous callback argument */
#ifdef SOCK_HAS_ASYNC_CTX #ifdef SOCK_HAS_ASYNC_CTX
sock_async_ctx_t async_ctx; /**< asynchronous event context */ sock_async_ctx_t async_ctx; /**< asynchronous event context */
#endif #endif

View File

@ -197,15 +197,17 @@ ssize_t sock_ip_send(sock_ip_t *sock, const void *data, size_t len,
} }
#ifdef SOCK_HAS_ASYNC #ifdef SOCK_HAS_ASYNC
if ((sock != NULL) && (sock->reg.async_cb.ip)) { if ((sock != NULL) && (sock->reg.async_cb.ip)) {
sock->reg.async_cb.ip(sock, SOCK_ASYNC_MSG_SENT); sock->reg.async_cb.ip(sock, SOCK_ASYNC_MSG_SENT,
sock->reg.async_cb_arg);
} }
#endif /* SOCK_HAS_ASYNC */ #endif /* SOCK_HAS_ASYNC */
return res; return res;
} }
#ifdef SOCK_HAS_ASYNC #ifdef SOCK_HAS_ASYNC
void sock_ip_set_cb(sock_ip_t *sock, sock_ip_cb_t cb) void sock_ip_set_cb(sock_ip_t *sock, sock_ip_cb_t cb, void *arg)
{ {
sock->reg.async_cb_arg = arg;
sock->reg.async_cb.ip = cb; sock->reg.async_cb.ip = cb;
} }

View File

@ -319,15 +319,17 @@ ssize_t sock_udp_send(sock_udp_t *sock, const void *data, size_t len,
} }
#ifdef SOCK_HAS_ASYNC #ifdef SOCK_HAS_ASYNC
if ((sock != NULL) && (sock->reg.async_cb.udp)) { if ((sock != NULL) && (sock->reg.async_cb.udp)) {
sock->reg.async_cb.udp(sock, SOCK_ASYNC_MSG_SENT); sock->reg.async_cb.udp(sock, SOCK_ASYNC_MSG_SENT,
sock->reg.async_cb_arg);
} }
#endif /* SOCK_HAS_ASYNC */ #endif /* SOCK_HAS_ASYNC */
return res; return res;
} }
#ifdef SOCK_HAS_ASYNC #ifdef SOCK_HAS_ASYNC
void sock_udp_set_cb(sock_udp_t *sock, sock_udp_cb_t cb) void sock_udp_set_cb(sock_udp_t *sock, sock_udp_cb_t cb, void *arg)
{ {
sock->reg.async_cb_arg = arg;
sock->reg.async_cb.udp = cb; sock->reg.async_cb.udp = cb;
} }

View File

@ -28,7 +28,10 @@ extern "C" {
* @brief Generalized callback type * @brief Generalized callback type
*/ */
typedef union { typedef union {
void (*generic)(void *, sock_async_flags_t); /**< anything goes */ /**
* @brief anything goes
*/
void (*generic)(void *, sock_async_flags_t, void *);
#ifdef MODULE_SOCK_DTLS #ifdef MODULE_SOCK_DTLS
sock_dtls_cb_t dtls; /**< DTLS callback */ sock_dtls_cb_t dtls; /**< DTLS callback */
#endif #endif
@ -51,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); 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);
} }
@ -46,86 +47,86 @@ 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) static void _dtls_cb(sock_dtls_t *sock, sock_async_flags_t type, 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, 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); 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) static void _ip_cb(sock_ip_t *sock, sock_async_flags_t type, 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, 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); 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) static void _tcp_cb(sock_tcp_t *sock, sock_async_flags_t type, 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, 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); 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)
{ {
_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, 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); 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) static void _udp_cb(sock_udp_t *sock, sock_async_flags_t type, 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, 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); sock_udp_set_cb(sock, _udp_cb, handler_arg);
} }
#endif /* MODULE_SOCK_UDP */ #endif /* MODULE_SOCK_UDP */

View File

@ -69,8 +69,9 @@ ipv6_hdr_t *gnrc_ipv6_get_header(gnrc_pktsnip_t *pkt)
return ((ipv6_hdr_t*) tmp->data); return ((ipv6_hdr_t*) tmp->data);
} }
static void _recv_udp(sock_udp_t *sock, sock_async_flags_t flags) static void _recv_udp(sock_udp_t *sock, sock_async_flags_t flags, void *arg)
{ {
assert(strcmp(arg, "test") == 0);
printf("UDP event triggered: %04X\n", flags); printf("UDP event triggered: %04X\n", flags);
if (flags & SOCK_ASYNC_MSG_RECV) { if (flags & SOCK_ASYNC_MSG_RECV) {
sock_udp_ep_t remote; sock_udp_ep_t remote;
@ -90,8 +91,9 @@ static void _recv_udp(sock_udp_t *sock, sock_async_flags_t flags)
} }
} }
static void _recv_ip(sock_ip_t *sock, sock_async_flags_t flags) static void _recv_ip(sock_ip_t *sock, sock_async_flags_t flags, void *arg)
{ {
assert(strcmp(arg, "test") == 0);
printf("IP event triggered: %04X\n", flags); printf("IP event triggered: %04X\n", flags);
if (flags & SOCK_ASYNC_MSG_RECV) { if (flags & SOCK_ASYNC_MSG_RECV) {
sock_ip_ep_t remote; sock_ip_ep_t remote;
@ -126,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, "test");
sock_ip_event_init(&_ip_sock, &_ev_queue, _recv_ip); sock_ip_event_init(&_ip_sock, &_ev_queue, _recv_ip, "test");
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

@ -44,8 +44,9 @@ static sock_ip_t server_sock;
static char server_stack[THREAD_STACKSIZE_DEFAULT]; static char server_stack[THREAD_STACKSIZE_DEFAULT];
static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE]; static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];
static void _ip_recv(sock_ip_t *sock, sock_async_flags_t flags) static void _ip_recv(sock_ip_t *sock, sock_async_flags_t flags, void *arg)
{ {
assert(strcmp(arg, "test") == 0);
if (flags & SOCK_ASYNC_MSG_RECV) { if (flags & SOCK_ASYNC_MSG_RECV) {
sock_ip_ep_t src; sock_ip_ep_t src;
int res; int res;
@ -89,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, "test");
event_loop(&queue); event_loop(&queue);
return NULL; return NULL;
} }

View File

@ -47,10 +47,11 @@ static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];
static char _addr_str[IPV6_ADDR_MAX_STR_LEN]; static char _addr_str[IPV6_ADDR_MAX_STR_LEN];
static event_queue_t _ev_queue; static event_queue_t _ev_queue;
static void _tcp_recv(sock_tcp_t *sock, sock_async_flags_t flags) static void _tcp_recv(sock_tcp_t *sock, sock_async_flags_t flags, void *arg)
{ {
sock_tcp_ep_t client; sock_tcp_ep_t client;
assert(strcmp(arg, "test") == 0);
if (sock_tcp_get_remote(sock, &client) < 0) { if (sock_tcp_get_remote(sock, &client) < 0) {
/* socket was disconnected between event firing and this handler */ /* socket was disconnected between event firing and this handler */
return; return;
@ -85,8 +86,10 @@ static void _tcp_recv(sock_tcp_t *sock, sock_async_flags_t flags)
} }
} }
static void _tcp_accept(sock_tcp_queue_t *queue, sock_async_flags_t flags) static void _tcp_accept(sock_tcp_queue_t *queue, sock_async_flags_t flags,
void *arg)
{ {
assert(strcmp(arg, "test") == 0);
if (flags & SOCK_ASYNC_CONN_RECV) { if (flags & SOCK_ASYNC_CONN_RECV) {
sock_tcp_t *sock = NULL; sock_tcp_t *sock = NULL;
int res; int res;
@ -97,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, "test");
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,
@ -129,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, "test");
event_loop(&_ev_queue); event_loop(&_ev_queue);
return NULL; return NULL;
} }

View File

@ -44,8 +44,9 @@ static sock_udp_t server_sock;
static char server_stack[THREAD_STACKSIZE_DEFAULT]; static char server_stack[THREAD_STACKSIZE_DEFAULT];
static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE]; static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];
static void _udp_recv(sock_udp_t *sock, sock_async_flags_t flags) static void _udp_recv(sock_udp_t *sock, sock_async_flags_t flags, void *arg)
{ {
assert(strcmp(arg, "test") == 0);
if (flags & SOCK_ASYNC_MSG_RECV) { if (flags & SOCK_ASYNC_MSG_RECV) {
sock_udp_ep_t src; sock_udp_ep_t src;
int res; int res;
@ -92,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, "test");
event_loop(&queue); event_loop(&queue);
return NULL; return NULL;
} }