Brandon Fictorie / Mbed 2 deprecated BF_Websocket

Dependencies:   mbed

Committer:
bfictorie
Date:
Sun Mar 25 17:26:30 2012 +0000
Revision:
0:8cdad1c73e8e

        

Who changed what in which revision?

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