#include "net\ip.h"
#include "net\tick.h"
//********************************************************************* //-------------------- TCP Configuration -------------------- //********************************************************************* //Maximum number of times a connection be retried before closing it down. #define TCP_MAX_RETRY_COUNTS (3ul) //TCP Timeout value to begin with. This value should normally be from 250ms to 5 seconds #define TCP_START_TIMEOUT_VAL_1 ((TICK16)TICKS_PER_SECOND * (TICK16)2) //Define ports #define TCP_LOCAL_PORT_START_NUMBER (1024ul) #define TCP_LOCAL_PORT_END_NUMBER (5000ul) //When defined, the code will be compiled for optimal speed. If not defined, code //is defined for smallest size. #define TCP_SPEED_OPTIMIZE //When defined, each TCP segment is sent twice. This might cause the remote node to //think that we timed out and retransmitted. It could thus immediately send back an //ACK and dramatically improve throuput. #define TCP_SEND_EACH_SEGMENT_TWICE //Comment following line if StackTsk should wait for acknowledgement from remote host //before transmitting another packet. Commenting following line may reduce throughput. #define TCP_NO_WAIT_FOR_ACK
#define INVALID_SOCKET (0xfeul) |
#define REMOTE_HOST | ( | s | ) | (TCB[s].remote) |
#define SIZEOF_MAC_HEADER (14ul) |
Returns the maximum size the TCP data is allowed to be. This value should never be execeed when writting data to the TCP transmit buffer before calling TCPFlush(). For slip: = MAC_TX_BUFFER_SIZE - sizeof(IP_HEADER) - sizeof(TCP_HEADER) = MAC_TX_BUFFER_SIZE - 20 - 20 = MAC_TX_BUFFER_SIZE - 40 For RTL MAC = MAC_TX_BUFFER_SIZE - SIZEOF_MAC_HEADER - sizeof(IP_HEADER) - sizeof(TCP_HEADER) = MAC_TX_BUFFER_SIZE - 14 - 20 - 20 = MAC_TX_BUFFER_SIZE - 54
#define TCP_GETARR_ALL (0x00ul) |
TCPGetArrayChr return codes
#define TCP_GETARR_END (0x10ul) |
#define TCP_GETARR_NOTREADY (0x30ul) |
#define TCP_GETARR_RETMASK (0xF0ul) |
#define TCP_GETARR_TRM (0x20ul) |
#define TCP_LOCAL_PORT_END_NUMBER (5000ul) |
#define TCP_LOCAL_PORT_START_NUMBER (1024ul) |
#define TCP_MAX_RETRY_COUNTS (3ul) |
Maximum number of times a connection be retried before closing it down.
TCP Timeout value to begin with.
#define TCPGetMaxDataLength | ( | ) | (MAC_TX_BUFFER_SIZE - SIZEOF_MAC_HEADER - sizeof(IP_HEADER) - sizeof(TCP_HEADER)) |
#define UNKNOWN_SOCKET (0xfful) |
typedef struct _SOCKET_INFO SOCKET_INFO |
Socket info. Union is used to create anonymous structure members.
typedef BYTE TCP_SOCKET |
A TCP socket. Is a number from 0-255 that identifies a TCP socket
typedef enum _TCP_STATE TCP_STATE |
TCP States as defined by rfc793
enum _TCP_STATE |
TCP_SOCKET TCPConnect | ( | NODE_INFO * | remote, | |
TCP_PORT | remotePort | |||
) |
Do an TCP active open. By default this function is not included in source. You must define STACK_CLIENT_MODE to be able to use this function.
After calling this function, the TCP stack will try and connect with the given target. The user should call the TCPIsConnected() function to see if the connection has been made. The TCP stack will continuously try and connect. It is up to the user to terminate the connection attempts if no connection is made after a certian time. The TCPDisconnect() function can be used to stop the TCP stack from trying to connect.
remote | Remote node address info | |
remotePort | remote port to be connected. |
BOOL TCPDiscard | ( | TCP_SOCKET | s | ) |
Discard any data contained in the given socket's receive buffer.
s | socket |
void TCPDisconnect | ( | TCP_SOCKET | s | ) |
A disconnect request is sent for given socket.
s | Socket to be disconnected. |
BOOL TCPFlush | ( | TCP_SOCKET | s | ) |
All and any data associated with this socket is marked as ready for transmission.
s | Socket whose data is to be transmitted. |
BOOL TCPGet | ( | TCP_SOCKET | s, | |
BYTE * | byte | |||
) |
This function reads the next byte from the current TCP packet. Reads a single byte from the given socket into the given byte pointer
s | socket | |
byte | Pointer to a byte. |
WORD TCPGetArray | ( | TCP_SOCKET | s, | |
BYTE * | buffer, | |||
WORD | count | |||
) |
Read the requested number of bytes from the given socket into the given buffer.
s | socket | |
[in] | buffer | Buffer to hold received data. |
count | Buffer length |
WORD TCPGetArrayChr | ( | TCP_SOCKET | s, | |
BYTE * | buffer, | |||
BYTE | count, | |||
BYTE | chr | |||
) |
Read bytes from the given TCP socket, and copies them to the given buffer until:
For example, when reading ASCII HTTP data from a TCP socket, we can pass a NULL terminating character to read a line of data.
s | socket | |
[in] | buffer | Buffer to hold received data. |
count | Buffer length, maximum is 255 | |
chr | Character to look for, that will terminate read |
WORD TCPGetRxBufferPos | ( | TCP_SOCKET | s | ) |
Gets the current receive buffer access pointer for given socket. This is the current offset in the TCP data. The value returned can be used as the Offset parameter for the TCPSetRxBuffer() function at a later stage to reset the receive buffer access pointer to it's current location. For example, when the current receive buffer access pointer points to the first byte of the TCP data, this function will return 0.
s | Socket |
SOCKET_INFO* TCPGetSocketInfo | ( | TCP_SOCKET | s | ) |
Returns a pointer to the SOCKET_INFO structure for the given socket.
s | socket |
void TCPInit | ( | void | ) |
Initialize all socket info. This function is called only one during lifetime of the application.
BOOL TCPIsConnected | ( | TCP_SOCKET | s | ) |
A socket is said to be connected if it is not in LISTEN and CLOSED mode. Socket may be in SYN_RCVD or FIN_WAIT_1 and may contain socket data.
s | Socket to be checked for connection. |
BOOL TCPIsGetReady | ( | TCP_SOCKET | s | ) |
A socket is said to be "Get" ready when it has already received some data. Sometime, a socket may be closed, but it still may contain data. Thus in order to ensure reuse of a socket, caller must make sure that it reads a socket, if it is ready. This function also checks that the given socket is valid, is not equal to INVALID_SOCKET for example
!!! IMPORTANT !!! The return type of this function will soon (future version) be changed from BOOL to WORD! It will return the number of available bytes. To stay compatible with future versions of this stack, make sure your code does not compare this return value to TRUE or FALSE. But, rather use code like this: "if(TCPIsGetReady(MySocket)){...}"
s | socket to test |
BOOL TCPIsPutReady | ( | TCP_SOCKET | s | ) |
Each socket maintains only one transmit buffer. Hence until a data packet is acknowledeged by remote node, socket will not be ready for next transmission. All control transmission such as Connect, Disconnect do not consume/reserve any transmit buffer. This function will check:
!!! IMPORTANT !!! The return type of this function will soon (future version) be changed from BOOL to WORD! It will return the number of available bytes. To stay compatible with future versions of this stack, make sure your code does not compare this return value to TRUE or FALSE. But, rather use code like this: "if(TCPIsPutReady(MySocket)){...}"
s | socket to test |
TCP_SOCKET TCPListen | ( | TCP_PORT | port | ) |
port | A TCP port to be opened. |
Performs TCP tasks.
remote | Remote node info | |
localIP | Local IP address | |
len | Total length of TCP semgent. |
BOOL TCPPut | ( | TCP_SOCKET | s, | |
BYTE | byte | |||
) |
Write the given byte to the given socket's transmit buffer. The data is NOT sent yet, and the TCPFlush() function must be called to send all data contained in the transmit buffer. If the transmit buffer fills up, this function will automatically call TCPFlush()! Each time before calling this function, the TCPIsPutReady() function should be called!
s | socket to use | |
byte | a data byte to send |
BOOL TCPPutArray | ( | TCP_SOCKET | s, | |
BYTE * | buffer, | |||
WORD | count | |||
) |
Given number of data bytes from the given array are put into the given socket's transmit buffer. The data is NOT sent yet, and the TCPFlush() function must be called to send all data contained in the transmit buffer.
If there is not enough space in the transmit buffer for all the data, the contents of the transmit buffer will be sent, and this function will return the actual amount of bytes that were sent. In this case, it is VERY IMPORTANT to call the TCPIsPutReady() function again before calling the TCPPut() or TCPPutArray() functions! This will however only happen if the transmit buffer fills up. The transmit buffer for TCP data is = (MAC_TX_BUFFER_SIZE - 54), which is usually 970 bytes. If writing less then this to the transmit buffer before calling TCPFlush(), then this function will always return the requested number of bytes!
s | socket to use | |
[in] | buffer | Buffer containing data that has to be sent. |
count | Number of bytes to send |
void TCPSetRxBuffer | ( | TCP_SOCKET | s, | |
WORD | offset | |||
) |
Sets the receive buffer access pointer to given offset in TCP Data. For example, if TCP data is a HTTP packet, an offset of 0 will set access to first byte of HTTP header. Layers that use TCP services (HTTP...) should call this function to set the access pointer for the current buffer.
s | Socket | |
offset | Offset, An offset with respect to TCP Data |
void TCPTick | ( | void | ) |
SOCKET_INFO TCB[MAX_SOCKETS] |
These are all the sockets supported by this TCP.