Network Component
Version 6.3
MDK-Professional Middleware for IP Networking
|
UDP socket routines enable simple IP communication using the user datagram protocol (UDP). More...
Functions | |
int32_t | udp_get_socket (uint8_t tos, uint8_t opt, net_udp_cb_t cb_func) |
Allocate a free UDP socket. | |
netStatus | udp_release_socket (int32_t socket) |
Release UDP socket and free resources. | |
netStatus | udp_open (int32_t socket, uint16_t port) |
Open UDP socket for communication. | |
netStatus | udp_close (int32_t socket) |
Stop UDP communication and close socket. | |
netStatus | udp_multicast_ttl (int32_t socket, uint8_t ttl) |
Set Time To Live value for multicast packets. | |
uint8_t * | udp_get_buf (uint32_t size) |
Allocate memory for UDP send buffer. | |
netStatus | udp_send (int32_t socket, const uint8_t *ip_addr, uint16_t port, uint8_t *buf, uint32_t len) |
Send data to a remote node. | |
typedef uint32_t(* | net_udp_cb_t )(int32_t socket, const uint8_t *ip_addr, uint16_t port, const uint8_t *buf, uint32_t len) |
UDP Event callback function. | |
UDP socket routines enable simple IP communication using the user datagram protocol (UDP).
The User Datagram Protocol (UDP) runs on top of the Internet Protocol (IP) and was developed for application that do not require reliability, acknowledgement, or flow control features at the transport layer. This simple protocol provides transport layer addressing in the form of UDP ports and an optional checksum capability.
uint32_t(* net_udp_cb_t)(int32_t socket, const uint8_t *ip_addr, uint16_t port, const uint8_t *buf, uint32_t len) |
UDP Event callback function.
[in] | socket | UDP socket handle of the local machine. |
[in] | ip_addr | Pointer to the IP address of the remote machine. |
[in] | port | UDP port number on the remote machine. |
[in] | buf | Pointer to buffer containing the received data. |
[in] | len | Number of bytes in the received packet. |
Is the type definition for the UDP callback function. The Network Core calls the callback function whenever a UDP data packet is received. Users must provide the function.
Parameter for:
netStatus udp_close | ( | int32_t | socket | ) |
Stop UDP communication and close socket.
[in] | socket | socket handle obtained with udp_get_socket. |
The udp_close function closes the UDP socket identified in the function argument. After calling the udp_close function, the UDP socket cannot send or receive any data packet.
Code Example
uint8_t * udp_get_buf | ( | uint32_t | size | ) |
Allocate memory for UDP send buffer.
[in] | size | number of bytes to allocate. |
The function udp_get_buf allocates memory for the UDP send buffer into which your application can write the outgoing data packet.
The argument size specifies the number of data bytes that the application wants to send. When the UDP frame has been sent, the Network Core automatically de-allocates the memory used by the send buffer.
Code Example
int32_t udp_get_socket | ( | uint8_t | tos, |
uint8_t | opt, | ||
net_udp_cb_t | cb_func | ||
) |
Allocate a free UDP socket.
[in] | tos | IP type of service:
|
[in] | opt | Checksum calculation option:
|
[in] | cb_func | event listening callback function. |
The function udp_get_socket allocates a free UDP socket. It initializes all state variables of the UDP socket to the default state.
The argument tos specifies the IP Type Of Service. The most common value for tos is 0.
The argument opt specifies the checksum option as shown in the following table:
Option Value | Description |
---|---|
UDP_OPT_SEND_CHECKSUM | Calculate transmit checksum |
UDP_OPT_VERIFY_CHECKSUM | Verify receive checksum |
0 | No checksum calculation |
You can also set opt to UDP_OPT_SEND_CHECKSUM|UDP_OPT_VERIFY_CHECKSUM to enable both options. This is recommended for a more secure UDP connection. You can also disable both options by setting opt to 0 for fast system reaction time.
The argument cb_func is the event callback function of the UDP socket. Users must provide this function. Refer to net_udp_cb_t.
Code Example
netStatus udp_multicast_ttl | ( | int32_t | socket, |
uint8_t | ttl | ||
) |
Set Time To Live value for multicast packets.
[in] | socket | socket handle obtained with udp_get_socket. |
[in] | ttl | number of routers the packet can cross before being expired. |
The function udp_multicast_ttl sets the time to live (TTL) value for multicast packets of an UDP socket identified with a parameter socket. The argument ttl specifies the TTL value for the UDP socket (default value is 1). A TTL value controls the number of routers the packet can cross before being expired. If this function is not called, a default TTL value of 1 is assumed for all transmitted Multicast UDP datagrams.
Code Example
netStatus udp_open | ( | int32_t | socket, |
uint16_t | port | ||
) |
Open UDP socket for communication.
[in] | socket | socket handle obtained with udp_get_socket. |
[in] | port | local port number or 0 for system assigned local port. |
The udp_open function opens the UDP socket identified by the argument socket for communication.
The argument port specifies the local port for sending and receiving data packets.
Code Example
netStatus udp_release_socket | ( | int32_t | socket | ) |
Release UDP socket and free resources.
[in] | socket | socket handle obtained with udp_get_socket. |
The function udp_release_socket releases the socket and de-allocates its memory.
Code Example
netStatus udp_send | ( | int32_t | socket, |
const uint8_t * | ip_addr, | ||
uint16_t | port, | ||
uint8_t * | buf, | ||
uint32_t | len | ||
) |
Send data to a remote node.
[in] | socket | socket handle obtained with udp_get_socket. |
[in] | ip_addr | IP address of the remote node. |
[in] | port | port number of the remote node to send data to. |
[in] | buf | buffer containing the data. |
[in] | len | length of data in bytes. |
The function udp_send sends the data packet to a remote machine. The argument socket specifies the socket handle to use for communication on the local machine. The argument ip_addr points to a buffer containing the four octets that make up the IP address of the remote machine. The argument port specifies the remote machine port to which to send the data packet. The argument buf points to the constructed UDP data packet. The argument dlen specifies the number of bytes in the data packet.
Code Example