Contiki 3.x
tcp-socket.h
1 /*
2  * Copyright (c) 2012-2014, Thingsquare, http://www.thingsquare.com/.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 #ifndef TCP_SOCKET_H
33 #define TCP_SOCKET_H
34 
35 struct tcp_socket;
36 
37 typedef enum {
38  TCP_SOCKET_CONNECTED,
39  TCP_SOCKET_CLOSED,
40  TCP_SOCKET_TIMEDOUT,
41  TCP_SOCKET_ABORTED,
42  TCP_SOCKET_DATA_SENT
43 } tcp_socket_event_t;
44 
45 /**
46  * \brief TCP data callback function
47  * \param s A pointer to a TCP socket
48  * \param ptr A user-defined pointer
49  * \param input_data_ptr A pointer to the incoming data
50  * \param input_data_len The length of the incoming data
51  * \return The function should return the number of bytes to leave in the input buffer
52  *
53  * The TCP socket input callback function gets
54  * called whenever there is new data on the socket. The
55  * function can choose to either consume the data
56  * directly, or leave it in the buffer for later. The
57  * function must return the amount of data to leave in the
58  * buffer. I.e., if the callback function consumes all
59  * incoming data, it should return 0.
60  */
61 typedef int (* tcp_socket_data_callback_t)(struct tcp_socket *s,
62  void *ptr,
63  const uint8_t *input_data_ptr,
64  int input_data_len);
65 
66 
67 /**
68  * \brief TCP event callback function
69  * \param s A pointer to a TCP socket
70  * \param ptr A user-defined pointer
71  * \param event The event number
72  *
73  * The TCP socket event callback function gets
74  * called whenever there is an event on a socket, such as
75  * the socket getting connected or closed.
76  */
77 typedef void (* tcp_socket_event_callback_t)(struct tcp_socket *s,
78  void *ptr,
79  tcp_socket_event_t event);
80 
81 struct tcp_socket {
82  struct tcp_socket *next;
83 
84  tcp_socket_data_callback_t input_callback;
85  tcp_socket_event_callback_t event_callback;
86  void *ptr;
87 
88  struct process *p;
89 
90  uint8_t *input_data_ptr;
91  uint8_t *output_data_ptr;
92 
93  uint16_t input_data_maxlen;
94  uint16_t input_data_len;
95  uint16_t output_data_maxlen;
96  uint16_t output_data_len;
97  uint16_t output_data_send_nxt;
98 
99  uint8_t flags;
100  uint16_t listen_port;
101  struct uip_conn *c;
102 };
103 
104 enum {
105  TCP_SOCKET_FLAGS_NONE = 0x00,
106  TCP_SOCKET_FLAGS_LISTENING = 0x01,
107  TCP_SOCKET_FLAGS_CLOSING = 0x02,
108 };
109 
110 /**
111  * \brief Register a TCP socket
112  * \param s A pointer to a TCP socket
113  * \param ptr A user-defined pointer that will be sent to callbacks for this socket
114  * \param input_databuf A pointer to a memory area this socket will use for input data
115  * \param input_databuf_len The size of the input data buffer
116  * \param output_databuf A pointer to a memory area this socket will use for outgoing data
117  * \param output_databuf_len The size of the output data buffer
118  * \param data_callback A pointer to the data callback function for this socket
119  * \param event_callback A pointer to the event callback function for this socket
120  * \retval -1 If an error occurs
121  * \retval 1 If the operation succeeds.
122  *
123  * This function registers a TCP socket. The function sets
124  * up the output and input buffers for the socket and
125  * callback pointers.
126  *
127  * TCP sockets use input and output buffers for incoming
128  * and outgoing data. The memory for these buffers must be
129  * allocated by the caller. The size of the buffers
130  * determine the amount of data that can be received and
131  * sent, and the principle is that the application that
132  * sets up the TCP socket will know roughly how large
133  * these buffers should be. The rule of thumb is that the
134  * input buffer should be large enough to hold the largest
135  * application layer message that the application will
136  * receive and the output buffer should be large enough to
137  * hold the largest application layer message the
138  * application will send.
139  *
140  * TCP throttles incoming data so that if the input buffer
141  * is filled, the connection will halt until the
142  * application has read out the data from the input
143  * buffer.
144  *
145  */
146 int tcp_socket_register(struct tcp_socket *s, void *ptr,
147  uint8_t *input_databuf, int input_databuf_len,
148  uint8_t *output_databuf, int output_databuf_len,
149  tcp_socket_data_callback_t data_callback,
150  tcp_socket_event_callback_t event_callback);
151 
152 /**
153  * \brief Connect a TCP socket to a remote host
154  * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
155  * \param ipaddr The IP address of the remote host
156  * \param port The TCP port number, in host byte order, of the remote host
157  * \retval -1 If an error occurs
158  * \retval 1 If the operation succeeds.
159  *
160  * This function connects a TCP socket to a remote host.
161  *
162  * When the socket has connected, the event callback will
163  * get called with the TCP_SOCKET_CONNECTED event. If the
164  * remote host does not accept the connection, the
165  * TCP_SOCKET_ABORTED will be sent to the callback. If the
166  * connection times out before conecting to the remote
167  * host, the TCP_SOCKET_TIMEDOUT event is sent to the
168  * callback.
169  *
170  */
171 int tcp_socket_connect(struct tcp_socket *s,
172  uip_ipaddr_t *ipaddr,
173  uint16_t port);
174 
175 /**
176  * \brief Start listening on a specific port
177  * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
178  * \param port The TCP port number, in host byte order, of the remote host
179  * \retval -1 If an error occurs
180  * \retval 1 If the operation succeeds.
181  *
182  * This function causes the TCP socket to start listening
183  * on the given TCP port.
184  *
185  * Several sockets can listen on the same port. If a
186  * remote host connects to the port, one of the listening
187  * sockets will get connected and the event callback will
188  * be called with the TCP_SOCKET_CONNECTED event. When the
189  * connection closes, the socket will go back to listening
190  * for new connections.
191  *
192  */
193 int tcp_socket_listen(struct tcp_socket *s,
194  uint16_t port);
195 
196 /**
197  * \brief Stop listening for new connections
198  * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
199  * \retval -1 If an error occurs
200  * \retval 1 If the operation succeeds.
201  *
202  * This function causes a listening TCP socket to stop
203  * listen. The socket must previously been put into listen
204  * mode with tcp_socket_listen().
205  *
206  */
207 int tcp_socket_unlisten(struct tcp_socket *s);
208 
209 /**
210  * \brief Send data on a connected TCP socket
211  * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
212  * \param dataptr A pointer to the data to be sent
213  * \param datalen The length of the data to be sent
214  * \retval -1 If an error occurs
215  * \return The number of bytes that were successfully sent
216  *
217  * This function sends data over a connected TCP
218  * socket. The data is placed in the output buffer and
219  * sent to the remote host as soon as possiblce. When the
220  * data has been acknowledged by the remote host, the
221  * event callback is sent with the TCP_SOCKET_DATA_SENT
222  * event.
223  */
224 int tcp_socket_send(struct tcp_socket *s,
225  const uint8_t *dataptr,
226  int datalen);
227 
228 /**
229  * \brief Send a string on a connected TCP socket
230  * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
231  * \param strptr A pointer to the string to be sent
232  * \retval -1 If an error occurs
233  * \return The number of bytes that were successfully sent
234  *
235  * This is a convenience function for sending strings on a
236  * TCP socket. The function calls tcp_socket_send() to
237  * send the string.
238  */
239 int tcp_socket_send_str(struct tcp_socket *s,
240  const char *strptr);
241 
242 /**
243  * \brief Close a connected TCP socket
244  * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
245  * \retval -1 If an error occurs
246  * \retval 1 If the operation succeeds.
247  *
248  * This function closes a connected TCP socket. When the
249  * socket has been successfully closed, the event callback
250  * is called with the TCP_SOCKET_CLOSED event.
251  *
252  */
253 int tcp_socket_close(struct tcp_socket *s);
254 
255 /**
256  * \brief Unregister a registered socket
257  * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
258  * \retval -1 If an error occurs
259  * \retval 1 If the operation succeeds.
260  *
261  * This function unregisters a previously registered
262  * socket. This must be done if the process will be
263  * unloaded from memory. If the TCP socket is connected,
264  * the connection will be reset.
265  *
266  */
267 int tcp_socket_unregister(struct tcp_socket *s);
268 #endif /* TCP_SOCKET_H */
Representation of a uIP TCP connection.
Definition: uip.h:1336