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