netifapi.c 6.2 KB

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