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