#include "net\ip.h"
#include "net\mac.h"
//********************************************************************* //-------------------- UDP Configuration -------------------- //********************************************************************* //When defined, the code will be compiled for optimal speed. If not defined, code is defined for smallest size. #define UDP_SPEED_OPTIMIZE
#define INVALID_UDP_PORT (0ul) |
#define INVALID_UDP_SOCKET (0xfful) |
#define SIZEOF_MAC_HEADER (14ul) |
Returns the maximum size the UDP data is allowed to be. This value should never be execeed when writting data to the UDP transmit buffer before calling UDPFlush(). For slip: = MAC_TX_BUFFER_SIZE - sizeof(IP_HEADER) - sizeof(UDP_HEADER) = MAC_TX_BUFFER_SIZE - 20 - 8 = MAC_TX_BUFFER_SIZE - 28 For RTL MAC = MAC_TX_BUFFER_SIZE - SIZEOF_MAC_HEADER - sizeof(IP_HEADER) - sizeof(UDP_HEADER) = MAC_TX_BUFFER_SIZE - 14 - 20 - 8 = MAC_TX_BUFFER_SIZE - 42
#define UDPGetMaxDataLength | ( | ) | (MAC_TX_BUFFER_SIZE - SIZEOF_MAC_HEADER - sizeof(IP_HEADER) - sizeof(UDP_HEADER)) |
#define UDPSetRxBuffer | ( | a | ) | IPSetRxBuffer(a+sizeof(UDP_HEADER)) |
Sets the receive buffer access point to given offset in UDP Data. For example, if UDP data is a HTTP message, an offset of 0 will set access to first byte of HTTP message (UDP data). Layers that use UDP services (HTTP) should call this macro to set the access pointer for the current buffer.
a | Offset, An offset with respect to UDP Data |
#define UDPSetTxBuffer | ( | buffer, | |||
offset | ) | (UDPSocketInfo[activeUDPSocket].TxOffset = offset, IPSetTxBuffer(buffer, offset+sizeof(UDP_HEADER))) |
This function makes the given transmit buffer active, and sets it's access pointer to be:
buffer | Buffer identifier | |
offset | Offset |
typedef struct _UDP_HEADER UDP_HEADER |
UDP Packet header
typedef BYTE UDP_SOCKET |
Handle to a UDP socket
typedef struct _UDP_SOCKET_INFO UDP_SOCKET_INFO |
UDP Socket - is 22 bytes long
void UDPClose | ( | UDP_SOCKET | s | ) |
Given socket is marked as available for future new communcations.
This function does not affect previous active UDP socket designation.
s | Socket that is to be closed. |
void UDPDiscard | ( | void | ) |
This function discards an active UDP socket content.
void UDPFlush | ( | void | ) |
This function transmit all data from an active UDP socket.
Get a byte of data.
Note: This function fetches data from the currently active UDP socket. The active UDP socket is set by the UDPIsGetReady() call.
[out] | v | Buffer to receive UDP data byte |
Read the requested number of bytes from the active UDP socket into the given buffer.
Note: This function fetches data from an active UDP socket as set by UDPIsGetReady() call.
[in] | buffer | Buffer to hold received data. |
count | Buffer length |
NODE_INFO* UDPGetNodeInfo | ( | void | ) |
Get's the MAC and IP address of the currently active UDP socket. This data is assigned the MAC and IP address of the remote node after a UDP message has been received. So, the MAC and IP address returned by this function will only be valid if a UDP message has already been received on this socket! The active socket is set by the UDPIsGetReady() call.
void UDPInit | ( | void | ) |
Initializes internal variables.
BOOL UDPIsGetReady | ( | UDP_SOCKET | s | ) |
Check if given socket contains any data that is ready to be read, and that the given socket is valid (not equal to INVALID_UDP_SOCKET for example). It also sets the given socket as the active UDP socket.
Side Effects: Given socket is set as the active UDP Socket.
Note: This function automatically sets supplied socket as the active socket. All subsequent calls will us this socket as the active socket.
s | A valid UDP socket that is already "Listen"ed on or opened. |
WORD UDPIsGetReady_ | ( | UDP_SOCKET | s | ) |
UDP function that returns number of UDP bytes that are available
s | A valid UDP socket that is already "Listen"ed on or opened. |
BOOL UDPIsPutReady | ( | UDP_SOCKET | s | ) |
Checks if there is a transmit buffer ready for accepting data, and that the given socket is valid (not equal to INVALID_UDP_SOCKET for example).
Side Effects: Given socket is set as the active UDP Socket.
Note: This function automatically sets supplied socket as the active socket. All subsequent calls will us this socket as the active socket.
s | Socket that is to be loaded and made an active UDP socket. |
UDP_SOCKET UDPOpen | ( | UDP_PORT | localPort, | |
NODE_INFO * | remoteNode, | |||
UDP_PORT | remotePort | |||
) |
A UDP packet header is assembled and loaded into UDP transmit buffer.
A localPort value of '0' is considered nonexistent port. This call must always have nonzero localPort value. This function sets returned socket as an active UDP socket.
localPort | A non-zero port number. | |
remoteNode | Remote Node info such as MAC and IP address. If NULL, broadcast node address is set. | |
remotePort | Remote Port to which to talk to. If INVALID_UDP_PORT, localPort is opened for Listen. |
Performs UDP related tasks. Must continuesly be called.
remoteNode | Remote node info | |
len | Total length of UDP semgent. | |
localIP | The IP Address of the currently received packet. |
Given data byte is put into the UDP transmit buffer and active UDP socket buffer length is incremented. The data is NOT sent yet, and the UDPFlush() function must be called to send all data contained in the transmit buffer.
If the transmit buffer filled up, the contents of the transmit buffer will be sent, and this function will return FALSE. In this case, it is VERY IMPORTANT to call the UDPIsPutReady() function again before calling the UDPPut() or UDPPutArray() functions!
Note: This function loads data into an active UDP socket as determined by previous call to UDPIsPutReady().
v | - Data byte to loaded into transmit buffer |
Given number of data bytes from the given array are put into the UDP transmit buffer and active UDP socket buffer length is incremented by number of bytes. The data is NOT sent yet, and the UDPFlush() 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 UDPIsPutReady() function again before calling the UDPPut() or UDPPutArray() functions! This will however only happen if the transmit buffer fills up. The transmit buffer for UDP data is = (MAC_TX_BUFFER_SIZE - 42), which is usually 982 bytes. If writing less then this to the transmit buffer before calling UDPFlush(), then this function will always return the requested number of bytes!
Note: This function loads data into an active UDP socket as determined by previous call to UDPIsPutReady().
[in] | buffer | Buffer containing data that has to be sent. |
count | Number of bytes to send |
UDP_SOCKET_INFO UDPSocketInfo[MAX_UDP_SOCKETS] |