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