1 |
hedin |
62 |
/*********************************************************************
|
2 |
|
|
*
|
3 |
|
|
* TCP Module Defs for Microchip TCP/IP Stack
|
4 |
|
|
*
|
5 |
|
|
*********************************************************************
|
6 |
|
|
* FileName: TCP.h
|
7 |
|
|
* Dependencies: StackTsk.h
|
8 |
|
|
* Processor: PIC18
|
9 |
|
|
* Complier: MCC18 v1.00.50 or higher
|
10 |
|
|
* HITECH PICC-18 V8.10PL1 or higher
|
11 |
|
|
* Company: Microchip Technology, Inc.
|
12 |
|
|
*
|
13 |
|
|
* Software License Agreement
|
14 |
|
|
*
|
15 |
|
|
* The software supplied herewith by Microchip Technology Incorporated
|
16 |
|
|
* (the “Company”) for its PICmicro® Microcontroller is intended and
|
17 |
|
|
* supplied to you, the Company’s customer, for use solely and
|
18 |
|
|
* exclusively on Microchip PICmicro Microcontroller products. The
|
19 |
|
|
* software is owned by the Company and/or its supplier, and is
|
20 |
|
|
* protected under applicable copyright laws. All rights are reserved.
|
21 |
|
|
* Any use in violation of the foregoing restrictions may subject the
|
22 |
|
|
* user to criminal sanctions under applicable laws, as well as to
|
23 |
|
|
* civil liability for the breach of the terms and conditions of this
|
24 |
|
|
* license.
|
25 |
|
|
*
|
26 |
|
|
* THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
|
27 |
|
|
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
|
28 |
|
|
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
29 |
|
|
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
|
30 |
|
|
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
|
31 |
|
|
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
|
32 |
|
|
*
|
33 |
|
|
* Author Date Comment
|
34 |
|
|
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
35 |
|
|
* Nilesh Rajbharti 5/8/01 Original (Rev 1.0)
|
36 |
|
|
* Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail)
|
37 |
|
|
********************************************************************/
|
38 |
|
|
|
39 |
|
|
#ifndef TCP_H
|
40 |
|
|
#define TCP_H
|
41 |
|
|
|
42 |
|
|
#include "stacktsk.h"
|
43 |
|
|
#include "tick.h"
|
44 |
|
|
|
45 |
|
|
typedef BYTE TCP_SOCKET;
|
46 |
|
|
typedef WORD TCP_PORT;
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
/*
|
50 |
|
|
* Maximum number of times a connection be retried before
|
51 |
|
|
* closing it down.
|
52 |
|
|
*/
|
53 |
|
|
#define MAX_RETRY_COUNTS (3)
|
54 |
|
|
|
55 |
|
|
#define INVALID_SOCKET (0xfe)
|
56 |
|
|
#define UNKNOWN_SOCKET (0xff)
|
57 |
|
|
|
58 |
|
|
#define REMOTE_HOST(s) (TCB[s].remote)
|
59 |
|
|
|
60 |
|
|
/*
|
61 |
|
|
* TCP States as defined by rfc793
|
62 |
|
|
*/
|
63 |
|
|
typedef enum _TCP_STATE
|
64 |
|
|
{
|
65 |
|
|
TCP_CLOSED,
|
66 |
|
|
TCP_LISTEN,
|
67 |
|
|
TCP_SYN_RCVD,
|
68 |
|
|
TCP_SYN_SENT,
|
69 |
|
|
TCP_EST,
|
70 |
|
|
TCP_FIN_WAIT_1,
|
71 |
|
|
TCP_CLOSING,
|
72 |
|
|
TCP_TIMED_WAIT,
|
73 |
|
|
TCP_SEND_RST,
|
74 |
|
|
TCP_DATA_READY,
|
75 |
|
|
TCP_LAST_ACK,
|
76 |
|
|
TCP_CLOSE,
|
77 |
|
|
TCP_INVALID
|
78 |
|
|
} TCP_STATE;
|
79 |
|
|
|
80 |
|
|
/*
|
81 |
|
|
* Socket info.
|
82 |
|
|
* Union is used to create anonymous structure members.
|
83 |
|
|
*/
|
84 |
|
|
typedef struct _SOCKET_INFO
|
85 |
|
|
{
|
86 |
|
|
TCP_STATE smState;
|
87 |
|
|
|
88 |
|
|
NODE_INFO remote;
|
89 |
|
|
TCP_PORT localPort;
|
90 |
|
|
TCP_PORT remotePort;
|
91 |
|
|
|
92 |
|
|
BUFFER TxBuffer;
|
93 |
|
|
WORD TxCount;
|
94 |
|
|
WORD RxCount;
|
95 |
|
|
|
96 |
|
|
DWORD SND_SEQ;
|
97 |
|
|
DWORD SND_ACK;
|
98 |
|
|
|
99 |
|
|
BYTE RetryCount;
|
100 |
|
|
TICK startTick;
|
101 |
|
|
TICK TimeOut;
|
102 |
|
|
|
103 |
|
|
struct
|
104 |
|
|
{
|
105 |
|
|
unsigned int bServer : 1;
|
106 |
|
|
unsigned int bIsPutReady : 1;
|
107 |
|
|
unsigned int bFirstRead : 1;
|
108 |
|
|
unsigned int bIsGetReady : 1;
|
109 |
|
|
unsigned int bIsTxInProgress : 1;
|
110 |
|
|
} Flags;
|
111 |
|
|
|
112 |
|
|
} SOCKET_INFO;
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
#if !defined(THIS_IS_TCP)
|
116 |
|
|
/*
|
117 |
|
|
* These are all sockets supported by this TCP.
|
118 |
|
|
*/
|
119 |
|
|
extern SOCKET_INFO TCB[MAX_SOCKETS];
|
120 |
|
|
#endif
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
/*********************************************************************
|
125 |
|
|
* Function: void TCPInit(void)
|
126 |
|
|
*
|
127 |
|
|
* PreCondition: None
|
128 |
|
|
*
|
129 |
|
|
* Input: None
|
130 |
|
|
*
|
131 |
|
|
* Output: TCP is initialized.
|
132 |
|
|
*
|
133 |
|
|
* Side Effects: None
|
134 |
|
|
*
|
135 |
|
|
* Overview: Initialize all socket info.
|
136 |
|
|
*
|
137 |
|
|
* Note: This function is called only one during lifetime
|
138 |
|
|
* of the application.
|
139 |
|
|
********************************************************************/
|
140 |
|
|
void TCPInit(void);
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
/*********************************************************************
|
145 |
|
|
* Function: TCP_SOCKET TCPListen(TCP_PORT port)
|
146 |
|
|
*
|
147 |
|
|
* PreCondition: TCPInit() is already called.
|
148 |
|
|
*
|
149 |
|
|
* Input: port - A TCP port to be opened.
|
150 |
|
|
*
|
151 |
|
|
* Output: Given port is opened and returned on success
|
152 |
|
|
* INVALID_SOCKET if no more sockets left.
|
153 |
|
|
*
|
154 |
|
|
* Side Effects: None
|
155 |
|
|
*
|
156 |
|
|
* Overview: None
|
157 |
|
|
*
|
158 |
|
|
* Note: None
|
159 |
|
|
********************************************************************/
|
160 |
|
|
TCP_SOCKET TCPListen(TCP_PORT port);
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
/*********************************************************************
|
165 |
|
|
* Function: TCP_SOCKET TCPConnect(NODE_INFO* remote,
|
166 |
|
|
* TCP_PORT remotePort)
|
167 |
|
|
*
|
168 |
|
|
* PreCondition: TCPInit() is already called.
|
169 |
|
|
*
|
170 |
|
|
* Input: remote - Remote node address info
|
171 |
|
|
* remotePort - remote port to be connected.
|
172 |
|
|
*
|
173 |
|
|
* Output: A new socket is created, connection request is
|
174 |
|
|
* sent and socket handle is returned.
|
175 |
|
|
*
|
176 |
|
|
* Side Effects: None
|
177 |
|
|
*
|
178 |
|
|
* Overview: None
|
179 |
|
|
*
|
180 |
|
|
* Note: By default this function is not included in
|
181 |
|
|
* source. You must define STACK_CLIENT_MODE to
|
182 |
|
|
* be able to use this function.
|
183 |
|
|
********************************************************************/
|
184 |
|
|
TCP_SOCKET TCPConnect(NODE_INFO *remote, TCP_PORT port);
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
/*********************************************************************
|
188 |
|
|
* Function: BOOL TCPIsConnected(TCP_SOCKET s)
|
189 |
|
|
*
|
190 |
|
|
* PreCondition: TCPInit() is already called.
|
191 |
|
|
*
|
192 |
|
|
* Input: s - Socket to be checked for connection.
|
193 |
|
|
*
|
194 |
|
|
* Output: TRUE if given socket is connected
|
195 |
|
|
* FALSE if given socket is not connected.
|
196 |
|
|
*
|
197 |
|
|
* Side Effects: None
|
198 |
|
|
*
|
199 |
|
|
* Overview: None
|
200 |
|
|
*
|
201 |
|
|
* Note: A socket is said to be connected if it is not
|
202 |
|
|
* in LISTEN and CLOSED mode. Socket may be in
|
203 |
|
|
* SYN_RCVD or FIN_WAIT_1 and may contain socket
|
204 |
|
|
* data.
|
205 |
|
|
********************************************************************/
|
206 |
|
|
BOOL TCPIsConnected(TCP_SOCKET s);
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
/*********************************************************************
|
210 |
|
|
* Function: void TCPDisconnect(TCP_SOCKET s)
|
211 |
|
|
*
|
212 |
|
|
* PreCondition: TCPInit() is already called AND
|
213 |
|
|
* TCPIsPutReady(s) == TRUE
|
214 |
|
|
*
|
215 |
|
|
* Input: s - Socket to be disconnected.
|
216 |
|
|
*
|
217 |
|
|
* Output: A disconnect request is sent for given socket.
|
218 |
|
|
*
|
219 |
|
|
* Side Effects: None
|
220 |
|
|
*
|
221 |
|
|
* Overview: None
|
222 |
|
|
*
|
223 |
|
|
* Note: None
|
224 |
|
|
********************************************************************/
|
225 |
|
|
void TCPDisconnect(TCP_SOCKET s);
|
226 |
|
|
|
227 |
|
|
|
228 |
|
|
/*********************************************************************
|
229 |
|
|
* Function: BOOL TCPIsPutReady(TCP_SOCKET s)
|
230 |
|
|
*
|
231 |
|
|
* PreCondition: TCPInit() is already called.
|
232 |
|
|
*
|
233 |
|
|
* Input: s - socket to test
|
234 |
|
|
*
|
235 |
|
|
* Output: TRUE if socket 's' is free to transmit
|
236 |
|
|
* FALSE if socket 's' is not free to transmit.
|
237 |
|
|
*
|
238 |
|
|
* Side Effects: None
|
239 |
|
|
*
|
240 |
|
|
* Overview: None
|
241 |
|
|
*
|
242 |
|
|
* Note: Each socket maintains only transmit buffer.
|
243 |
|
|
* Hence until a data packet is acknowledeged by
|
244 |
|
|
* remote node, socket will not be ready for
|
245 |
|
|
* next transmission.
|
246 |
|
|
* All control transmission such as Connect,
|
247 |
|
|
* Disconnect do not consume/reserve any transmit
|
248 |
|
|
* buffer.
|
249 |
|
|
********************************************************************/
|
250 |
|
|
BOOL TCPIsPutReady(TCP_SOCKET s);
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
/*********************************************************************
|
254 |
|
|
* Function: BOOL TCPPut(TCP_SOCKET s, BYTE byte)
|
255 |
|
|
*
|
256 |
|
|
* PreCondition: TCPIsPutReady() == TRUE
|
257 |
|
|
*
|
258 |
|
|
* Input: s - socket to use
|
259 |
|
|
* byte - a data byte to send
|
260 |
|
|
*
|
261 |
|
|
* Output: TRUE if given byte was put in transmit buffer
|
262 |
|
|
* FALSE if transmit buffer is full.
|
263 |
|
|
*
|
264 |
|
|
* Side Effects: None
|
265 |
|
|
*
|
266 |
|
|
* Overview: None
|
267 |
|
|
*
|
268 |
|
|
* Note: None
|
269 |
|
|
********************************************************************/
|
270 |
|
|
BOOL TCPPut(TCP_SOCKET socket, BYTE byte);
|
271 |
|
|
|
272 |
|
|
|
273 |
|
|
/*********************************************************************
|
274 |
|
|
* Function: BOOL TCPFlush(TCP_SOCKET s)
|
275 |
|
|
*
|
276 |
|
|
* PreCondition: TCPInit() is already called.
|
277 |
|
|
*
|
278 |
|
|
* Input: s - Socket whose data is to be transmitted.
|
279 |
|
|
*
|
280 |
|
|
* Output: All and any data associated with this socket
|
281 |
|
|
* is marked as ready for transmission.
|
282 |
|
|
*
|
283 |
|
|
* Side Effects: None
|
284 |
|
|
*
|
285 |
|
|
* Overview: None
|
286 |
|
|
*
|
287 |
|
|
* Note: None
|
288 |
|
|
********************************************************************/
|
289 |
|
|
BOOL TCPFlush(TCP_SOCKET socket);
|
290 |
|
|
|
291 |
|
|
/*********************************************************************
|
292 |
|
|
* Function: BOOL TCPIsGetReady(TCP_SOCKET s)
|
293 |
|
|
*
|
294 |
|
|
* PreCondition: TCPInit() is already called.
|
295 |
|
|
*
|
296 |
|
|
* Input: s - socket to test
|
297 |
|
|
*
|
298 |
|
|
* Output: TRUE if socket 's' contains any data.
|
299 |
|
|
* FALSE if socket 's' does not contain any data.
|
300 |
|
|
*
|
301 |
|
|
* Side Effects: None
|
302 |
|
|
*
|
303 |
|
|
* Overview: None
|
304 |
|
|
*
|
305 |
|
|
* Note: None
|
306 |
|
|
********************************************************************/
|
307 |
|
|
BOOL TCPIsGetReady(TCP_SOCKET s);
|
308 |
|
|
|
309 |
|
|
|
310 |
|
|
/*********************************************************************
|
311 |
|
|
* Function: BOOL TCPGet(TCP_SOCKET s, BYTE *byte)
|
312 |
|
|
*
|
313 |
|
|
* PreCondition: TCPInit() is already called AND
|
314 |
|
|
* TCPIsGetReady(s) == TRUE
|
315 |
|
|
*
|
316 |
|
|
* Input: s - socket
|
317 |
|
|
* byte - Pointer to a byte.
|
318 |
|
|
*
|
319 |
|
|
* Output: TRUE if a byte was read.
|
320 |
|
|
* FALSE if byte was not read.
|
321 |
|
|
*
|
322 |
|
|
* Side Effects: None
|
323 |
|
|
*
|
324 |
|
|
* Overview: None
|
325 |
|
|
*
|
326 |
|
|
* Note: None
|
327 |
|
|
********************************************************************/
|
328 |
|
|
BOOL TCPGet(TCP_SOCKET socket, BYTE *byte);
|
329 |
|
|
|
330 |
|
|
|
331 |
|
|
/*********************************************************************
|
332 |
|
|
* Function: WORD TCPGetArray(TCP_SOCKET s, BYTE *buffer,
|
333 |
|
|
* WORD count)
|
334 |
|
|
*
|
335 |
|
|
* PreCondition: TCPInit() is already called AND
|
336 |
|
|
* TCPIsGetReady(s) == TRUE
|
337 |
|
|
*
|
338 |
|
|
* Input: s - socket
|
339 |
|
|
* buffer - Buffer to hold received data.
|
340 |
|
|
* count - Buffer length
|
341 |
|
|
*
|
342 |
|
|
* Output: Number of bytes loaded into buffer.
|
343 |
|
|
*
|
344 |
|
|
* Side Effects: None
|
345 |
|
|
*
|
346 |
|
|
* Overview: None
|
347 |
|
|
*
|
348 |
|
|
* Note: None
|
349 |
|
|
********************************************************************/
|
350 |
|
|
WORD TCPGetArray(TCP_SOCKET s, BYTE *buffer, WORD count);
|
351 |
|
|
|
352 |
|
|
|
353 |
|
|
/*********************************************************************
|
354 |
|
|
* Function: BOOL TCPDiscard(TCP_SOCKET s)
|
355 |
|
|
*
|
356 |
|
|
* PreCondition: TCPInit() is already called.
|
357 |
|
|
*
|
358 |
|
|
* Input: s - socket
|
359 |
|
|
*
|
360 |
|
|
* Output: TRUE if socket received data was discarded
|
361 |
|
|
* FALSE if socket received data was already
|
362 |
|
|
* discarded.
|
363 |
|
|
*
|
364 |
|
|
* Side Effects: None
|
365 |
|
|
*
|
366 |
|
|
* Overview: None
|
367 |
|
|
*
|
368 |
|
|
* Note: None
|
369 |
|
|
********************************************************************/
|
370 |
|
|
BOOL TCPDiscard(TCP_SOCKET socket);
|
371 |
|
|
|
372 |
|
|
/*********************************************************************
|
373 |
|
|
* Function: BOOL TCPProcess(NODE_INFO* remote,
|
374 |
|
|
* WORD len)
|
375 |
|
|
*
|
376 |
|
|
* PreCondition: TCPInit() is already called AND
|
377 |
|
|
* TCP segment is ready in MAC buffer
|
378 |
|
|
*
|
379 |
|
|
* Input: remote - Remote node info
|
380 |
|
|
* len - Total length of TCP semgent.
|
381 |
|
|
*
|
382 |
|
|
* Output: TRUE if this function has completed its task
|
383 |
|
|
* FALSE otherwise
|
384 |
|
|
*
|
385 |
|
|
* Side Effects: None
|
386 |
|
|
*
|
387 |
|
|
* Overview: None
|
388 |
|
|
*
|
389 |
|
|
* Note: None
|
390 |
|
|
********************************************************************/
|
391 |
|
|
BOOL TCPProcess(NODE_INFO *remote, WORD len);
|
392 |
|
|
|
393 |
|
|
|
394 |
|
|
/*********************************************************************
|
395 |
|
|
* Function: void TCPTick(void)
|
396 |
|
|
*
|
397 |
|
|
* PreCondition: TCPInit() is already called.
|
398 |
|
|
*
|
399 |
|
|
* Input: None
|
400 |
|
|
*
|
401 |
|
|
* Output: Each socket FSM is executed for any timeout
|
402 |
|
|
* situation.
|
403 |
|
|
*
|
404 |
|
|
* Side Effects: None
|
405 |
|
|
*
|
406 |
|
|
* Overview: None
|
407 |
|
|
*
|
408 |
|
|
* Note: None
|
409 |
|
|
********************************************************************/
|
410 |
|
|
void TCPTick(void);
|
411 |
|
|
|
412 |
|
|
|
413 |
|
|
#endif
|