netifapi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. void
  40. do_netifapi_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. void
  59. do_netifapi_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. void
  73. do_netifapi_netif_common( struct netifapi_msg_msg *msg)
  74. {
  75. if (msg->msg.common.errtfunc!=NULL) {
  76. msg->err =
  77. msg->msg.common.errtfunc(msg->netif);
  78. } else {
  79. msg->err = ERR_OK;
  80. msg->msg.common.voidfunc(msg->netif);
  81. }
  82. TCPIP_NETIFAPI_ACK(msg);
  83. }
  84. /**
  85. * Call netif_add() in a thread-safe way by running that function inside the
  86. * tcpip_thread context.
  87. *
  88. * @note for params @see netif_add()
  89. */
  90. err_t
  91. netifapi_netif_add(struct netif *netif,
  92. struct ip_addr *ipaddr,
  93. struct ip_addr *netmask,
  94. struct ip_addr *gw,
  95. void *state,
  96. err_t (* init)(struct netif *netif),
  97. err_t (* input)(struct pbuf *p, struct netif *netif))
  98. {
  99. struct netifapi_msg msg;
  100. msg.function = do_netifapi_netif_add;
  101. msg.msg.netif = netif;
  102. msg.msg.msg.add.ipaddr = ipaddr;
  103. msg.msg.msg.add.netmask = netmask;
  104. msg.msg.msg.add.gw = gw;
  105. msg.msg.msg.add.state = state;
  106. msg.msg.msg.add.init = init;
  107. msg.msg.msg.add.input = input;
  108. TCPIP_NETIFAPI(&msg);
  109. return msg.msg.err;
  110. }
  111. /**
  112. * Call netif_set_addr() in a thread-safe way by running that function inside the
  113. * tcpip_thread context.
  114. *
  115. * @note for params @see netif_set_addr()
  116. */
  117. err_t
  118. netifapi_netif_set_addr(struct netif *netif,
  119. struct ip_addr *ipaddr,
  120. struct ip_addr *netmask,
  121. struct ip_addr *gw)
  122. {
  123. struct netifapi_msg msg;
  124. msg.function = do_netifapi_netif_set_addr;
  125. msg.msg.netif = netif;
  126. msg.msg.msg.add.ipaddr = ipaddr;
  127. msg.msg.msg.add.netmask = netmask;
  128. msg.msg.msg.add.gw = gw;
  129. TCPIP_NETIFAPI(&msg);
  130. return msg.msg.err;
  131. }
  132. /**
  133. * call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) in a thread-safe
  134. * way by running that function inside the tcpip_thread context.
  135. *
  136. * @note use only for functions where there is only "netif" parameter.
  137. */
  138. err_t
  139. netifapi_netif_common( struct netif *netif,
  140. void (* voidfunc)(struct netif *netif),
  141. err_t (* errtfunc)(struct netif *netif) )
  142. {
  143. struct netifapi_msg msg;
  144. msg.function = do_netifapi_netif_common;
  145. msg.msg.netif = netif;
  146. msg.msg.msg.common.voidfunc = voidfunc;
  147. msg.msg.msg.common.errtfunc = errtfunc;
  148. TCPIP_NETIFAPI(&msg);
  149. return msg.msg.err;
  150. }
  151. #endif /* LWIP_NETIF_API */