3328 |
povik |
1 |
/* |
|
|
2 |
* Copyright (c) 2001-2003, Adam Dunkels. |
|
|
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. The name of the author may not be used to endorse or promote |
|
|
14 |
* products derived from this software without specific prior |
|
|
15 |
* written permission. |
|
|
16 |
* |
|
|
17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
|
|
18 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
|
19 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
|
20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|
|
21 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
|
22 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
|
|
23 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
|
24 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
|
25 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
|
26 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
|
27 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
28 |
* |
|
|
29 |
* This file is part of the uIP TCP/IP stack. |
|
|
30 |
* |
|
|
31 |
* $Id: main.c,v 1.10.2.1 2003/10/04 22:54:17 adam Exp $ |
|
|
32 |
* |
|
|
33 |
*/ |
|
|
34 |
|
|
|
35 |
|
|
|
36 |
#include <stdio.h> |
|
|
37 |
|
|
|
38 |
#include "uip.h" |
|
|
39 |
#include "uip_arp.h" |
|
|
40 |
#include "httpd.h" |
|
|
41 |
#include "stm32_eth.h" |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
// Define NULL |
|
|
45 |
#ifndef NULL |
|
|
46 |
#define NULL (void *)0 |
|
|
47 |
#endif |
|
|
48 |
|
|
|
49 |
// Globals |
|
|
50 |
extern char gPacketReceived; |
|
|
51 |
|
|
|
52 |
/* The start of the uIP buffer, which will contain the frame headers. */ |
|
|
53 |
#define pucUIP_Buffer ( ( struct uip_eth_hdr * ) &uip_buf[ 0 ] ) |
|
|
54 |
|
|
|
55 |
/* uIP update frequencies. */ |
|
|
56 |
#define RT_CLOCK_SECOND ( configTICK_RATE_HZ ) |
|
|
57 |
#define uipARP_FREQUENCY ( 20 ) |
|
|
58 |
#define uipMAX_BLOCK_TIME ( RT_CLOCK_SECOND / 4 ) |
|
|
59 |
|
|
|
60 |
// Define Prototypes |
|
|
61 |
void TransmitPacket(void); |
|
|
62 |
|
|
|
63 |
/*-----------------------------------------------------------------------------------*/ |
|
|
64 |
void uIPMain(void) |
|
|
65 |
{ |
|
|
66 |
u8_t i, arptimer; |
|
|
67 |
uip_eth_hdr *BUF = (uip_eth_hdr*)uip_buf; |
|
|
68 |
|
|
|
69 |
u32 size; |
|
|
70 |
/* Initialize the uIP TCP/IP stack. */ |
|
|
71 |
uip_init(); |
|
|
72 |
|
|
|
73 |
/* Initialize the HTTP server. */ |
|
|
74 |
httpd_init(); |
|
|
75 |
|
|
|
76 |
arptimer = 0; |
|
|
77 |
|
|
|
78 |
while(1) |
|
|
79 |
{ |
|
|
80 |
/* Let the tapdev network device driver read an entire IP packet |
|
|
81 |
into the uip_buf. If it must wait for more than 0.5 seconds, it |
|
|
82 |
will return with the return value 0. If so, we know that it is |
|
|
83 |
time to call upon the uip_periodic(). Otherwise, the tapdev has |
|
|
84 |
received an IP packet that is to be processed by uIP. */ |
|
|
85 |
|
|
|
86 |
size = ETH_HandleRxPkt(uip_buf); |
|
|
87 |
|
|
|
88 |
if (size > 0) { |
|
|
89 |
//printf("Packet! len: %x\r\n", size); |
|
|
90 |
uip_len = size; |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
if(uip_len <= 0x0) |
|
|
94 |
{ |
|
|
95 |
for(i = 0; i < UIP_CONNS; i++) |
|
|
96 |
{ |
|
|
97 |
uip_periodic(i); |
|
|
98 |
|
|
|
99 |
/* If the above function invocation resulted in data that |
|
|
100 |
should be sent out on the network, the global variable |
|
|
101 |
uip_len is set to a value > 0. */ |
|
|
102 |
|
|
|
103 |
if(uip_len > 0) |
|
|
104 |
{ |
|
|
105 |
uip_arp_out(); |
|
|
106 |
//printf("Reply! len: %x\r\n", uip_len); |
|
|
107 |
TransmitPacket(); |
|
|
108 |
} |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
#if UIP_UDP |
|
|
112 |
for(i = 0; i < UIP_UDP_CONNS; i++) |
|
|
113 |
{ |
|
|
114 |
uip_udp_periodic(i); |
|
|
115 |
/* If the above function invocation resulted in data that |
|
|
116 |
should be sent out on the network, the global variable |
|
|
117 |
uip_len is set to a value > 0. */ |
|
|
118 |
if(uip_len > 0) |
|
|
119 |
{ |
|
|
120 |
uip_arp_out(); |
|
|
121 |
//printf("Reply! len: %x\r\n", uip_len); |
|
|
122 |
TransmitPacket(); |
|
|
123 |
} |
|
|
124 |
} |
|
|
125 |
#endif /* UIP_UDP */ |
|
|
126 |
|
|
|
127 |
/* Call the ARP timer function every 10 seconds. */ |
|
|
128 |
if(++arptimer == 20) |
|
|
129 |
{ |
|
|
130 |
uip_arp_timer(); |
|
|
131 |
arptimer = 0; |
|
|
132 |
} |
|
|
133 |
} |
|
|
134 |
|
|
|
135 |
else |
|
|
136 |
{ |
|
|
137 |
if(BUF->type == htons(UIP_ETHTYPE_IP)) |
|
|
138 |
{ |
|
|
139 |
uip_arp_ipin(); |
|
|
140 |
uip_input(); |
|
|
141 |
|
|
|
142 |
/* If the above function invocation resulted in data that |
|
|
143 |
should be sent out on the network, the global variable |
|
|
144 |
uip_len is set to a value > 0. */ |
|
|
145 |
if(uip_len > 0) |
|
|
146 |
{ |
|
|
147 |
uip_arp_out(); |
|
|
148 |
//printf("Reply! len: %x\r\n", uip_len); |
|
|
149 |
TransmitPacket(); |
|
|
150 |
} |
|
|
151 |
} |
|
|
152 |
else if(BUF->type == htons(UIP_ETHTYPE_ARP)) |
|
|
153 |
{ |
|
|
154 |
uip_arp_arpin(); |
|
|
155 |
|
|
|
156 |
/* If the above function invocation resulted in data that |
|
|
157 |
should be sent out on the network, the global variable |
|
|
158 |
uip_len is set to a value > 0. */ |
|
|
159 |
if(uip_len > 0) |
|
|
160 |
{ |
|
|
161 |
//printf("Reply! len: %x\r\n", uip_len); |
|
|
162 |
TransmitPacket(); |
|
|
163 |
} |
|
|
164 |
} |
|
|
165 |
} |
|
|
166 |
} |
|
|
167 |
} |
|
|
168 |
|
|
|
169 |
/*-----------------------------------------------------------------------------------*/ |
|
|
170 |
|
|
|
171 |
void TransmitPacket(void) |
|
|
172 |
{ |
|
|
173 |
int i; |
|
|
174 |
u8 data[1500]; |
|
|
175 |
|
|
|
176 |
// Copy the header portion part |
|
|
177 |
for(i=0; i < (UIP_LLH_LEN + 40); ++i) { |
|
|
178 |
data[i] = uip_buf[i]; |
|
|
179 |
} |
|
|
180 |
|
|
|
181 |
// Copy the data portion part |
|
|
182 |
for(; i < uip_len; ++i) { |
|
|
183 |
data[i] = uip_appdata[i - UIP_LLH_LEN - 40 ]; |
|
|
184 |
} |
|
|
185 |
|
|
|
186 |
ETH_HandleTxPkt(data,uip_len); |
|
|
187 |
} |
|
|
188 |
|
|
|
189 |
/*-----------------------------------------------------------------------------------*/ |
|
|
190 |
void uip_log(char *m) |
|
|
191 |
{ |
|
|
192 |
//printf("uIP log message: %s\n", m); |
|
|
193 |
} |
|
|
194 |
|
|
|
195 |
void udp_appcall(void) |
|
|
196 |
{ |
|
|
197 |
} |
|
|
198 |
|
|
|
199 |
|
|
|
200 |
/*-----------------------------------------------------------------------------------*/ |