Functions | |
int32_t | user_callback (SOCKET *so, void *data, uint32_t len, uint32_t ec) |
This function is called each time new data is received on a specific socket or the error status has changed (MSB set in error code) More... | |
SOCKET * | socket_open (char *dest_addr, uint16_t dest_port, uint16_t src_port, uint8_t data_type, int32_t(*init_func)(SOCKET *)) |
Create a new socket. More... | |
int32_t | socket_close (SOCKET *so) |
Close a socket and free all associated memory. More... | |
void | socket_define_callback (SOCKET *so, int32_t(*call_back_function)(SOCKET *, void *, uint32_t, uint32_t), void *data, uint16_t maxdata) |
Install a user callback function for a specific socket. More... | |
void * | socket_get_data_pointer (SOCKET *so) |
Retrieve a data pointer from socket. More... | |
uint32_t | set_socket_option (SOCKET *so, uint32_t option) |
Modify socket options. More... | |
so | - socket |
data | - pointer to received data |
len | - received data length |
ec | - error code |
Use a callback function for event notification on a specific socket. The advantage is that no additional net_recv() function call has to be performed and the latency from the moment a packet is read from Ethernet to the moment it is signalled to the user is minimized.
The user callback function is assigned to a specific socket with function socket_define_callback().
After a callback function was assigned to a socket, function net_isq() calls this function any time new data is received on this socket or an error condition has occurred and needs to be signalled to the user.
The user is responsible to maintain the receive buffer. That means after returning from a callback function the main network process (net_isq()) re-uses this buffer. If a buffer cannot be processed immediately and is flagged for later processing somewhere in the application, a new buffer must be assigned with set_recv_buffer() to prevent the net_isq() to overwrite the buffer content with new incoming data.
Example TCP callback function:
Example UDP callback function
SOCKET * socket_open | ( | char * | dest_addr, |
uint16_t | dest_port, | ||
uint16_t | src_port, | ||
uint8_t | data_type, | ||
int32_t(*)(SOCKET *) | init_func | ||
) |
dest_addr | - Destination address string (zero terminated)
|
dest_port | - Destination port
|
src_port | - Source (local) port |
data_type | - Type of data to be transmitted/received
|
init_func | - Function to initialize socket, use UDP_INIT_FUNC or TCP_INIT_FUNC |
A socket describes either a server or a client connection. As a server, the DSP waits for incoming data on its local port (parameter src_port). If ANY_IP was specified for the dest_addr parameter, any client can access this server. If a host name or IP address was specified, only the specified client can access this service. The response of the DSP server is sent to the port specified as dest_port. Typically a server socket will use ANY_PORT and let the client specify its preferred port.
A client socket will connect to a specific server and a specific port, ANY_IP and ANY_PORT cannot be used as dest_addr and dest_port parameters for client sockets. As a client, the DSP sends data to the specified server and the specified port (dest_addr and dest_port), and expects a response (if any) on the src_port. If ANY_PORT is specified as src_port, the TCP/IP software will determine a suitable port itself.
The last parameter, init_func, is a pointer to a function which handles either the UDP or TCP protocol. Use only the pre-defined UDP_INIT_FUNC or - for a TCP socket - TCP_INIT_FUNC. This technique results in only the actually used protocol code being loaded from the netlib library and helps to reduce code size.
Example:
create an UDP socket to receive the current time from a network time server which is running on a machine with IP address 192.168.1.12 on UDP port 13 (well known port for time server)
int32_t socket_close | ( | SOCKET * | so | ) |
so | - the socket to close |
If a socket is no longer used, it can be closed to free the memory occupied by the socket structure.
void socket_define_callback | ( | SOCKET * | so, |
int32_t(*)(SOCKET *, void *, uint32_t, uint32_t) | call_back_function, | ||
void * | data, | ||
uint16_t | maxdata | ||
) |
Define a callback function for event notification of a specific socket. Possible events are new received data or socket errors. This function calls set_recv_buffer() with parameters so, data and maxdata to pre-initialize a receive buffer.
so | - Socket for callback |
call_back_function | - User defined call back function or NULL |
data | - pointer to receive buffer or NULL |
maxdata | - receive buffer size |
Example:
or disabling a receive function:
void * socket_get_data_pointer | ( | SOCKET * | so | ) |
so | - the socket to modify |
option | - options, use SO_xxx constants |
This function is used to specify non-standard socket options. Currently implemented are