Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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