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