netifapi.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/tcpip.h"
  36. /**
  37. * Call netif_add() inside the tcpip_thread context.
  38. */
  39. static void
  40. netifapi_do_netif_add(struct netifapi_msg_msg *msg)
  41. {
  42. if (!netif_add( msg->netif,
  43. msg->msg.add.ipaddr,
  44. msg->msg.add.netmask,
  45. msg->msg.add.gw,
  46. msg->msg.add.state,
  47. msg->msg.add.init,
  48. msg->msg.add.input)) {
  49. msg->err = ERR_IF;
  50. } else {
  51. msg->err = ERR_OK;
  52. }
  53. TCPIP_NETIFAPI_ACK(msg);
  54. }
  55. /**
  56. * Call netif_set_addr() inside the tcpip_thread context.
  57. */
  58. static void
  59. netifapi_do_netif_set_addr(struct netifapi_msg_msg *msg)
  60. {
  61. netif_set_addr( msg->netif,
  62. msg->msg.add.ipaddr,
  63. msg->msg.add.netmask,
  64. msg->msg.add.gw);
  65. msg->err = ERR_OK;
  66. TCPIP_NETIFAPI_ACK(msg);
  67. }
  68. /**
  69. * Call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) inside the
  70. * tcpip_thread context.
  71. */
  72. static void
  73. netifapi_do_netif_common(struct netifapi_msg_msg *msg)
  74. {
  75. if (msg->msg.common.errtfunc != NULL) {
  76. msg->err = msg->msg.common.errtfunc(msg->netif);
  77. } else {
  78. msg->err = ERR_OK;
  79. msg->msg.common.voidfunc(msg->netif);
  80. }
  81. TCPIP_NETIFAPI_ACK(msg);
  82. }
  83. /**
  84. * Call netif_add() in a thread-safe way by running that function inside the
  85. * tcpip_thread context.
  86. *
  87. * @note for params @see netif_add()
  88. */
  89. err_t
  90. netifapi_netif_add(struct netif *netif,
  91. ip_addr_t *ipaddr,
  92. ip_addr_t *netmask,
  93. ip_addr_t *gw,
  94. void *state,
  95. netif_init_fn init,
  96. netif_input_fn input)
  97. {
  98. struct netifapi_msg msg;
  99. msg.function = netifapi_do_netif_add;
  100. msg.msg.netif = netif;
  101. msg.msg.msg.add.ipaddr = ipaddr;
  102. msg.msg.msg.add.netmask = netmask;
  103. msg.msg.msg.add.gw = gw;
  104. msg.msg.msg.add.state = state;
  105. msg.msg.msg.add.init = init;
  106. msg.msg.msg.add.input = input;
  107. TCPIP_NETIFAPI(&msg);
  108. return msg.msg.err;
  109. }
  110. /**
  111. * Call netif_set_addr() in a thread-safe way by running that function inside the
  112. * tcpip_thread context.
  113. *
  114. * @note for params @see netif_set_addr()
  115. */
  116. err_t
  117. netifapi_netif_set_addr(struct netif *netif,
  118. ip_addr_t *ipaddr,
  119. ip_addr_t *netmask,
  120. ip_addr_t *gw)
  121. {
  122. struct netifapi_msg msg;
  123. msg.function = netifapi_do_netif_set_addr;
  124. msg.msg.netif = netif;
  125. msg.msg.msg.add.ipaddr = ipaddr;
  126. msg.msg.msg.add.netmask = netmask;
  127. msg.msg.msg.add.gw = gw;
  128. TCPIP_NETIFAPI(&msg);
  129. return msg.msg.err;
  130. }
  131. /**
  132. * call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) in a thread-safe
  133. * way by running that function inside the tcpip_thread context.
  134. *
  135. * @note use only for functions where there is only "netif" parameter.
  136. */
  137. err_t
  138. netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc,
  139. netifapi_errt_fn errtfunc)
  140. {
  141. struct netifapi_msg msg;
  142. msg.function = netifapi_do_netif_common;
  143. msg.msg.netif = netif;
  144. msg.msg.msg.common.voidfunc = voidfunc;
  145. msg.msg.msg.common.errtfunc = errtfunc;
  146. TCPIP_NETIFAPI(&msg);
  147. return msg.msg.err;
  148. }
  149. #endif /* LWIP_NETIF_API */