1 |
hedin |
15 |
/*********************************************************************
|
2 |
|
|
*
|
3 |
|
|
* TCP/IP Stack Manager
|
4 |
|
|
* Module for Microchip TCP/IP Stack
|
5 |
|
|
* -Handles internal RX packet pre-processing prior to dispatching
|
6 |
|
|
* to upper application layers.
|
7 |
|
|
* -Reference: AN833
|
8 |
|
|
*
|
9 |
|
|
*********************************************************************
|
10 |
|
|
* FileName: StackTsk.c
|
11 |
|
|
* Dependencies: StackTsk.H
|
12 |
|
|
* ARP.h
|
13 |
|
|
* MAC.h
|
14 |
|
|
* IP.h
|
15 |
|
|
* ICMP.h
|
16 |
|
|
* Tcp.h
|
17 |
|
|
* http.h
|
18 |
|
|
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F
|
19 |
|
|
* Complier: Microchip C18 v3.02 or higher
|
20 |
|
|
* Microchip C30 v2.01 or higher
|
21 |
|
|
* Company: Microchip Technology, Inc.
|
22 |
|
|
*
|
23 |
|
|
* Software License Agreement
|
24 |
|
|
*
|
25 |
|
|
* Copyright © 2002-2007 Microchip Technology Inc. All rights
|
26 |
|
|
* reserved.
|
27 |
|
|
*
|
28 |
|
|
* Microchip licenses to you the right to use, modify, copy, and
|
29 |
|
|
* distribute:
|
30 |
|
|
* (i) the Software when embedded on a Microchip microcontroller or
|
31 |
|
|
* digital signal controller product (“Device”) which is
|
32 |
|
|
* integrated into Licensee’s product; or
|
33 |
|
|
* (ii) ONLY the Software driver source files ENC28J60.c and
|
34 |
|
|
* ENC28J60.h ported to a non-Microchip device used in
|
35 |
|
|
* conjunction with a Microchip ethernet controller for the
|
36 |
|
|
* sole purpose of interfacing with the ethernet controller.
|
37 |
|
|
*
|
38 |
|
|
* You should refer to the license agreement accompanying this
|
39 |
|
|
* Software for additional information regarding your rights and
|
40 |
|
|
* obligations.
|
41 |
|
|
*
|
42 |
|
|
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT
|
43 |
|
|
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
|
44 |
|
|
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
|
45 |
|
|
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
46 |
|
|
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
|
47 |
|
|
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
|
48 |
|
|
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
|
49 |
|
|
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
|
50 |
|
|
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
|
51 |
|
|
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
|
52 |
|
|
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
|
53 |
|
|
*
|
54 |
|
|
*
|
55 |
|
|
* Author Date Comment
|
56 |
|
|
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
57 |
|
|
* Nilesh Rajbharti 8/14/01 Original (Rev. 1.0)
|
58 |
|
|
* Nilesh Rajbharti 2/9/02 Cleanup
|
59 |
|
|
* Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail)
|
60 |
|
|
* Nilesh Rajbharti 12/5/02 Modified UDPProcess() and TCPProcess()
|
61 |
|
|
* to include localIP as third param.
|
62 |
|
|
* This was done to allow these functions
|
63 |
|
|
* to calculate checksum correctly.
|
64 |
|
|
* Nilesh Rajbharti 7/26/04 Added code in StackTask() to not
|
65 |
|
|
* clear statically IP address if link is
|
66 |
|
|
* removed and DHCP module is disabled
|
67 |
|
|
* at runtime.
|
68 |
|
|
* Howard Schlunder 03/16/07 Rewrote stack manager to be linear
|
69 |
|
|
********************************************************************/
|
70 |
|
|
#define __STACKTSK_C
|
71 |
|
|
|
72 |
|
|
#include "TCPIP Stack/TCPIP.h"
|
73 |
|
|
|
74 |
|
|
// myDHCPBindCount defined in MainDemo.c; Used to force an IP
|
75 |
|
|
// address display update for IP Gleaning
|
76 |
|
|
extern BYTE myDHCPBindCount;
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
// Stack FSM states.
|
80 |
|
|
typedef enum _SM_STACK
|
81 |
|
|
{
|
82 |
|
|
SM_STACK_IDLE,
|
83 |
|
|
SM_STACK_MAC,
|
84 |
|
|
SM_STACK_IP,
|
85 |
|
|
SM_STACK_ARP,
|
86 |
|
|
SM_STACK_TCP,
|
87 |
|
|
SM_STACK_UDP
|
88 |
|
|
} SM_STACK;
|
89 |
|
|
static SM_STACK smStack;
|
90 |
|
|
|
91 |
|
|
NODE_INFO remoteNode;
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
/*********************************************************************
|
96 |
|
|
* Function: void StackInit(void)
|
97 |
|
|
*
|
98 |
|
|
* PreCondition: None
|
99 |
|
|
*
|
100 |
|
|
* Input: None
|
101 |
|
|
*
|
102 |
|
|
* Output: Stack and its componets are initialized
|
103 |
|
|
*
|
104 |
|
|
* Side Effects: None
|
105 |
|
|
*
|
106 |
|
|
* Note: This function must be called before any of the
|
107 |
|
|
* stack or its component routines are used.
|
108 |
|
|
*
|
109 |
|
|
********************************************************************/
|
110 |
|
|
void StackInit(void)
|
111 |
|
|
{
|
112 |
|
|
smStack = SM_STACK_IDLE;
|
113 |
|
|
|
114 |
|
|
#if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT)
|
115 |
|
|
/*
|
116 |
|
|
* If DHCP or IP Gleaning is enabled,
|
117 |
|
|
* startup in Config Mode.
|
118 |
|
|
*/
|
119 |
|
|
AppConfig.Flags.bInConfigMode = TRUE;
|
120 |
|
|
|
121 |
|
|
#endif
|
122 |
|
|
|
123 |
|
|
MACInit();
|
124 |
|
|
|
125 |
|
|
ARPInit();
|
126 |
|
|
|
127 |
|
|
#if defined(STACK_USE_UDP)
|
128 |
|
|
UDPInit();
|
129 |
|
|
#endif
|
130 |
|
|
|
131 |
|
|
#if defined(STACK_USE_TCP)
|
132 |
|
|
TCPInit();
|
133 |
|
|
#endif
|
134 |
|
|
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
/*********************************************************************
|
139 |
|
|
* Function: void StackTask(void)
|
140 |
|
|
*
|
141 |
|
|
* PreCondition: StackInit() is already called.
|
142 |
|
|
*
|
143 |
|
|
* Input: None
|
144 |
|
|
*
|
145 |
|
|
* Output: Stack FSM is executed.
|
146 |
|
|
*
|
147 |
|
|
* Side Effects: None
|
148 |
|
|
*
|
149 |
|
|
* Note: This FSM checks for new incoming packets,
|
150 |
|
|
* and routes it to appropriate stack components.
|
151 |
|
|
* It also performs timed operations.
|
152 |
|
|
*
|
153 |
|
|
* This function must be called periodically to
|
154 |
|
|
* ensure timely responses.
|
155 |
|
|
*
|
156 |
|
|
********************************************************************/
|
157 |
|
|
void StackTask(void)
|
158 |
|
|
{
|
159 |
|
|
WORD dataCount;
|
160 |
|
|
IP_ADDR tempLocalIP;
|
161 |
|
|
BYTE cFrameType;
|
162 |
|
|
BYTE cIPFrameType;
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
#if defined(STACK_USE_DHCP_CLIENT)
|
166 |
|
|
// Normally, an application would not include DHCP module
|
167 |
|
|
// if it is not enabled. But in case some one wants to disable
|
168 |
|
|
// DHCP module at run-time, remember to not clear our IP
|
169 |
|
|
// address if link is removed.
|
170 |
|
|
if(AppConfig.Flags.bIsDHCPEnabled)
|
171 |
|
|
{
|
172 |
|
|
if(!MACIsLinked())
|
173 |
|
|
{
|
174 |
|
|
AppConfig.MyIPAddr.Val = AppConfig.DefaultIPAddr.Val;
|
175 |
|
|
AppConfig.MyMask.Val = AppConfig.DefaultMask.Val;
|
176 |
|
|
DHCPFlags.bits.bDHCPServerDetected = FALSE;
|
177 |
|
|
AppConfig.Flags.bInConfigMode = TRUE;
|
178 |
|
|
DHCPReset();
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
// DHCP must be called all the time even after IP configuration is
|
182 |
|
|
// discovered.
|
183 |
|
|
// DHCP has to account lease expiration time and renew the configuration
|
184 |
|
|
// time.
|
185 |
|
|
DHCPTask();
|
186 |
|
|
|
187 |
|
|
if(DHCPIsBound())
|
188 |
|
|
AppConfig.Flags.bInConfigMode = FALSE;
|
189 |
|
|
}
|
190 |
|
|
#endif
|
191 |
|
|
|
192 |
|
|
#if defined(STACK_USE_TCP)
|
193 |
|
|
// Perform all TCP time related tasks (retransmit, send acknowledge, close connection, etc)
|
194 |
|
|
TCPTick();
|
195 |
|
|
#endif
|
196 |
|
|
|
197 |
|
|
|
198 |
|
|
// Process as many incomming packets as we can
|
199 |
|
|
while(MACGetHeader(&remoteNode.MACAddr, &cFrameType))
|
200 |
|
|
{
|
201 |
|
|
switch(cFrameType)
|
202 |
|
|
{
|
203 |
|
|
case MAC_ARP:
|
204 |
|
|
ARPProcess();
|
205 |
|
|
break;
|
206 |
|
|
|
207 |
|
|
case MAC_IP:
|
208 |
|
|
if(!IPGetHeader(&tempLocalIP, &remoteNode, &cIPFrameType, &dataCount))
|
209 |
|
|
break;
|
210 |
|
|
|
211 |
|
|
#if defined(STACK_USE_ICMP_SERVER) || defined(STACK_USE_ICMP_CLIENT)
|
212 |
|
|
if(cIPFrameType == IP_PROT_ICMP)
|
213 |
|
|
{
|
214 |
|
|
ICMPProcess(&remoteNode, dataCount);
|
215 |
|
|
|
216 |
|
|
#if defined(STACK_USE_IP_GLEANING)
|
217 |
|
|
if(AppConfig.Flags.bInConfigMode && AppConfig.Flags.bIsDHCPEnabled)
|
218 |
|
|
{
|
219 |
|
|
// Accoriding to "IP Gleaning" procedure,
|
220 |
|
|
// when we receive an ICMP packet with a valid
|
221 |
|
|
// IP address while we are still in configuration
|
222 |
|
|
// mode, accept that address as ours and conclude
|
223 |
|
|
// configuration mode.
|
224 |
|
|
if(tempLocalIP.Val != 0xffffffff)
|
225 |
|
|
{
|
226 |
|
|
AppConfig.Flags.bInConfigMode = FALSE;
|
227 |
|
|
AppConfig.MyIPAddr = tempLocalIP;
|
228 |
|
|
myDHCPBindCount--;
|
229 |
|
|
}
|
230 |
|
|
}
|
231 |
|
|
#endif
|
232 |
|
|
break;
|
233 |
|
|
}
|
234 |
|
|
#endif
|
235 |
|
|
|
236 |
|
|
#if defined(STACK_USE_TCP)
|
237 |
|
|
if(cIPFrameType == IP_PROT_TCP)
|
238 |
|
|
{
|
239 |
|
|
TCPProcess(&remoteNode, &tempLocalIP, dataCount);
|
240 |
|
|
break;
|
241 |
|
|
}
|
242 |
|
|
#endif
|
243 |
|
|
|
244 |
|
|
#if defined(STACK_USE_UDP)
|
245 |
|
|
if(cIPFrameType == IP_PROT_UDP)
|
246 |
|
|
{
|
247 |
|
|
UDPProcess(&remoteNode, &tempLocalIP, dataCount);
|
248 |
|
|
break;
|
249 |
|
|
}
|
250 |
|
|
#endif
|
251 |
|
|
|
252 |
|
|
break;
|
253 |
|
|
}
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
|