For each listening port and each connection there exists a TCP socket. Sockets are allocated using tcp_socket_alloc() and deallocated with tcp_socket_free(). Each socket has a callback function associated with it. This function is called whenever an event happens on the socket. The callback function then acts on this event.
To listen for incoming connections on a specific port, call tcp_listen() on a socket. When a remote host wants to establish a connection, an event is generated and tcp_accept() should be called to accept the connection. In this case, a new socket is automatically allocated for the connection.
To actively connect to a remote host, call tcp_connect() and wait for an event signalling a successful connection attempt.
Use tcp_write() and tcp_read() to send and receive data over a socket. Events are generated whenever data was successfully sent or received. Use tcp_buffer_used_rx() to check how much data is waiting to be read from a socket.
Files | |
file | tcp.c |
TCP implementation (license: GPLv2). | |
file | tcp.h |
TCP header (license: GPLv2). | |
file | tcp_queue.c |
TCP data queue implementation (license: GPLv2). | |
file | tcp_queue.h |
TCP data queue header (license: GPLv2). | |
Modules | |
TCP configuration | |
Enumerations | |
enum | tcp_event { TCP_EVT_NONE, TCP_EVT_ERROR, TCP_EVT_RESET, TCP_EVT_TIMEOUT, TCP_EVT_CONN_ESTABLISHED, TCP_EVT_CONN_INCOMING, TCP_EVT_CONN_CLOSED, TCP_EVT_CONN_IDLE, TCP_EVT_DATA_RECEIVED, TCP_EVT_DATA_SENT } |
Events generated by a TCP socket. More... | |
Functions | |
void | tcp_init () |
Initializes the TCP layer. | |
bool | tcp_handle_packet (const uint8_t *ip, const struct tcp_header *packet, uint16_t packet_len) |
Forwards incoming packets to the appropriate TCP socket. | |
int | tcp_socket_alloc (tcp_callback callback) |
Allocates a TCP socket. | |
bool | tcp_socket_free (int socket) |
Deallocates a TCP socket. | |
bool | tcp_connect (int socket, const uint8_t *ip, uint16_t port) |
Actively opens a connection to the given remote host and port. | |
bool | tcp_disconnect (int socket) |
Actively closes a connection. | |
bool | tcp_listen (int socket, uint16_t port) |
Starts listening on a local port for incoming connections. | |
bool | tcp_accept (int socket, tcp_callback callback) |
Accepts an incoming connection. | |
int16_t | tcp_write (int socket, const uint8_t *data, uint16_t data_len) |
Writes data for transmission to the socket. | |
int16_t | tcp_write_string_P (int socket, PGM_P str) |
Writes a string from flash memory space to the socket. | |
int16_t | tcp_read (int socket, uint8_t *buffer, uint16_t buffer_len) |
Reads data which was received over a socket. | |
int16_t | tcp_peek (int socket, uint8_t *buffer, uint16_t offset, uint16_t buffer_len) |
Reads data which was received over a socket, without actually removing it from the input buffer. | |
int16_t | tcp_skip (int socket, uint16_t len) |
Removes data from the receive buffer of a socket without reading it. | |
int16_t | tcp_reserve (int socket, uint16_t len) |
Declares unused transmit buffer space of a socket as valid. | |
uint8_t * | tcp_buffer_rx (int socket) |
Retrieves a pointer to the occupied part of the receive buffer space of a socket. | |
uint8_t * | tcp_buffer_tx (int socket) |
Retrieves a pointer to the empty part of the transmit buffer space of a socket. | |
int16_t | tcp_buffer_used_rx (int socket) |
Retrieves the number of available data bytes waiting to be read from the socket. | |
int16_t | tcp_buffer_space_rx (int socket) |
Retrieves the number of data bytes which currently can be received until the receive buffer of the socket is full. | |
int16_t | tcp_buffer_used_tx (int socket) |
Retrieves the number of data bytes which have been written to the socket but which have not been sent to the remote host or which have not been acknowledged yet. | |
int16_t | tcp_buffer_space_tx (int socket) |
Retrieves the number of data bytes which currently can be written to the socket. |
enum tcp_event |
Events generated by a TCP socket.
bool tcp_handle_packet | ( | const uint8_t * | ip, | |
const struct tcp_header * | packet, | |||
uint16_t | packet_len | |||
) |
Forwards incoming packets to the appropriate TCP socket.
true
if a matching socket was found, false
if the packet was discarded. int tcp_socket_alloc | ( | tcp_callback | callback | ) |
Allocates a TCP socket.
The given callback function will be called whenever an event is generated for the socket.
[in] | callback | A pointer to the event callback function. |
-1
on failure. bool tcp_socket_free | ( | int | socket | ) |
Deallocates a TCP socket.
After freeing the socket it can no longer be used.
[in] | socket | The identifier of the socket to deallocate. |
true
if the socket has been deallocated, false
on failure. bool tcp_connect | ( | int | socket, | |
const uint8_t * | ip, | |||
uint16_t | port | |||
) |
Actively opens a connection to the given remote host and port.
When the connection was successfully established, a TCP_EVT_CONN_ESTABLISHED event is generated for the socket.
[in] | socket | The identifier of the socket over which the connection will be established. |
[in] | ip | The buffer containing the remote IP address. |
[in] | port | The port on the remote host. |
true
if the connection initiation was started successfully, false
otherwise. bool tcp_disconnect | ( | int | socket | ) |
Actively closes a connection.
When the connection has been closed, a TCP_EVT_CONN_CLOSED event is generated for the socket.
[in] | socket | The identifier of the socket whose connection should get closed. |
true
if the connection shutdown was started successfully, false
otherwise. bool tcp_listen | ( | int | socket, | |
uint16_t | port | |||
) |
Starts listening on a local port for incoming connections.
When an incoming connection is detected, a TCP_EVT_CONN_INCOMING event is generated for the socket and tcp_accept() should be called to accept the connection.
[in] | socket | The identifier of the port on which to listen for connections. |
[in] | port | The number of the port on which to listen. |
true
if the socket now listens for connections, false
otherwise. bool tcp_accept | ( | int | socket, | |
tcp_callback | callback | |||
) |
Accepts an incoming connection.
This function has to be called in the event callback function of a listening socket when a TCP_EVT_CONN_INCOMING event is being handled. tcp_accept() returns a new socket which is from now on used to handle the new connection. The given callback function is attached to this new socket.
[in] | socket | The identifier of the listening socket for which the event was generated. |
[in] | callback | The callback function which should be attached to the new socket. |
-1
on failure. int16_t tcp_write | ( | int | socket, | |
const uint8_t * | data, | |||
uint16_t | data_len | |||
) |
Writes data for transmission to the socket.
When (part of) the data has been successfully sent and acknowledged by the remote host, a TCP_EVT_DATA_SENT event is generated for the socket.
[in] | socket | The identifier of the socket over which the data will be sent. |
[in] | data | A pointer to the buffer which contains the data to be sent. |
[in] | data_len | The number of data bytes to send. |
-1
on failure. int16_t tcp_write_string_P | ( | int | socket, | |
PGM_P | str | |||
) |
Writes a string from flash memory space to the socket.
When (part of) the data has been successfully sent and acknowledged by the remote host, a TCP_EVT_DATA_SENT event is generated for the socket.
[in] | socket | The identifier of the socket over which the data will be sent. |
[in] | str | A pointer to the string in flash memory space. |
-1
on failure. int16_t tcp_read | ( | int | socket, | |
uint8_t * | buffer, | |||
uint16_t | buffer_len | |||
) |
Reads data which was received over a socket.
This function is usually used in response to a TCP_EVT_DATA_RECEIVED event which has been generated for the socket.
[in] | socket | The identifier of the socket from which the data should be read. |
[out] | buffer | A pointer to the buffer to which the received data should be written. |
[in] | buffer_len | The maximum number of bytes to read from the socket. |
-1
on failure. int16_t tcp_peek | ( | int | socket, | |
uint8_t * | buffer, | |||
uint16_t | offset, | |||
uint16_t | buffer_len | |||
) |
Reads data which was received over a socket, without actually removing it from the input buffer.
[in] | socket | The identifier of the socket from which the data should be read. |
[out] | buffer | A pointer to the buffer to which the received data should be written. |
[in] | offset | Skip the first offset bytes before starting to read. |
[in] | buffer_len | The maximum number of bytes to read. |
-1
on failure. int16_t tcp_skip | ( | int | socket, | |
uint16_t | len | |||
) |
Removes data from the receive buffer of a socket without reading it.
[in] | socket | The identifier of the socket from which the data should be removed. |
[in] | len | The maximum number of bytes to remove. |
int16_t tcp_reserve | ( | int | socket, | |
uint16_t | len | |||
) |
Declares unused transmit buffer space of a socket as valid.
This is only useful if tcp_buffer_tx() has been used before to retrieve and fill the unused buffer space with valid data.
[in] | socket | The identifier of the socket over which the data will be sent. |
[in] | len | The number of data bytes to declare as valid. |
-1
on failure. uint8_t * tcp_buffer_rx | ( | int | socket | ) |
Retrieves a pointer to the occupied part of the receive buffer space of a socket.
The buffer is guaranteed to be linear and contiguous. Its size is retrieved with tcp_buffer_used_rx().
[in] | socket | The identifier of the socket of which to retrieve the occupied receive buffer space. |
NULL
on failure. uint8_t * tcp_buffer_tx | ( | int | socket | ) |
Retrieves a pointer to the empty part of the transmit buffer space of a socket.
The buffer is guaranteed to be linear and contiguous. Its size is retrieved with tcp_buffer_space_tx(). After writing to the buffer, be sure to call tcp_reserve() to actually add the data to the transmit buffer.
[in] | socket | The identifier of the socket of which to retrieve the empty transmit buffer space. |
NULL
on failure. int16_t tcp_buffer_used_rx | ( | int | socket | ) |
Retrieves the number of available data bytes waiting to be read from the socket.
[in] | socket | The identifier of the socket to ask for available data. |
-1
on failure. int16_t tcp_buffer_space_rx | ( | int | socket | ) |
Retrieves the number of data bytes which currently can be received until the receive buffer of the socket is full.
[in] | socket | The identifier of the socket to ask for unused receive buffer space. |
-1
on failure. int16_t tcp_buffer_used_tx | ( | int | socket | ) |
Retrieves the number of data bytes which have been written to the socket but which have not been sent to the remote host or which have not been acknowledged yet.
[in] | socket | The identifier of the socket of which to get the occupied transmit buffer space. |
-1
on failure. int16_t tcp_buffer_space_tx | ( | int | socket | ) |
Retrieves the number of data bytes which currently can be written to the socket.
[in] | socket | The identifier of the socket of which to get the available transmit buffer space. |
-1
on failure.