Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

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