Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 5:3f93dd1d4cb3 1 /**
sam_grove 5:3f93dd1d4cb3 2 * @file
sam_grove 5:3f93dd1d4cb3 3 * lwIP network interface abstraction
sam_grove 5:3f93dd1d4cb3 4 *
sam_grove 5:3f93dd1d4cb3 5 */
sam_grove 5:3f93dd1d4cb3 6
sam_grove 5:3f93dd1d4cb3 7 /*
sam_grove 5:3f93dd1d4cb3 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
sam_grove 5:3f93dd1d4cb3 9 * All rights reserved.
sam_grove 5:3f93dd1d4cb3 10 *
sam_grove 5:3f93dd1d4cb3 11 * Redistribution and use in source and binary forms, with or without modification,
sam_grove 5:3f93dd1d4cb3 12 * are permitted provided that the following conditions are met:
sam_grove 5:3f93dd1d4cb3 13 *
sam_grove 5:3f93dd1d4cb3 14 * 1. Redistributions of source code must retain the above copyright notice,
sam_grove 5:3f93dd1d4cb3 15 * this list of conditions and the following disclaimer.
sam_grove 5:3f93dd1d4cb3 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
sam_grove 5:3f93dd1d4cb3 17 * this list of conditions and the following disclaimer in the documentation
sam_grove 5:3f93dd1d4cb3 18 * and/or other materials provided with the distribution.
sam_grove 5:3f93dd1d4cb3 19 * 3. The name of the author may not be used to endorse or promote products
sam_grove 5:3f93dd1d4cb3 20 * derived from this software without specific prior written permission.
sam_grove 5:3f93dd1d4cb3 21 *
sam_grove 5:3f93dd1d4cb3 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
sam_grove 5:3f93dd1d4cb3 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
sam_grove 5:3f93dd1d4cb3 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
sam_grove 5:3f93dd1d4cb3 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
sam_grove 5:3f93dd1d4cb3 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
sam_grove 5:3f93dd1d4cb3 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
sam_grove 5:3f93dd1d4cb3 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
sam_grove 5:3f93dd1d4cb3 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
sam_grove 5:3f93dd1d4cb3 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
sam_grove 5:3f93dd1d4cb3 31 * OF SUCH DAMAGE.
sam_grove 5:3f93dd1d4cb3 32 *
sam_grove 5:3f93dd1d4cb3 33 * This file is part of the lwIP TCP/IP stack.
sam_grove 5:3f93dd1d4cb3 34 *
sam_grove 5:3f93dd1d4cb3 35 * Author: Adam Dunkels <adam@sics.se>
sam_grove 5:3f93dd1d4cb3 36 *
sam_grove 5:3f93dd1d4cb3 37 */
sam_grove 5:3f93dd1d4cb3 38
sam_grove 5:3f93dd1d4cb3 39 #include "lwip/opt.h"
sam_grove 5:3f93dd1d4cb3 40
sam_grove 5:3f93dd1d4cb3 41 #include "lwip/def.h"
sam_grove 5:3f93dd1d4cb3 42 #include "lwip/ip_addr.h"
sam_grove 5:3f93dd1d4cb3 43 #include "lwip/netif.h"
sam_grove 5:3f93dd1d4cb3 44 #include "lwip/tcp_impl.h"
sam_grove 5:3f93dd1d4cb3 45 #include "lwip/snmp.h"
sam_grove 5:3f93dd1d4cb3 46 #include "lwip/igmp.h"
sam_grove 5:3f93dd1d4cb3 47 #include "netif/etharp.h"
sam_grove 5:3f93dd1d4cb3 48 #include "lwip/stats.h"
sam_grove 5:3f93dd1d4cb3 49 #if ENABLE_LOOPBACK
sam_grove 5:3f93dd1d4cb3 50 #include "lwip/sys.h"
sam_grove 5:3f93dd1d4cb3 51 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
sam_grove 5:3f93dd1d4cb3 52 #include "lwip/tcpip.h"
sam_grove 5:3f93dd1d4cb3 53 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
sam_grove 5:3f93dd1d4cb3 54 #endif /* ENABLE_LOOPBACK */
sam_grove 5:3f93dd1d4cb3 55
sam_grove 5:3f93dd1d4cb3 56 #if LWIP_AUTOIP
sam_grove 5:3f93dd1d4cb3 57 #include "lwip/autoip.h"
sam_grove 5:3f93dd1d4cb3 58 #endif /* LWIP_AUTOIP */
sam_grove 5:3f93dd1d4cb3 59 #if LWIP_DHCP
sam_grove 5:3f93dd1d4cb3 60 #include "lwip/dhcp.h"
sam_grove 5:3f93dd1d4cb3 61 #endif /* LWIP_DHCP */
sam_grove 5:3f93dd1d4cb3 62
sam_grove 5:3f93dd1d4cb3 63 #if LWIP_NETIF_STATUS_CALLBACK
sam_grove 5:3f93dd1d4cb3 64 #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
sam_grove 5:3f93dd1d4cb3 65 #else
sam_grove 5:3f93dd1d4cb3 66 #define NETIF_STATUS_CALLBACK(n)
sam_grove 5:3f93dd1d4cb3 67 #endif /* LWIP_NETIF_STATUS_CALLBACK */
sam_grove 5:3f93dd1d4cb3 68
sam_grove 5:3f93dd1d4cb3 69 #if LWIP_NETIF_LINK_CALLBACK
sam_grove 5:3f93dd1d4cb3 70 #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
sam_grove 5:3f93dd1d4cb3 71 #else
sam_grove 5:3f93dd1d4cb3 72 #define NETIF_LINK_CALLBACK(n)
sam_grove 5:3f93dd1d4cb3 73 #endif /* LWIP_NETIF_LINK_CALLBACK */
sam_grove 5:3f93dd1d4cb3 74
sam_grove 5:3f93dd1d4cb3 75 struct netif *netif_list;
sam_grove 5:3f93dd1d4cb3 76 struct netif *netif_default;
sam_grove 5:3f93dd1d4cb3 77
sam_grove 5:3f93dd1d4cb3 78 #if LWIP_HAVE_LOOPIF
sam_grove 5:3f93dd1d4cb3 79 static struct netif loop_netif;
sam_grove 5:3f93dd1d4cb3 80
sam_grove 5:3f93dd1d4cb3 81 /**
sam_grove 5:3f93dd1d4cb3 82 * Initialize a lwip network interface structure for a loopback interface
sam_grove 5:3f93dd1d4cb3 83 *
sam_grove 5:3f93dd1d4cb3 84 * @param netif the lwip network interface structure for this loopif
sam_grove 5:3f93dd1d4cb3 85 * @return ERR_OK if the loopif is initialized
sam_grove 5:3f93dd1d4cb3 86 * ERR_MEM if private data couldn't be allocated
sam_grove 5:3f93dd1d4cb3 87 */
sam_grove 5:3f93dd1d4cb3 88 static err_t
sam_grove 5:3f93dd1d4cb3 89 netif_loopif_init(struct netif *netif)
sam_grove 5:3f93dd1d4cb3 90 {
sam_grove 5:3f93dd1d4cb3 91 /* initialize the snmp variables and counters inside the struct netif
sam_grove 5:3f93dd1d4cb3 92 * ifSpeed: no assumption can be made!
sam_grove 5:3f93dd1d4cb3 93 */
sam_grove 5:3f93dd1d4cb3 94 NETIF_INIT_SNMP(netif, snmp_ifType_softwareLoopback, 0);
sam_grove 5:3f93dd1d4cb3 95
sam_grove 5:3f93dd1d4cb3 96 netif->name[0] = 'l';
sam_grove 5:3f93dd1d4cb3 97 netif->name[1] = 'o';
sam_grove 5:3f93dd1d4cb3 98 netif->output = netif_loop_output;
sam_grove 5:3f93dd1d4cb3 99 return ERR_OK;
sam_grove 5:3f93dd1d4cb3 100 }
sam_grove 5:3f93dd1d4cb3 101 #endif /* LWIP_HAVE_LOOPIF */
sam_grove 5:3f93dd1d4cb3 102
sam_grove 5:3f93dd1d4cb3 103 void
sam_grove 5:3f93dd1d4cb3 104 netif_init(void)
sam_grove 5:3f93dd1d4cb3 105 {
sam_grove 5:3f93dd1d4cb3 106 #if LWIP_HAVE_LOOPIF
sam_grove 5:3f93dd1d4cb3 107 ip_addr_t loop_ipaddr, loop_netmask, loop_gw;
sam_grove 5:3f93dd1d4cb3 108 IP4_ADDR(&loop_gw, 127,0,0,1);
sam_grove 5:3f93dd1d4cb3 109 IP4_ADDR(&loop_ipaddr, 127,0,0,1);
sam_grove 5:3f93dd1d4cb3 110 IP4_ADDR(&loop_netmask, 255,0,0,0);
sam_grove 5:3f93dd1d4cb3 111
sam_grove 5:3f93dd1d4cb3 112 #if NO_SYS
sam_grove 5:3f93dd1d4cb3 113 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, ip_input);
sam_grove 5:3f93dd1d4cb3 114 #else /* NO_SYS */
sam_grove 5:3f93dd1d4cb3 115 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, tcpip_input);
sam_grove 5:3f93dd1d4cb3 116 #endif /* NO_SYS */
sam_grove 5:3f93dd1d4cb3 117 netif_set_up(&loop_netif);
sam_grove 5:3f93dd1d4cb3 118
sam_grove 5:3f93dd1d4cb3 119 #endif /* LWIP_HAVE_LOOPIF */
sam_grove 5:3f93dd1d4cb3 120 }
sam_grove 5:3f93dd1d4cb3 121
sam_grove 5:3f93dd1d4cb3 122 /**
sam_grove 5:3f93dd1d4cb3 123 * Add a network interface to the list of lwIP netifs.
sam_grove 5:3f93dd1d4cb3 124 *
sam_grove 5:3f93dd1d4cb3 125 * @param netif a pre-allocated netif structure
sam_grove 5:3f93dd1d4cb3 126 * @param ipaddr IP address for the new netif
sam_grove 5:3f93dd1d4cb3 127 * @param netmask network mask for the new netif
sam_grove 5:3f93dd1d4cb3 128 * @param gw default gateway IP address for the new netif
sam_grove 5:3f93dd1d4cb3 129 * @param state opaque data passed to the new netif
sam_grove 5:3f93dd1d4cb3 130 * @param init callback function that initializes the interface
sam_grove 5:3f93dd1d4cb3 131 * @param input callback function that is called to pass
sam_grove 5:3f93dd1d4cb3 132 * ingress packets up in the protocol layer stack.
sam_grove 5:3f93dd1d4cb3 133 *
sam_grove 5:3f93dd1d4cb3 134 * @return netif, or NULL if failed.
sam_grove 5:3f93dd1d4cb3 135 */
sam_grove 5:3f93dd1d4cb3 136 struct netif *
sam_grove 5:3f93dd1d4cb3 137 netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
sam_grove 5:3f93dd1d4cb3 138 ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
sam_grove 5:3f93dd1d4cb3 139 {
sam_grove 5:3f93dd1d4cb3 140 static u8_t netifnum = 0;
sam_grove 5:3f93dd1d4cb3 141
sam_grove 5:3f93dd1d4cb3 142 LWIP_ASSERT("No init function given", init != NULL);
sam_grove 5:3f93dd1d4cb3 143
sam_grove 5:3f93dd1d4cb3 144 /* reset new interface configuration state */
sam_grove 5:3f93dd1d4cb3 145 ip_addr_set_zero(&netif->ip_addr);
sam_grove 5:3f93dd1d4cb3 146 ip_addr_set_zero(&netif->netmask);
sam_grove 5:3f93dd1d4cb3 147 ip_addr_set_zero(&netif->gw);
sam_grove 5:3f93dd1d4cb3 148 netif->flags = 0;
sam_grove 5:3f93dd1d4cb3 149 #if LWIP_DHCP
sam_grove 5:3f93dd1d4cb3 150 /* netif not under DHCP control by default */
sam_grove 5:3f93dd1d4cb3 151 netif->dhcp = NULL;
sam_grove 5:3f93dd1d4cb3 152 #endif /* LWIP_DHCP */
sam_grove 5:3f93dd1d4cb3 153 #if LWIP_AUTOIP
sam_grove 5:3f93dd1d4cb3 154 /* netif not under AutoIP control by default */
sam_grove 5:3f93dd1d4cb3 155 netif->autoip = NULL;
sam_grove 5:3f93dd1d4cb3 156 #endif /* LWIP_AUTOIP */
sam_grove 5:3f93dd1d4cb3 157 #if LWIP_NETIF_STATUS_CALLBACK
sam_grove 5:3f93dd1d4cb3 158 netif->status_callback = NULL;
sam_grove 5:3f93dd1d4cb3 159 #endif /* LWIP_NETIF_STATUS_CALLBACK */
sam_grove 5:3f93dd1d4cb3 160 #if LWIP_NETIF_LINK_CALLBACK
sam_grove 5:3f93dd1d4cb3 161 netif->link_callback = NULL;
sam_grove 5:3f93dd1d4cb3 162 #endif /* LWIP_NETIF_LINK_CALLBACK */
sam_grove 5:3f93dd1d4cb3 163 #if LWIP_IGMP
sam_grove 5:3f93dd1d4cb3 164 netif->igmp_mac_filter = NULL;
sam_grove 5:3f93dd1d4cb3 165 #endif /* LWIP_IGMP */
sam_grove 5:3f93dd1d4cb3 166 #if ENABLE_LOOPBACK
sam_grove 5:3f93dd1d4cb3 167 netif->loop_first = NULL;
sam_grove 5:3f93dd1d4cb3 168 netif->loop_last = NULL;
sam_grove 5:3f93dd1d4cb3 169 #endif /* ENABLE_LOOPBACK */
sam_grove 5:3f93dd1d4cb3 170
sam_grove 5:3f93dd1d4cb3 171 /* remember netif specific state information data */
sam_grove 5:3f93dd1d4cb3 172 netif->state = state;
sam_grove 5:3f93dd1d4cb3 173 netif->num = netifnum++;
sam_grove 5:3f93dd1d4cb3 174 netif->input = input;
sam_grove 5:3f93dd1d4cb3 175 #if LWIP_NETIF_HWADDRHINT
sam_grove 5:3f93dd1d4cb3 176 netif->addr_hint = NULL;
sam_grove 5:3f93dd1d4cb3 177 #endif /* LWIP_NETIF_HWADDRHINT*/
sam_grove 5:3f93dd1d4cb3 178 #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
sam_grove 5:3f93dd1d4cb3 179 netif->loop_cnt_current = 0;
sam_grove 5:3f93dd1d4cb3 180 #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
sam_grove 5:3f93dd1d4cb3 181
sam_grove 5:3f93dd1d4cb3 182 netif_set_addr(netif, ipaddr, netmask, gw);
sam_grove 5:3f93dd1d4cb3 183
sam_grove 5:3f93dd1d4cb3 184 /* call user specified initialization function for netif */
sam_grove 5:3f93dd1d4cb3 185 if (init(netif) != ERR_OK) {
sam_grove 5:3f93dd1d4cb3 186 return NULL;
sam_grove 5:3f93dd1d4cb3 187 }
sam_grove 5:3f93dd1d4cb3 188
sam_grove 5:3f93dd1d4cb3 189 /* add this netif to the list */
sam_grove 5:3f93dd1d4cb3 190 netif->next = netif_list;
sam_grove 5:3f93dd1d4cb3 191 netif_list = netif;
sam_grove 5:3f93dd1d4cb3 192 snmp_inc_iflist();
sam_grove 5:3f93dd1d4cb3 193
sam_grove 5:3f93dd1d4cb3 194 #if LWIP_IGMP
sam_grove 5:3f93dd1d4cb3 195 /* start IGMP processing */
sam_grove 5:3f93dd1d4cb3 196 if (netif->flags & NETIF_FLAG_IGMP) {
sam_grove 5:3f93dd1d4cb3 197 igmp_start(netif);
sam_grove 5:3f93dd1d4cb3 198 }
sam_grove 5:3f93dd1d4cb3 199 #endif /* LWIP_IGMP */
sam_grove 5:3f93dd1d4cb3 200
sam_grove 5:3f93dd1d4cb3 201 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
sam_grove 5:3f93dd1d4cb3 202 netif->name[0], netif->name[1]));
sam_grove 5:3f93dd1d4cb3 203 ip_addr_debug_print(NETIF_DEBUG, ipaddr);
sam_grove 5:3f93dd1d4cb3 204 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
sam_grove 5:3f93dd1d4cb3 205 ip_addr_debug_print(NETIF_DEBUG, netmask);
sam_grove 5:3f93dd1d4cb3 206 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
sam_grove 5:3f93dd1d4cb3 207 ip_addr_debug_print(NETIF_DEBUG, gw);
sam_grove 5:3f93dd1d4cb3 208 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
sam_grove 5:3f93dd1d4cb3 209 return netif;
sam_grove 5:3f93dd1d4cb3 210 }
sam_grove 5:3f93dd1d4cb3 211
sam_grove 5:3f93dd1d4cb3 212 /**
sam_grove 5:3f93dd1d4cb3 213 * Change IP address configuration for a network interface (including netmask
sam_grove 5:3f93dd1d4cb3 214 * and default gateway).
sam_grove 5:3f93dd1d4cb3 215 *
sam_grove 5:3f93dd1d4cb3 216 * @param netif the network interface to change
sam_grove 5:3f93dd1d4cb3 217 * @param ipaddr the new IP address
sam_grove 5:3f93dd1d4cb3 218 * @param netmask the new netmask
sam_grove 5:3f93dd1d4cb3 219 * @param gw the new default gateway
sam_grove 5:3f93dd1d4cb3 220 */
sam_grove 5:3f93dd1d4cb3 221 void
sam_grove 5:3f93dd1d4cb3 222 netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
sam_grove 5:3f93dd1d4cb3 223 ip_addr_t *gw)
sam_grove 5:3f93dd1d4cb3 224 {
sam_grove 5:3f93dd1d4cb3 225 netif_set_ipaddr(netif, ipaddr);
sam_grove 5:3f93dd1d4cb3 226 netif_set_netmask(netif, netmask);
sam_grove 5:3f93dd1d4cb3 227 netif_set_gw(netif, gw);
sam_grove 5:3f93dd1d4cb3 228 }
sam_grove 5:3f93dd1d4cb3 229
sam_grove 5:3f93dd1d4cb3 230 /**
sam_grove 5:3f93dd1d4cb3 231 * Remove a network interface from the list of lwIP netifs.
sam_grove 5:3f93dd1d4cb3 232 *
sam_grove 5:3f93dd1d4cb3 233 * @param netif the network interface to remove
sam_grove 5:3f93dd1d4cb3 234 */
sam_grove 5:3f93dd1d4cb3 235 void
sam_grove 5:3f93dd1d4cb3 236 netif_remove(struct netif *netif)
sam_grove 5:3f93dd1d4cb3 237 {
sam_grove 5:3f93dd1d4cb3 238 if (netif == NULL) {
sam_grove 5:3f93dd1d4cb3 239 return;
sam_grove 5:3f93dd1d4cb3 240 }
sam_grove 5:3f93dd1d4cb3 241
sam_grove 5:3f93dd1d4cb3 242 #if LWIP_IGMP
sam_grove 5:3f93dd1d4cb3 243 /* stop IGMP processing */
sam_grove 5:3f93dd1d4cb3 244 if (netif->flags & NETIF_FLAG_IGMP) {
sam_grove 5:3f93dd1d4cb3 245 igmp_stop(netif);
sam_grove 5:3f93dd1d4cb3 246 }
sam_grove 5:3f93dd1d4cb3 247 #endif /* LWIP_IGMP */
sam_grove 5:3f93dd1d4cb3 248 if (netif_is_up(netif)) {
sam_grove 5:3f93dd1d4cb3 249 /* set netif down before removing (call callback function) */
sam_grove 5:3f93dd1d4cb3 250 netif_set_down(netif);
sam_grove 5:3f93dd1d4cb3 251 }
sam_grove 5:3f93dd1d4cb3 252
sam_grove 5:3f93dd1d4cb3 253 snmp_delete_ipaddridx_tree(netif);
sam_grove 5:3f93dd1d4cb3 254
sam_grove 5:3f93dd1d4cb3 255 /* is it the first netif? */
sam_grove 5:3f93dd1d4cb3 256 if (netif_list == netif) {
sam_grove 5:3f93dd1d4cb3 257 netif_list = netif->next;
sam_grove 5:3f93dd1d4cb3 258 } else {
sam_grove 5:3f93dd1d4cb3 259 /* look for netif further down the list */
sam_grove 5:3f93dd1d4cb3 260 struct netif * tmpNetif;
sam_grove 5:3f93dd1d4cb3 261 for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
sam_grove 5:3f93dd1d4cb3 262 if (tmpNetif->next == netif) {
sam_grove 5:3f93dd1d4cb3 263 tmpNetif->next = netif->next;
sam_grove 5:3f93dd1d4cb3 264 break;
sam_grove 5:3f93dd1d4cb3 265 }
sam_grove 5:3f93dd1d4cb3 266 }
sam_grove 5:3f93dd1d4cb3 267 if (tmpNetif == NULL)
sam_grove 5:3f93dd1d4cb3 268 return; /* we didn't find any netif today */
sam_grove 5:3f93dd1d4cb3 269 }
sam_grove 5:3f93dd1d4cb3 270 snmp_dec_iflist();
sam_grove 5:3f93dd1d4cb3 271 /* this netif is default? */
sam_grove 5:3f93dd1d4cb3 272 if (netif_default == netif) {
sam_grove 5:3f93dd1d4cb3 273 /* reset default netif */
sam_grove 5:3f93dd1d4cb3 274 netif_set_default(NULL);
sam_grove 5:3f93dd1d4cb3 275 }
sam_grove 5:3f93dd1d4cb3 276 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
sam_grove 5:3f93dd1d4cb3 277 }
sam_grove 5:3f93dd1d4cb3 278
sam_grove 5:3f93dd1d4cb3 279 /**
sam_grove 5:3f93dd1d4cb3 280 * Find a network interface by searching for its name
sam_grove 5:3f93dd1d4cb3 281 *
sam_grove 5:3f93dd1d4cb3 282 * @param name the name of the netif (like netif->name) plus concatenated number
sam_grove 5:3f93dd1d4cb3 283 * in ascii representation (e.g. 'en0')
sam_grove 5:3f93dd1d4cb3 284 */
sam_grove 5:3f93dd1d4cb3 285 struct netif *
sam_grove 5:3f93dd1d4cb3 286 netif_find(char *name)
sam_grove 5:3f93dd1d4cb3 287 {
sam_grove 5:3f93dd1d4cb3 288 struct netif *netif;
sam_grove 5:3f93dd1d4cb3 289 u8_t num;
sam_grove 5:3f93dd1d4cb3 290
sam_grove 5:3f93dd1d4cb3 291 if (name == NULL) {
sam_grove 5:3f93dd1d4cb3 292 return NULL;
sam_grove 5:3f93dd1d4cb3 293 }
sam_grove 5:3f93dd1d4cb3 294
sam_grove 5:3f93dd1d4cb3 295 num = name[2] - '0';
sam_grove 5:3f93dd1d4cb3 296
sam_grove 5:3f93dd1d4cb3 297 for(netif = netif_list; netif != NULL; netif = netif->next) {
sam_grove 5:3f93dd1d4cb3 298 if (num == netif->num &&
sam_grove 5:3f93dd1d4cb3 299 name[0] == netif->name[0] &&
sam_grove 5:3f93dd1d4cb3 300 name[1] == netif->name[1]) {
sam_grove 5:3f93dd1d4cb3 301 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
sam_grove 5:3f93dd1d4cb3 302 return netif;
sam_grove 5:3f93dd1d4cb3 303 }
sam_grove 5:3f93dd1d4cb3 304 }
sam_grove 5:3f93dd1d4cb3 305 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
sam_grove 5:3f93dd1d4cb3 306 return NULL;
sam_grove 5:3f93dd1d4cb3 307 }
sam_grove 5:3f93dd1d4cb3 308
sam_grove 5:3f93dd1d4cb3 309 /**
sam_grove 5:3f93dd1d4cb3 310 * Change the IP address of a network interface
sam_grove 5:3f93dd1d4cb3 311 *
sam_grove 5:3f93dd1d4cb3 312 * @param netif the network interface to change
sam_grove 5:3f93dd1d4cb3 313 * @param ipaddr the new IP address
sam_grove 5:3f93dd1d4cb3 314 *
sam_grove 5:3f93dd1d4cb3 315 * @note call netif_set_addr() if you also want to change netmask and
sam_grove 5:3f93dd1d4cb3 316 * default gateway
sam_grove 5:3f93dd1d4cb3 317 */
sam_grove 5:3f93dd1d4cb3 318 void
sam_grove 5:3f93dd1d4cb3 319 netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
sam_grove 5:3f93dd1d4cb3 320 {
sam_grove 5:3f93dd1d4cb3 321 /* TODO: Handling of obsolete pcbs */
sam_grove 5:3f93dd1d4cb3 322 /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
sam_grove 5:3f93dd1d4cb3 323 #if LWIP_TCP
sam_grove 5:3f93dd1d4cb3 324 struct tcp_pcb *pcb;
sam_grove 5:3f93dd1d4cb3 325 struct tcp_pcb_listen *lpcb;
sam_grove 5:3f93dd1d4cb3 326
sam_grove 5:3f93dd1d4cb3 327 /* address is actually being changed? */
sam_grove 5:3f93dd1d4cb3 328 if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) {
sam_grove 5:3f93dd1d4cb3 329 /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
sam_grove 5:3f93dd1d4cb3 330 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
sam_grove 5:3f93dd1d4cb3 331 pcb = tcp_active_pcbs;
sam_grove 5:3f93dd1d4cb3 332 while (pcb != NULL) {
sam_grove 5:3f93dd1d4cb3 333 /* PCB bound to current local interface address? */
sam_grove 5:3f93dd1d4cb3 334 if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))
sam_grove 5:3f93dd1d4cb3 335 #if LWIP_AUTOIP
sam_grove 5:3f93dd1d4cb3 336 /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
sam_grove 5:3f93dd1d4cb3 337 && !ip_addr_islinklocal(&(pcb->local_ip))
sam_grove 5:3f93dd1d4cb3 338 #endif /* LWIP_AUTOIP */
sam_grove 5:3f93dd1d4cb3 339 ) {
sam_grove 5:3f93dd1d4cb3 340 /* this connection must be aborted */
sam_grove 5:3f93dd1d4cb3 341 struct tcp_pcb *next = pcb->next;
sam_grove 5:3f93dd1d4cb3 342 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
sam_grove 5:3f93dd1d4cb3 343 tcp_abort(pcb);
sam_grove 5:3f93dd1d4cb3 344 pcb = next;
sam_grove 5:3f93dd1d4cb3 345 } else {
sam_grove 5:3f93dd1d4cb3 346 pcb = pcb->next;
sam_grove 5:3f93dd1d4cb3 347 }
sam_grove 5:3f93dd1d4cb3 348 }
sam_grove 5:3f93dd1d4cb3 349 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
sam_grove 5:3f93dd1d4cb3 350 /* PCB bound to current local interface address? */
sam_grove 5:3f93dd1d4cb3 351 if ((!(ip_addr_isany(&(lpcb->local_ip)))) &&
sam_grove 5:3f93dd1d4cb3 352 (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr)))) {
sam_grove 5:3f93dd1d4cb3 353 /* The PCB is listening to the old ipaddr and
sam_grove 5:3f93dd1d4cb3 354 * is set to listen to the new one instead */
sam_grove 5:3f93dd1d4cb3 355 ip_addr_set(&(lpcb->local_ip), ipaddr);
sam_grove 5:3f93dd1d4cb3 356 }
sam_grove 5:3f93dd1d4cb3 357 }
sam_grove 5:3f93dd1d4cb3 358 }
sam_grove 5:3f93dd1d4cb3 359 #endif
sam_grove 5:3f93dd1d4cb3 360 snmp_delete_ipaddridx_tree(netif);
sam_grove 5:3f93dd1d4cb3 361 snmp_delete_iprteidx_tree(0,netif);
sam_grove 5:3f93dd1d4cb3 362 /* set new IP address to netif */
sam_grove 5:3f93dd1d4cb3 363 ip_addr_set(&(netif->ip_addr), ipaddr);
sam_grove 5:3f93dd1d4cb3 364 snmp_insert_ipaddridx_tree(netif);
sam_grove 5:3f93dd1d4cb3 365 snmp_insert_iprteidx_tree(0,netif);
sam_grove 5:3f93dd1d4cb3 366
sam_grove 5:3f93dd1d4cb3 367 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IP address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
sam_grove 5:3f93dd1d4cb3 368 netif->name[0], netif->name[1],
sam_grove 5:3f93dd1d4cb3 369 ip4_addr1_16(&netif->ip_addr),
sam_grove 5:3f93dd1d4cb3 370 ip4_addr2_16(&netif->ip_addr),
sam_grove 5:3f93dd1d4cb3 371 ip4_addr3_16(&netif->ip_addr),
sam_grove 5:3f93dd1d4cb3 372 ip4_addr4_16(&netif->ip_addr)));
sam_grove 5:3f93dd1d4cb3 373 }
sam_grove 5:3f93dd1d4cb3 374
sam_grove 5:3f93dd1d4cb3 375 /**
sam_grove 5:3f93dd1d4cb3 376 * Change the default gateway for a network interface
sam_grove 5:3f93dd1d4cb3 377 *
sam_grove 5:3f93dd1d4cb3 378 * @param netif the network interface to change
sam_grove 5:3f93dd1d4cb3 379 * @param gw the new default gateway
sam_grove 5:3f93dd1d4cb3 380 *
sam_grove 5:3f93dd1d4cb3 381 * @note call netif_set_addr() if you also want to change ip address and netmask
sam_grove 5:3f93dd1d4cb3 382 */
sam_grove 5:3f93dd1d4cb3 383 void
sam_grove 5:3f93dd1d4cb3 384 netif_set_gw(struct netif *netif, ip_addr_t *gw)
sam_grove 5:3f93dd1d4cb3 385 {
sam_grove 5:3f93dd1d4cb3 386 ip_addr_set(&(netif->gw), gw);
sam_grove 5:3f93dd1d4cb3 387 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
sam_grove 5:3f93dd1d4cb3 388 netif->name[0], netif->name[1],
sam_grove 5:3f93dd1d4cb3 389 ip4_addr1_16(&netif->gw),
sam_grove 5:3f93dd1d4cb3 390 ip4_addr2_16(&netif->gw),
sam_grove 5:3f93dd1d4cb3 391 ip4_addr3_16(&netif->gw),
sam_grove 5:3f93dd1d4cb3 392 ip4_addr4_16(&netif->gw)));
sam_grove 5:3f93dd1d4cb3 393 }
sam_grove 5:3f93dd1d4cb3 394
sam_grove 5:3f93dd1d4cb3 395 /**
sam_grove 5:3f93dd1d4cb3 396 * Change the netmask of a network interface
sam_grove 5:3f93dd1d4cb3 397 *
sam_grove 5:3f93dd1d4cb3 398 * @param netif the network interface to change
sam_grove 5:3f93dd1d4cb3 399 * @param netmask the new netmask
sam_grove 5:3f93dd1d4cb3 400 *
sam_grove 5:3f93dd1d4cb3 401 * @note call netif_set_addr() if you also want to change ip address and
sam_grove 5:3f93dd1d4cb3 402 * default gateway
sam_grove 5:3f93dd1d4cb3 403 */
sam_grove 5:3f93dd1d4cb3 404 void
sam_grove 5:3f93dd1d4cb3 405 netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
sam_grove 5:3f93dd1d4cb3 406 {
sam_grove 5:3f93dd1d4cb3 407 snmp_delete_iprteidx_tree(0, netif);
sam_grove 5:3f93dd1d4cb3 408 /* set new netmask to netif */
sam_grove 5:3f93dd1d4cb3 409 ip_addr_set(&(netif->netmask), netmask);
sam_grove 5:3f93dd1d4cb3 410 snmp_insert_iprteidx_tree(0, netif);
sam_grove 5:3f93dd1d4cb3 411 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
sam_grove 5:3f93dd1d4cb3 412 netif->name[0], netif->name[1],
sam_grove 5:3f93dd1d4cb3 413 ip4_addr1_16(&netif->netmask),
sam_grove 5:3f93dd1d4cb3 414 ip4_addr2_16(&netif->netmask),
sam_grove 5:3f93dd1d4cb3 415 ip4_addr3_16(&netif->netmask),
sam_grove 5:3f93dd1d4cb3 416 ip4_addr4_16(&netif->netmask)));
sam_grove 5:3f93dd1d4cb3 417 }
sam_grove 5:3f93dd1d4cb3 418
sam_grove 5:3f93dd1d4cb3 419 /**
sam_grove 5:3f93dd1d4cb3 420 * Set a network interface as the default network interface
sam_grove 5:3f93dd1d4cb3 421 * (used to output all packets for which no specific route is found)
sam_grove 5:3f93dd1d4cb3 422 *
sam_grove 5:3f93dd1d4cb3 423 * @param netif the default network interface
sam_grove 5:3f93dd1d4cb3 424 */
sam_grove 5:3f93dd1d4cb3 425 void
sam_grove 5:3f93dd1d4cb3 426 netif_set_default(struct netif *netif)
sam_grove 5:3f93dd1d4cb3 427 {
sam_grove 5:3f93dd1d4cb3 428 if (netif == NULL) {
sam_grove 5:3f93dd1d4cb3 429 /* remove default route */
sam_grove 5:3f93dd1d4cb3 430 snmp_delete_iprteidx_tree(1, netif);
sam_grove 5:3f93dd1d4cb3 431 } else {
sam_grove 5:3f93dd1d4cb3 432 /* install default route */
sam_grove 5:3f93dd1d4cb3 433 snmp_insert_iprteidx_tree(1, netif);
sam_grove 5:3f93dd1d4cb3 434 }
sam_grove 5:3f93dd1d4cb3 435 netif_default = netif;
sam_grove 5:3f93dd1d4cb3 436 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
sam_grove 5:3f93dd1d4cb3 437 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
sam_grove 5:3f93dd1d4cb3 438 }
sam_grove 5:3f93dd1d4cb3 439
sam_grove 5:3f93dd1d4cb3 440 /**
sam_grove 5:3f93dd1d4cb3 441 * Bring an interface up, available for processing
sam_grove 5:3f93dd1d4cb3 442 * traffic.
sam_grove 5:3f93dd1d4cb3 443 *
sam_grove 5:3f93dd1d4cb3 444 * @note: Enabling DHCP on a down interface will make it come
sam_grove 5:3f93dd1d4cb3 445 * up once configured.
sam_grove 5:3f93dd1d4cb3 446 *
sam_grove 5:3f93dd1d4cb3 447 * @see dhcp_start()
sam_grove 5:3f93dd1d4cb3 448 */
sam_grove 5:3f93dd1d4cb3 449 void netif_set_up(struct netif *netif)
sam_grove 5:3f93dd1d4cb3 450 {
sam_grove 5:3f93dd1d4cb3 451 if (!(netif->flags & NETIF_FLAG_UP)) {
sam_grove 5:3f93dd1d4cb3 452 netif->flags |= NETIF_FLAG_UP;
sam_grove 5:3f93dd1d4cb3 453
sam_grove 5:3f93dd1d4cb3 454 #if LWIP_SNMP
sam_grove 5:3f93dd1d4cb3 455 snmp_get_sysuptime(&netif->ts);
sam_grove 5:3f93dd1d4cb3 456 #endif /* LWIP_SNMP */
sam_grove 5:3f93dd1d4cb3 457
sam_grove 5:3f93dd1d4cb3 458 NETIF_STATUS_CALLBACK(netif);
sam_grove 5:3f93dd1d4cb3 459
sam_grove 5:3f93dd1d4cb3 460 if (netif->flags & NETIF_FLAG_LINK_UP) {
sam_grove 5:3f93dd1d4cb3 461 #if LWIP_ARP
sam_grove 5:3f93dd1d4cb3 462 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
sam_grove 5:3f93dd1d4cb3 463 if (netif->flags & (NETIF_FLAG_ETHARP)) {
sam_grove 5:3f93dd1d4cb3 464 etharp_gratuitous(netif);
sam_grove 5:3f93dd1d4cb3 465 }
sam_grove 5:3f93dd1d4cb3 466 #endif /* LWIP_ARP */
sam_grove 5:3f93dd1d4cb3 467
sam_grove 5:3f93dd1d4cb3 468 #if LWIP_IGMP
sam_grove 5:3f93dd1d4cb3 469 /* resend IGMP memberships */
sam_grove 5:3f93dd1d4cb3 470 if (netif->flags & NETIF_FLAG_IGMP) {
sam_grove 5:3f93dd1d4cb3 471 igmp_report_groups( netif);
sam_grove 5:3f93dd1d4cb3 472 }
sam_grove 5:3f93dd1d4cb3 473 #endif /* LWIP_IGMP */
sam_grove 5:3f93dd1d4cb3 474 }
sam_grove 5:3f93dd1d4cb3 475 }
sam_grove 5:3f93dd1d4cb3 476 }
sam_grove 5:3f93dd1d4cb3 477
sam_grove 5:3f93dd1d4cb3 478 /**
sam_grove 5:3f93dd1d4cb3 479 * Bring an interface down, disabling any traffic processing.
sam_grove 5:3f93dd1d4cb3 480 *
sam_grove 5:3f93dd1d4cb3 481 * @note: Enabling DHCP on a down interface will make it come
sam_grove 5:3f93dd1d4cb3 482 * up once configured.
sam_grove 5:3f93dd1d4cb3 483 *
sam_grove 5:3f93dd1d4cb3 484 * @see dhcp_start()
sam_grove 5:3f93dd1d4cb3 485 */
sam_grove 5:3f93dd1d4cb3 486 void netif_set_down(struct netif *netif)
sam_grove 5:3f93dd1d4cb3 487 {
sam_grove 5:3f93dd1d4cb3 488 if (netif->flags & NETIF_FLAG_UP) {
sam_grove 5:3f93dd1d4cb3 489 netif->flags &= ~NETIF_FLAG_UP;
sam_grove 5:3f93dd1d4cb3 490 #if LWIP_SNMP
sam_grove 5:3f93dd1d4cb3 491 snmp_get_sysuptime(&netif->ts);
sam_grove 5:3f93dd1d4cb3 492 #endif
sam_grove 5:3f93dd1d4cb3 493
sam_grove 5:3f93dd1d4cb3 494 NETIF_STATUS_CALLBACK(netif);
sam_grove 5:3f93dd1d4cb3 495 }
sam_grove 5:3f93dd1d4cb3 496 }
sam_grove 5:3f93dd1d4cb3 497
sam_grove 5:3f93dd1d4cb3 498 #if LWIP_NETIF_STATUS_CALLBACK
sam_grove 5:3f93dd1d4cb3 499 /**
sam_grove 5:3f93dd1d4cb3 500 * Set callback to be called when interface is brought up/down
sam_grove 5:3f93dd1d4cb3 501 */
sam_grove 5:3f93dd1d4cb3 502 void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
sam_grove 5:3f93dd1d4cb3 503 {
sam_grove 5:3f93dd1d4cb3 504 if (netif) {
sam_grove 5:3f93dd1d4cb3 505 netif->status_callback = status_callback;
sam_grove 5:3f93dd1d4cb3 506 }
sam_grove 5:3f93dd1d4cb3 507 }
sam_grove 5:3f93dd1d4cb3 508 #endif /* LWIP_NETIF_STATUS_CALLBACK */
sam_grove 5:3f93dd1d4cb3 509
sam_grove 5:3f93dd1d4cb3 510 /**
sam_grove 5:3f93dd1d4cb3 511 * Called by a driver when its link goes up
sam_grove 5:3f93dd1d4cb3 512 */
sam_grove 5:3f93dd1d4cb3 513 void netif_set_link_up(struct netif *netif )
sam_grove 5:3f93dd1d4cb3 514 {
sam_grove 5:3f93dd1d4cb3 515 if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
sam_grove 5:3f93dd1d4cb3 516 netif->flags |= NETIF_FLAG_LINK_UP;
sam_grove 5:3f93dd1d4cb3 517
sam_grove 5:3f93dd1d4cb3 518 #if LWIP_DHCP
sam_grove 5:3f93dd1d4cb3 519 if (netif->dhcp) {
sam_grove 5:3f93dd1d4cb3 520 dhcp_network_changed(netif);
sam_grove 5:3f93dd1d4cb3 521 }
sam_grove 5:3f93dd1d4cb3 522 #endif /* LWIP_DHCP */
sam_grove 5:3f93dd1d4cb3 523
sam_grove 5:3f93dd1d4cb3 524 #if LWIP_AUTOIP
sam_grove 5:3f93dd1d4cb3 525 if (netif->autoip) {
sam_grove 5:3f93dd1d4cb3 526 autoip_network_changed(netif);
sam_grove 5:3f93dd1d4cb3 527 }
sam_grove 5:3f93dd1d4cb3 528 #endif /* LWIP_AUTOIP */
sam_grove 5:3f93dd1d4cb3 529
sam_grove 5:3f93dd1d4cb3 530 if (netif->flags & NETIF_FLAG_UP) {
sam_grove 5:3f93dd1d4cb3 531 #if LWIP_ARP
sam_grove 5:3f93dd1d4cb3 532 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
sam_grove 5:3f93dd1d4cb3 533 if (netif->flags & NETIF_FLAG_ETHARP) {
sam_grove 5:3f93dd1d4cb3 534 etharp_gratuitous(netif);
sam_grove 5:3f93dd1d4cb3 535 }
sam_grove 5:3f93dd1d4cb3 536 #endif /* LWIP_ARP */
sam_grove 5:3f93dd1d4cb3 537
sam_grove 5:3f93dd1d4cb3 538 #if LWIP_IGMP
sam_grove 5:3f93dd1d4cb3 539 /* resend IGMP memberships */
sam_grove 5:3f93dd1d4cb3 540 if (netif->flags & NETIF_FLAG_IGMP) {
sam_grove 5:3f93dd1d4cb3 541 igmp_report_groups( netif);
sam_grove 5:3f93dd1d4cb3 542 }
sam_grove 5:3f93dd1d4cb3 543 #endif /* LWIP_IGMP */
sam_grove 5:3f93dd1d4cb3 544 }
sam_grove 5:3f93dd1d4cb3 545 NETIF_LINK_CALLBACK(netif);
sam_grove 5:3f93dd1d4cb3 546 }
sam_grove 5:3f93dd1d4cb3 547 }
sam_grove 5:3f93dd1d4cb3 548
sam_grove 5:3f93dd1d4cb3 549 /**
sam_grove 5:3f93dd1d4cb3 550 * Called by a driver when its link goes down
sam_grove 5:3f93dd1d4cb3 551 */
sam_grove 5:3f93dd1d4cb3 552 void netif_set_link_down(struct netif *netif )
sam_grove 5:3f93dd1d4cb3 553 {
sam_grove 5:3f93dd1d4cb3 554 if (netif->flags & NETIF_FLAG_LINK_UP) {
sam_grove 5:3f93dd1d4cb3 555 netif->flags &= ~NETIF_FLAG_LINK_UP;
sam_grove 5:3f93dd1d4cb3 556 NETIF_LINK_CALLBACK(netif);
sam_grove 5:3f93dd1d4cb3 557 }
sam_grove 5:3f93dd1d4cb3 558 }
sam_grove 5:3f93dd1d4cb3 559
sam_grove 5:3f93dd1d4cb3 560 #if LWIP_NETIF_LINK_CALLBACK
sam_grove 5:3f93dd1d4cb3 561 /**
sam_grove 5:3f93dd1d4cb3 562 * Set callback to be called when link is brought up/down
sam_grove 5:3f93dd1d4cb3 563 */
sam_grove 5:3f93dd1d4cb3 564 void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
sam_grove 5:3f93dd1d4cb3 565 {
sam_grove 5:3f93dd1d4cb3 566 if (netif) {
sam_grove 5:3f93dd1d4cb3 567 netif->link_callback = link_callback;
sam_grove 5:3f93dd1d4cb3 568 }
sam_grove 5:3f93dd1d4cb3 569 }
sam_grove 5:3f93dd1d4cb3 570 #endif /* LWIP_NETIF_LINK_CALLBACK */
sam_grove 5:3f93dd1d4cb3 571
sam_grove 5:3f93dd1d4cb3 572 #if ENABLE_LOOPBACK
sam_grove 5:3f93dd1d4cb3 573 /**
sam_grove 5:3f93dd1d4cb3 574 * Send an IP packet to be received on the same netif (loopif-like).
sam_grove 5:3f93dd1d4cb3 575 * The pbuf is simply copied and handed back to netif->input.
sam_grove 5:3f93dd1d4cb3 576 * In multithreaded mode, this is done directly since netif->input must put
sam_grove 5:3f93dd1d4cb3 577 * the packet on a queue.
sam_grove 5:3f93dd1d4cb3 578 * In callback mode, the packet is put on an internal queue and is fed to
sam_grove 5:3f93dd1d4cb3 579 * netif->input by netif_poll().
sam_grove 5:3f93dd1d4cb3 580 *
sam_grove 5:3f93dd1d4cb3 581 * @param netif the lwip network interface structure
sam_grove 5:3f93dd1d4cb3 582 * @param p the (IP) packet to 'send'
sam_grove 5:3f93dd1d4cb3 583 * @param ipaddr the ip address to send the packet to (not used)
sam_grove 5:3f93dd1d4cb3 584 * @return ERR_OK if the packet has been sent
sam_grove 5:3f93dd1d4cb3 585 * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
sam_grove 5:3f93dd1d4cb3 586 */
sam_grove 5:3f93dd1d4cb3 587 err_t
sam_grove 5:3f93dd1d4cb3 588 netif_loop_output(struct netif *netif, struct pbuf *p,
sam_grove 5:3f93dd1d4cb3 589 ip_addr_t *ipaddr)
sam_grove 5:3f93dd1d4cb3 590 {
sam_grove 5:3f93dd1d4cb3 591 struct pbuf *r;
sam_grove 5:3f93dd1d4cb3 592 err_t err;
sam_grove 5:3f93dd1d4cb3 593 struct pbuf *last;
sam_grove 5:3f93dd1d4cb3 594 #if LWIP_LOOPBACK_MAX_PBUFS
sam_grove 5:3f93dd1d4cb3 595 u8_t clen = 0;
sam_grove 5:3f93dd1d4cb3 596 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
sam_grove 5:3f93dd1d4cb3 597 /* If we have a loopif, SNMP counters are adjusted for it,
sam_grove 5:3f93dd1d4cb3 598 * if not they are adjusted for 'netif'. */
sam_grove 5:3f93dd1d4cb3 599 #if LWIP_SNMP
sam_grove 5:3f93dd1d4cb3 600 #if LWIP_HAVE_LOOPIF
sam_grove 5:3f93dd1d4cb3 601 struct netif *stats_if = &loop_netif;
sam_grove 5:3f93dd1d4cb3 602 #else /* LWIP_HAVE_LOOPIF */
sam_grove 5:3f93dd1d4cb3 603 struct netif *stats_if = netif;
sam_grove 5:3f93dd1d4cb3 604 #endif /* LWIP_HAVE_LOOPIF */
sam_grove 5:3f93dd1d4cb3 605 #endif /* LWIP_SNMP */
sam_grove 5:3f93dd1d4cb3 606 SYS_ARCH_DECL_PROTECT(lev);
sam_grove 5:3f93dd1d4cb3 607 LWIP_UNUSED_ARG(ipaddr);
sam_grove 5:3f93dd1d4cb3 608
sam_grove 5:3f93dd1d4cb3 609 /* Allocate a new pbuf */
sam_grove 5:3f93dd1d4cb3 610 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
sam_grove 5:3f93dd1d4cb3 611 if (r == NULL) {
sam_grove 5:3f93dd1d4cb3 612 LINK_STATS_INC(link.memerr);
sam_grove 5:3f93dd1d4cb3 613 LINK_STATS_INC(link.drop);
sam_grove 5:3f93dd1d4cb3 614 snmp_inc_ifoutdiscards(stats_if);
sam_grove 5:3f93dd1d4cb3 615 return ERR_MEM;
sam_grove 5:3f93dd1d4cb3 616 }
sam_grove 5:3f93dd1d4cb3 617 #if LWIP_LOOPBACK_MAX_PBUFS
sam_grove 5:3f93dd1d4cb3 618 clen = pbuf_clen(r);
sam_grove 5:3f93dd1d4cb3 619 /* check for overflow or too many pbuf on queue */
sam_grove 5:3f93dd1d4cb3 620 if(((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
sam_grove 5:3f93dd1d4cb3 621 ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
sam_grove 5:3f93dd1d4cb3 622 pbuf_free(r);
sam_grove 5:3f93dd1d4cb3 623 LINK_STATS_INC(link.memerr);
sam_grove 5:3f93dd1d4cb3 624 LINK_STATS_INC(link.drop);
sam_grove 5:3f93dd1d4cb3 625 snmp_inc_ifoutdiscards(stats_if);
sam_grove 5:3f93dd1d4cb3 626 return ERR_MEM;
sam_grove 5:3f93dd1d4cb3 627 }
sam_grove 5:3f93dd1d4cb3 628 netif->loop_cnt_current += clen;
sam_grove 5:3f93dd1d4cb3 629 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
sam_grove 5:3f93dd1d4cb3 630
sam_grove 5:3f93dd1d4cb3 631 /* Copy the whole pbuf queue p into the single pbuf r */
sam_grove 5:3f93dd1d4cb3 632 if ((err = pbuf_copy(r, p)) != ERR_OK) {
sam_grove 5:3f93dd1d4cb3 633 pbuf_free(r);
sam_grove 5:3f93dd1d4cb3 634 LINK_STATS_INC(link.memerr);
sam_grove 5:3f93dd1d4cb3 635 LINK_STATS_INC(link.drop);
sam_grove 5:3f93dd1d4cb3 636 snmp_inc_ifoutdiscards(stats_if);
sam_grove 5:3f93dd1d4cb3 637 return err;
sam_grove 5:3f93dd1d4cb3 638 }
sam_grove 5:3f93dd1d4cb3 639
sam_grove 5:3f93dd1d4cb3 640 /* Put the packet on a linked list which gets emptied through calling
sam_grove 5:3f93dd1d4cb3 641 netif_poll(). */
sam_grove 5:3f93dd1d4cb3 642
sam_grove 5:3f93dd1d4cb3 643 /* let last point to the last pbuf in chain r */
sam_grove 5:3f93dd1d4cb3 644 for (last = r; last->next != NULL; last = last->next);
sam_grove 5:3f93dd1d4cb3 645
sam_grove 5:3f93dd1d4cb3 646 SYS_ARCH_PROTECT(lev);
sam_grove 5:3f93dd1d4cb3 647 if(netif->loop_first != NULL) {
sam_grove 5:3f93dd1d4cb3 648 LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
sam_grove 5:3f93dd1d4cb3 649 netif->loop_last->next = r;
sam_grove 5:3f93dd1d4cb3 650 netif->loop_last = last;
sam_grove 5:3f93dd1d4cb3 651 } else {
sam_grove 5:3f93dd1d4cb3 652 netif->loop_first = r;
sam_grove 5:3f93dd1d4cb3 653 netif->loop_last = last;
sam_grove 5:3f93dd1d4cb3 654 }
sam_grove 5:3f93dd1d4cb3 655 SYS_ARCH_UNPROTECT(lev);
sam_grove 5:3f93dd1d4cb3 656
sam_grove 5:3f93dd1d4cb3 657 LINK_STATS_INC(link.xmit);
sam_grove 5:3f93dd1d4cb3 658 snmp_add_ifoutoctets(stats_if, p->tot_len);
sam_grove 5:3f93dd1d4cb3 659 snmp_inc_ifoutucastpkts(stats_if);
sam_grove 5:3f93dd1d4cb3 660
sam_grove 5:3f93dd1d4cb3 661 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
sam_grove 5:3f93dd1d4cb3 662 /* For multithreading environment, schedule a call to netif_poll */
sam_grove 5:3f93dd1d4cb3 663 tcpip_callback((tcpip_callback_fn)netif_poll, netif);
sam_grove 5:3f93dd1d4cb3 664 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
sam_grove 5:3f93dd1d4cb3 665
sam_grove 5:3f93dd1d4cb3 666 return ERR_OK;
sam_grove 5:3f93dd1d4cb3 667 }
sam_grove 5:3f93dd1d4cb3 668
sam_grove 5:3f93dd1d4cb3 669 /**
sam_grove 5:3f93dd1d4cb3 670 * Call netif_poll() in the main loop of your application. This is to prevent
sam_grove 5:3f93dd1d4cb3 671 * reentering non-reentrant functions like tcp_input(). Packets passed to
sam_grove 5:3f93dd1d4cb3 672 * netif_loop_output() are put on a list that is passed to netif->input() by
sam_grove 5:3f93dd1d4cb3 673 * netif_poll().
sam_grove 5:3f93dd1d4cb3 674 */
sam_grove 5:3f93dd1d4cb3 675 void
sam_grove 5:3f93dd1d4cb3 676 netif_poll(struct netif *netif)
sam_grove 5:3f93dd1d4cb3 677 {
sam_grove 5:3f93dd1d4cb3 678 struct pbuf *in;
sam_grove 5:3f93dd1d4cb3 679 /* If we have a loopif, SNMP counters are adjusted for it,
sam_grove 5:3f93dd1d4cb3 680 * if not they are adjusted for 'netif'. */
sam_grove 5:3f93dd1d4cb3 681 #if LWIP_SNMP
sam_grove 5:3f93dd1d4cb3 682 #if LWIP_HAVE_LOOPIF
sam_grove 5:3f93dd1d4cb3 683 struct netif *stats_if = &loop_netif;
sam_grove 5:3f93dd1d4cb3 684 #else /* LWIP_HAVE_LOOPIF */
sam_grove 5:3f93dd1d4cb3 685 struct netif *stats_if = netif;
sam_grove 5:3f93dd1d4cb3 686 #endif /* LWIP_HAVE_LOOPIF */
sam_grove 5:3f93dd1d4cb3 687 #endif /* LWIP_SNMP */
sam_grove 5:3f93dd1d4cb3 688 SYS_ARCH_DECL_PROTECT(lev);
sam_grove 5:3f93dd1d4cb3 689
sam_grove 5:3f93dd1d4cb3 690 do {
sam_grove 5:3f93dd1d4cb3 691 /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
sam_grove 5:3f93dd1d4cb3 692 SYS_ARCH_PROTECT(lev);
sam_grove 5:3f93dd1d4cb3 693 in = netif->loop_first;
sam_grove 5:3f93dd1d4cb3 694 if (in != NULL) {
sam_grove 5:3f93dd1d4cb3 695 struct pbuf *in_end = in;
sam_grove 5:3f93dd1d4cb3 696 #if LWIP_LOOPBACK_MAX_PBUFS
sam_grove 5:3f93dd1d4cb3 697 u8_t clen = pbuf_clen(in);
sam_grove 5:3f93dd1d4cb3 698 /* adjust the number of pbufs on queue */
sam_grove 5:3f93dd1d4cb3 699 LWIP_ASSERT("netif->loop_cnt_current underflow",
sam_grove 5:3f93dd1d4cb3 700 ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
sam_grove 5:3f93dd1d4cb3 701 netif->loop_cnt_current -= clen;
sam_grove 5:3f93dd1d4cb3 702 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
sam_grove 5:3f93dd1d4cb3 703 while (in_end->len != in_end->tot_len) {
sam_grove 5:3f93dd1d4cb3 704 LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
sam_grove 5:3f93dd1d4cb3 705 in_end = in_end->next;
sam_grove 5:3f93dd1d4cb3 706 }
sam_grove 5:3f93dd1d4cb3 707 /* 'in_end' now points to the last pbuf from 'in' */
sam_grove 5:3f93dd1d4cb3 708 if (in_end == netif->loop_last) {
sam_grove 5:3f93dd1d4cb3 709 /* this was the last pbuf in the list */
sam_grove 5:3f93dd1d4cb3 710 netif->loop_first = netif->loop_last = NULL;
sam_grove 5:3f93dd1d4cb3 711 } else {
sam_grove 5:3f93dd1d4cb3 712 /* pop the pbuf off the list */
sam_grove 5:3f93dd1d4cb3 713 netif->loop_first = in_end->next;
sam_grove 5:3f93dd1d4cb3 714 LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
sam_grove 5:3f93dd1d4cb3 715 }
sam_grove 5:3f93dd1d4cb3 716 /* De-queue the pbuf from its successors on the 'loop_' list. */
sam_grove 5:3f93dd1d4cb3 717 in_end->next = NULL;
sam_grove 5:3f93dd1d4cb3 718 }
sam_grove 5:3f93dd1d4cb3 719 SYS_ARCH_UNPROTECT(lev);
sam_grove 5:3f93dd1d4cb3 720
sam_grove 5:3f93dd1d4cb3 721 if (in != NULL) {
sam_grove 5:3f93dd1d4cb3 722 LINK_STATS_INC(link.recv);
sam_grove 5:3f93dd1d4cb3 723 snmp_add_ifinoctets(stats_if, in->tot_len);
sam_grove 5:3f93dd1d4cb3 724 snmp_inc_ifinucastpkts(stats_if);
sam_grove 5:3f93dd1d4cb3 725 /* loopback packets are always IP packets! */
sam_grove 5:3f93dd1d4cb3 726 if (ip_input(in, netif) != ERR_OK) {
sam_grove 5:3f93dd1d4cb3 727 pbuf_free(in);
sam_grove 5:3f93dd1d4cb3 728 }
sam_grove 5:3f93dd1d4cb3 729 /* Don't reference the packet any more! */
sam_grove 5:3f93dd1d4cb3 730 in = NULL;
sam_grove 5:3f93dd1d4cb3 731 }
sam_grove 5:3f93dd1d4cb3 732 /* go on while there is a packet on the list */
sam_grove 5:3f93dd1d4cb3 733 } while (netif->loop_first != NULL);
sam_grove 5:3f93dd1d4cb3 734 }
sam_grove 5:3f93dd1d4cb3 735
sam_grove 5:3f93dd1d4cb3 736 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
sam_grove 5:3f93dd1d4cb3 737 /**
sam_grove 5:3f93dd1d4cb3 738 * Calls netif_poll() for every netif on the netif_list.
sam_grove 5:3f93dd1d4cb3 739 */
sam_grove 5:3f93dd1d4cb3 740 void
sam_grove 5:3f93dd1d4cb3 741 netif_poll_all(void)
sam_grove 5:3f93dd1d4cb3 742 {
sam_grove 5:3f93dd1d4cb3 743 struct netif *netif = netif_list;
sam_grove 5:3f93dd1d4cb3 744 /* loop through netifs */
sam_grove 5:3f93dd1d4cb3 745 while (netif != NULL) {
sam_grove 5:3f93dd1d4cb3 746 netif_poll(netif);
sam_grove 5:3f93dd1d4cb3 747 /* proceed to next network interface */
sam_grove 5:3f93dd1d4cb3 748 netif = netif->next;
sam_grove 5:3f93dd1d4cb3 749 }
sam_grove 5:3f93dd1d4cb3 750 }
sam_grove 5:3f93dd1d4cb3 751 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
sam_grove 5:3f93dd1d4cb3 752 #endif /* ENABLE_LOOPBACK */