Contiki-NG
Toggle main menu visibility
Loading...
Searching...
No Matches
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
#include "
uip.h
"
36
37
struct
tcp_socket;
38
39
typedef
enum
{
40
TCP_SOCKET_CONNECTED,
41
TCP_SOCKET_CLOSED,
42
TCP_SOCKET_TIMEDOUT,
43
TCP_SOCKET_ABORTED,
44
TCP_SOCKET_DATA_SENT
45
} tcp_socket_event_t;
46
47
/**
48
* \brief TCP data callback function
49
* \param s A pointer to a TCP socket
50
* \param ptr A user-defined pointer
51
* \param input_data_ptr A pointer to the incoming data
52
* \param input_data_len The length of the incoming data
53
* \return The function should return the number of bytes to leave in the input buffer
54
*
55
* The TCP socket input callback function gets
56
* called whenever there is new data on the socket. The
57
* function can choose to either consume the data
58
* directly, or leave it in the buffer for later. The
59
* function must return the amount of data to leave in the
60
* buffer. I.e., if the callback function consumes all
61
* incoming data, it should return 0.
62
*/
63
typedef
int (* tcp_socket_data_callback_t)(
struct
tcp_socket *s,
64
void
*ptr,
65
const
uint8_t *input_data_ptr,
66
int
input_data_len);
67
68
69
/**
70
* \brief TCP event callback function
71
* \param s A pointer to a TCP socket
72
* \param ptr A user-defined pointer
73
* \param event The event number
74
*
75
* The TCP socket event callback function gets
76
* called whenever there is an event on a socket, such as
77
* the socket getting connected or closed.
78
*/
79
typedef
void (* tcp_socket_event_callback_t)(
struct
tcp_socket *s,
80
void
*ptr,
81
tcp_socket_event_t event);
82
83
struct
tcp_socket {
84
struct
tcp_socket *next;
85
86
tcp_socket_data_callback_t input_callback;
87
tcp_socket_event_callback_t event_callback;
88
void
*ptr;
89
90
struct
process *p;
91
92
uint8_t *input_data_ptr;
93
uint8_t *output_data_ptr;
94
95
uint16_t input_data_maxlen;
96
uint16_t input_data_len;
97
uint16_t output_data_maxlen;
98
uint16_t output_data_len;
99
uint16_t output_data_send_nxt;
100
uint16_t output_senddata_len;
101
uint16_t output_data_max_seg;
102
103
uint8_t flags;
104
uint16_t listen_port;
105
struct
uip_conn
*c;
106
};
107
108
enum
{
109
TCP_SOCKET_FLAGS_NONE = 0x00,
110
TCP_SOCKET_FLAGS_LISTENING = 0x01,
111
TCP_SOCKET_FLAGS_CLOSING = 0x02,
112
};
113
114
/**
115
* \brief Register a TCP socket
116
* \param s A pointer to a TCP socket
117
* \param ptr A user-defined pointer that will be sent to callbacks for this socket
118
* \param input_databuf A pointer to a memory area this socket will use for input data
119
* \param input_databuf_len The size of the input data buffer
120
* \param output_databuf A pointer to a memory area this socket will use for outgoing data
121
* \param output_databuf_len The size of the output data buffer
122
* \param data_callback A pointer to the data callback function for this socket
123
* \param event_callback A pointer to the event callback function for this socket
124
* \retval -1 If an error occurs
125
* \retval 1 If the operation succeeds.
126
*
127
* This function registers a TCP socket. The function sets
128
* up the output and input buffers for the socket and
129
* callback pointers.
130
*
131
* TCP sockets use input and output buffers for incoming
132
* and outgoing data. The memory for these buffers must be
133
* allocated by the caller. The sizes of the buffers
134
* determine the amount of data that can be received and
135
* sent, and the principle is that the application that
136
* sets up the TCP socket will know roughly how large
137
* these buffers should be. The rule of thumb is that the
138
* input buffer should be large enough to hold the largest
139
* application layer message that the application will
140
* receive and the output buffer should be large enough to
141
* hold the largest application layer message the
142
* application will send.
143
*
144
* TCP throttles incoming data so that if the input buffer
145
* is filled, the connection will halt until the
146
* application has read out the data from the input
147
* buffer.
148
*
149
*/
150
int
tcp_socket_register(
struct
tcp_socket *s,
void
*ptr,
151
uint8_t *input_databuf,
int
input_databuf_len,
152
uint8_t *output_databuf,
int
output_databuf_len,
153
tcp_socket_data_callback_t data_callback,
154
tcp_socket_event_callback_t event_callback);
155
156
/**
157
* \brief Connect a TCP socket to a remote host
158
* \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
159
* \param ipaddr The IP address of the remote host
160
* \param port The TCP port number, in host byte order, of the remote host
161
* \retval -1 If an error occurs
162
* \retval 1 If the operation succeeds.
163
*
164
* This function connects a TCP socket to a remote host.
165
*
166
* When the socket has connected, the event callback will
167
* get called with the TCP_SOCKET_CONNECTED event. If the
168
* remote host does not accept the connection, the
169
* TCP_SOCKET_ABORTED will be sent to the callback. If the
170
* connection times out before conecting to the remote
171
* host, the TCP_SOCKET_TIMEDOUT event is sent to the
172
* callback.
173
*
174
*/
175
int
tcp_socket_connect(
struct
tcp_socket *s,
176
const
uip_ipaddr_t *
ipaddr
,
177
uint16_t port);
178
179
/**
180
* \brief Start listening on a specific port
181
* \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
182
* \param port The TCP port number, in host byte order, of the remote host
183
* \retval -1 If an error occurs
184
* \retval 1 If the operation succeeds.
185
*
186
* This function causes the TCP socket to start listening
187
* on the given TCP port.
188
*
189
* Several sockets can listen on the same port. If a
190
* remote host connects to the port, one of the listening
191
* sockets will get connected and the event callback will
192
* be called with the TCP_SOCKET_CONNECTED event. When the
193
* connection closes, the socket will go back to listening
194
* for new connections.
195
*
196
*/
197
int
tcp_socket_listen(
struct
tcp_socket *s,
198
uint16_t port);
199
200
/**
201
* \brief Stop listening for new connections
202
* \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
203
* \retval -1 If an error occurs
204
* \retval 1 If the operation succeeds.
205
*
206
* This function causes a listening TCP socket to stop
207
* listen. The socket must previously been put into listen
208
* mode with tcp_socket_listen().
209
*
210
*/
211
int
tcp_socket_unlisten(
struct
tcp_socket *s);
212
213
/**
214
* \brief Send data on a connected TCP socket
215
* \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
216
* \param dataptr A pointer to the data to be sent
217
* \param datalen The length of the data to be sent
218
* \retval -1 If an error occurs
219
* \return The number of bytes that were successfully sent
220
*
221
* This function sends data over a connected TCP
222
* socket. The data is placed in the output buffer and
223
* sent to the remote host as soon as possible. When the
224
* data has been acknowledged by the remote host, the
225
* socket's event callback is called with the event
226
* argument set to TCP_SOCKET_DATA_SENT.
227
*/
228
int
tcp_socket_send(
struct
tcp_socket *s,
229
const
uint8_t *dataptr,
230
int
datalen);
231
232
/**
233
* \brief Send a string on a connected TCP socket
234
* \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
235
* \param strptr A pointer to the string to be sent
236
* \retval -1 If an error occurs
237
* \return The number of bytes that were successfully sent
238
*
239
* This is a convenience function for sending strings on a
240
* TCP socket. The function calls tcp_socket_send() to
241
* send the string.
242
*/
243
int
tcp_socket_send_str(
struct
tcp_socket *s,
244
const
char
*strptr);
245
246
/**
247
* \brief Close a connected TCP socket
248
* \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
249
* \retval -1 If an error occurs
250
* \retval 1 If the operation succeeds.
251
*
252
* This function closes a connected TCP socket. When the
253
* socket has been successfully closed, the event callback
254
* is called with the TCP_SOCKET_CLOSED event.
255
*
256
*/
257
int
tcp_socket_close(
struct
tcp_socket *s);
258
259
/**
260
* \brief Unregister a registered socket
261
* \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
262
* \retval -1 If an error occurs
263
* \retval 1 If the operation succeeds.
264
*
265
* This function unregisters a previously registered
266
* socket. This must be done if the process will be
267
* unloaded from memory. If the TCP socket is connected,
268
* the connection will be reset.
269
*
270
*/
271
int
tcp_socket_unregister(
struct
tcp_socket *s);
272
273
/**
274
* \brief The maximum amount of data that could currently be sent
275
* \param s A pointer to a TCP socket
276
* \return The number of bytes available in the output buffer
277
*
278
* This function queries the TCP socket and returns the
279
* number of bytes available in the output buffer. This
280
* function is used before calling tcp_socket_send() to
281
* ensure that one application level message can be held
282
* in the output buffer.
283
*
284
*/
285
int
tcp_socket_max_sendlen(
struct
tcp_socket *s);
286
287
/**
288
* \brief The number of bytes waiting to be sent
289
* \param s A pointer to a TCP socket
290
* \return The number of bytes that have not yet been acknowledged by the receiver.
291
*
292
* This function queries the TCP socket and returns the
293
* number of bytes that are currently not yet known to
294
* have been successfully received by the receiver.
295
*
296
*/
297
int
tcp_socket_queuelen(
struct
tcp_socket *s);
298
299
#endif
/* TCP_SOCKET_H */
uip_conn
struct uip_conn * uip_conn
Pointer to the current TCP connection.
Definition
uip6.c:174
ipaddr
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition
uip-nd6.c:116
uip.h
Header file for the uIP TCP/IP stack.
os
net
ipv6
tcp-socket.h
Generated on
for Contiki-NG by
1.17.0