Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TCP.h 12KB

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