HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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