Contiki 2.5
Main Page
Related Pages
Modules
Data Structures
Files
Examples
File List
Globals
core
net
psock.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2004, Swedish Institute of Computer Science.
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 Institute nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*
29
* This file is part of the Contiki operating system.
30
*
31
* Author: Adam Dunkels <adam@sics.se>
32
*
33
* $Id: psock.h,v 1.8 2010/06/15 14:19:22 nifi Exp $
34
*/
35
36
/**
37
* \addtogroup uip
38
* @{
39
*/
40
41
/**
42
* \defgroup psock Protosockets library
43
* @{
44
*
45
* The protosocket library provides an interface to the uIP stack that is
46
* similar to the traditional BSD socket interface. Unlike programs
47
* written for the ordinary uIP event-driven interface, programs
48
* written with the protosocket library are executed in a sequential
49
* fashion and does not have to be implemented as explicit state
50
* machines.
51
*
52
* Protosockets only work with TCP connections.
53
*
54
* The protosocket library uses \ref pt protothreads to provide
55
* sequential control flow. This makes the protosockets lightweight in
56
* terms of memory, but also means that protosockets inherits the
57
* functional limitations of protothreads. Each protosocket lives only
58
* within a single function block. Automatic variables (stack
59
* variables) are not necessarily retained across a protosocket
60
* library function call.
61
*
62
* \note Because the protosocket library uses protothreads, local variables
63
* will not always be saved across a call to a protosocket library
64
* function. It is therefore advised that local variables are used
65
* with extreme care.
66
*
67
* The protosocket library provides functions for sending data without
68
* having to deal with retransmissions and acknowledgements, as well
69
* as functions for reading data without having to deal with data
70
* being split across more than one TCP segment.
71
*
72
* Because each protosocket runs as a protothread, the protosocket has to be
73
* started with a call to PSOCK_BEGIN() at the start of the function
74
* in which the protosocket is used. Similarly, the protosocket protothread can
75
* be terminated by a call to PSOCK_EXIT().
76
*
77
*/
78
79
/**
80
* \file
81
* Protosocket library header file
82
* \author
83
* Adam Dunkels <adam@sics.se>
84
*
85
*/
86
87
#ifndef __PSOCK_H__
88
#define __PSOCK_H__
89
90
#include "contiki.h"
91
#include "contiki-lib.h"
92
#include "contiki-net.h"
93
94
/*
95
* The structure that holds the state of a buffer.
96
*
97
* This structure holds the state of a uIP buffer. The structure has
98
* no user-visible elements, but is used through the functions
99
* provided by the library.
100
*
101
*/
102
struct
psock_buf {
103
u8_t *ptr;
104
unsigned
short
left;
105
};
106
107
/**
108
* The representation of a protosocket.
109
*
110
* The protosocket structrure is an opaque structure with no user-visible
111
* elements.
112
*/
113
struct
psock
{
114
struct
pt pt, psockpt;
/* Protothreads - one that's using the psock
115
functions, and one that runs inside the
116
psock functions. */
117
const
u8_t *sendptr;
/* Pointer to the next data to be sent. */
118
u8_t *readptr;
/* Pointer to the next data to be read. */
119
120
uint8_t *bufptr;
/* Pointer to the buffer used for buffering
121
incoming data. */
122
123
u16_t sendlen;
/* The number of bytes left to be sent. */
124
u16_t readlen;
/* The number of bytes left to be read. */
125
126
struct
psock_buf buf;
/* The structure holding the state of the
127
input buffer. */
128
unsigned
int
bufsize;
/* The size of the input buffer. */
129
130
unsigned
char
state;
/* The state of the protosocket. */
131
};
132
133
void
psock_init(
struct
psock
*
psock
, uint8_t *buffer,
unsigned
int
buffersize);
134
/**
135
* Initialize a protosocket.
136
*
137
* This macro initializes a protosocket and must be called before the
138
* protosocket is used. The initialization also specifies the input buffer
139
* for the protosocket.
140
*
141
* \param psock (struct psock *) A pointer to the protosocket to be
142
* initialized
143
*
144
* \param buffer (uint8_t *) A pointer to the input buffer for the
145
* protosocket.
146
*
147
* \param buffersize (unsigned int) The size of the input buffer.
148
*
149
* \hideinitializer
150
*/
151
#define PSOCK_INIT(psock, buffer, buffersize) \
152
psock_init(psock, buffer, buffersize)
153
154
/**
155
* Start the protosocket protothread in a function.
156
*
157
* This macro starts the protothread associated with the protosocket and
158
* must come before other protosocket calls in the function it is used.
159
*
160
* \param psock (struct psock *) A pointer to the protosocket to be
161
* started.
162
*
163
* \hideinitializer
164
*/
165
#define PSOCK_BEGIN(psock) PT_BEGIN(&((psock)->pt))
166
167
PT_THREAD
(psock_send(
struct
psock
*
psock
,
const
uint8_t *buf,
unsigned
int
len));
168
/**
169
* Send data.
170
*
171
* This macro sends data over a protosocket. The protosocket protothread blocks
172
* until all data has been sent and is known to have been received by
173
* the remote end of the TCP connection.
174
*
175
* \param psock (struct psock *) A pointer to the protosocket over which
176
* data is to be sent.
177
*
178
* \param data (uint8_t *) A pointer to the data that is to be sent.
179
*
180
* \param datalen (unsigned int) The length of the data that is to be
181
* sent.
182
*
183
* \hideinitializer
184
*/
185
#define PSOCK_SEND(psock, data, datalen) \
186
PT_WAIT_THREAD(&((psock)->pt), psock_send(psock, data, datalen))
187
188
/**
189
* \brief Send a null-terminated string.
190
* \param psock Pointer to the protosocket.
191
* \param str The string to be sent.
192
*
193
* This function sends a null-terminated string over the
194
* protosocket.
195
*
196
* \hideinitializer
197
*/
198
#define PSOCK_SEND_STR(psock, str) \
199
PT_WAIT_THREAD(&((psock)->pt), psock_send(psock, (uint8_t *)str, strlen(str)))
200
201
PT_THREAD
(psock_generator_send(
struct
psock
*
psock
,
202
unsigned
short
(*f)(
void
*),
void
*arg));
203
204
/**
205
* \brief Generate data with a function and send it
206
* \param psock Pointer to the protosocket.
207
* \param generator Pointer to the generator function
208
* \param arg Argument to the generator function
209
*
210
* This function generates data and sends it over the
211
* protosocket. This can be used to dynamically generate
212
* data for a transmission, instead of generating the data
213
* in a buffer beforehand. This function reduces the need for
214
* buffer memory. The generator function is implemented by
215
* the application, and a pointer to the function is given
216
* as an argument with the call to PSOCK_GENERATOR_SEND().
217
*
218
* The generator function should place the generated data
219
* directly in the uip_appdata buffer, and return the
220
* length of the generated data. The generator function is
221
* called by the protosocket layer when the data first is
222
* sent, and once for every retransmission that is needed.
223
*
224
* \hideinitializer
225
*/
226
#define PSOCK_GENERATOR_SEND(psock, generator, arg) \
227
PT_WAIT_THREAD(&((psock)->pt), \
228
psock_generator_send(psock, generator, arg))
229
230
231
/**
232
* Close a protosocket.
233
*
234
* This macro closes a protosocket and can only be called from within the
235
* protothread in which the protosocket lives.
236
*
237
* \param psock (struct psock *) A pointer to the protosocket that is to
238
* be closed.
239
*
240
* \hideinitializer
241
*/
242
#define PSOCK_CLOSE(psock) uip_close()
243
244
PT_THREAD
(psock_readbuf_len(
struct
psock *psock, uint16_t len));
245
/**
246
* Read data until the buffer is full.
247
*
248
* This macro will block waiting for data and read the data into the
249
* input buffer specified with the call to PSOCK_INIT(). Data is read
250
* until the buffer is full..
251
*
252
* \param psock (struct psock *) A pointer to the protosocket from which
253
* data should be read.
254
*
255
* \hideinitializer
256
*/
257
#define PSOCK_READBUF(psock) \
258
PT_WAIT_THREAD(&((psock)->pt), psock_readbuf_len(psock, 1))
259
260
261
/**
262
* Read data until at least len bytes have been read.
263
*
264
* This macro will block waiting for data and read the data into the
265
* input buffer specified with the call to PSOCK_INIT(). Data is read
266
* until the buffer is full or len bytes have been read.
267
*
268
* \param psock (struct psock *) A pointer to the protosocket from which
269
* data should be read.
270
* \param len (uint16_t) The minimum number of bytes to read.
271
*
272
* \hideinitializer
273
*/
274
#define PSOCK_READBUF_LEN(psock, len) \
275
PT_WAIT_THREAD(&((psock)->pt), psock_readbuf_len(psock, len))
276
277
PT_THREAD
(psock_readto(
struct
psock *psock,
unsigned
char
c));
278
/**
279
* Read data up to a specified character.
280
*
281
* This macro will block waiting for data and read the data into the
282
* input buffer specified with the call to PSOCK_INIT(). Data is only
283
* read until the specified character appears in the data stream.
284
*
285
* \param psock (struct psock *) A pointer to the protosocket from which
286
* data should be read.
287
*
288
* \param c (char) The character at which to stop reading.
289
*
290
* \hideinitializer
291
*/
292
#define PSOCK_READTO(psock, c) \
293
PT_WAIT_THREAD(&((psock)->pt), psock_readto(psock, c))
294
295
/**
296
* The length of the data that was previously read.
297
*
298
* This macro returns the length of the data that was previously read
299
* using PSOCK_READTO() or PSOCK_READ().
300
*
301
* \param psock (struct psock *) A pointer to the protosocket holding the data.
302
*
303
* \hideinitializer
304
*/
305
#define PSOCK_DATALEN(psock) psock_datalen(psock)
306
307
u16_t psock_datalen(
struct
psock *psock);
308
309
/**
310
* Exit the protosocket's protothread.
311
*
312
* This macro terminates the protothread of the protosocket and should
313
* almost always be used in conjunction with PSOCK_CLOSE().
314
*
315
* \sa PSOCK_CLOSE_EXIT()
316
*
317
* \param psock (struct psock *) A pointer to the protosocket.
318
*
319
* \hideinitializer
320
*/
321
#define PSOCK_EXIT(psock) PT_EXIT(&((psock)->pt))
322
323
/**
324
* Close a protosocket and exit the protosocket's protothread.
325
*
326
* This macro closes a protosocket and exits the protosocket's protothread.
327
*
328
* \param psock (struct psock *) A pointer to the protosocket.
329
*
330
* \hideinitializer
331
*/
332
#define PSOCK_CLOSE_EXIT(psock) \
333
do { \
334
PSOCK_CLOSE(psock); \
335
PSOCK_EXIT(psock); \
336
} while(0)
337
338
/**
339
* Declare the end of a protosocket's protothread.
340
*
341
* This macro is used for declaring that the protosocket's protothread
342
* ends. It must always be used together with a matching PSOCK_BEGIN()
343
* macro.
344
*
345
* \param psock (struct psock *) A pointer to the protosocket.
346
*
347
* \hideinitializer
348
*/
349
#define PSOCK_END(psock) PT_END(&((psock)->pt))
350
351
char
psock_newdata(
struct
psock *s);
352
353
/**
354
* Check if new data has arrived on a protosocket.
355
*
356
* This macro is used in conjunction with the PSOCK_WAIT_UNTIL()
357
* macro to check if data has arrived on a protosocket.
358
*
359
* \param psock (struct psock *) A pointer to the protosocket.
360
*
361
* \hideinitializer
362
*/
363
#define PSOCK_NEWDATA(psock) psock_newdata(psock)
364
365
/**
366
* Wait until a condition is true.
367
*
368
* This macro blocks the protothread until the specified condition is
369
* true. The macro PSOCK_NEWDATA() can be used to check if new data
370
* arrives when the protosocket is waiting.
371
*
372
* Typically, this macro is used as follows:
373
*
374
\code
375
PT_THREAD(thread(struct psock *s, struct timer *t))
376
{
377
PSOCK_BEGIN(s);
378
379
PSOCK_WAIT_UNTIL(s, PSOCK_NEWDATA(s) || timer_expired(t));
380
381
if(PSOCK_NEWDATA(s)) {
382
PSOCK_READTO(s, '\n');
383
} else {
384
handle_timed_out(s);
385
}
386
387
PSOCK_END(s);
388
}
389
\endcode
390
*
391
* \param psock (struct psock *) A pointer to the protosocket.
392
* \param condition The condition to wait for.
393
*
394
* \hideinitializer
395
*/
396
#define PSOCK_WAIT_UNTIL(psock, condition) \
397
PT_WAIT_UNTIL(&((psock)->pt), (condition));
398
399
#define PSOCK_WAIT_THREAD(psock, condition) \
400
PT_WAIT_THREAD(&((psock)->pt), (condition))
401
402
#endif
/* __PSOCK_H__ */
403
404
/** @} */
405
/** @} */
Generated on Fri Aug 30 2013 12:34:05 for Contiki 2.5 by
1.8.3.1