root/webserver/example/freeRTOSexample/EnergyMeters/Common/ethernet/lwIP/netif/slipif.c

Revision 14, 5.6 kB (checked in by phil, 15 years ago)

added unmodified FreeRTOS package V5.4.1 with only web srv demo source for LPC2368 for CrossWorks?

Line 
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * This file is built upon the file: src/arch/rtxc/netif/sioslip.c
30  *
31  * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com>
32  */
33
34 /*
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
37  */
38
39 #include "netif/slipif.h"
40 #include "lwip/opt.h"
41 #include "lwip/def.h"
42 #include "lwip/pbuf.h"
43 #include "lwip/sys.h"
44 #include "lwip/stats.h"
45 #include "lwip/sio.h"
46
47 #define SLIP_END     0300
48 #define SLIP_ESC     0333
49 #define SLIP_ESC_END 0334
50 #define SLIP_ESC_ESC 0335
51
52 #define MAX_SIZE     1500
53
54 /**
55  * Send a pbuf doing the necessary SLIP encapsulation
56  *
57  * Uses the serial layer's sio_send()
58  */
59 err_t
60 slipif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)
61 {
62   struct pbuf *q;
63   u16_t i;
64   u8_t c;
65
66   /* Send pbuf out on the serial I/O device. */
67   sio_send(SLIP_END, netif->state);
68
69   for (q = p; q != NULL; q = q->next) {
70     for (i = 0; i < q->len; i++) {
71       c = ((u8_t *)q->payload)[i];
72       switch (c) {
73       case SLIP_END:
74         sio_send(SLIP_ESC, netif->state);
75         sio_send(SLIP_ESC_END, netif->state);
76         break;
77       case SLIP_ESC:
78         sio_send(SLIP_ESC, netif->state);
79         sio_send(SLIP_ESC_ESC, netif->state);
80         break;
81       default:
82         sio_send(c, netif->state);
83         break;
84       }
85     }
86   }
87   sio_send(SLIP_END, netif->state);
88   return 0;
89 }
90
91 /**
92  * Handle the incoming SLIP stream character by character
93  *
94  * Poll the serial layer by calling sio_recv()
95  *
96  * @return The IP packet when SLIP_END is received
97  */
98 static struct pbuf *
99 slipif_input(struct netif *netif)
100 {
101   u8_t c;
102   struct pbuf *p, *q;
103   u16_t recved;
104   u16_t i;
105
106   q = p = NULL;
107   recved = i = 0;
108   c = 0;
109
110   while (1) {
111     c = sio_recv(netif->state);
112     switch (c) {
113     case SLIP_END:
114       if (recved > 0) {
115         /* Received whole packet. */
116         pbuf_realloc(q, recved);
117        
118         LINK_STATS_INC(link.recv);
119        
120         LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet\n"));
121         return q;
122       }
123       break;
124
125     case SLIP_ESC:
126       c = sio_recv(netif->state);
127       switch (c) {
128       case SLIP_ESC_END:
129         c = SLIP_END;
130         break;
131       case SLIP_ESC_ESC:
132         c = SLIP_ESC;
133         break;
134       }
135       /* FALLTHROUGH */
136
137     default:
138       if (p == NULL) {
139         LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n"));
140         p = pbuf_alloc(PBUF_LINK, PBUF_POOL_BUFSIZE, PBUF_POOL);
141
142         if (p == NULL) {
143           LINK_STATS_INC(link.drop);
144           LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n"));
145         }
146
147         if (q != NULL) {
148           pbuf_cat(q, p);
149         } else {
150           q = p;
151         }
152       }
153       if (p != NULL && recved < MAX_SIZE) {
154         ((u8_t *)p->payload)[i] = c;
155         recved++;
156         i++;
157         if (i >= p->len) {
158           i = 0;
159           if (p->next != NULL && p->next->len > 0)
160             p = p->next;
161           else
162             p = NULL;
163         }
164       }
165       break;
166     }
167
168   }
169   return NULL;
170 }
171
172 /**
173  * The SLIP input thread.
174  *
175  * Feed the IP layer with incoming packets
176  */
177 static void
178 slipif_loop(void *nf)
179 {
180   struct pbuf *p;
181   struct netif *netif = (struct netif *)nf;
182
183   while (1) {
184     p = slipif_input(netif);
185     netif->input(p, netif);
186   }
187 }
188
189 /**
190  * SLIP netif initialization
191  *
192  * Call the arch specific sio_open and remember
193  * the opened device in the state field of the netif.
194  */
195 err_t
196 slipif_init(struct netif *netif)
197 {
198
199   LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)netif->num));
200
201   netif->name[0] = 's';
202   netif->name[1] = 'l';
203   netif->output = slipif_output;
204   netif->mtu = 1500;
205   netif->flags = NETIF_FLAG_POINTTOPOINT;
206
207   netif->state = sio_open(netif->num);
208   if (!netif->state)
209     return ERR_IF;
210
211   sys_thread_new(slipif_loop, netif, SLIPIF_THREAD_PRIO);
212   return ERR_OK;
213 }
Note: See TracBrowser for help on using the browser.