netifapi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * @file
  3. * Network Interface Sequential API module
  4. *
  5. * @defgroup netifapi NETIF API
  6. * @ingroup sequential_api
  7. * Thread-safe functions to be called from non-TCPIP threads
  8. *
  9. * @defgroup netifapi_netif NETIF related
  10. * @ingroup netifapi
  11. * To be called from non-TCPIP threads
  12. */
  13. /*
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. The name of the author may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  28. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  30. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  33. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  34. * OF SUCH DAMAGE.
  35. *
  36. * This file is part of the lwIP TCP/IP stack.
  37. *
  38. */
  39. #include "lwip/opt.h"
  40. #if LWIP_NETIF_API /* don't build if not configured for use in lwipopts.h */
  41. #include "lwip/netifapi.h"
  42. #include "lwip/memp.h"
  43. #include "lwip/priv/tcpip_priv.h"
  44. #define NETIFAPI_VAR_REF(name) API_VAR_REF(name)
  45. #define NETIFAPI_VAR_DECLARE(name) API_VAR_DECLARE(struct netifapi_msg, name)
  46. #define NETIFAPI_VAR_ALLOC(name) API_VAR_ALLOC(struct netifapi_msg, MEMP_NETIFAPI_MSG, name, ERR_MEM)
  47. #define NETIFAPI_VAR_FREE(name) API_VAR_FREE(MEMP_NETIFAPI_MSG, name)
  48. /**
  49. * Call netif_add() inside the tcpip_thread context.
  50. */
  51. static err_t
  52. netifapi_do_netif_add(struct tcpip_api_call_data *m)
  53. {
  54. /* cast through void* to silence alignment warnings.
  55. * We know it works because the structs have been instantiated as struct netifapi_msg */
  56. struct netifapi_msg *msg = (struct netifapi_msg*)(void*)m;
  57. if (!netif_add( msg->netif,
  58. #if LWIP_IPV4
  59. API_EXPR_REF(msg->msg.add.ipaddr),
  60. API_EXPR_REF(msg->msg.add.netmask),
  61. API_EXPR_REF(msg->msg.add.gw),
  62. #endif /* LWIP_IPV4 */
  63. msg->msg.add.state,
  64. msg->msg.add.init,
  65. msg->msg.add.input)) {
  66. return ERR_IF;
  67. } else {
  68. return ERR_OK;
  69. }
  70. }
  71. #if LWIP_IPV4
  72. /**
  73. * Call netif_set_addr() inside the tcpip_thread context.
  74. */
  75. static err_t
  76. netifapi_do_netif_set_addr(struct tcpip_api_call_data *m)
  77. {
  78. /* cast through void* to silence alignment warnings.
  79. * We know it works because the structs have been instantiated as struct netifapi_msg */
  80. struct netifapi_msg *msg = (struct netifapi_msg*)(void*)m;
  81. netif_set_addr( msg->netif,
  82. API_EXPR_REF(msg->msg.add.ipaddr),
  83. API_EXPR_REF(msg->msg.add.netmask),
  84. API_EXPR_REF(msg->msg.add.gw));
  85. return ERR_OK;
  86. }
  87. #endif /* LWIP_IPV4 */
  88. /**
  89. * Call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) inside the
  90. * tcpip_thread context.
  91. */
  92. static err_t
  93. netifapi_do_netif_common(struct tcpip_api_call_data *m)
  94. {
  95. /* cast through void* to silence alignment warnings.
  96. * We know it works because the structs have been instantiated as struct netifapi_msg */
  97. struct netifapi_msg *msg = (struct netifapi_msg*)(void*)m;
  98. if (msg->msg.common.errtfunc != NULL) {
  99. return msg->msg.common.errtfunc(msg->netif);
  100. } else {
  101. msg->msg.common.voidfunc(msg->netif);
  102. return ERR_OK;
  103. }
  104. }
  105. /**
  106. * @ingroup netifapi_netif
  107. * Call netif_add() in a thread-safe way by running that function inside the
  108. * tcpip_thread context.
  109. *
  110. * @note for params @see netif_add()
  111. */
  112. err_t
  113. netifapi_netif_add(struct netif *netif,
  114. #if LWIP_IPV4
  115. const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
  116. #endif /* LWIP_IPV4 */
  117. void *state, netif_init_fn init, netif_input_fn input)
  118. {
  119. err_t err;
  120. NETIFAPI_VAR_DECLARE(msg);
  121. NETIFAPI_VAR_ALLOC(msg);
  122. #if LWIP_IPV4
  123. if (ipaddr == NULL) {
  124. ipaddr = IP4_ADDR_ANY4;
  125. }
  126. if (netmask == NULL) {
  127. netmask = IP4_ADDR_ANY4;
  128. }
  129. if (gw == NULL) {
  130. gw = IP4_ADDR_ANY4;
  131. }
  132. #endif /* LWIP_IPV4 */
  133. NETIFAPI_VAR_REF(msg).netif = netif;
  134. #if LWIP_IPV4
  135. NETIFAPI_VAR_REF(msg).msg.add.ipaddr = NETIFAPI_VAR_REF(ipaddr);
  136. NETIFAPI_VAR_REF(msg).msg.add.netmask = NETIFAPI_VAR_REF(netmask);
  137. NETIFAPI_VAR_REF(msg).msg.add.gw = NETIFAPI_VAR_REF(gw);
  138. #endif /* LWIP_IPV4 */
  139. NETIFAPI_VAR_REF(msg).msg.add.state = state;
  140. NETIFAPI_VAR_REF(msg).msg.add.init = init;
  141. NETIFAPI_VAR_REF(msg).msg.add.input = input;
  142. err = tcpip_api_call(netifapi_do_netif_add, &API_VAR_REF(msg).call);
  143. NETIFAPI_VAR_FREE(msg);
  144. return err;
  145. }
  146. #if LWIP_IPV4
  147. /**
  148. * @ingroup netifapi_netif
  149. * Call netif_set_addr() in a thread-safe way by running that function inside the
  150. * tcpip_thread context.
  151. *
  152. * @note for params @see netif_set_addr()
  153. */
  154. err_t
  155. netifapi_netif_set_addr(struct netif *netif,
  156. const ip4_addr_t *ipaddr,
  157. const ip4_addr_t *netmask,
  158. const ip4_addr_t *gw)
  159. {
  160. err_t err;
  161. NETIFAPI_VAR_DECLARE(msg);
  162. NETIFAPI_VAR_ALLOC(msg);
  163. if (ipaddr == NULL) {
  164. ipaddr = IP4_ADDR_ANY4;
  165. }
  166. if (netmask == NULL) {
  167. netmask = IP4_ADDR_ANY4;
  168. }
  169. if (gw == NULL) {
  170. gw = IP4_ADDR_ANY4;
  171. }
  172. NETIFAPI_VAR_REF(msg).netif = netif;
  173. NETIFAPI_VAR_REF(msg).msg.add.ipaddr = NETIFAPI_VAR_REF(ipaddr);
  174. NETIFAPI_VAR_REF(msg).msg.add.netmask = NETIFAPI_VAR_REF(netmask);
  175. NETIFAPI_VAR_REF(msg).msg.add.gw = NETIFAPI_VAR_REF(gw);
  176. err = tcpip_api_call(netifapi_do_netif_set_addr, &API_VAR_REF(msg).call);
  177. NETIFAPI_VAR_FREE(msg);
  178. return err;
  179. }
  180. #endif /* LWIP_IPV4 */
  181. /**
  182. * call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) in a thread-safe
  183. * way by running that function inside the tcpip_thread context.
  184. *
  185. * @note use only for functions where there is only "netif" parameter.
  186. */
  187. err_t
  188. netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc,
  189. netifapi_errt_fn errtfunc)
  190. {
  191. err_t err;
  192. NETIFAPI_VAR_DECLARE(msg);
  193. NETIFAPI_VAR_ALLOC(msg);
  194. NETIFAPI_VAR_REF(msg).netif = netif;
  195. NETIFAPI_VAR_REF(msg).msg.common.voidfunc = voidfunc;
  196. NETIFAPI_VAR_REF(msg).msg.common.errtfunc = errtfunc;
  197. err = tcpip_api_call(netifapi_do_netif_common, &API_VAR_REF(msg).call);
  198. NETIFAPI_VAR_FREE(msg);
  199. return err;
  200. }
  201. #endif /* LWIP_NETIF_API */