ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_netifapi.c Source File

lwip_netifapi.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Network Interface Sequential API module
00004  *
00005  */
00006 
00007 /*
00008  * Redistribution and use in source and binary forms, with or without modification,
00009  * are permitted provided that the following conditions are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright notice,
00012  *    this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright notice,
00014  *    this list of conditions and the following disclaimer in the documentation
00015  *    and/or other materials provided with the distribution.
00016  * 3. The name of the author may not be used to endorse or promote products
00017  *    derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00020  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00021  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00022  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00024  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00027  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00028  * OF SUCH DAMAGE.
00029  *
00030  * This file is part of the lwIP TCP/IP stack.
00031  *
00032  */
00033 
00034 /**
00035  * @defgroup netifapi NETIF API
00036  * @ingroup threadsafe_api
00037  * Thread-safe functions to be called from non-TCPIP threads
00038  * 
00039  * @defgroup netifapi_netif NETIF related
00040  * @ingroup netifapi
00041  * To be called from non-TCPIP threads 
00042  */
00043 
00044 #include "lwip/opt.h"
00045 
00046 #if LWIP_NETIF_API /* don't build if not configured for use in lwipopts.h */
00047 
00048 #include "lwip/netifapi.h"
00049 #include "lwip/memp.h"
00050 #include "lwip/priv/tcpip_priv.h"
00051 
00052 #define NETIFAPI_VAR_REF(name)      API_VAR_REF(name)
00053 #define NETIFAPI_VAR_DECLARE(name)  API_VAR_DECLARE(struct netifapi_msg, name)
00054 #define NETIFAPI_VAR_ALLOC(name)    API_VAR_ALLOC(struct netifapi_msg, MEMP_NETIFAPI_MSG, name, ERR_MEM)
00055 #define NETIFAPI_VAR_FREE(name)     API_VAR_FREE(MEMP_NETIFAPI_MSG, name)
00056 
00057 /**
00058  * Call netif_add() inside the tcpip_thread context.
00059  */
00060 static err_t
00061 netifapi_do_netif_add(struct tcpip_api_call_data *m)
00062 {
00063   /* cast through void* to silence alignment warnings. 
00064    * We know it works because the structs have been instantiated as struct netifapi_msg */
00065   struct netifapi_msg *msg = (struct netifapi_msg*)(void*)m;
00066   
00067   if (!netif_add( msg->netif,
00068 #if LWIP_IPV4
00069                   API_EXPR_REF(msg->msg.add.ipaddr),
00070                   API_EXPR_REF(msg->msg.add.netmask),
00071                   API_EXPR_REF(msg->msg.add.gw),
00072 #endif /* LWIP_IPV4 */
00073                   msg->msg.add.state,
00074                   msg->msg.add.init,
00075                   msg->msg.add.input)) {
00076     return ERR_IF;
00077   } else {
00078     return ERR_OK;
00079   }
00080 }
00081 
00082 #if LWIP_IPV4
00083 /**
00084  * Call netif_set_addr() inside the tcpip_thread context.
00085  */
00086 static err_t
00087 netifapi_do_netif_set_addr(struct tcpip_api_call_data *m)
00088 {
00089   /* cast through void* to silence alignment warnings. 
00090    * We know it works because the structs have been instantiated as struct netifapi_msg */
00091   struct netifapi_msg *msg = (struct netifapi_msg*)(void*)m;
00092 
00093   netif_set_addr( msg->netif,
00094                   API_EXPR_REF(msg->msg.add.ipaddr),
00095                   API_EXPR_REF(msg->msg.add.netmask),
00096                   API_EXPR_REF(msg->msg.add.gw));
00097   return ERR_OK;
00098 }
00099 #endif /* LWIP_IPV4 */
00100 
00101 /**
00102  * Call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) inside the
00103  * tcpip_thread context.
00104  */
00105 static err_t
00106 netifapi_do_netif_common(struct tcpip_api_call_data *m)
00107 {
00108   /* cast through void* to silence alignment warnings. 
00109    * We know it works because the structs have been instantiated as struct netifapi_msg */
00110   struct netifapi_msg *msg = (struct netifapi_msg*)(void*)m;
00111 
00112   if (msg->msg.common.errtfunc != NULL) {
00113     return msg->msg.common.errtfunc(msg->netif);
00114   } else {
00115     msg->msg.common.voidfunc(msg->netif);
00116     return ERR_OK;
00117   }
00118 }
00119 
00120 /**
00121  * @ingroup netifapi_netif
00122  * Call netif_add() in a thread-safe way by running that function inside the
00123  * tcpip_thread context.
00124  *
00125  * @note for params @see netif_add()
00126  */
00127 err_t
00128 netifapi_netif_add(struct netif *netif,
00129 #if LWIP_IPV4
00130                    const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
00131 #endif /* LWIP_IPV4 */
00132                    void *state, netif_init_fn init, netif_input_fn input)
00133 {
00134   err_t err;
00135   NETIFAPI_VAR_DECLARE(msg);
00136   NETIFAPI_VAR_ALLOC(msg);
00137 
00138 #if LWIP_IPV4
00139   if (ipaddr == NULL) {
00140     ipaddr = IP4_ADDR_ANY;
00141   }
00142   if (netmask == NULL) {
00143     netmask = IP4_ADDR_ANY;
00144   }
00145   if (gw == NULL) {
00146     gw = IP4_ADDR_ANY;
00147   }
00148 #endif /* LWIP_IPV4 */
00149 
00150   NETIFAPI_VAR_REF(msg).netif = netif;
00151 #if LWIP_IPV4
00152   NETIFAPI_VAR_REF(msg).msg.add.ipaddr  = NETIFAPI_VAR_REF(ipaddr);
00153   NETIFAPI_VAR_REF(msg).msg.add.netmask = NETIFAPI_VAR_REF(netmask);
00154   NETIFAPI_VAR_REF(msg).msg.add.gw      = NETIFAPI_VAR_REF(gw);
00155 #endif /* LWIP_IPV4 */
00156   NETIFAPI_VAR_REF(msg).msg.add.state   = state;
00157   NETIFAPI_VAR_REF(msg).msg.add.init    = init;
00158   NETIFAPI_VAR_REF(msg).msg.add.input   = input;
00159   err = tcpip_api_call(netifapi_do_netif_add, &API_VAR_REF(msg).call);
00160   NETIFAPI_VAR_FREE(msg);
00161   return err;
00162 }
00163 
00164 #if LWIP_IPV4
00165 /**
00166  * @ingroup netifapi_netif
00167  * Call netif_set_addr() in a thread-safe way by running that function inside the
00168  * tcpip_thread context.
00169  *
00170  * @note for params @see netif_set_addr()
00171  */
00172 err_t
00173 netifapi_netif_set_addr(struct netif *netif,
00174                         const ip4_addr_t *ipaddr,
00175                         const ip4_addr_t *netmask,
00176                         const ip4_addr_t *gw)
00177 {
00178   err_t err;
00179   NETIFAPI_VAR_DECLARE(msg);
00180   NETIFAPI_VAR_ALLOC(msg);
00181 
00182   if (ipaddr == NULL) {
00183     ipaddr = IP4_ADDR_ANY;
00184   }
00185   if (netmask == NULL) {
00186     netmask = IP4_ADDR_ANY;
00187   }
00188   if (gw == NULL) {
00189     gw = IP4_ADDR_ANY;
00190   }
00191 
00192   NETIFAPI_VAR_REF(msg).netif = netif;
00193   NETIFAPI_VAR_REF(msg).msg.add.ipaddr  = NETIFAPI_VAR_REF(ipaddr);
00194   NETIFAPI_VAR_REF(msg).msg.add.netmask = NETIFAPI_VAR_REF(netmask);
00195   NETIFAPI_VAR_REF(msg).msg.add.gw      = NETIFAPI_VAR_REF(gw);
00196   err = tcpip_api_call(netifapi_do_netif_set_addr, &API_VAR_REF(msg).call);
00197   NETIFAPI_VAR_FREE(msg);
00198   return err;
00199 }
00200 #endif /* LWIP_IPV4 */
00201 
00202 /**
00203  * call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) in a thread-safe
00204  * way by running that function inside the tcpip_thread context.
00205  *
00206  * @note use only for functions where there is only "netif" parameter.
00207  */
00208 err_t
00209 netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc,
00210                        netifapi_errt_fn errtfunc)
00211 {
00212   err_t err;
00213   NETIFAPI_VAR_DECLARE(msg);
00214   NETIFAPI_VAR_ALLOC(msg);
00215 
00216   NETIFAPI_VAR_REF(msg).netif = netif;
00217   NETIFAPI_VAR_REF(msg).msg.common.voidfunc = voidfunc;
00218   NETIFAPI_VAR_REF(msg).msg.common.errtfunc = errtfunc;
00219   err = tcpip_api_call(netifapi_do_netif_common, &API_VAR_REF(msg).call);
00220   NETIFAPI_VAR_FREE(msg);
00221   return err;
00222 }
00223 
00224 #endif /* LWIP_NETIF_API */