2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file is built upon the file: src/arch/rtxc/netif/sioslip.c
31 * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com>
35 * This is an arch independent SLIP netif. The specific serial hooks must be
36 * provided by another file. They are sio_open, sio_recv and sio_send
39 #include "netif/slipif.h"
42 #include "lwip/pbuf.h"
44 #include "lwip/stats.h"
49 #define SLIP_ESC_END 0334
50 #define SLIP_ESC_ESC 0335
55 * Send a pbuf doing the necessary SLIP encapsulation
57 * Uses the serial layer's sio_send()
60 slipif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)
66 /* Send pbuf out on the serial I/O device. */
67 sio_send(SLIP_END, netif->state);
69 for (q = p; q != NULL; q = q->next) {
70 for (i = 0; i < q->len; i++) {
71 c = ((u8_t *)q->payload)[i];
74 sio_send(SLIP_ESC, netif->state);
75 sio_send(SLIP_ESC_END, netif->state);
78 sio_send(SLIP_ESC, netif->state);
79 sio_send(SLIP_ESC_ESC, netif->state);
82 sio_send(c, netif->state);
87 sio_send(SLIP_END, netif->state);
92 * Handle the incoming SLIP stream character by character
94 * Poll the serial layer by calling sio_recv()
96 * @return The IP packet when SLIP_END is received
99 slipif_input(struct netif *netif)
111 c = sio_recv(netif->state);
115 /* Received whole packet. */
116 pbuf_realloc(q, recved);
118 LINK_STATS_INC(link.recv);
120 LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet\n"));
126 c = sio_recv(netif->state);
139 LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n"));
140 p = pbuf_alloc(PBUF_LINK, PBUF_POOL_BUFSIZE, PBUF_POOL);
143 LINK_STATS_INC(link.drop);
144 LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n"));
153 if (p != NULL && recved < MAX_SIZE) {
154 ((u8_t *)p->payload)[i] = c;
159 if (p->next != NULL && p->next->len > 0)
173 * The SLIP input thread.
175 * Feed the IP layer with incoming packets
178 slipif_loop(void *nf)
181 struct netif *netif = (struct netif *)nf;
184 p = slipif_input(netif);
185 netif->input(p, netif);
190 * SLIP netif initialization
192 * Call the arch specific sio_open and remember
193 * the opened device in the state field of the netif.
196 slipif_init(struct netif *netif)
199 LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)netif->num));
201 netif->name[0] = 's';
202 netif->name[1] = 'l';
203 netif->output = slipif_output;
205 netif->flags = NETIF_FLAG_POINTTOPOINT;
207 netif->state = sio_open(netif->num);
211 sys_thread_new(slipif_loop, netif, SLIPIF_THREAD_PRIO);