From d6fbe452966fa338b7d06ab693b7a170f7c5a255 Mon Sep 17 00:00:00 2001 From: fbernon Date: Thu, 28 Jun 2007 10:11:05 +0000 Subject: [PATCH] netifapi.h, netifapi.c, tcpip.h, tcpip.c: Update code to handle the option LWIP_TCPIP_CORE_LOCKING, and do some changes to be coherent with last modifications in api_lib/api_msg (use pointers and not type with table, etc...) --- CHANGELOG | 5 ++ src/api/netifapi.c | 125 ++++++++++++++++++++---------------- src/api/tcpip.c | 37 ++++++++--- src/include/lwip/netifapi.h | 35 +++++----- src/include/lwip/tcpip.h | 23 +++++-- 5 files changed, 140 insertions(+), 85 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3e5433b4..2d4e32af 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -238,6 +238,11 @@ HISTORY ++ Bug fixes: + 2007-06-28 Frédéric Bernon + * netifapi.h, netifapi.c, tcpip.h, tcpip.c: Update code to handle the option + LWIP_TCPIP_CORE_LOCKING, and do some changes to be coherent with last modifications + in api_lib/api_msg (use pointers and not type with table, etc...) + 2007-06-26 Simon Goldschmidt * udp.h: Fixed bug #20259: struct udp_hdr was lacking the packin defines. diff --git a/src/api/netifapi.c b/src/api/netifapi.c index e7e205a9..e1ff8d48 100644 --- a/src/api/netifapi.c +++ b/src/api/netifapi.c @@ -48,16 +48,16 @@ netifapi_netif_add(struct netif *netif, err_t (* input)(struct pbuf *p, struct netif *netif)) { struct netifapi_msg msg; - msg.type = NETIFAPI_MSG_NETIF_ADD; - msg.netif = netif; - msg.msg.add.ipaddr = ipaddr; - msg.msg.add.netmask = netmask; - msg.msg.add.gw = gw; - msg.msg.add.state = state; - msg.msg.add.init = init; - msg.msg.add.input = input; - TCPIP_APIMSG(&msg); - return msg.err; + msg.function = do_netifapi_netif_add; + msg.msg.netif = netif; + msg.msg.msg.add.ipaddr = ipaddr; + msg.msg.msg.add.netmask = netmask; + msg.msg.msg.add.gw = gw; + msg.msg.msg.add.state = state; + msg.msg.msg.add.init = init; + msg.msg.msg.add.input = input; + TCPIP_NETIFAPI(&msg); + return msg.msg.err; } /** @@ -70,12 +70,13 @@ err_t netifapi_netif_remove(struct netif *netif) { struct netifapi_msg msg; - msg.type = NETIFAPI_MSG_NETIF_REMOVE; - msg.netif = netif; - TCPIP_APIMSG(&msg); - return msg.err; + msg.function = do_netifapi_netif_remove; + msg.msg.netif = netif; + TCPIP_NETIFAPI(&msg); + return msg.msg.err; } +#if LWIP_DHCP /** * Call dhcp_start() in a thread-safe way by running that function inside the * tcpip_thread context. @@ -86,10 +87,10 @@ err_t netifapi_dhcp_start(struct netif *netif) { struct netifapi_msg msg; - msg.type = NETIFAPI_MSG_DHCP_START; - msg.netif = netif; - TCPIP_APIMSG(&msg); - return msg.err; + msg.function = do_netifapi_dhcp_start; + msg.msg.netif = netif; + TCPIP_NETIFAPI(&msg); + return msg.msg.err; } /** @@ -102,48 +103,64 @@ err_t netifapi_dhcp_stop(struct netif *netif) { struct netifapi_msg msg; - msg.type = NETIFAPI_MSG_DHCP_STOP; - msg.netif = netif; - TCPIP_APIMSG(&msg); - return msg.err; + msg.function = do_netifapi_dhcp_stop; + msg.msg.netif = netif; + TCPIP_NETIFAPI(&msg); + return msg.msg.err; +} +#endif /* LWIP_DHCP */ + +/** + * TODO + */ +void +do_netifapi_netif_add( struct netifapi_msg_msg *msg) +{ + msg->err = ERR_OK; + if (!netif_add( msg->netif, + msg->msg.add.ipaddr, + msg->msg.add.netmask, + msg->msg.add.gw, + msg->msg.add.state, + msg->msg.add.init, + msg->msg.add.input)) { + msg->err = ERR_IF; + } + TCPIP_NETIFAPI_ACK(msg); +} + +/** + * TODO + */ +void +do_netifapi_netif_remove( struct netifapi_msg_msg *msg) +{ + msg->err = ERR_OK; + netif_remove(msg->netif); + TCPIP_NETIFAPI_ACK(msg); +} + +#if LWIP_DHCP +/** + * TODO + */ +void +do_netifapi_dhcp_start( struct netifapi_msg_msg *msg) +{ + msg->err = dhcp_start(msg->netif); + TCPIP_NETIFAPI_ACK(msg); } /** - * This function processes the messages posted to the tcpip_thread - * by the above functions. - * - * It is responsible for doing the actual work inside the thread context - * of tcpip_thread. + * TODO */ void -netifapi_msg_input(struct netifapi_msg *msg) -{ +do_netifapi_dhcp_stop( struct netifapi_msg_msg *msg) +{ msg->err = ERR_OK; - switch (msg->type) { - case NETIFAPI_MSG_NETIF_ADD: { - if (!netif_add( msg->netif, - msg->msg.add.ipaddr, - msg->msg.add.netmask, - msg->msg.add.gw, - msg->msg.add.state, - msg->msg.add.init, - msg->msg.add.input)) - msg->err = ERR_IF; - break; - } - case NETIFAPI_MSG_NETIF_REMOVE: - netif_remove(msg->netif); - break; -#if LWIP_DHCP - case NETIFAPI_MSG_DHCP_START: - msg->err = dhcp_start(msg->netif); - break; - case NETIFAPI_MSG_DHCP_STOP: - dhcp_stop(msg->netif); - break; -#endif /* LWIP_DHCP */ - } - sys_sem_signal(msg->sem); + dhcp_stop(msg->netif); + TCPIP_NETIFAPI_ACK(msg); } +#endif /* LWIP_DHCP */ #endif /* LWIP_NETIF_API */ diff --git a/src/api/tcpip.c b/src/api/tcpip.c index 8ab0fd91..338fcd6c 100644 --- a/src/api/tcpip.c +++ b/src/api/tcpip.c @@ -313,7 +313,7 @@ tcpip_thread(void *arg) #if LWIP_NETIF_API case TCPIP_MSG_NETIFAPI: LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg)); - netifapi_msg_input(msg->msg.netifapimsg); + msg->msg.netifapimsg->function(&(msg->msg.netifapimsg->msg)); break; #endif /* LWIP_NETIF_API */ @@ -440,7 +440,7 @@ tcpip_apimsg(struct api_msg *apimsg) #if LWIP_TCPIP_CORE_LOCKING /** * Call the lower part of a netconn_* function - * This function ihas exclusive access to lwIP core code by locking it + * This function has exclusive access to lwIP core code by locking it * before the function is called. * * @param apimsg a struct containing the function to call and its parameters @@ -458,6 +458,7 @@ tcpip_apimsg_lock(struct api_msg *apimsg) #endif /* LWIP_TCPIP_CORE_LOCKING */ #if LWIP_NETIF_API +#if !LWIP_TCPIP_CORE_LOCKING /** * Much like tcpip_apimsg, but calls the lower part of a netifapi_* * function. @@ -471,21 +472,39 @@ tcpip_netifapi(struct netifapi_msg* netifapimsg) struct tcpip_msg msg; if (mbox != SYS_MBOX_NULL) { - netifapimsg->sem = sys_sem_new(0); - if (netifapimsg->sem == SYS_SEM_NULL) { - netifapimsg->err = ERR_MEM; - return netifapimsg->err; + netifapimsg->msg.sem = sys_sem_new(0); + if (netifapimsg->msg.sem == SYS_SEM_NULL) { + netifapimsg->msg.err = ERR_MEM; + return netifapimsg->msg.err; } msg.type = TCPIP_MSG_NETIFAPI; msg.msg.netifapimsg = netifapimsg; sys_mbox_post(mbox, &msg); - sys_sem_wait(netifapimsg->sem); - sys_sem_free(netifapimsg->sem); - return netifapimsg->err; + sys_sem_wait(netifapimsg->msg.sem); + sys_sem_free(netifapimsg->msg.sem); + return netifapimsg->msg.err; } return ERR_VAL; } +#else /* !LWIP_TCPIP_CORE_LOCKING */ +/** + * Call the lower part of a netifapi_* function + * This function has exclusive access to lwIP core code by locking it + * before the function is called. + * + * @param netifapimsg a struct containing the function to call and its parameters + * @return ERR_OK (only for compatibility fo tcpip_netifapi()) + */ +err_t +tcpip_netifapi_lock(struct netifapi_msg* netifapimsg) +{ + LOCK_TCPIP_CORE(); + netifapimsg->function(&(netifapimsg->msg)); + UNLOCK_TCPIP_CORE(); + return netifapimsg->msg.err; +} +#endif /* !LWIP_TCPIP_CORE_LOCKING */ #endif /* LWIP_NETIF_API */ /** diff --git a/src/include/lwip/netifapi.h b/src/include/lwip/netifapi.h index 1e1a6180..19b84735 100644 --- a/src/include/lwip/netifapi.h +++ b/src/include/lwip/netifapi.h @@ -40,18 +40,10 @@ extern "C" { #endif -enum netifapi_msg_type { - NETIFAPI_MSG_NETIF_ADD, - NETIFAPI_MSG_NETIF_REMOVE, -#if LWIP_DHCP - NETIFAPI_MSG_DHCP_START, - NETIFAPI_MSG_DHCP_STOP -#endif /* LWIP_DHCP */ -}; - -struct netifapi_msg { - enum netifapi_msg_type type; +struct netifapi_msg_msg { +#if !LWIP_TCPIP_CORE_LOCKING sys_sem_t sem; +#endif /* !LWIP_TCPIP_CORE_LOCKING */ err_t err; struct netif *netif; union { @@ -66,6 +58,11 @@ struct netifapi_msg { } msg; }; +struct netifapi_msg { + void (* function)(struct netifapi_msg_msg *msg); + struct netifapi_msg_msg msg; +}; + /* API for application */ err_t netifapi_netif_add ( struct netif *netif, @@ -78,13 +75,19 @@ err_t netifapi_netif_add ( struct netif *netif, err_t netifapi_netif_remove( struct netif *netif); -err_t netifapi_dhcp_start ( struct netif *netif); - -err_t netifapi_dhcp_stop ( struct netif *netif); - +#if LWIP_DHCP +err_t netifapi_dhcp_start ( struct netif *netif); +err_t netifapi_dhcp_stop ( struct netif *netif); +#endif /* LWIP_DHCP */ /* API for tcpip_thread */ -void netifapi_msg_input(struct netifapi_msg *msg); +void do_netifapi_netif_add ( struct netifapi_msg_msg *msg); +void do_netifapi_netif_remove( struct netifapi_msg_msg *msg); + +#if LWIP_DHCP +void do_netifapi_dhcp_start ( struct netifapi_msg_msg *msg); +void do_netifapi_dhcp_stop ( struct netifapi_msg_msg *msg); +#endif /* LWIP_DHCP */ #ifdef __cplusplus } diff --git a/src/include/lwip/tcpip.h b/src/include/lwip/tcpip.h index 977c6f18..449c8fc3 100644 --- a/src/include/lwip/tcpip.h +++ b/src/include/lwip/tcpip.h @@ -43,15 +43,19 @@ extern "C" { #if LWIP_TCPIP_CORE_LOCKING /** The global semaphore to lock the stack. */ extern sys_sem_t lock_tcpip_core; -#define LOCK_TCPIP_CORE() sys_sem_wait(lock_tcpip_core) -#define UNLOCK_TCPIP_CORE() sys_sem_signal(lock_tcpip_core) -#define TCPIP_APIMSG(m) tcpip_apimsg_lock(m) +#define LOCK_TCPIP_CORE() sys_sem_wait(lock_tcpip_core) +#define UNLOCK_TCPIP_CORE() sys_sem_signal(lock_tcpip_core) +#define TCPIP_APIMSG(m) tcpip_apimsg_lock(m) #define TCPIP_APIMSG_ACK(m) +#define TCPIP_NETIFAPI(m) tcpip_netifapi_lock(m) +#define TCPIP_NETIFAPI_ACK(m) #else #define LOCK_TCPIP_CORE() #define UNLOCK_TCPIP_CORE() -#define TCPIP_APIMSG(m) tcpip_apimsg(m) -#define TCPIP_APIMSG_ACK(m) sys_mbox_post(m->conn->mbox, NULL) +#define TCPIP_APIMSG(m) tcpip_apimsg(m) +#define TCPIP_APIMSG_ACK(m) sys_mbox_post(m->conn->mbox, NULL) +#define TCPIP_NETIFAPI(m) tcpip_netifapi(m) +#define TCPIP_NETIFAPI_ACK(m) sys_sem_signal(m->sem) #endif /* LWIP_TCPIP_CORE_LOCKING */ void tcpip_init(void (* tcpip_init_done)(void *), void *arg); @@ -59,15 +63,22 @@ err_t tcpip_apimsg(struct api_msg *apimsg); #if LWIP_TCPIP_CORE_LOCKING err_t tcpip_apimsg_lock(struct api_msg *apimsg); #endif /* LWIP_TCPIP_CORE_LOCKING */ + #if ETHARP_TCPIP_INPUT err_t tcpip_input(struct pbuf *p, struct netif *inp); #endif /* ETHARP_TCPIP_INPUT */ + #if ETHARP_TCPIP_ETHINPUT err_t tcpip_ethinput(struct pbuf *p, struct netif *inp); #endif /* ETHARP_TCPIP_ETHINPUT */ + #if LWIP_NETIF_API -err_t tcpip_netifapi(struct netifapi_msg* netifapimsg); +err_t tcpip_netifapi(struct netifapi_msg *netifapimsg); +#if LWIP_TCPIP_CORE_LOCKING +err_t tcpip_netifapi_lock(struct netifapi_msg *netifapimsg); +#endif /* LWIP_TCPIP_CORE_LOCKING */ #endif /* LWIP_NETIF_API */ + err_t tcpip_callback(void (*f)(void *ctx), void *ctx); enum tcpip_msg_type {