Control a robot over the internet using UDP and a Ethernet interface.
Dependencies: EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip
EthernetInterface/lwip/api/netifapi.c@0:1496281373a5, 2013-10-17 (annotated)
- Committer:
- apatel336
- Date:
- Thu Oct 17 13:26:38 2013 +0000
- Revision:
- 0:1496281373a5
Initial Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apatel336 | 0:1496281373a5 | 1 | /** |
apatel336 | 0:1496281373a5 | 2 | * @file |
apatel336 | 0:1496281373a5 | 3 | * Network Interface Sequential API module |
apatel336 | 0:1496281373a5 | 4 | * |
apatel336 | 0:1496281373a5 | 5 | */ |
apatel336 | 0:1496281373a5 | 6 | |
apatel336 | 0:1496281373a5 | 7 | /* |
apatel336 | 0:1496281373a5 | 8 | * Redistribution and use in source and binary forms, with or without modification, |
apatel336 | 0:1496281373a5 | 9 | * are permitted provided that the following conditions are met: |
apatel336 | 0:1496281373a5 | 10 | * |
apatel336 | 0:1496281373a5 | 11 | * 1. Redistributions of source code must retain the above copyright notice, |
apatel336 | 0:1496281373a5 | 12 | * this list of conditions and the following disclaimer. |
apatel336 | 0:1496281373a5 | 13 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
apatel336 | 0:1496281373a5 | 14 | * this list of conditions and the following disclaimer in the documentation |
apatel336 | 0:1496281373a5 | 15 | * and/or other materials provided with the distribution. |
apatel336 | 0:1496281373a5 | 16 | * 3. The name of the author may not be used to endorse or promote products |
apatel336 | 0:1496281373a5 | 17 | * derived from this software without specific prior written permission. |
apatel336 | 0:1496281373a5 | 18 | * |
apatel336 | 0:1496281373a5 | 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
apatel336 | 0:1496281373a5 | 20 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
apatel336 | 0:1496281373a5 | 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
apatel336 | 0:1496281373a5 | 22 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
apatel336 | 0:1496281373a5 | 23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
apatel336 | 0:1496281373a5 | 24 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
apatel336 | 0:1496281373a5 | 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
apatel336 | 0:1496281373a5 | 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
apatel336 | 0:1496281373a5 | 27 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
apatel336 | 0:1496281373a5 | 28 | * OF SUCH DAMAGE. |
apatel336 | 0:1496281373a5 | 29 | * |
apatel336 | 0:1496281373a5 | 30 | * This file is part of the lwIP TCP/IP stack. |
apatel336 | 0:1496281373a5 | 31 | * |
apatel336 | 0:1496281373a5 | 32 | */ |
apatel336 | 0:1496281373a5 | 33 | |
apatel336 | 0:1496281373a5 | 34 | #include "lwip/opt.h" |
apatel336 | 0:1496281373a5 | 35 | |
apatel336 | 0:1496281373a5 | 36 | #if LWIP_NETIF_API /* don't build if not configured for use in lwipopts.h */ |
apatel336 | 0:1496281373a5 | 37 | |
apatel336 | 0:1496281373a5 | 38 | #include "lwip/netifapi.h" |
apatel336 | 0:1496281373a5 | 39 | #include "lwip/tcpip.h" |
apatel336 | 0:1496281373a5 | 40 | |
apatel336 | 0:1496281373a5 | 41 | /** |
apatel336 | 0:1496281373a5 | 42 | * Call netif_add() inside the tcpip_thread context. |
apatel336 | 0:1496281373a5 | 43 | */ |
apatel336 | 0:1496281373a5 | 44 | void |
apatel336 | 0:1496281373a5 | 45 | do_netifapi_netif_add(struct netifapi_msg_msg *msg) |
apatel336 | 0:1496281373a5 | 46 | { |
apatel336 | 0:1496281373a5 | 47 | if (!netif_add( msg->netif, |
apatel336 | 0:1496281373a5 | 48 | msg->msg.add.ipaddr, |
apatel336 | 0:1496281373a5 | 49 | msg->msg.add.netmask, |
apatel336 | 0:1496281373a5 | 50 | msg->msg.add.gw, |
apatel336 | 0:1496281373a5 | 51 | msg->msg.add.state, |
apatel336 | 0:1496281373a5 | 52 | msg->msg.add.init, |
apatel336 | 0:1496281373a5 | 53 | msg->msg.add.input)) { |
apatel336 | 0:1496281373a5 | 54 | msg->err = ERR_IF; |
apatel336 | 0:1496281373a5 | 55 | } else { |
apatel336 | 0:1496281373a5 | 56 | msg->err = ERR_OK; |
apatel336 | 0:1496281373a5 | 57 | } |
apatel336 | 0:1496281373a5 | 58 | TCPIP_NETIFAPI_ACK(msg); |
apatel336 | 0:1496281373a5 | 59 | } |
apatel336 | 0:1496281373a5 | 60 | |
apatel336 | 0:1496281373a5 | 61 | /** |
apatel336 | 0:1496281373a5 | 62 | * Call netif_set_addr() inside the tcpip_thread context. |
apatel336 | 0:1496281373a5 | 63 | */ |
apatel336 | 0:1496281373a5 | 64 | void |
apatel336 | 0:1496281373a5 | 65 | do_netifapi_netif_set_addr(struct netifapi_msg_msg *msg) |
apatel336 | 0:1496281373a5 | 66 | { |
apatel336 | 0:1496281373a5 | 67 | netif_set_addr( msg->netif, |
apatel336 | 0:1496281373a5 | 68 | msg->msg.add.ipaddr, |
apatel336 | 0:1496281373a5 | 69 | msg->msg.add.netmask, |
apatel336 | 0:1496281373a5 | 70 | msg->msg.add.gw); |
apatel336 | 0:1496281373a5 | 71 | msg->err = ERR_OK; |
apatel336 | 0:1496281373a5 | 72 | TCPIP_NETIFAPI_ACK(msg); |
apatel336 | 0:1496281373a5 | 73 | } |
apatel336 | 0:1496281373a5 | 74 | |
apatel336 | 0:1496281373a5 | 75 | /** |
apatel336 | 0:1496281373a5 | 76 | * Call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) inside the |
apatel336 | 0:1496281373a5 | 77 | * tcpip_thread context. |
apatel336 | 0:1496281373a5 | 78 | */ |
apatel336 | 0:1496281373a5 | 79 | void |
apatel336 | 0:1496281373a5 | 80 | do_netifapi_netif_common(struct netifapi_msg_msg *msg) |
apatel336 | 0:1496281373a5 | 81 | { |
apatel336 | 0:1496281373a5 | 82 | if (msg->msg.common.errtfunc != NULL) { |
apatel336 | 0:1496281373a5 | 83 | msg->err = msg->msg.common.errtfunc(msg->netif); |
apatel336 | 0:1496281373a5 | 84 | } else { |
apatel336 | 0:1496281373a5 | 85 | msg->err = ERR_OK; |
apatel336 | 0:1496281373a5 | 86 | msg->msg.common.voidfunc(msg->netif); |
apatel336 | 0:1496281373a5 | 87 | } |
apatel336 | 0:1496281373a5 | 88 | TCPIP_NETIFAPI_ACK(msg); |
apatel336 | 0:1496281373a5 | 89 | } |
apatel336 | 0:1496281373a5 | 90 | |
apatel336 | 0:1496281373a5 | 91 | /** |
apatel336 | 0:1496281373a5 | 92 | * Call netif_add() in a thread-safe way by running that function inside the |
apatel336 | 0:1496281373a5 | 93 | * tcpip_thread context. |
apatel336 | 0:1496281373a5 | 94 | * |
apatel336 | 0:1496281373a5 | 95 | * @note for params @see netif_add() |
apatel336 | 0:1496281373a5 | 96 | */ |
apatel336 | 0:1496281373a5 | 97 | err_t |
apatel336 | 0:1496281373a5 | 98 | netifapi_netif_add(struct netif *netif, |
apatel336 | 0:1496281373a5 | 99 | ip_addr_t *ipaddr, |
apatel336 | 0:1496281373a5 | 100 | ip_addr_t *netmask, |
apatel336 | 0:1496281373a5 | 101 | ip_addr_t *gw, |
apatel336 | 0:1496281373a5 | 102 | void *state, |
apatel336 | 0:1496281373a5 | 103 | netif_init_fn init, |
apatel336 | 0:1496281373a5 | 104 | netif_input_fn input) |
apatel336 | 0:1496281373a5 | 105 | { |
apatel336 | 0:1496281373a5 | 106 | struct netifapi_msg msg; |
apatel336 | 0:1496281373a5 | 107 | msg.function = do_netifapi_netif_add; |
apatel336 | 0:1496281373a5 | 108 | msg.msg.netif = netif; |
apatel336 | 0:1496281373a5 | 109 | msg.msg.msg.add.ipaddr = ipaddr; |
apatel336 | 0:1496281373a5 | 110 | msg.msg.msg.add.netmask = netmask; |
apatel336 | 0:1496281373a5 | 111 | msg.msg.msg.add.gw = gw; |
apatel336 | 0:1496281373a5 | 112 | msg.msg.msg.add.state = state; |
apatel336 | 0:1496281373a5 | 113 | msg.msg.msg.add.init = init; |
apatel336 | 0:1496281373a5 | 114 | msg.msg.msg.add.input = input; |
apatel336 | 0:1496281373a5 | 115 | TCPIP_NETIFAPI(&msg); |
apatel336 | 0:1496281373a5 | 116 | return msg.msg.err; |
apatel336 | 0:1496281373a5 | 117 | } |
apatel336 | 0:1496281373a5 | 118 | |
apatel336 | 0:1496281373a5 | 119 | /** |
apatel336 | 0:1496281373a5 | 120 | * Call netif_set_addr() in a thread-safe way by running that function inside the |
apatel336 | 0:1496281373a5 | 121 | * tcpip_thread context. |
apatel336 | 0:1496281373a5 | 122 | * |
apatel336 | 0:1496281373a5 | 123 | * @note for params @see netif_set_addr() |
apatel336 | 0:1496281373a5 | 124 | */ |
apatel336 | 0:1496281373a5 | 125 | err_t |
apatel336 | 0:1496281373a5 | 126 | netifapi_netif_set_addr(struct netif *netif, |
apatel336 | 0:1496281373a5 | 127 | ip_addr_t *ipaddr, |
apatel336 | 0:1496281373a5 | 128 | ip_addr_t *netmask, |
apatel336 | 0:1496281373a5 | 129 | ip_addr_t *gw) |
apatel336 | 0:1496281373a5 | 130 | { |
apatel336 | 0:1496281373a5 | 131 | struct netifapi_msg msg; |
apatel336 | 0:1496281373a5 | 132 | msg.function = do_netifapi_netif_set_addr; |
apatel336 | 0:1496281373a5 | 133 | msg.msg.netif = netif; |
apatel336 | 0:1496281373a5 | 134 | msg.msg.msg.add.ipaddr = ipaddr; |
apatel336 | 0:1496281373a5 | 135 | msg.msg.msg.add.netmask = netmask; |
apatel336 | 0:1496281373a5 | 136 | msg.msg.msg.add.gw = gw; |
apatel336 | 0:1496281373a5 | 137 | TCPIP_NETIFAPI(&msg); |
apatel336 | 0:1496281373a5 | 138 | return msg.msg.err; |
apatel336 | 0:1496281373a5 | 139 | } |
apatel336 | 0:1496281373a5 | 140 | |
apatel336 | 0:1496281373a5 | 141 | /** |
apatel336 | 0:1496281373a5 | 142 | * call the "errtfunc" (or the "voidfunc" if "errtfunc" is NULL) in a thread-safe |
apatel336 | 0:1496281373a5 | 143 | * way by running that function inside the tcpip_thread context. |
apatel336 | 0:1496281373a5 | 144 | * |
apatel336 | 0:1496281373a5 | 145 | * @note use only for functions where there is only "netif" parameter. |
apatel336 | 0:1496281373a5 | 146 | */ |
apatel336 | 0:1496281373a5 | 147 | err_t |
apatel336 | 0:1496281373a5 | 148 | netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc, |
apatel336 | 0:1496281373a5 | 149 | netifapi_errt_fn errtfunc) |
apatel336 | 0:1496281373a5 | 150 | { |
apatel336 | 0:1496281373a5 | 151 | struct netifapi_msg msg; |
apatel336 | 0:1496281373a5 | 152 | msg.function = do_netifapi_netif_common; |
apatel336 | 0:1496281373a5 | 153 | msg.msg.netif = netif; |
apatel336 | 0:1496281373a5 | 154 | msg.msg.msg.common.voidfunc = voidfunc; |
apatel336 | 0:1496281373a5 | 155 | msg.msg.msg.common.errtfunc = errtfunc; |
apatel336 | 0:1496281373a5 | 156 | TCPIP_NETIFAPI(&msg); |
apatel336 | 0:1496281373a5 | 157 | return msg.msg.err; |
apatel336 | 0:1496281373a5 | 158 | } |
apatel336 | 0:1496281373a5 | 159 | |
apatel336 | 0:1496281373a5 | 160 | #endif /* LWIP_NETIF_API */ |