diff options
| author | Jack Humbert <jack.humb@gmail.com> | 2017-07-07 11:55:23 -0400 |
|---|---|---|
| committer | Jack Humbert <jack.humb@gmail.com> | 2017-07-07 11:55:23 -0400 |
| commit | 8655d4f4948b2deef7844503c8d690f23ac1a062 (patch) | |
| tree | b2c6effc9d6cd5b5b43933a1e53b8bf17e9e82cf /lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/TCP.h | |
| parent | 1896c76a2928c96f9ab7947bec2ef8dd37623cff (diff) | |
| parent | 60b30c036397cb5627fa374bb930794b225daa29 (diff) | |
| download | qmk_firmware-8655d4f4948b2deef7844503c8d690f23ac1a062.tar.gz qmk_firmware-8655d4f4948b2deef7844503c8d690f23ac1a062.zip | |
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
Diffstat (limited to 'lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/TCP.h')
| -rw-r--r-- | lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/TCP.h | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/TCP.h b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/TCP.h new file mode 100644 index 000000000..ce8a9a2d0 --- /dev/null +++ b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/TCP.h | |||
| @@ -0,0 +1,260 @@ | |||
| 1 | /* | ||
| 2 | LUFA Library | ||
| 3 | Copyright (C) Dean Camera, 2017. | ||
| 4 | |||
| 5 | dean [at] fourwalledcubicle [dot] com | ||
| 6 | www.lufa-lib.org | ||
| 7 | */ | ||
| 8 | |||
| 9 | /* | ||
| 10 | Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) | ||
| 11 | |||
| 12 | Permission to use, copy, modify, distribute, and sell this | ||
| 13 | software and its documentation for any purpose is hereby granted | ||
| 14 | without fee, provided that the above copyright notice appear in | ||
| 15 | all 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 disclaims 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 "EthernetProtocols.h" | ||
| 44 | #include "Ethernet.h" | ||
| 45 | #include "ProtocolDecoders.h" | ||
| 46 | |||
| 47 | /* Macros: */ | ||
| 48 | /** Maximum number of TCP ports which can be open at the one time. */ | ||
| 49 | #define MAX_OPEN_TCP_PORTS 1 | ||
| 50 | |||
| 51 | /** Maximum number of TCP connections which can be sustained at the one time. */ | ||
| 52 | #define MAX_TCP_CONNECTIONS 3 | ||
| 53 | |||
| 54 | /** TCP window size, giving the maximum number of bytes which can be buffered at the one time. */ | ||
| 55 | #define TCP_WINDOW_SIZE 512 | ||
| 56 | |||
| 57 | /** Port number for HTTP transmissions. */ | ||
| 58 | #define TCP_PORT_HTTP SwapEndian_16(80) | ||
| 59 | |||
| 60 | /** Data direction indicator for a TCP application buffer, indicating data from host-to-device. */ | ||
| 61 | #define TCP_PACKETDIR_IN false | ||
| 62 | |||
| 63 | /** Data direction indicator for a TCP application buffer, indicating data from device-to-host. */ | ||
| 64 | #define TCP_PACKETDIR_OUT true | ||
| 65 | |||
| 66 | /** Congestion Window Reduced TCP flag mask. */ | ||
| 67 | #define TCP_FLAG_CWR (1 << 7) | ||
| 68 | |||
| 69 | /** Explicit Congestion Notification TCP flag mask. */ | ||
| 70 | #define TCP_FLAG_ECE (1 << 6) | ||
| 71 | |||
| 72 | /** Urgent TCP flag mask. */ | ||
| 73 | #define TCP_FLAG_URG (1 << 5) | ||
| 74 | |||
| 75 | /** Data Acknowledge TCP flag mask. */ | ||
| 76 | #define TCP_FLAG_ACK (1 << 4) | ||
| 77 | |||
| 78 | /** Data Push TCP flag mask. */ | ||
| 79 | #define TCP_FLAG_PSH (1 << 3) | ||
| 80 | |||
| 81 | /** Reset TCP flag mask. */ | ||
| 82 | #define TCP_FLAG_RST (1 << 2) | ||
| 83 | |||
| 84 | /** Synchronize TCP flag mask. */ | ||
| 85 | #define TCP_FLAG_SYN (1 << 1) | ||
| 86 | |||
| 87 | /** Connection Finalize TCP flag mask. */ | ||
| 88 | #define TCP_FLAG_FIN (1 << 0) | ||
| 89 | |||
| 90 | /** Application macro: Determines if the given application buffer contains a packet received from the host | ||
| 91 | * | ||
| 92 | * \param[in] Buffer Application buffer to check | ||
| 93 | * | ||
| 94 | * \return Boolean \c true if the buffer contains a packet from the host, \c false otherwise | ||
| 95 | */ | ||
| 96 | #define TCP_APP_HAS_RECEIVED_PACKET(Buffer) (Buffer->Ready && (Buffer->Direction == TCP_PACKETDIR_IN)) | ||
| 97 | |||
| 98 | /** Application macro: Indicates if the application buffer is currently locked by the application for device-to-host transfers. | ||
| 99 | * | ||
| 100 | * \param[in] Buffer Application buffer to check | ||
| 101 | * | ||
| 102 | * \return Boolean \c true if the buffer has been captured by the application for device-to-host transmissions, \c false otherwise | ||
| 103 | */ | ||
| 104 | #define TCP_APP_HAVE_CAPTURED_BUFFER(Buffer) (!(Buffer->Ready) && Buffer->InUse && \ | ||
| 105 | (Buffer->Direction == TCP_PACKETDIR_OUT)) | ||
| 106 | |||
| 107 | /** Application macro: Indicates if the application can lock the buffer for multiple continued device-to-host transmissions. | ||
| 108 | * | ||
| 109 | * \param[in] Buffer Application buffer to check | ||
| 110 | * | ||
| 111 | * \return Boolean \c true if the buffer may be captured by the application for device-to-host transmissions, \c false otherwise | ||
| 112 | */ | ||
| 113 | #define TCP_APP_CAN_CAPTURE_BUFFER(Buffer) Buffer->InUse | ||
| 114 | |||
| 115 | /** Application macro: Captures the application buffer, locking it for device-to-host transmissions only. This should be | ||
| 116 | * performed when the application needs to transmit several packets worth of data in succession with no interruptions from the host. | ||
| 117 | * | ||
| 118 | * \pre The application must check that the buffer can be locked first using TCP_APP_CAN_CAPTURE_BUFFER(). | ||
| 119 | * | ||
| 120 | * \param[in] Buffer Application buffer to lock | ||
| 121 | */ | ||
| 122 | #define TCP_APP_CAPTURE_BUFFER(Buffer) do { Buffer->Direction = TCP_PACKETDIR_OUT; Buffer->InUse = true; } while (0) | ||
| 123 | |||
| 124 | /** Application macro: Releases a captured application buffer, allowing for host-to-device packets to be received. | ||
| 125 | * | ||
| 126 | * \param[in] Buffer Application buffer to release | ||
| 127 | */ | ||
| 128 | #define TCP_APP_RELEASE_BUFFER(Buffer) do { Buffer->InUse = false; } while (0) | ||
| 129 | |||
| 130 | /** Application macro: Sends the contents of the given application buffer to the host. | ||
| 131 | * | ||
| 132 | * \param[in] Buffer Application buffer to send | ||
| 133 | * \param[in] Len Length of data contained in the buffer | ||
| 134 | */ | ||
| 135 | #define TCP_APP_SEND_BUFFER(Buffer, Len) do { Buffer->Direction = TCP_PACKETDIR_OUT; Buffer->Length = Len; Buffer->Ready = true; } while (0) | ||
| 136 | |||
| 137 | /** Application macro: Clears the application buffer, ready for a packet to be written to it. | ||
| 138 | * | ||
| 139 | * \param[in] Buffer Application buffer to clear | ||
| 140 | */ | ||
| 141 | #define TCP_APP_CLEAR_BUFFER(Buffer) do { Buffer->Ready = false; Buffer->Length = 0; } while (0) | ||
| 142 | |||
| 143 | /** Application macro: Closes an open connection to a host. | ||
| 144 | * | ||
| 145 | * \param[in] Connection Open TCP connection to close | ||
| 146 | */ | ||
| 147 | #define TCP_APP_CLOSECONNECTION(Connection) do { Connection->State = TCP_Connection_Closing; } while (0) | ||
| 148 | |||
| 149 | /* Enums: */ | ||
| 150 | /** Enum for possible TCP port states. */ | ||
| 151 | enum TCP_PortStates_t | ||
| 152 | { | ||
| 153 | TCP_Port_Closed = 0, /**< TCP port closed, no connections to a host may be made on this port. */ | ||
| 154 | TCP_Port_Open = 1, /**< TCP port open, connections to a host may be made on this port. */ | ||
| 155 | }; | ||
| 156 | |||
| 157 | /** Enum for possible TCP connection states. */ | ||
| 158 | enum TCP_ConnectionStates_t | ||
| 159 | { | ||
| 160 | TCP_Connection_Listen = 0, /**< Listening for a connection from a host */ | ||
| 161 | TCP_Connection_SYNSent = 1, /**< Unused */ | ||
| 162 | TCP_Connection_SYNReceived = 2, /**< SYN received, waiting for ACK */ | ||
| 163 | TCP_Connection_Established = 3, /**< Connection established in both directions */ | ||
| 164 | TCP_Connection_FINWait1 = 4, /**< Closing, waiting for ACK */ | ||
| 165 | TCP_Connection_FINWait2 = 5, /**< Closing, waiting for FIN ACK */ | ||
| 166 | TCP_Connection_CloseWait = 6, /**< Closing, waiting for ACK */ | ||
| 167 | TCP_Connection_Closing = 7, /**< Unused */ | ||
| 168 | TCP_Connection_LastACK = 8, /**< Unused */ | ||
| 169 | TCP_Connection_TimeWait = 9, /**< Unused */ | ||
| 170 | TCP_Connection_Closed = 10, /**< Connection closed in both directions */ | ||
| 171 | }; | ||
| 172 | |||
| 173 | /* Type Defines: */ | ||
| 174 | /** Type define for a TCP connection buffer structure, including size, data and direction. */ | ||
| 175 | typedef struct | ||
| 176 | { | ||
| 177 | uint16_t Length; /**< Length of data in the TCP application buffer */ | ||
| 178 | uint8_t Data[TCP_WINDOW_SIZE]; /**< TCP application data buffer */ | ||
| 179 | bool Direction; /**< Buffer transmission direction, either TCP_PACKETDIR_IN or TCP_PACKETDIR_OUT */ | ||
| 180 | bool Ready; /**< If data from host, indicates buffer ready to be read, otherwise indicates | ||
| 181 | * buffer ready to be sent to the host | ||
| 182 | */ | ||
| 183 | bool InUse; /**< Indicates if the buffer is locked to to the current direction, and cannot be changed */ | ||
| 184 | } TCP_ConnectionBuffer_t; | ||
| 185 | |||
| 186 | /** Type define for a TCP connection information structure. */ | ||
| 187 | typedef struct | ||
| 188 | { | ||
| 189 | uint32_t SequenceNumberIn; /**< Current TCP sequence number for host-to-device */ | ||
| 190 | uint32_t SequenceNumberOut; /**< Current TCP sequence number for device-to-host */ | ||
| 191 | TCP_ConnectionBuffer_t Buffer; /**< Connection application data buffer */ | ||
| 192 | } TCP_ConnectionInfo_t; | ||
| 193 | |||
| 194 | /** Type define for a complete TCP connection state. */ | ||
| 195 | typedef struct | ||
| 196 | { | ||
| 197 | uint16_t Port; /**< Connection port number on the device */ | ||
| 198 | uint16_t RemotePort; /**< Connection port number on the host */ | ||
| 199 | IP_Address_t RemoteAddress; /**< Connection protocol IP address of the host */ | ||
| 200 | TCP_ConnectionInfo_t Info; /**< Connection information, including application buffer */ | ||
| 201 | uint8_t State; /**< Current connection state, a value from the \ref TCP_ConnectionStates_t enum */ | ||
| 202 | } TCP_ConnectionState_t; | ||
| 203 | |||
| 204 | /** Type define for a TCP port state. */ | ||
| 205 | typedef struct | ||
| 206 | { | ||
| 207 | uint16_t Port; /**< TCP port number on the device */ | ||
| 208 | uint8_t State; /**< Current port state, a value from the \ref TCP_PortStates_t enum */ | ||
| 209 | void (*ApplicationHandler) (TCP_ConnectionState_t* ConnectionState, | ||
| 210 | TCP_ConnectionBuffer_t* Buffer); /**< Port application handler */ | ||
| 211 | } TCP_PortState_t; | ||
| 212 | |||
| 213 | /** Type define for a TCP packet header. */ | ||
| 214 | typedef struct | ||
| 215 | { | ||
| 216 | uint16_t SourcePort; /**< Source port of the TCP packet */ | ||
| 217 | uint16_t DestinationPort; /**< Destination port of the TCP packet */ | ||
| 218 | |||
| 219 | uint32_t SequenceNumber; /**< Data sequence number of the packet */ | ||
| 220 | uint32_t AcknowledgmentNumber; /**< Data acknowledgment number of the packet */ | ||
| 221 | |||
| 222 | unsigned Reserved : 4; /**< Reserved, must be all 0 */ | ||
| 223 | unsigned DataOffset : 4; /**< Offset of the data from the start of the header, in 4 byte chunks */ | ||
| 224 | uint8_t Flags; /**< TCP packet flags */ | ||
| 225 | uint16_t WindowSize; /**< Current data window size (bytes remaining in reception buffer) */ | ||
| 226 | |||
| 227 | uint16_t Checksum; /**< TCP checksum */ | ||
| 228 | uint16_t UrgentPointer; /**< Urgent data pointer */ | ||
| 229 | } TCP_Header_t; | ||
| 230 | |||
| 231 | /* Function Prototypes: */ | ||
| 232 | void TCP_Init(void); | ||
| 233 | void TCP_Task(void); | ||
| 234 | bool TCP_SetPortState(const uint16_t Port, | ||
| 235 | const uint8_t State, | ||
| 236 | void (*Handler)(TCP_ConnectionState_t*, TCP_ConnectionBuffer_t*)); | ||
| 237 | uint8_t TCP_GetPortState(const uint16_t Port); | ||
| 238 | bool TCP_SetConnectionState(const uint16_t Port, | ||
| 239 | const IP_Address_t* RemoteAddress, | ||
| 240 | const uint16_t RemotePort, | ||
| 241 | const uint8_t State); | ||
| 242 | uint8_t TCP_GetConnectionState(const uint16_t Port, | ||
| 243 | const IP_Address_t* RemoteAddress, | ||
| 244 | const uint16_t RemotePort); | ||
| 245 | TCP_ConnectionInfo_t* TCP_GetConnectionInfo(const uint16_t Port, | ||
| 246 | const IP_Address_t* RemoteAddress, | ||
| 247 | const uint16_t RemotePort); | ||
| 248 | int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart, | ||
| 249 | void* TCPHeaderInStart, | ||
| 250 | void* TCPHeaderOutStart); | ||
| 251 | |||
| 252 | #if defined(INCLUDE_FROM_TCP_C) | ||
| 253 | static uint16_t TCP_Checksum16(void* TCPHeaderOutStart, | ||
| 254 | const IP_Address_t* SourceAddress, | ||
| 255 | const IP_Address_t* DestinationAddress, | ||
| 256 | uint16_t TCPOutSize); | ||
| 257 | #endif | ||
| 258 | |||
| 259 | #endif | ||
| 260 | |||
