LwIP with PPP & Ethernet integration
This is the mbed port of the LwIP stack: http://savannah.nongnu.org/projects/lwip/
It includes contributed content from NXP's port for LPCxxxx devices: http://www.lpcware.com/content/project/lightweight-ip-lwip-networking-stack
Licence
LwIP is licenced under the BSD licence:
Copyright (c) 2001-2004 Swedish Institute of Computer Science. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
src/netif/slipif.c@2:1a87f74b8e3b, 2012-05-25 (annotated)
- Committer:
- donatien
- Date:
- Fri May 25 08:56:35 2012 +0000
- Revision:
- 2:1a87f74b8e3b
- Parent:
- 0:8e01dca41002
Removed compilation of EMAC driver when using PPP
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:8e01dca41002 | 1 | /** |
donatien | 0:8e01dca41002 | 2 | * @file |
donatien | 0:8e01dca41002 | 3 | * SLIP Interface |
donatien | 0:8e01dca41002 | 4 | * |
donatien | 0:8e01dca41002 | 5 | */ |
donatien | 0:8e01dca41002 | 6 | |
donatien | 0:8e01dca41002 | 7 | /* |
donatien | 0:8e01dca41002 | 8 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
donatien | 0:8e01dca41002 | 9 | * All rights reserved. |
donatien | 0:8e01dca41002 | 10 | * |
donatien | 0:8e01dca41002 | 11 | * Redistribution and use in source and binary forms, with or without |
donatien | 0:8e01dca41002 | 12 | * modification, are permitted provided that the following conditions |
donatien | 0:8e01dca41002 | 13 | * are met: |
donatien | 0:8e01dca41002 | 14 | * 1. Redistributions of source code must retain the above copyright |
donatien | 0:8e01dca41002 | 15 | * notice, this list of conditions and the following disclaimer. |
donatien | 0:8e01dca41002 | 16 | * 2. Redistributions in binary form must reproduce the above copyright |
donatien | 0:8e01dca41002 | 17 | * notice, this list of conditions and the following disclaimer in the |
donatien | 0:8e01dca41002 | 18 | * documentation and/or other materials provided with the distribution. |
donatien | 0:8e01dca41002 | 19 | * 3. Neither the name of the Institute nor the names of its contributors |
donatien | 0:8e01dca41002 | 20 | * may be used to endorse or promote products derived from this software |
donatien | 0:8e01dca41002 | 21 | * without specific prior written permission. |
donatien | 0:8e01dca41002 | 22 | * |
donatien | 0:8e01dca41002 | 23 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
donatien | 0:8e01dca41002 | 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
donatien | 0:8e01dca41002 | 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
donatien | 0:8e01dca41002 | 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
donatien | 0:8e01dca41002 | 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
donatien | 0:8e01dca41002 | 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
donatien | 0:8e01dca41002 | 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
donatien | 0:8e01dca41002 | 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
donatien | 0:8e01dca41002 | 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
donatien | 0:8e01dca41002 | 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
donatien | 0:8e01dca41002 | 33 | * SUCH DAMAGE. |
donatien | 0:8e01dca41002 | 34 | * |
donatien | 0:8e01dca41002 | 35 | * This file is built upon the file: src/arch/rtxc/netif/sioslip.c |
donatien | 0:8e01dca41002 | 36 | * |
donatien | 0:8e01dca41002 | 37 | * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com> |
donatien | 0:8e01dca41002 | 38 | */ |
donatien | 0:8e01dca41002 | 39 | |
donatien | 0:8e01dca41002 | 40 | /* |
donatien | 0:8e01dca41002 | 41 | * This is an arch independent SLIP netif. The specific serial hooks must be |
donatien | 0:8e01dca41002 | 42 | * provided by another file. They are sio_open, sio_read/sio_tryread and sio_send |
donatien | 0:8e01dca41002 | 43 | */ |
donatien | 0:8e01dca41002 | 44 | |
donatien | 0:8e01dca41002 | 45 | #include "netif/slipif.h" |
donatien | 0:8e01dca41002 | 46 | #include "lwip/opt.h" |
donatien | 0:8e01dca41002 | 47 | |
donatien | 0:8e01dca41002 | 48 | #if LWIP_HAVE_SLIPIF |
donatien | 0:8e01dca41002 | 49 | |
donatien | 0:8e01dca41002 | 50 | #include "lwip/def.h" |
donatien | 0:8e01dca41002 | 51 | #include "lwip/pbuf.h" |
donatien | 0:8e01dca41002 | 52 | #include "lwip/sys.h" |
donatien | 0:8e01dca41002 | 53 | #include "lwip/stats.h" |
donatien | 0:8e01dca41002 | 54 | #include "lwip/snmp.h" |
donatien | 0:8e01dca41002 | 55 | #include "lwip/sio.h" |
donatien | 0:8e01dca41002 | 56 | |
donatien | 0:8e01dca41002 | 57 | #define SLIP_BLOCK 1 |
donatien | 0:8e01dca41002 | 58 | #define SLIP_DONTBLOCK 0 |
donatien | 0:8e01dca41002 | 59 | |
donatien | 0:8e01dca41002 | 60 | #define SLIP_END 0300 /* 0xC0 */ |
donatien | 0:8e01dca41002 | 61 | #define SLIP_ESC 0333 /* 0xDB */ |
donatien | 0:8e01dca41002 | 62 | #define SLIP_ESC_END 0334 /* 0xDC */ |
donatien | 0:8e01dca41002 | 63 | #define SLIP_ESC_ESC 0335 /* 0xDD */ |
donatien | 0:8e01dca41002 | 64 | |
donatien | 0:8e01dca41002 | 65 | #define SLIP_MAX_SIZE 1500 |
donatien | 0:8e01dca41002 | 66 | |
donatien | 0:8e01dca41002 | 67 | enum slipif_recv_state { |
donatien | 0:8e01dca41002 | 68 | SLIP_RECV_NORMAL, |
donatien | 0:8e01dca41002 | 69 | SLIP_RECV_ESCAPE, |
donatien | 0:8e01dca41002 | 70 | }; |
donatien | 0:8e01dca41002 | 71 | |
donatien | 0:8e01dca41002 | 72 | struct slipif_priv { |
donatien | 0:8e01dca41002 | 73 | sio_fd_t sd; |
donatien | 0:8e01dca41002 | 74 | /* q is the whole pbuf chain for a packet, p is the current pbuf in the chain */ |
donatien | 0:8e01dca41002 | 75 | struct pbuf *p, *q; |
donatien | 0:8e01dca41002 | 76 | enum slipif_recv_state state; |
donatien | 0:8e01dca41002 | 77 | u16_t i, recved; |
donatien | 0:8e01dca41002 | 78 | }; |
donatien | 0:8e01dca41002 | 79 | |
donatien | 0:8e01dca41002 | 80 | /** |
donatien | 0:8e01dca41002 | 81 | * Send a pbuf doing the necessary SLIP encapsulation |
donatien | 0:8e01dca41002 | 82 | * |
donatien | 0:8e01dca41002 | 83 | * Uses the serial layer's sio_send() |
donatien | 0:8e01dca41002 | 84 | * |
donatien | 0:8e01dca41002 | 85 | * @param netif the lwip network interface structure for this slipif |
donatien | 0:8e01dca41002 | 86 | * @param p the pbuf chaing packet to send |
donatien | 0:8e01dca41002 | 87 | * @param ipaddr the ip address to send the packet to (not used for slipif) |
donatien | 0:8e01dca41002 | 88 | * @return always returns ERR_OK since the serial layer does not provide return values |
donatien | 0:8e01dca41002 | 89 | */ |
donatien | 0:8e01dca41002 | 90 | err_t |
donatien | 0:8e01dca41002 | 91 | slipif_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr) |
donatien | 0:8e01dca41002 | 92 | { |
donatien | 0:8e01dca41002 | 93 | struct slipif_priv *priv; |
donatien | 0:8e01dca41002 | 94 | struct pbuf *q; |
donatien | 0:8e01dca41002 | 95 | u16_t i; |
donatien | 0:8e01dca41002 | 96 | u8_t c; |
donatien | 0:8e01dca41002 | 97 | |
donatien | 0:8e01dca41002 | 98 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
donatien | 0:8e01dca41002 | 99 | LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); |
donatien | 0:8e01dca41002 | 100 | LWIP_ASSERT("p != NULL", (p != NULL)); |
donatien | 0:8e01dca41002 | 101 | |
donatien | 0:8e01dca41002 | 102 | LWIP_UNUSED_ARG(ipaddr); |
donatien | 0:8e01dca41002 | 103 | |
donatien | 0:8e01dca41002 | 104 | priv = netif->state; |
donatien | 0:8e01dca41002 | 105 | |
donatien | 0:8e01dca41002 | 106 | /* Send pbuf out on the serial I/O device. */ |
donatien | 0:8e01dca41002 | 107 | sio_send(SLIP_END, priv->sd); |
donatien | 0:8e01dca41002 | 108 | |
donatien | 0:8e01dca41002 | 109 | for (q = p; q != NULL; q = q->next) { |
donatien | 0:8e01dca41002 | 110 | for (i = 0; i < q->len; i++) { |
donatien | 0:8e01dca41002 | 111 | c = ((u8_t *)q->payload)[i]; |
donatien | 0:8e01dca41002 | 112 | switch (c) { |
donatien | 0:8e01dca41002 | 113 | case SLIP_END: |
donatien | 0:8e01dca41002 | 114 | sio_send(SLIP_ESC, priv->sd); |
donatien | 0:8e01dca41002 | 115 | sio_send(SLIP_ESC_END, priv->sd); |
donatien | 0:8e01dca41002 | 116 | break; |
donatien | 0:8e01dca41002 | 117 | case SLIP_ESC: |
donatien | 0:8e01dca41002 | 118 | sio_send(SLIP_ESC, priv->sd); |
donatien | 0:8e01dca41002 | 119 | sio_send(SLIP_ESC_ESC, priv->sd); |
donatien | 0:8e01dca41002 | 120 | break; |
donatien | 0:8e01dca41002 | 121 | default: |
donatien | 0:8e01dca41002 | 122 | sio_send(c, priv->sd); |
donatien | 0:8e01dca41002 | 123 | break; |
donatien | 0:8e01dca41002 | 124 | } |
donatien | 0:8e01dca41002 | 125 | } |
donatien | 0:8e01dca41002 | 126 | } |
donatien | 0:8e01dca41002 | 127 | sio_send(SLIP_END, priv->sd); |
donatien | 0:8e01dca41002 | 128 | return ERR_OK; |
donatien | 0:8e01dca41002 | 129 | } |
donatien | 0:8e01dca41002 | 130 | |
donatien | 0:8e01dca41002 | 131 | /** |
donatien | 0:8e01dca41002 | 132 | * Static function for easy use of blockig or non-blocking |
donatien | 0:8e01dca41002 | 133 | * sio_read |
donatien | 0:8e01dca41002 | 134 | * |
donatien | 0:8e01dca41002 | 135 | * @param fd serial device handle |
donatien | 0:8e01dca41002 | 136 | * @param data pointer to data buffer for receiving |
donatien | 0:8e01dca41002 | 137 | * @param len maximum length (in bytes) of data to receive |
donatien | 0:8e01dca41002 | 138 | * @param block if 1, call sio_read; if 0, call sio_tryread |
donatien | 0:8e01dca41002 | 139 | * @return return value of sio_read of sio_tryread |
donatien | 0:8e01dca41002 | 140 | */ |
donatien | 0:8e01dca41002 | 141 | static u32_t |
donatien | 0:8e01dca41002 | 142 | slip_sio_read(sio_fd_t fd, u8_t* data, u32_t len, u8_t block) |
donatien | 0:8e01dca41002 | 143 | { |
donatien | 0:8e01dca41002 | 144 | if (block) { |
donatien | 0:8e01dca41002 | 145 | return sio_read(fd, data, len); |
donatien | 0:8e01dca41002 | 146 | } else { |
donatien | 0:8e01dca41002 | 147 | return sio_tryread(fd, data, len); |
donatien | 0:8e01dca41002 | 148 | } |
donatien | 0:8e01dca41002 | 149 | } |
donatien | 0:8e01dca41002 | 150 | |
donatien | 0:8e01dca41002 | 151 | /** |
donatien | 0:8e01dca41002 | 152 | * Handle the incoming SLIP stream character by character |
donatien | 0:8e01dca41002 | 153 | * |
donatien | 0:8e01dca41002 | 154 | * Poll the serial layer by calling sio_read() or sio_tryread(). |
donatien | 0:8e01dca41002 | 155 | * |
donatien | 0:8e01dca41002 | 156 | * @param netif the lwip network interface structure for this slipif |
donatien | 0:8e01dca41002 | 157 | * @param block if 1, block until data is received; if 0, return when all data |
donatien | 0:8e01dca41002 | 158 | * from the buffer is received (multiple calls to this function will |
donatien | 0:8e01dca41002 | 159 | * return a complete packet, NULL is returned before - used for polling) |
donatien | 0:8e01dca41002 | 160 | * @return The IP packet when SLIP_END is received |
donatien | 0:8e01dca41002 | 161 | */ |
donatien | 0:8e01dca41002 | 162 | static struct pbuf * |
donatien | 0:8e01dca41002 | 163 | slipif_input(struct netif *netif, u8_t block) |
donatien | 0:8e01dca41002 | 164 | { |
donatien | 0:8e01dca41002 | 165 | struct slipif_priv *priv; |
donatien | 0:8e01dca41002 | 166 | u8_t c; |
donatien | 0:8e01dca41002 | 167 | struct pbuf *t; |
donatien | 0:8e01dca41002 | 168 | |
donatien | 0:8e01dca41002 | 169 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
donatien | 0:8e01dca41002 | 170 | LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); |
donatien | 0:8e01dca41002 | 171 | |
donatien | 0:8e01dca41002 | 172 | priv = netif->state; |
donatien | 0:8e01dca41002 | 173 | |
donatien | 0:8e01dca41002 | 174 | while (slip_sio_read(priv->sd, &c, 1, block) > 0) { |
donatien | 0:8e01dca41002 | 175 | switch (priv->state) { |
donatien | 0:8e01dca41002 | 176 | case SLIP_RECV_NORMAL: |
donatien | 0:8e01dca41002 | 177 | switch (c) { |
donatien | 0:8e01dca41002 | 178 | case SLIP_END: |
donatien | 0:8e01dca41002 | 179 | if (priv->recved > 0) { |
donatien | 0:8e01dca41002 | 180 | /* Received whole packet. */ |
donatien | 0:8e01dca41002 | 181 | /* Trim the pbuf to the size of the received packet. */ |
donatien | 0:8e01dca41002 | 182 | pbuf_realloc(priv->q, priv->recved); |
donatien | 0:8e01dca41002 | 183 | |
donatien | 0:8e01dca41002 | 184 | LINK_STATS_INC(link.recv); |
donatien | 0:8e01dca41002 | 185 | |
donatien | 0:8e01dca41002 | 186 | LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet\n")); |
donatien | 0:8e01dca41002 | 187 | t = priv->q; |
donatien | 0:8e01dca41002 | 188 | priv->p = priv->q = NULL; |
donatien | 0:8e01dca41002 | 189 | priv->i = priv->recved = 0; |
donatien | 0:8e01dca41002 | 190 | return t; |
donatien | 0:8e01dca41002 | 191 | } |
donatien | 0:8e01dca41002 | 192 | continue; |
donatien | 0:8e01dca41002 | 193 | case SLIP_ESC: |
donatien | 0:8e01dca41002 | 194 | priv->state = SLIP_RECV_ESCAPE; |
donatien | 0:8e01dca41002 | 195 | continue; |
donatien | 0:8e01dca41002 | 196 | } |
donatien | 0:8e01dca41002 | 197 | break; |
donatien | 0:8e01dca41002 | 198 | case SLIP_RECV_ESCAPE: |
donatien | 0:8e01dca41002 | 199 | switch (c) { |
donatien | 0:8e01dca41002 | 200 | case SLIP_ESC_END: |
donatien | 0:8e01dca41002 | 201 | c = SLIP_END; |
donatien | 0:8e01dca41002 | 202 | break; |
donatien | 0:8e01dca41002 | 203 | case SLIP_ESC_ESC: |
donatien | 0:8e01dca41002 | 204 | c = SLIP_ESC; |
donatien | 0:8e01dca41002 | 205 | break; |
donatien | 0:8e01dca41002 | 206 | } |
donatien | 0:8e01dca41002 | 207 | priv->state = SLIP_RECV_NORMAL; |
donatien | 0:8e01dca41002 | 208 | /* FALLTHROUGH */ |
donatien | 0:8e01dca41002 | 209 | } |
donatien | 0:8e01dca41002 | 210 | |
donatien | 0:8e01dca41002 | 211 | /* byte received, packet not yet completely received */ |
donatien | 0:8e01dca41002 | 212 | if (priv->p == NULL) { |
donatien | 0:8e01dca41002 | 213 | /* allocate a new pbuf */ |
donatien | 0:8e01dca41002 | 214 | LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n")); |
donatien | 0:8e01dca41002 | 215 | priv->p = pbuf_alloc(PBUF_LINK, (PBUF_POOL_BUFSIZE - PBUF_LINK_HLEN), PBUF_POOL); |
donatien | 0:8e01dca41002 | 216 | |
donatien | 0:8e01dca41002 | 217 | if (priv->p == NULL) { |
donatien | 0:8e01dca41002 | 218 | LINK_STATS_INC(link.drop); |
donatien | 0:8e01dca41002 | 219 | LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n")); |
donatien | 0:8e01dca41002 | 220 | /* don't process any further since we got no pbuf to receive to */ |
donatien | 0:8e01dca41002 | 221 | break; |
donatien | 0:8e01dca41002 | 222 | } |
donatien | 0:8e01dca41002 | 223 | |
donatien | 0:8e01dca41002 | 224 | if (priv->q != NULL) { |
donatien | 0:8e01dca41002 | 225 | /* 'chain' the pbuf to the existing chain */ |
donatien | 0:8e01dca41002 | 226 | pbuf_cat(priv->q, priv->p); |
donatien | 0:8e01dca41002 | 227 | } else { |
donatien | 0:8e01dca41002 | 228 | /* p is the first pbuf in the chain */ |
donatien | 0:8e01dca41002 | 229 | priv->q = priv->p; |
donatien | 0:8e01dca41002 | 230 | } |
donatien | 0:8e01dca41002 | 231 | } |
donatien | 0:8e01dca41002 | 232 | |
donatien | 0:8e01dca41002 | 233 | /* this automatically drops bytes if > SLIP_MAX_SIZE */ |
donatien | 0:8e01dca41002 | 234 | if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) { |
donatien | 0:8e01dca41002 | 235 | ((u8_t *)priv->p->payload)[priv->i] = c; |
donatien | 0:8e01dca41002 | 236 | priv->recved++; |
donatien | 0:8e01dca41002 | 237 | priv->i++; |
donatien | 0:8e01dca41002 | 238 | if (priv->i >= priv->p->len) { |
donatien | 0:8e01dca41002 | 239 | /* on to the next pbuf */ |
donatien | 0:8e01dca41002 | 240 | priv->i = 0; |
donatien | 0:8e01dca41002 | 241 | if (priv->p->next != NULL && priv->p->next->len > 0) { |
donatien | 0:8e01dca41002 | 242 | /* p is a chain, on to the next in the chain */ |
donatien | 0:8e01dca41002 | 243 | priv->p = priv->p->next; |
donatien | 0:8e01dca41002 | 244 | } else { |
donatien | 0:8e01dca41002 | 245 | /* p is a single pbuf, set it to NULL so next time a new |
donatien | 0:8e01dca41002 | 246 | * pbuf is allocated */ |
donatien | 0:8e01dca41002 | 247 | priv->p = NULL; |
donatien | 0:8e01dca41002 | 248 | } |
donatien | 0:8e01dca41002 | 249 | } |
donatien | 0:8e01dca41002 | 250 | } |
donatien | 0:8e01dca41002 | 251 | } |
donatien | 0:8e01dca41002 | 252 | |
donatien | 0:8e01dca41002 | 253 | return NULL; |
donatien | 0:8e01dca41002 | 254 | } |
donatien | 0:8e01dca41002 | 255 | |
donatien | 0:8e01dca41002 | 256 | #if !NO_SYS |
donatien | 0:8e01dca41002 | 257 | /** |
donatien | 0:8e01dca41002 | 258 | * The SLIP input thread. |
donatien | 0:8e01dca41002 | 259 | * |
donatien | 0:8e01dca41002 | 260 | * Feed the IP layer with incoming packets |
donatien | 0:8e01dca41002 | 261 | * |
donatien | 0:8e01dca41002 | 262 | * @param nf the lwip network interface structure for this slipif |
donatien | 0:8e01dca41002 | 263 | */ |
donatien | 0:8e01dca41002 | 264 | static void |
donatien | 0:8e01dca41002 | 265 | slipif_loop_thread(void *nf) |
donatien | 0:8e01dca41002 | 266 | { |
donatien | 0:8e01dca41002 | 267 | struct pbuf *p; |
donatien | 0:8e01dca41002 | 268 | struct netif *netif = (struct netif *)nf; |
donatien | 0:8e01dca41002 | 269 | |
donatien | 0:8e01dca41002 | 270 | while (1) { |
donatien | 0:8e01dca41002 | 271 | p = slipif_input(netif, SLIP_BLOCK); |
donatien | 0:8e01dca41002 | 272 | if (p != NULL) { |
donatien | 0:8e01dca41002 | 273 | if (netif->input(p, netif) != ERR_OK) { |
donatien | 0:8e01dca41002 | 274 | pbuf_free(p); |
donatien | 0:8e01dca41002 | 275 | p = NULL; |
donatien | 0:8e01dca41002 | 276 | } |
donatien | 0:8e01dca41002 | 277 | } |
donatien | 0:8e01dca41002 | 278 | } |
donatien | 0:8e01dca41002 | 279 | } |
donatien | 0:8e01dca41002 | 280 | #endif /* !NO_SYS */ |
donatien | 0:8e01dca41002 | 281 | |
donatien | 0:8e01dca41002 | 282 | /** |
donatien | 0:8e01dca41002 | 283 | * SLIP netif initialization |
donatien | 0:8e01dca41002 | 284 | * |
donatien | 0:8e01dca41002 | 285 | * Call the arch specific sio_open and remember |
donatien | 0:8e01dca41002 | 286 | * the opened device in the state field of the netif. |
donatien | 0:8e01dca41002 | 287 | * |
donatien | 0:8e01dca41002 | 288 | * @param netif the lwip network interface structure for this slipif |
donatien | 0:8e01dca41002 | 289 | * @return ERR_OK if serial line could be opened, |
donatien | 0:8e01dca41002 | 290 | * ERR_MEM if no memory could be allocated, |
donatien | 0:8e01dca41002 | 291 | * ERR_IF is serial line couldn't be opened |
donatien | 0:8e01dca41002 | 292 | * |
donatien | 0:8e01dca41002 | 293 | * @note netif->num must contain the number of the serial port to open |
donatien | 0:8e01dca41002 | 294 | * (0 by default) |
donatien | 0:8e01dca41002 | 295 | */ |
donatien | 0:8e01dca41002 | 296 | err_t |
donatien | 0:8e01dca41002 | 297 | slipif_init(struct netif *netif) |
donatien | 0:8e01dca41002 | 298 | { |
donatien | 0:8e01dca41002 | 299 | struct slipif_priv *priv; |
donatien | 0:8e01dca41002 | 300 | |
donatien | 0:8e01dca41002 | 301 | LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)netif->num)); |
donatien | 0:8e01dca41002 | 302 | |
donatien | 0:8e01dca41002 | 303 | /* Allocate private data */ |
donatien | 0:8e01dca41002 | 304 | priv = mem_malloc(sizeof(struct slipif_priv)); |
donatien | 0:8e01dca41002 | 305 | if (!priv) { |
donatien | 0:8e01dca41002 | 306 | return ERR_MEM; |
donatien | 0:8e01dca41002 | 307 | } |
donatien | 0:8e01dca41002 | 308 | |
donatien | 0:8e01dca41002 | 309 | netif->name[0] = 's'; |
donatien | 0:8e01dca41002 | 310 | netif->name[1] = 'l'; |
donatien | 0:8e01dca41002 | 311 | netif->output = slipif_output; |
donatien | 0:8e01dca41002 | 312 | netif->mtu = SLIP_MAX_SIZE; |
donatien | 0:8e01dca41002 | 313 | netif->flags |= NETIF_FLAG_POINTTOPOINT; |
donatien | 0:8e01dca41002 | 314 | |
donatien | 0:8e01dca41002 | 315 | /* Try to open the serial port (netif->num contains the port number). */ |
donatien | 0:8e01dca41002 | 316 | priv->sd = sio_open(netif->num); |
donatien | 0:8e01dca41002 | 317 | if (!priv->sd) { |
donatien | 0:8e01dca41002 | 318 | /* Opening the serial port failed. */ |
donatien | 0:8e01dca41002 | 319 | mem_free(priv); |
donatien | 0:8e01dca41002 | 320 | return ERR_IF; |
donatien | 0:8e01dca41002 | 321 | } |
donatien | 0:8e01dca41002 | 322 | |
donatien | 0:8e01dca41002 | 323 | /* Initialize private data */ |
donatien | 0:8e01dca41002 | 324 | priv->p = NULL; |
donatien | 0:8e01dca41002 | 325 | priv->q = NULL; |
donatien | 0:8e01dca41002 | 326 | priv->state = SLIP_RECV_NORMAL; |
donatien | 0:8e01dca41002 | 327 | priv->i = 0; |
donatien | 0:8e01dca41002 | 328 | priv->recved = 0; |
donatien | 0:8e01dca41002 | 329 | |
donatien | 0:8e01dca41002 | 330 | netif->state = priv; |
donatien | 0:8e01dca41002 | 331 | |
donatien | 0:8e01dca41002 | 332 | /* initialize the snmp variables and counters inside the struct netif |
donatien | 0:8e01dca41002 | 333 | * ifSpeed: no assumption can be made without knowing more about the |
donatien | 0:8e01dca41002 | 334 | * serial line! |
donatien | 0:8e01dca41002 | 335 | */ |
donatien | 0:8e01dca41002 | 336 | NETIF_INIT_SNMP(netif, snmp_ifType_slip, 0); |
donatien | 0:8e01dca41002 | 337 | |
donatien | 0:8e01dca41002 | 338 | /* Create a thread to poll the serial line. */ |
donatien | 0:8e01dca41002 | 339 | sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif, |
donatien | 0:8e01dca41002 | 340 | SLIPIF_THREAD_STACKSIZE, SLIPIF_THREAD_PRIO); |
donatien | 0:8e01dca41002 | 341 | return ERR_OK; |
donatien | 0:8e01dca41002 | 342 | } |
donatien | 0:8e01dca41002 | 343 | |
donatien | 0:8e01dca41002 | 344 | /** |
donatien | 0:8e01dca41002 | 345 | * Polls the serial device and feeds the IP layer with incoming packets. |
donatien | 0:8e01dca41002 | 346 | * |
donatien | 0:8e01dca41002 | 347 | * @param netif The lwip network interface structure for this slipif |
donatien | 0:8e01dca41002 | 348 | */ |
donatien | 0:8e01dca41002 | 349 | void |
donatien | 0:8e01dca41002 | 350 | slipif_poll(struct netif *netif) |
donatien | 0:8e01dca41002 | 351 | { |
donatien | 0:8e01dca41002 | 352 | struct pbuf *p; |
donatien | 0:8e01dca41002 | 353 | struct slipif_priv *priv; |
donatien | 0:8e01dca41002 | 354 | |
donatien | 0:8e01dca41002 | 355 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
donatien | 0:8e01dca41002 | 356 | LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); |
donatien | 0:8e01dca41002 | 357 | |
donatien | 0:8e01dca41002 | 358 | priv = netif->state; |
donatien | 0:8e01dca41002 | 359 | |
donatien | 0:8e01dca41002 | 360 | while ((p = slipif_input(netif, SLIP_DONTBLOCK)) != NULL) { |
donatien | 0:8e01dca41002 | 361 | if (netif->input(p, netif) != ERR_OK) { |
donatien | 0:8e01dca41002 | 362 | pbuf_free(p); |
donatien | 0:8e01dca41002 | 363 | } |
donatien | 0:8e01dca41002 | 364 | } |
donatien | 0:8e01dca41002 | 365 | } |
donatien | 0:8e01dca41002 | 366 | |
donatien | 0:8e01dca41002 | 367 | #endif /* LWIP_HAVE_SLIPIF */ |