]> git.sur5r.net Git - freertos/blob - Demo/uIP_Demo_Rowley_ARM7/uip/slipdev.c
Renamed the CORTEX_M4_ATSAM4S_AVR_Studio directory to the correct CORTEX_M4_ATSAM4S_A...
[freertos] / Demo / uIP_Demo_Rowley_ARM7 / uip / slipdev.c
1 /**\r
2  * \addtogroup uip\r
3  * @{\r
4  */\r
5 \r
6 /**\r
7  * \defgroup slip Serial Line IP (SLIP) protocol\r
8  * @{\r
9  *\r
10  * The SLIP protocol is a very simple way to transmit IP packets over\r
11  * a serial line. It does not provide any framing or error control,\r
12  * and is therefore not very widely used today.\r
13  *\r
14  * This SLIP implementation requires two functions for accessing the\r
15  * serial device: slipdev_char_poll() and slipdev_char_put(). These\r
16  * must be implemented specifically for the system on which the SLIP\r
17  * protocol is to be run.\r
18  */\r
19 \r
20 /**\r
21  * \file\r
22  * SLIP protocol implementation\r
23  * \author Adam Dunkels <adam@dunkels.com>\r
24  */\r
25 \r
26 /*\r
27  * Copyright (c) 2001, Adam Dunkels.\r
28  * All rights reserved. \r
29  *\r
30  * Redistribution and use in source and binary forms, with or without \r
31  * modification, are permitted provided that the following conditions \r
32  * are met: \r
33  * 1. Redistributions of source code must retain the above copyright \r
34  *    notice, this list of conditions and the following disclaimer. \r
35  * 2. Redistributions in binary form must reproduce the above copyright \r
36  *    notice, this list of conditions and the following disclaimer in the \r
37  *    documentation and/or other materials provided with the distribution. \r
38  * 3. The name of the author may not be used to endorse or promote\r
39  *    products derived from this software without specific prior\r
40  *    written permission.  \r
41  *\r
42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS\r
43  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
44  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\r
46  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\r
48  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
49  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
50  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
51  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
52  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  \r
53  *\r
54  * This file is part of the uIP TCP/IP stack.\r
55  *\r
56  * $Id: slipdev.c,v 1.1.2.3 2003/10/07 13:23:01 adam Exp $\r
57  *\r
58  */\r
59 \r
60 /*\r
61  * This is a generic implementation of the SLIP protocol over an RS232\r
62  * (serial) device. \r
63  *\r
64  * Huge thanks to Ullrich von Bassewitz <uz@cc65.org> of cc65 fame for\r
65  * and endless supply of bugfixes, insightsful comments and\r
66  * suggestions, and improvements to this code!\r
67  */\r
68 \r
69 #include "uip.h"\r
70 \r
71 #define SLIP_END     0300\r
72 #define SLIP_ESC     0333\r
73 #define SLIP_ESC_END 0334\r
74 #define SLIP_ESC_ESC 0335\r
75 \r
76 static u8_t slip_buf[UIP_BUFSIZE];\r
77 \r
78 static u16_t len, tmplen;\r
79 static u8_t lastc;\r
80 \r
81 /*-----------------------------------------------------------------------------------*/\r
82 /**\r
83  * Send the packet in the uip_buf and uip_appdata buffers using the\r
84  * SLIP protocol.\r
85  *\r
86  * The first 40 bytes of the packet (the IP and TCP headers) are read\r
87  * from the uip_buf buffer, and the following bytes (the application\r
88  * data) are read from the uip_appdata buffer.\r
89  *\r
90  */\r
91 /*-----------------------------------------------------------------------------------*/\r
92 void\r
93 slipdev_send(void)\r
94 {\r
95   u16_t i;\r
96   u8_t *ptr;\r
97   u8_t c;\r
98 \r
99   slipdev_char_put(SLIP_END);\r
100 \r
101   ptr = uip_buf;\r
102   for(i = 0; i < uip_len; ++i) {\r
103     if(i == 40) {\r
104       ptr = (u8_t *)uip_appdata;\r
105     }\r
106     c = *ptr++;\r
107     switch(c) {\r
108     case SLIP_END:\r
109       slipdev_char_put(SLIP_ESC);\r
110       slipdev_char_put(SLIP_ESC_END);\r
111       break;\r
112     case SLIP_ESC:\r
113       slipdev_char_put(SLIP_ESC);\r
114       slipdev_char_put(SLIP_ESC_ESC);\r
115       break;\r
116     default:\r
117       slipdev_char_put(c);\r
118       break;\r
119     }\r
120   }\r
121   slipdev_char_put(SLIP_END);\r
122 }\r
123 /*-----------------------------------------------------------------------------------*/\r
124 /** \r
125  * Poll the SLIP device for an available packet.\r
126  *\r
127  * This function will poll the SLIP device to see if a packet is\r
128  * available. It uses a buffer in which all avaliable bytes from the\r
129  * RS232 interface are read into. When a full packet has been read\r
130  * into the buffer, the packet is copied into the uip_buf buffer and\r
131  * the length of the packet is returned.\r
132  *\r
133  * \return The length of the packet placed in the uip_buf buffer, or\r
134  * zero if no packet is available.\r
135  */\r
136 /*-----------------------------------------------------------------------------------*/\r
137 u16_t\r
138 slipdev_poll(void)\r
139 {\r
140   u8_t c;\r
141   \r
142   while(slipdev_char_poll(c)) {\r
143     switch(c) {\r
144     case SLIP_ESC:\r
145       lastc = c;\r
146       break;\r
147       \r
148     case SLIP_END:\r
149       lastc = c;\r
150       /* End marker found, we copy our input buffer to the uip_buf\r
151          buffer and return the size of the packet we copied. */\r
152       memcpy(uip_buf, slip_buf, len);\r
153       tmplen = len;\r
154       len = 0;\r
155       return tmplen;\r
156       \r
157     default:     \r
158       if(lastc == SLIP_ESC) {\r
159         lastc = c;\r
160         /* Previous read byte was an escape byte, so this byte will be\r
161            interpreted differently from others. */\r
162         switch(c) {\r
163         case SLIP_ESC_END:\r
164           c = SLIP_END;\r
165           break;\r
166         case SLIP_ESC_ESC:\r
167           c = SLIP_ESC;\r
168           break;\r
169         }\r
170       } else {\r
171         lastc = c;\r
172       }\r
173       \r
174       slip_buf[len] = c;\r
175       ++len;\r
176       \r
177       if(len > UIP_BUFSIZE) {\r
178         len = 0;\r
179       }\r
180     \r
181       break;\r
182     }\r
183   }\r
184   return 0;\r
185 }\r
186 /*-----------------------------------------------------------------------------------*/\r
187 /**\r
188  * Initialize the SLIP module.\r
189  *\r
190  * This function does not initialize the underlying RS232 device, but\r
191  * only the SLIP part.\r
192  */ \r
193 /*-----------------------------------------------------------------------------------*/\r
194 void\r
195 slipdev_init(void)\r
196 {\r
197   lastc = len = 0;\r
198 }\r
199 /*-----------------------------------------------------------------------------------*/\r
200 \r
201 /** @} */\r
202 /** @} */\r