SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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