uIP 1.0 based webserver for LPC1114 + ENC28J60

Dependencies:   mbed TMP102

Committer:
ban4jp
Date:
Sat Jun 14 16:02:21 2014 +0000
Revision:
0:685224d2f66d
Child:
3:a2715e9c7737
initial commit.

Who changed what in which revision?

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