Microchip® Advanced Software Framework

tcp-socket.c File Reference
#include "contiki.h"
#include "contiki-net.h"
#include "lib/list.h"
#include "tcp-socket.h"
#include <stdio.h>
#include <string.h>

Macros

#define MIN(a, b)   ((a) < (b) ? (a) : (b))
 

Functions

static void acked (struct tcp_socket *s)
 
static void appcall (void *state)
 
static void call_event (struct tcp_socket *s, tcp_socket_event_t event)
 
static void init (void)
 
 LIST (socketlist)
 
static void newdata (struct tcp_socket *s)
 
 PROCESS (tcp_socket_process,"TCP socket process")
 
 PROCESS_THREAD (tcp_socket_process, ev, data)
 
static void relisten (struct tcp_socket *s)
 
static void senddata (struct tcp_socket *s)
 
int tcp_socket_close (struct tcp_socket *s)
 Close a connected TCP socket. More...
 
int tcp_socket_connect (struct tcp_socket *s, uip_ipaddr_t *ipaddr, uint16_t port)
 Connect a TCP socket to a remote host. More...
 
int tcp_socket_listen (struct tcp_socket *s, uint16_t port)
 Start listening on a specific port. More...
 
int tcp_socket_register (struct tcp_socket *s, void *ptr, uint8_t *input_databuf, int input_databuf_len, uint8_t *output_databuf, int output_databuf_len, tcp_socket_data_callback_t input_callback, tcp_socket_event_callback_t event_callback)
 Register a TCP socket. More...
 
int tcp_socket_send (struct tcp_socket *s, const uint8_t *data, int datalen)
 Send data on a connected TCP socket. More...
 
int tcp_socket_send_str (struct tcp_socket *s, const char *str)
 Send a string on a connected TCP socket. More...
 
int tcp_socket_unlisten (struct tcp_socket *s)
 Stop listening for new connections. More...
 
int tcp_socket_unregister (struct tcp_socket *s)
 Unregister a registered socket. More...
 

#define MIN (   a,
 
)    ((a) < (b) ? (a) : (b))

Referenced by newdata(), senddata(), and tcp_socket_send().

static void call_event ( struct tcp_socket s,
tcp_socket_event_t  event 
)
static

References tcp_socket::event_callback, NULL, and tcp_socket::ptr.

Referenced by acked(), and appcall().

static void init ( void  )
static

References inited, list_init(), NULL, and process_start().

LIST ( socketlist  )
PROCESS ( tcp_socket_process  ,
"TCP socket process  
)
PROCESS_THREAD ( tcp_socket_process  ,
ev  ,
data   
)
static void relisten ( struct tcp_socket s)
static
static void senddata ( struct tcp_socket s)
static
int tcp_socket_close ( struct tcp_socket s)

Close a connected TCP socket.

Parameters
sA pointer to a TCP socket that must have been previously registered with tcp_socket_register()
Return values
-1If an error occurs
1If the operation succeeds.
        This function closes a connected TCP socket. When the
        socket has been successfully closed, the event callback
        is called with the TCP_SOCKET_CLOSED event.

References tcp_socket::flags, NULL, and TCP_SOCKET_FLAGS_CLOSING.

Referenced by http_socket_close(), input_pt(), parse_header_byte(), and PROCESS_THREAD().

int tcp_socket_connect ( struct tcp_socket s,
uip_ipaddr_t ipaddr,
uint16_t  port 
)

Connect a TCP socket to a remote host.

Parameters
sA pointer to a TCP socket that must have been previously registered with tcp_socket_register()
ipaddrThe IP address of the remote host
portThe TCP port number, in host byte order, of the remote host
Return values
-1If an error occurs
1If the operation succeeds.
        This function connects a TCP socket to a remote host.

        When the socket has connected, the event callback will
        get called with the TCP_SOCKET_CONNECTED event. If the
        remote host does not accept the connection, the
        TCP_SOCKET_ABORTED will be sent to the callback. If the
        connection times out before conecting to the remote
        host, the TCP_SOCKET_TIMEDOUT event is sent to the
        callback.

References tcp_socket::c, NULL, PROCESS_CONTEXT_BEGIN, PROCESS_CONTEXT_END, tcp_connect(), and uip_htons().

Referenced by start_request().

int tcp_socket_listen ( struct tcp_socket s,
uint16_t  port 
)

Start listening on a specific port.

Parameters
sA pointer to a TCP socket that must have been previously registered with tcp_socket_register()
portThe TCP port number, in host byte order, of the remote host
Return values
-1If an error occurs
1If the operation succeeds.
        This function causes the TCP socket to start listening
        on the given TCP port.

        Several sockets can listen on the same port. If a
        remote host connects to the port, one of the listening
        sockets will get connected and the event callback will
        be called with the TCP_SOCKET_CONNECTED event. When the
        connection closes, the socket will go back to listening
        for new connections.

