Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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