]> git.sur5r.net Git - freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/lwip-1.1.0/src/netif/slipif.c
Update to V4.3.0 as described in http://www.FreeRTOS.org/History.txt
[freertos] / Demo / lwIP_Demo_Rowley_ARM7 / lwip-1.1.0 / src / netif / slipif.c
1 /*\r
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.\r
3  * All rights reserved. \r
4  *\r
5  * Redistribution and use in source and binary forms, with or without \r
6  * modification, are permitted provided that the following conditions \r
7  * are met: \r
8  * 1. Redistributions of source code must retain the above copyright \r
9  *    notice, this list of conditions and the following disclaimer. \r
10  * 2. Redistributions in binary form must reproduce the above copyright \r
11  *    notice, this list of conditions and the following disclaimer in the \r
12  *    documentation and/or other materials provided with the distribution. \r
13  * 3. Neither the name of the Institute nor the names of its contributors \r
14  *    may be used to endorse or promote products derived from this software \r
15  *    without specific prior written permission. \r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND \r
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE \r
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE \r
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE \r
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL \r
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS \r
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) \r
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT \r
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY \r
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF \r
27  * SUCH DAMAGE. \r
28  *\r
29  * This file is built upon the file: src/arch/rtxc/netif/sioslip.c\r
30  *\r
31  * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com> \r
32  */\r
33 \r
34 /* \r
35  * This is an arch independent SLIP netif. The specific serial hooks must be provided \r
36  * by another file.They are sio_open, sio_recv and sio_send\r
37  */ \r
38 \r
39 #include "netif/slipif.h"\r
40 #include "lwip/opt.h"\r
41 #include "lwip/def.h"\r
42 #include "lwip/pbuf.h"\r
43 #include "lwip/sys.h"\r
44 #include "lwip/stats.h"\r
45 #include "lwip/sio.h"\r
46 \r
47 #define SLIP_END     0300\r
48 #define SLIP_ESC     0333\r
49 #define SLIP_ESC_END 0334\r
50 #define SLIP_ESC_ESC 0335\r
51 \r
52 #define MAX_SIZE     1500\r
53 \r
54 /**\r
55  * Send a pbuf doing the necessary SLIP encapsulation\r
56  *\r
57  * Uses the serial layer's sio_send() \r
58  */ \r
59 err_t\r
60 slipif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)\r
61 {\r
62   struct pbuf *q;\r
63   int i;\r
64   u8_t c;\r
65 \r
66   /* Send pbuf out on the serial I/O device. */\r
67   sio_send(SLIP_END, netif->state);\r
68 \r
69   for(q = p; q != NULL; q = q->next) {\r
70     for(i = 0; i < q->len; i++) {\r
71       c = ((u8_t *)q->payload)[i];\r
72       switch (c) {\r
73       case SLIP_END:\r
74   sio_send(SLIP_ESC, netif->state);\r
75   sio_send(SLIP_ESC_END, netif->state);\r
76   break;\r
77       case SLIP_ESC:\r
78   sio_send(SLIP_ESC, netif->state);\r
79   sio_send(SLIP_ESC_ESC, netif->state);\r
80   break;\r
81       default:\r
82   sio_send(c, netif->state);\r
83   break;\r
84       }\r
85     }\r
86   }\r
87   sio_send(SLIP_END, netif->state);\r
88   return 0;\r
89 }\r
90 \r
91 /**\r
92  * Handle the incoming SLIP stream character by character\r
93  *\r
94  * Poll the serial layer by calling sio_recv()\r
95  * \r
96  * @return The IP packet when SLIP_END is received \r
97  */ \r
98 static struct pbuf *\r
99 slipif_input( struct netif * netif )\r
100 {\r
101   u8_t c;\r
102   struct pbuf *p, *q;\r
103   int recved;\r
104   int i;\r
105 \r
106   q = p = NULL;\r
107   recved = i = 0;\r
108   c = 0;\r
109 \r
110   while (1) {\r
111     c = sio_recv(netif->state);\r
112     switch (c) {\r
113     case SLIP_END:\r
114       if (recved > 0) {\r
115   /* Received whole packet. */\r
116   pbuf_realloc(q, recved);\r
117   \r
118   LINK_STATS_INC(link.recv);\r
119   \r
120   LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet\n"));\r
121   return q;\r
122       }\r
123       break;\r
124 \r
125     case SLIP_ESC:\r
126       c = sio_recv(netif->state);\r
127       switch (c) {\r
128       case SLIP_ESC_END:\r
129   c = SLIP_END;\r
130   break;\r
131       case SLIP_ESC_ESC:\r
132   c = SLIP_ESC;\r
133   break;\r
134       }\r
135       /* FALLTHROUGH */\r
136       \r
137     default:\r
138       if (p == NULL) {\r
139   LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n"));\r
140   p = pbuf_alloc(PBUF_LINK, PBUF_POOL_BUFSIZE, PBUF_POOL);\r
141 \r
142   if (p == NULL) {\r
143     LINK_STATS_INC(link.drop);\r
144     LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n"));\r
145   }\r
146   \r
147   if (q != NULL) {\r
148     pbuf_cat(q, p);\r
149   } else {\r
150     q = p;\r
151   }\r
152       }\r
153       if (p != NULL && recved < MAX_SIZE) {\r
154   ((u8_t *)p->payload)[i] = c;\r
155   recved++;\r
156   i++;\r
157   if (i >= p->len) {\r
158     i = 0;\r
159     p = NULL;\r
160   }\r
161       }\r
162       break;\r
163     }\r
164     \r
165   }\r
166   return NULL;\r
167 }\r
168 \r
169 /**\r
170  * The SLIP input thread \r
171  *\r
172  * Feed the IP layer with incoming packets\r
173  */ \r
174 static void\r
175 slipif_loop(void *nf)\r
176 {\r
177   struct pbuf *p;\r
178   struct netif *netif = (struct netif *)nf;\r
179 \r
180   while (1) {\r
181     p = slipif_input(netif);\r
182     netif->input(p, netif);\r
183   }\r
184 }\r
185 \r
186 /**\r
187  * SLIP netif initialization\r
188  *\r
189  * Call the arch specific sio_open and remember\r
190  * the opened device in the state field of the netif.\r
191  */ \r
192 err_t\r
193 slipif_init(struct netif *netif)\r
194 {\r
195   \r
196   LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%x\n", (int)netif->num));\r
197 \r
198   netif->name[0] = 's';\r
199   netif->name[1] = 'l';\r
200   netif->output = slipif_output;\r
201   netif->mtu = 1500;  \r
202   netif->flags = NETIF_FLAG_POINTTOPOINT;\r
203 \r
204   netif->state = sio_open(netif->num);\r
205   if (!netif->state)\r
206       return ERR_IF;\r
207 \r
208   sys_thread_new(slipif_loop, netif, SLIPIF_THREAD_PRIO);\r
209   return ERR_OK;\r
210 }\r