References tcp_socket::flags, tcp_socket::listen_port, NULL, port, PROCESS_CONTEXT_BEGIN, PROCESS_CONTEXT_END, tcp_listen(), TCP_SOCKET_FLAGS_LISTENING, and uip_htons().

int tcp_socket_register ( struct tcp_socket s,
void *  ptr,
uint8_t *  input_databuf,
int  input_databuf_len,
uint8_t *  output_databuf,
int  output_databuf_len,
tcp_socket_data_callback_t  data_callback,
tcp_socket_event_callback_t  event_callback 
)

Register a TCP socket.

Parameters
sA pointer to a TCP socket
ptrA user-defined pointer that will be sent to callbacks for this socket
input_databufA pointer to a memory area this socket will use for input data
input_databuf_lenThe size of the input data buffer
output_databufA pointer to a memory area this socket will use for outgoing data
output_databuf_lenThe size of the output data buffer
data_callbackA pointer to the data callback function for this socket
event_callbackA pointer to the event callback function for this socket
Return values
-1If an error occurs
1If the operation succeeds.
        This function registers a TCP socket. The function sets
        up the output and input buffers for the socket and
        callback pointers.

        TCP sockets use input and output buffers for incoming
        and outgoing data. The memory for these buffers must be
        allocated by the caller. The size of the buffers
        determine the amount of data that can be received and
        sent, and the principle is that the application that
        sets up the TCP socket will know roughly how large
        these buffers should be. The rule of thumb is that the
        input buffer should be large enough to hold the largest
        application layer message that the application will
        receive and the output buffer should be large enough to
        hold the largest application layer message the
        application will send.

        TCP throttles incoming data so that if the input buffer
        is filled, the connection will halt until the
        application has read out the data from the input
        buffer.

References tcp_socket::event_callback, tcp_socket::flags, init, tcp_socket::input_callback, input_callback, tcp_socket::input_data_maxlen, tcp_socket::input_data_ptr, list_add(), tcp_socket::listen_port, NULL, tcp_socket::output_data_maxlen, tcp_socket::output_data_ptr, tcp_socket::ptr, ptr, and TCP_SOCKET_FLAGS_NONE.

Referenced by http_socket_get(), and http_socket_post().

int tcp_socket_send ( struct tcp_socket s,
const uint8_t *  dataptr,
int  datalen 
)

Send data on a connected TCP socket.

Parameters
sA pointer to a TCP socket that must have been previously registered with tcp_socket_register()
dataptrA pointer to the data to be sent
datalenThe length of the data to be sent
Return values
-1If an error occurs
Returns
The number of bytes that were successfully sent
        This function sends data over a connected TCP
        socket. The data is placed in the output buffer and
        sent to the remote host as soon as possiblce. When the
        data has been acknowledged by the remote host, the
        event callback is sent with the TCP_SOCKET_DATA_SENT
        event.

References len, MIN, NULL, tcp_socket::output_data_len, tcp_socket::output_data_maxlen, and tcp_socket::output_data_ptr.

Referenced by event(), and tcp_socket_send_str().

int tcp_socket_send_str ( struct tcp_socket s,
const char *  strptr 
)

Send a string on a connected TCP socket.

Parameters
sA pointer to a TCP socket that must have been previously registered with tcp_socket_register()
strptrA pointer to the string to be sent
Return values
-1If an error occurs
Returns
The number of bytes that were successfully sent
        This is a convenience function for sending strings on a
        TCP socket. The function calls 
() to send the string.

References tcp_socket_send().

Referenced by event().

int tcp_socket_unlisten ( struct tcp_socket s)

Stop listening for new connections.

Parameters
sA pointer to a TCP socket that must have been previously registered with tcp_socket_register()
Return values
-1If an error occurs
1If the operation succeeds.
        This function causes a listening TCP socket to stop
        listen. The socket must previously been put into listen
        mode with tcp_socket_listen().

References tcp_socket::flags, tcp_socket::listen_port, NULL, PROCESS_CONTEXT_BEGIN, PROCESS_CONTEXT_END, TCP_SOCKET_FLAGS_LISTENING, tcp_unlisten(), and uip_htons().

Referenced by tcp_socket_unregister().

int tcp_socket_unregister ( struct tcp_socket s)

Unregister a registered socket.

Parameters
sA pointer to a TCP socket that must have been previously registered with tcp_socket_register()
Return values
-1If an error occurs
1If the operation succeeds.
        This function unregisters a previously registered
        socket. This must be done if the process will be
        unloaded from memory. If the TCP socket is connected,
        the connection will be reset.

References tcp_socket::c, list_remove(), NULL, tcp_attach(), and tcp_socket_unlisten().