Committer:
mbed714
Date:
Tue Nov 09 19:32:44 2010 +0000
Revision:
3:0c324737064d
Parent:
0:98aadd37a5c5

        

Who changed what in which revision?

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