Move new Class Driver powered demos to a new ClassDriver subdirectory, re-add old...
[pub/USBasp.git] / Demos / Device / LowLevel / RNDISEthernet / Lib / TCP.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Header file for TCP.c.
34 */
35
36 #ifndef _TCP_H_
37 #define _TCP_H_
38
39 /* Includes: */
40 #include <avr/io.h>
41 #include <stdbool.h>
42
43 #include <LUFA/Scheduler/Scheduler.h>
44
45 #include "EthernetProtocols.h"
46 #include "Ethernet.h"
47 #include "ProtocolDecoders.h"
48
49 /* Macros: */
50 /** Maximum number of TCP ports which can be open at the one time */
51 #define MAX_OPEN_TCP_PORTS 1
52
53 /** Maximum number of TCP connections which can be sustained at the one time */
54 #define MAX_TCP_CONNECTIONS 1
55
56 /** TCP window size, giving the maximum number of bytes which can be buffered at the one time */
57 #define TCP_WINDOW_SIZE 1024
58
59 /** Port number for HTTP transmissions */
60 #define TCP_PORT_HTTP SwapEndian_16(80)
61
62 /** Data direction indicator for a TCP application buffer, indicating data from host-to-device */
63 #define TCP_PACKETDIR_IN false
64
65 /** Data direction indicator for a TCP application buffer, indicating data from device-to-host */
66 #define TCP_PACKETDIR_OUT true
67
68 /** Congestion Window Reduced TCP flag mask */
69 #define TCP_FLAG_CWR (1 << 7)
70
71 /** Explicit Congestion Notification TCP flag mask */
72 #define TCP_FLAG_ECE (1 << 6)
73
74 /** Urgent TCP flag mask */
75 #define TCP_FLAG_URG (1 << 5)
76
77 /** Data Acknowledge TCP flag mask */
78 #define TCP_FLAG_ACK (1 << 4)
79
80 /** Data Push TCP flag mask */
81 #define TCP_FLAG_PSH (1 << 3)
82
83 /** Reset TCP flag mask */
84 #define TCP_FLAG_RST (1 << 2)
85
86 /** Synchronize TCP flag mask */
87 #define TCP_FLAG_SYN (1 << 1)
88
89 /** Connection Finalize TCP flag mask */
90 #define TCP_FLAG_FIN (1 << 0)
91
92 /** Application macro: Determines if the given application buffer contains a packet received from the host
93 *
94 * \param Buffer Application buffer to check
95 *
96 * \return Boolean true if the buffer contains a packet from the host, false otherwise
97 */
98 #define TCP_APP_HAS_RECEIVED_PACKET(Buffer) (Buffer->Ready && (Buffer->Direction == TCP_PACKETDIR_IN))
99
100 /** Application macro: Indicates if the application buffer is currently locked by the application for device-to-host transfers.
101 *
102 * \param Buffer Application buffer to check
103 *
104 * \return Boolean true if the buffer has been captured by the application for device-to-host transmissions, false otherwise
105 */
106 #define TCP_APP_HAVE_CAPTURED_BUFFER(Buffer) (!(Buffer->Ready) && Buffer->InUse && \
107 (Buffer->Direction == TCP_PACKETDIR_OUT))
108
109 /** Application macro: Indicates if the application can lock the buffer for multiple continued device-to-host transmissions.
110 *
111 * \param Buffer Application buffer to check
112 *
113 * \return Boolean true if the buffer may be captured by the application for device-to-host transmissions, false otherwise
114 */
115 #define TCP_APP_CAN_CAPTURE_BUFFER(Buffer) Buffer->InUse
116
117 /** Application macro: Captures the application buffer, locking it for device-to-host transmissions only. This should be
118 * performed when the application needs to transmit several packets worth of data in succession with no interruptions from the host.
119 *
120 * \note The application must check that the buffer can be locked first using TCP_APP_CAN_CAPTURE_BUFFER().
121 *
122 * \param Buffer Application buffer to lock
123 */
124 #define TCP_APP_CAPTURE_BUFFER(Buffer) MACROS{ Buffer->Direction = TCP_PACKETDIR_OUT; Buffer->InUse = true; }MACROE
125
126 /** Application macro: Releases a captured application buffer, allowing for host-to-device packets to be received.
127 *
128 * \param Buffer Application buffer to release
129 */
130 #define TCP_APP_RELEASE_BUFFER(Buffer) MACROS{ Buffer->InUse = false; }MACROE
131
132 /** Application macro: Sends the contents of the given application buffer to the host.
133 *
134 * \param Buffer Application buffer to send
135 * \param Len Length of data contained in the buffer
136 */
137 #define TCP_APP_SEND_BUFFER(Buffer, Len) MACROS{ Buffer->Direction = TCP_PACKETDIR_OUT; Buffer->Length = Len; Buffer->Ready = true; }MACROE
138
139 /** Application macro: Clears the application buffer, ready for a packet to be written to it.
140 *
141 * \param Buffer Application buffer to clear
142 */
143 #define TCP_APP_CLEAR_BUFFER(Buffer) MACROS{ Buffer->Ready = false; Buffer->Length = 0; }MACROE
144
145 /** Application macro: Closes an open connection to a host.
146 *
147 * \param Connection Open TCP connection to close
148 */
149 #define TCP_APP_CLOSECONNECTION(Connection) MACROS{ Connection->State = TCP_Connection_Closing; }MACROE
150
151 /* Enums: */
152 /** Enum for possible TCP port states */
153 enum TCP_PortStates_t
154 {
155 TCP_Port_Closed = 0, /**< TCP port closed, no connections to a host may be made on this port. */
156 TCP_Port_Open = 1, /**< TCP port open, connections to a host may be made on this port. */
157 };
158
159 /** Enum for possible TCP connection states */
160 enum TCP_ConnectionStates_t
161 {
162 TCP_Connection_Listen = 0, /**< Listening for a connection from a host */
163 TCP_Connection_SYNSent = 1, /**< Unused */
164 TCP_Connection_SYNReceived = 2, /**< SYN received, waiting for ACK */
165 TCP_Connection_Established = 3, /**< Connection established in both directions */
166 TCP_Connection_FINWait1 = 4, /**< Closing, waiting for ACK */
167 TCP_Connection_FINWait2 = 5, /**< Closing, waiting for FIN ACK */
168 TCP_Connection_CloseWait = 6, /**< Closing, waiting for ACK */
169 TCP_Connection_Closing = 7, /**< Unused */
170 TCP_Connection_LastACK = 8, /**< Unused */
171 TCP_Connection_TimeWait = 9, /**< Unused */
172 TCP_Connection_Closed = 10, /**< Connection closed in both directions */
173 };
174
175 /* Type Defines: */
176 /** Type define for a TCP connection buffer structure, including size, data and direction */
177 typedef struct
178 {
179 uint16_t Length; /**< Length of data in the TCP application buffer */
180 uint8_t Data[TCP_WINDOW_SIZE]; /**< TCP application data buffer */
181 bool Direction; /**< Buffer transmission direction, either TCP_PACKETDIR_IN or TCP_PACKETDIR_OUT */
182 bool Ready; /**< If data from host, indicates buffer ready to be read, otherwise indicates
183 * buffer ready to be sent to the host
184 */
185 bool InUse; /** Indicates if the buffer is locked to to the current direction, and cannot be changed */
186 } TCP_ConnectionBuffer_t;
187
188 /** Type define for a TCP connection information structure */
189 typedef struct
190 {
191 uint32_t SequenceNumberIn; /**< Current TCP sequence number for host-to-device */
192 uint32_t SequenceNumberOut; /**< Current TCP sequence number for device-to-host */
193 TCP_ConnectionBuffer_t Buffer; /**< Connection application data buffer */
194 } TCP_ConnectionInfo_t;
195
196 /** Type define for a complete TCP connection state */
197 typedef struct
198 {
199 uint16_t Port; /**< Connection port number on the device */
200 uint16_t RemotePort; /**< Connection port number on the host */
201 IP_Address_t RemoteAddress; /**< Connection protocol IP address of the host */
202 TCP_ConnectionInfo_t Info; /**< Connection information, including application buffer */
203 uint8_t State; /**< Current connection state, a value from the TCP_ConnectionStates_t enum */
204 } TCP_ConnectionState_t;
205
206 /** Type define for a TCP port state */
207 typedef struct
208 {
209 uint16_t Port; /**< TCP port number on the device */
210 uint8_t State; /**< Current port state, a value from the TCP_PortStates_t enum */
211 void (*ApplicationHandler) (TCP_ConnectionState_t* ConnectionState,
212 TCP_ConnectionBuffer_t* Buffer); /**< Port application handler */
213 } TCP_PortState_t;
214
215 /** Type define for a TCP packet header */
216 typedef struct
217 {
218 uint16_t SourcePort; /**< Source port of the TCP packet */
219 uint16_t DestinationPort; /**< Destination port of the TCP packet */
220
221 uint32_t SequenceNumber; /**< Data sequence number of the packet */
222 uint32_t AcknowledgmentNumber; /**< Data acknowledgment number of the packet */
223
224 unsigned char Reserved : 4; /**< Reserved, must be all 0 */
225 unsigned char DataOffset : 4; /**< Offset of the data from the start of the header, in 4 byte chunks */
226 uint8_t Flags; /**< TCP packet flags */
227 uint16_t WindowSize; /**< Current data window size (bytes remaining in reception buffer) */
228
229 uint16_t Checksum; /**< TCP checksum */
230 uint16_t UrgentPointer; /**< Urgent data pointer */
231 } TCP_Header_t;
232
233 /* Tasks: */
234 TASK(TCP_Task);
235
236 /* External Variables: */
237 TCP_PortState_t PortStateTable[MAX_OPEN_TCP_PORTS];
238
239 /* Function Prototypes: */
240 void TCP_Init(void);
241 bool TCP_SetPortState(uint16_t Port, uint8_t State, void (*Handler)(TCP_ConnectionState_t*, TCP_ConnectionBuffer_t*));
242 uint8_t TCP_GetPortState(uint16_t Port);
243 bool TCP_SetConnectionState(uint16_t Port, IP_Address_t RemoteAddress, uint16_t RemotePort, uint8_t State);
244 uint8_t TCP_GetConnectionState(uint16_t Port, IP_Address_t RemoteAddress, uint16_t RemotePort);
245 TCP_ConnectionInfo_t* TCP_GetConnectionInfo(uint16_t Port, IP_Address_t RemoteAddress, uint16_t RemotePort);
246 int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart, void* TCPHeaderInStart, void* TCPHeaderOutStart);
247
248 #if defined(INCLUDE_FROM_TCP_C)
249 static uint16_t TCP_Checksum16(void* TCPHeaderOutStart, IP_Address_t SourceAddress,
250 IP_Address_t DestinationAddress, uint16_t TCPOutSize);
251 #endif
252
253 #endif