Small Testprogram to have WebAccess via Webserver to a 433Mhz tranmitter to control remotly some devices from remote, with TFTP, NTP and RMF. This could be a base to develop applications.

Dependencies:   ChaNFSSD TFTPServer RMFWeb

Dependents:   RMFWeb

Committer:
ED7418
Date:
Mon Jun 16 07:40:08 2014 +0000
Revision:
1:809b59c7a800
Parent:
0:51f1ef89ec7b
mbed-lib and other libs are a based on a project, published in a Elektor-book "ARM-microkontroller Part II".

Who changed what in which revision?

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