Microchip® Advanced Software Framework

udp-socket.h File Reference
#include "net/ip/uip.h"

Data Structures

struct  udp_socket
 

Typedefs

typedef void(* udp_socket_input_callback_t )(struct udp_socket *c, void *ptr, const uip_ipaddr_t *source_addr, uint16_t source_port, const uip_ipaddr_t *dest_addr, uint16_t dest_port, const uint8_t *data, uint16_t datalen)
 A UDP socket callback function. More...
 

Functions

int udp_socket_bind (struct udp_socket *c, uint16_t local_port)
 Bind a UDP socket to a local port. More...
 
int udp_socket_close (struct udp_socket *c)
 Close a UDP socket. More...
 
int udp_socket_connect (struct udp_socket *c, uip_ipaddr_t *remote_addr, uint16_t remote_port)
 Bind a UDP socket to a remote address and port. More...
 
int udp_socket_register (struct udp_socket *c, void *ptr, udp_socket_input_callback_t receive_callback)
 Register a UDP socket. More...
 
int udp_socket_send (struct udp_socket *c, const void *data, uint16_t datalen)
 Send data on a UDP socket. More...
 
int udp_socket_sendto (struct udp_socket *c, const void *data, uint16_t datalen, const uip_ipaddr_t *addr, uint16_t port)
 Send data on a UDP socket to a specific address and port. More...
 

typedef void(* udp_socket_input_callback_t)(struct udp_socket *c, void *ptr, const uip_ipaddr_t *source_addr, uint16_t source_port, const uip_ipaddr_t *dest_addr, uint16_t dest_port, const uint8_t *data, uint16_t datalen)

A UDP socket callback function.

Parameters
cA pointer to the struct udp_socket that received the data
ptrAn opaque pointer that was specified when the UDP socket was registered with udp_socket_register()
source_addrThe IP address from which the datagram was sent
source_portThe UDP port number, in host byte order, from which the datagram was sent
dest_addrThe IP address that this datagram was sent to
dest_portThe UDP port number, in host byte order, that the datagram was sent to
dataA pointer to the data contents of the UDP datagram
datalenThe length of the data being pointed to by the data pointer
        Each UDP socket has a callback function that is
        registered as part of the call to
        udp_socket_register(). The callback function gets
        called every time a UDP packet is received.

int udp_socket_bind ( struct udp_socket c,
uint16_t  local_port 
)

Bind a UDP socket to a local port.

Parameters
cA pointer to the struct udp_socket that should be bound to a local port
local_portThe UDP port number, in host byte order, to bind the UDP socket to
Return values
-1Binding the UDP socket to the local port failed
1Binding the UDP socket to the local port succeeded
        This function binds the UDP socket to a local port so
        that it will begin to receive data that arrives on the
        specified port. A UDP socket will receive data
        addressed to the specified port number on any IP
        address of the host.

        A UDP socket that is bound to a local port will use
        this port number as a source port in outgoing UDP
        messages.

References NULL, udp_bind, udp_socket::udp_conn, and UIP_HTONS.

Referenced by httpd_init().

int udp_socket_close ( struct udp_socket c)

Close a UDP socket.

Parameters
cA pointer to the struct udp_socket to be closed
Return values
-1If closing the UDP socket failed
1If closing the UDP socket succeeded
        This function closes a UDP socket that has previously
        been registered with udp_socket_register(). All
        registered UDP sockets must be closed before exiting
        the process that registered them, or undefined behavior
        may occur.

References NULL, udp_socket::udp_conn, and uip_udp_remove.

int udp_socket_connect ( struct udp_socket c,
uip_ipaddr_t remote_addr,
uint16_t  remote_port 
)

Bind a UDP socket to a remote address and port.

Parameters
cA pointer to the struct udp_socket that should be connected
remote_addrThe IP address of the remote host, or NULL if the UDP socket should only be connected to a specific port
remote_portThe UDP port number, in host byte order, to which the UDP socket should be connected
Return values
-1Connecting the UDP socket failed
1Connecting the UDP socket succeeded
        This function connects the UDP socket to a specific
        remote port and optional remote IP address. When a UDP
        socket is connected to a remote port and address, it
        will only receive packets that are sent from the remote
        port and address. When sending data over a connected
        UDP socket, the data will be sent to the connected
        remote address.

        A UDP socket can be connected to a remote port, but not
        a remote IP address, by providing a NULL parameter as
        the remote_addr parameter. This lets the UDP socket
        receive data from any IP address on the specified port.

References NULL, uip_udp_conn::ripaddr, uip_udp_conn::rport, udp_socket::udp_conn, UIP_HTONS, and uip_ipaddr_copy.

int udp_socket_register ( struct udp_socket c,
void *  ptr,
udp_socket_input_callback_t  receive_callback 
)

Register a UDP socket.

Parameters
cA pointer to the struct udp_socket that should be registered
ptrAn opaque pointer that will be passed to callbacks
receive_callbackA function pointer to the callback function that will be called when data arrives
Return values
-1The registration failed
1The registration succeeded
        This function registers the UDP socket with the
        system. A UDP socket must be registered before any data
        can be sent or received over the socket.

        The caller must allocate memory for the struct
        udp_socket that is to be registered.

        A UDP socket can begin to receive data by calling
        udp_socket_bind().

References init, udp_socket::input_callback, input_callback, NULL, udp_socket::p, PROCESS_CONTEXT_BEGIN, PROCESS_CONTEXT_END, PROCESS_CURRENT, udp_socket::ptr, ptr, udp_socket::udp_conn, and udp_new().

Referenced by httpd_init().

int udp_socket_send ( struct udp_socket c,
const void *  data,
uint16_t  datalen 
)

Send data on a UDP socket.

Parameters
cA pointer to the struct udp_socket on which the data should be sent
dataA pointer to the data that should be sent
datalenThe length of the data to be sent
Returns
The number of bytes sent, or -1 if an error occurred
        This function sends data over a UDP socket. The UDP
        socket must have been connected to a remote address and
        port with udp_socket_connect().

References NULL, udp_socket::udp_conn, and uip_udp_packet_send().

int udp_socket_sendto ( struct udp_socket c,
const void *  data,
uint16_t  datalen,
const uip_ipaddr_t addr,
uint16_t  port 
)

Send data on a UDP socket to a specific address and port.

Parameters
cA pointer to the struct udp_socket on which the data should be sent
dataA pointer to the data that should be sent
datalenThe length of the data to be sent
addrThe IP address to which the data should be sent
portThe UDP port number, in host byte order, to which the data should be sent
Returns
The number of bytes sent, or -1 if an error occurred
        This function sends data over a UDP socket to a
        specific address and port.

        The UDP socket does not have to be connected to use
        this function.

References NULL, udp_socket::udp_conn, UIP_HTONS, and uip_udp_packet_sendto().