api.h, api_lib.c, api_msg.c: Add macro API_EVENT in the same spirit than TCP_EVENT_xxx macros to get a code more readable. It could also help to remove some code (like we have talk in "patch #5919 : Create compile switch to remove select code"), but it could be done later.

This commit is contained in:
fbernon 2007-10-24 12:09:18 +00:00
parent 270c7c1110
commit 7077d51f1f
4 changed files with 37 additions and 49 deletions

View File

@ -19,6 +19,12 @@ HISTORY
++ New features: ++ New features:
2007-10-24 Frédéric Bernon
* api.h, api_lib.c, api_msg.c: Add macro API_EVENT in the same spirit than
TCP_EVENT_xxx macros to get a code more readable. It could also help to remove
some code (like we have talk in "patch #5919 : Create compile switch to remove
select code"), but it could be done later.
2007-10-08 Simon Goldschmidt 2007-10-08 Simon Goldschmidt
* many files: Changed initialization: many init functions are not needed any * many files: Changed initialization: many init functions are not needed any
more since we now rely on the compiler initializing global and static more since we now rely on the compiler initializing global and static

View File

@ -385,8 +385,7 @@ netconn_accept(struct netconn *conn)
#endif /* LWIP_SO_RCVTIMEO*/ #endif /* LWIP_SO_RCVTIMEO*/
/* Register event with callback */ /* Register event with callback */
if (conn->callback) API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
(*conn->callback)(conn, NETCONN_EVT_RCVMINUS, 0);
return newconn; return newconn;
} }
@ -449,14 +448,12 @@ netconn_recv(struct netconn *conn)
} }
/* Register event with callback */ /* Register event with callback */
if (conn->callback) { API_EVENT(conn, NETCONN_EVT_RCVMINUS, len);
(*conn->callback)(conn, NETCONN_EVT_RCVMINUS, len);
}
/* If we are closed, we indicate that we no longer wish to use the socket */ /* If we are closed, we indicate that we no longer wish to use the socket */
if (p == NULL) { if (p == NULL) {
memp_free(MEMP_NETBUF, buf); memp_free(MEMP_NETBUF, buf);
/* Avoid to loose any previous error code */ /* Avoid to lose any previous error code */
if (conn->err == ERR_OK) { if (conn->err == ERR_OK) {
conn->err = ERR_CLSD; conn->err = ERR_CLSD;
} }
@ -490,8 +487,7 @@ netconn_recv(struct netconn *conn)
if (buf!=NULL) { if (buf!=NULL) {
conn->recv_avail -= buf->p->tot_len; conn->recv_avail -= buf->p->tot_len;
/* Register event with callback */ /* Register event with callback */
if (conn->callback) API_EVENT(conn, NETCONN_EVT_RCVMINUS, buf->p->tot_len);
(*conn->callback)(conn, NETCONN_EVT_RCVMINUS, buf->p->tot_len);
} }
#endif /* (LWIP_UDP || LWIP_RAW) */ #endif /* (LWIP_UDP || LWIP_RAW) */
} }

View File

