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