#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, | |
b | |||
) | ((a) < (b) ? (a) : (b)) |
Referenced by newdata(), senddata(), and tcp_socket_send().
|
static |
References call_event(), tcp_socket::output_data_len, tcp_socket::output_data_maxlen, tcp_socket::output_data_ptr, tcp_socket::output_data_send_nxt, printf, and TCP_SOCKET_DATA_SENT.
Referenced by appcall().
|
static |
References acked(), call_event(), tcp_socket::flags, list_head(), list_item_next(), tcp_socket::listen_port, uip_conn::lport, newdata(), NULL, tcp_socket::output_data_len, relisten(), s, senddata(), state, tcp_markconn, TCP_SOCKET_ABORTED, TCP_SOCKET_CLOSED, TCP_SOCKET_CONNECTED, TCP_SOCKET_FLAGS_CLOSING, TCP_SOCKET_FLAGS_LISTENING, TCP_SOCKET_TIMEDOUT, uip_abort, uip_aborted, uip_acked, uip_close, uip_closed, uip_connected, uip_htons(), uip_newdata, uip_poll, uip_rexmit, and uip_timedout.
Referenced by PROCESS_THREAD().
|
static |
References tcp_socket::event_callback, NULL, and tcp_socket::ptr.
|
static |
References inited, list_init(), NULL, and process_start().
LIST | ( | socketlist | ) |
|
static |
References tcp_socket::input_callback, tcp_socket::input_data_maxlen, tcp_socket::input_data_ptr, len, MIN, printf, tcp_socket::ptr, uip_appdata, and uip_datalen.
Referenced by appcall().
PROCESS_THREAD | ( | tcp_socket_process | , |
ev | , | ||
data | |||
) |
References appcall(), data, PROCESS_BEGIN, PROCESS_END, PROCESS_WAIT_EVENT, and tcpip_event.
|
static |
References tcp_socket::flags, tcp_socket::listen_port, NULL, and TCP_SOCKET_FLAGS_LISTENING.
Referenced by appcall().
|
static |
References len, MIN, tcp_socket::output_data_len, tcp_socket::output_data_ptr, tcp_socket::output_data_send_nxt, uip_mss, and uip_send().
Referenced by appcall().
int tcp_socket_close | ( | struct tcp_socket * | s | ) |
Close a connected TCP socket.
s | A pointer to a TCP socket that must have been previously registered with tcp_socket_register() |
-1 | If an error occurs |
1 | If 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.
s | A pointer to a TCP socket that must have been previously registered with tcp_socket_register() |
ipaddr | The IP address of the remote host |
port | The TCP port number, in host byte order, of the remote host |
-1 | If an error occurs |
1 | If 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.
s | A pointer to a TCP socket that must have been previously registered with tcp_socket_register() |
port | The TCP port number, in host byte order, of the remote host |
-1 | If an error occurs |
1 | If 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.
s | A pointer to a TCP socket |
ptr | A user-defined pointer that will be sent to callbacks for this socket |
input_databuf | A pointer to a memory area this socket will use for input data |
input_databuf_len | The size of the input data buffer |
output_databuf | A pointer to a memory area this socket will use for outgoing data |
output_databuf_len | The size of the output data buffer |
data_callback | A pointer to the data callback function for this socket |
event_callback | A pointer to the event callback function for this socket |
-1 | If an error occurs |
1 | If 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.
s | A pointer to a TCP socket that must have been previously registered with tcp_socket_register() |
dataptr | A pointer to the data to be sent |
datalen | The length of the data to be sent |
-1 | If an error occurs |
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.
s | A pointer to a TCP socket that must have been previously registered with tcp_socket_register() |
strptr | A pointer to the string to be sent |
-1 | If an error occurs |
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.
s | A pointer to a TCP socket that must have been previously registered with tcp_socket_register() |
-1 | If an error occurs |
1 | If 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.
s | A pointer to a TCP socket that must have been previously registered with tcp_socket_register() |
-1 | If an error occurs |
1 | If 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().