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