32#define DEBUG DEBUG_NONE
37#include "contiki-net.h"
41#include "tcp-socket.h"
47static void relisten(
struct tcp_socket *s);
51PROCESS(tcp_socket_process,
"TCP socket process");
54call_event(
struct tcp_socket *s, tcp_socket_event_t event)
56 if(s != NULL && s->event_callback != NULL) {
57 s->event_callback(s, s->ptr, event);
62senddata(
struct tcp_socket *s)
64 int len = MIN(s->output_data_max_seg,
uip_mss());
66 if(s->output_senddata_len > 0) {
67 len = MIN(s->output_senddata_len, len);
68 s->output_data_send_nxt = len;
74acked(
struct tcp_socket *s)
76 if(s->output_senddata_len > 0) {
80 if(s->output_data_send_nxt > 0) {
81 memmove(&s->output_data_ptr[0],
82 &s->output_data_ptr[s->output_data_send_nxt],
83 s->output_data_maxlen - s->output_data_send_nxt);
85 if(s->output_data_len < s->output_data_send_nxt) {
86 PRINTF(
"tcp: acked assertion failed s->output_data_len (%d) < s->output_data_send_nxt (%d)\n",
88 s->output_data_send_nxt);
91 call_event(s, TCP_SOCKET_ABORTED);
95 s->output_data_len -= s->output_data_send_nxt;
96 s->output_senddata_len = s->output_data_len;
97 s->output_data_send_nxt = 0;
99 call_event(s, TCP_SOCKET_DATA_SENT);
106 uint16_t len, copylen, bytesleft;
118 copylen = MIN(len, s->input_data_maxlen);
119 memcpy(s->input_data_ptr, dataptr, copylen);
120 if(s->input_callback) {
121 bytesleft = s->input_callback(s, s->ptr,
122 s->input_data_ptr, copylen);
127 PRINTF(
"tcp: newdata, bytesleft > 0 (%d) not implemented\n", bytesleft);
136relisten(
struct tcp_socket *s)
138 if(s != NULL && s->listen_port != 0) {
139 s->flags |= TCP_SOCKET_FLAGS_LISTENING;
146 struct tcp_socket *s = state;
148 if(s != NULL && s->c != NULL && s->c !=
uip_conn) {
163 if((s->flags & TCP_SOCKET_FLAGS_LISTENING) != 0 &&
164 s->listen_port != 0 &&
166 s->flags &= ~TCP_SOCKET_FLAGS_LISTENING;
167 s->output_data_max_seg =
uip_mss();
169 call_event(s, TCP_SOCKET_CONNECTED);
174 s->output_data_max_seg =
uip_mss();
175 call_event(s, TCP_SOCKET_CONNECTED);
190 call_event(s, TCP_SOCKET_TIMEDOUT);
196 call_event(s, TCP_SOCKET_ABORTED);
221 if(s->output_data_len == 0 && s->flags & TCP_SOCKET_FLAGS_CLOSING) {
222 s->flags &= ~TCP_SOCKET_FLAGS_CLOSING;
234 call_event(s, TCP_SOCKET_CLOSED);
255 static uint8_t inited = 0;
264tcp_socket_register(
struct tcp_socket *s,
void *ptr,
265 uint8_t *input_databuf,
int input_databuf_len,
266 uint8_t *output_databuf,
int output_databuf_len,
267 tcp_socket_data_callback_t input_callback,
268 tcp_socket_event_callback_t event_callback)
277 s->input_data_ptr = input_databuf;
278 s->input_data_maxlen = input_databuf_len;
279 s->output_data_len = 0;
280 s->output_data_ptr = output_databuf;
281 s->output_data_maxlen = output_databuf_len;
282 s->input_callback = input_callback;
283 s->event_callback = event_callback;
287 s->flags = TCP_SOCKET_FLAGS_NONE;
292tcp_socket_connect(
struct tcp_socket *s,
293 const uip_ipaddr_t *
ipaddr,
300 tcp_markconn(s->c, NULL);
313tcp_socket_listen(
struct tcp_socket *s,
320 s->listen_port = port;
324 s->flags |= TCP_SOCKET_FLAGS_LISTENING;
329tcp_socket_unlisten(
struct tcp_socket *s)
339 s->flags &= ~TCP_SOCKET_FLAGS_LISTENING;
344tcp_socket_send(
struct tcp_socket *s,
345 const uint8_t *data,
int datalen)
353 len = MIN(datalen, s->output_data_maxlen - s->output_data_len);
355 memmove(&s->output_data_ptr[s->output_data_len], data, len);
356 s->output_data_len += len;
358 if(s->output_senddata_len == 0) {
359 s->output_senddata_len = s->output_data_len;
368tcp_socket_send_str(
struct tcp_socket *s,
371 return tcp_socket_send(s, (
const uint8_t *)str, strlen(str));
375tcp_socket_close(
struct tcp_socket *s)
381 s->flags |= TCP_SOCKET_FLAGS_CLOSING;
386tcp_socket_unregister(
struct tcp_socket *s)
392 tcp_socket_unlisten(s);
401tcp_socket_max_sendlen(
struct tcp_socket *s)
403 return s->output_data_maxlen - s->output_data_len;
407tcp_socket_queuelen(
struct tcp_socket *s)
409 return s->output_data_len;
Default definitions of C compiler quirk work-arounds.
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
void list_init(list_t list)
Initialize a list.
#define LIST(name)
Declare a linked list.
void list_add(list_t list, void *item)
Add an item at the end of a list.
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
void * list_item_next(const void *item)
Get the next item following this item.
void * list_head(const_list_t list)
Get a pointer to the first element of a list.
#define PROCESS(name, strname)
Declare a process.
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
#define PROCESS_BEGIN()
Define the beginning of a process.
#define PROCESS_END()
Define the end of a process.
void process_start(struct process *p, process_data_t data)
Start a process.
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
#define PROCESS_CONTEXT_END(p)
End a context switch.
void tcp_unlisten(uint16_t port)
Close a listening TCP port.
process_event_t tcpip_event
The uIP event.
struct uip_conn * tcp_connect(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Open a TCP connection to the specified IP address and port.
void tcp_attach(struct uip_conn *conn, void *appstate)
Attach a TCP connection to the current process.
void tcpip_poll_tcp(struct uip_conn *conn)
Cause a specified TCP connection to be polled.
void tcp_listen(uint16_t port)
Open a TCP port.
void * uip_appdata
Pointer to the application data in the packet buffer.
void uip_send(const void *data, int len)
Send data on the current connection.
#define uip_datalen()
The length of any incoming data that is currently available (if available) in the uip_appdata buffer.
#define uip_newdata()
Is new incoming data available?
#define uip_poll()
Is the connection being polled by uIP?
#define uip_close()
Close the current connection.
#define uip_timedout()
Has the connection timed out?
#define uip_abort()
Abort the current connection.
#define uip_rexmit()
Do we need to retransmit previously data?
#define uip_mss()
Get the current maximum segment size that can be sent on the current connection.
#define uip_connected()
Has the connection just been connected?
#define uip_acked()
Has previously sent data been acknowledged?
#define uip_closed()
Has the connection been closed by the other end?
#define uip_aborted()
Has the connection been aborted by the other end?
uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
static void newdata(void)
Linked list manipulation routines.
Representation of a uIP TCP connection.
uint16_t lport
The local TCP port, in network byte order.
A set of debugging macros for the IP stack.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.