@ -82,9 +82,7 @@ recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
conn->recv_avail += p->tot_len; conn->recv_avail += p->tot_len;
/* Register event with callback */ /* Register event with callback */
if (conn->callback) { API_EVENT(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
(*conn->callback)(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
}
sys_mbox_post(conn->recvmbox, buf); sys_mbox_post(conn->recvmbox, buf);
} }
@ -128,9 +126,7 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
conn->recv_avail += p->tot_len; conn->recv_avail += p->tot_len;
/* Register event with callback */ /* Register event with callback */
if (conn->callback) { API_EVENT(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
(*conn->callback)(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
}
sys_mbox_post(conn->recvmbox, buf); sys_mbox_post(conn->recvmbox, buf);
} }
#endif /* LWIP_UDP */ #endif /* LWIP_UDP */
@ -165,9 +161,7 @@ recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
len = 0; len = 0;
} }
/* Register event with callback */ /* Register event with callback */
if (conn->callback) { API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
(*conn->callback)(conn, NETCONN_EVT_RCVPLUS, len);
}
sys_mbox_post(conn->recvmbox, p); sys_mbox_post(conn->recvmbox, p);
return ERR_OK; return ERR_OK;
@ -203,7 +197,7 @@ poll_tcp(void *arg, struct tcp_pcb *pcb)
/** /**
* Sent callback function for TCP netconns. * Sent callback function for TCP netconns.
* Signals the conn->sem and calls conn->callback. * Signals the conn->sem and calls API_EVENT.
* netconn_write waits for conn->sem if send buffer is low. * netconn_write waits for conn->sem if send buffer is low.
* *
* @see tcp.h (struct tcp_pcb.sent) for parameters and return value * @see tcp.h (struct tcp_pcb.sent) for parameters and return value
@ -223,9 +217,9 @@ sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
do_close_internal(conn); do_close_internal(conn);
} }
if (conn && conn->callback) { if (conn) {
if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT)) { if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT)) {
(*conn->callback)(conn, NETCONN_EVT_SENDPLUS, len); API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
} }
} }
@ -234,7 +228,7 @@ sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
/** /**
* Error callback function for TCP netconns. * Error callback function for TCP netconns.
* Signals conn->sem, posts to all conn mboxes and calls conn->callback. * Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
* The application thread has then to decide what to do. * The application thread has then to decide what to do.
* *
* @see tcp.h (struct tcp_pcb.err) for parameters * @see tcp.h (struct tcp_pcb.err) for parameters
@ -252,9 +246,7 @@ err_tcp(void *arg, err_t err)
conn->err = err; conn->err = err;
if (conn->recvmbox != SYS_MBOX_NULL) { if (conn->recvmbox != SYS_MBOX_NULL) {
/* Register event with callback */ /* Register event with callback */
if (conn->callback) { API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
(*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0);
}
sys_mbox_post(conn->recvmbox, NULL); sys_mbox_post(conn->recvmbox, NULL);
} }
if (conn->mbox != SYS_MBOX_NULL && conn->state == NETCONN_CONNECT) { if (conn->mbox != SYS_MBOX_NULL && conn->state == NETCONN_CONNECT) {
@ -262,10 +254,8 @@ err_tcp(void *arg, err_t err)
sys_mbox_post(conn->mbox, NULL); sys_mbox_post(conn->mbox, NULL);
} }
if (conn->acceptmbox != SYS_MBOX_NULL) { if (conn->acceptmbox != SYS_MBOX_NULL) {
/* Register event with callback */ /* Register event with callback */
if (conn->callback) { API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
(*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0);
}
sys_mbox_post(conn->acceptmbox, NULL); sys_mbox_post(conn->acceptmbox, NULL);
} }
if ((conn->state == NETCONN_WRITE) || (conn->state == NETCONN_CLOSE)) { if ((conn->state == NETCONN_WRITE) || (conn->state == NETCONN_CLOSE)) {
@ -341,9 +331,7 @@ accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
newconn->acceptmbox = SYS_MBOX_NULL; newconn->acceptmbox = SYS_MBOX_NULL;
newconn->err = err; newconn->err = err;
/* Register event with callback */ /* Register event with callback */
if (conn->callback) { API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
(*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0);
}
/* We have to set the callback here even though /* We have to set the callback here even though
* the new socket is unknown. Mark the socket as -1. */ * the new socket is unknown. Mark the socket as -1. */
newconn->callback = conn->callback; newconn->callback = conn->callback;
@ -469,13 +457,10 @@ do_close_internal(struct netconn *conn)
tcp_arg(conn->pcb.tcp, NULL); tcp_arg(conn->pcb.tcp, NULL);
conn->pcb.tcp = NULL; conn->pcb.tcp = NULL;
conn->err = ERR_OK; conn->err = ERR_OK;
/* Trigger select() in socket layer */ /* Trigger select() in socket layer. This send should something else so the
if (conn->callback) { errorfd is set, not the read and write fd! */
/* this should send something else so the errorfd is set, API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
not the read and write fd! */ API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
(*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0);
(*conn->callback)(conn, NETCONN_EVT_SENDPLUS, 0);
}
/* wake up the application task */ /* wake up the application task */
sys_mbox_post(conn->mbox, NULL); sys_mbox_post(conn->mbox, NULL);
} }
@ -510,7 +495,7 @@ do_delconn(struct api_msg_msg *msg)
case NETCONN_TCP: case NETCONN_TCP:
msg->conn->state = NETCONN_CLOSE; msg->conn->state = NETCONN_CLOSE;
do_close_internal(msg->conn); do_close_internal(msg->conn);
/* conn->callback is called inside do_close_internal, before releasing /* API_EVENT is called inside do_close_internal, before releasing
the application thread, so we can return at this point! */ the application thread, so we can return at this point! */
return; return;
#endif /* LWIP_TCP */ #endif /* LWIP_TCP */
@ -520,13 +505,10 @@ do_delconn(struct api_msg_msg *msg)
} }
/* tcp netconns don't come here! */ /* tcp netconns don't come here! */
/* Trigger select() in socket layer */ /* Trigger select() in socket layer. This send should something else so the
if (msg->conn->callback) { errorfd is set, not the read and write fd! */
/* this send should something else so the errorfd is set, API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
not the read and write fd! */ API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
(*msg->conn->callback)(msg->conn, NETCONN_EVT_RCVPLUS, 0);
(*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDPLUS, 0);
}
if (msg->conn->mbox != SYS_MBOX_NULL) { if (msg->conn->mbox != SYS_MBOX_NULL) {
sys_mbox_post(msg->conn->mbox, NULL); sys_mbox_post(msg->conn->mbox, NULL);
@ -814,9 +796,8 @@ do_writemore(struct netconn *conn)
} }
err = tcp_output_nagle(conn->pcb.tcp); err = tcp_output_nagle(conn->pcb.tcp);
conn->err = err; conn->err = err;
if ((conn->callback) && (err == ERR_OK) && if ((err == ERR_OK) && (tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT)) {
(tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT)) { API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
(*conn->callback)(conn, NETCONN_EVT_SENDMINUS, len);
} }
} else if (err == ERR_MEM) { } else if (err == ERR_MEM) {
#if LWIP_TCPIP_CORE_LOCKING #if LWIP_TCPIP_CORE_LOCKING

View File

@ -53,6 +53,7 @@ extern "C" {
* the same byte order as in the corresponding pcb. * the same byte order as in the corresponding pcb.
*/ */
/* Flags for netconn_write */
#define NETCONN_NOCOPY 0x00 #define NETCONN_NOCOPY 0x00
#define NETCONN_COPY 0x01 #define NETCONN_COPY 0x01
@ -128,6 +129,10 @@ struct netconn {
void (* callback)(struct netconn *, enum netconn_evt, u16_t len); void (* callback)(struct netconn *, enum netconn_evt, u16_t len);
}; };
/* Register an Network connection event */
#define API_EVENT(c,e,l) if (c->callback) { \
(*c->callback)(c, e, l); \
}
/* Network connection functions: */ /* Network connection functions: */
#define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL) #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL)