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-2005, 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: pt.h,v 1.2 2006/06/12 08:00:30 adam Exp $
ban4jp 0:685224d2f66d 34 */
ban4jp 0:685224d2f66d 35
ban4jp 0:685224d2f66d 36 /**
ban4jp 0:685224d2f66d 37 * \addtogroup pt
ban4jp 0:685224d2f66d 38 * @{
ban4jp 0:685224d2f66d 39 */
ban4jp 0:685224d2f66d 40
ban4jp 0:685224d2f66d 41 /**
ban4jp 0:685224d2f66d 42 * \file
ban4jp 0:685224d2f66d 43 * Protothreads implementation.
ban4jp 0:685224d2f66d 44 * \author
ban4jp 0:685224d2f66d 45 * Adam Dunkels <adam@sics.se>
ban4jp 0:685224d2f66d 46 *
ban4jp 0:685224d2f66d 47 */
ban4jp 0:685224d2f66d 48
ban4jp 0:685224d2f66d 49 #ifndef __PT_H__
ban4jp 0:685224d2f66d 50 #define __PT_H__
ban4jp 0:685224d2f66d 51
ban4jp 0:685224d2f66d 52 #include "lc.h"
ban4jp 0:685224d2f66d 53
ban4jp 0:685224d2f66d 54 struct pt {
ban4jp 0:685224d2f66d 55 lc_t lc;
ban4jp 0:685224d2f66d 56 };
ban4jp 0:685224d2f66d 57
ban4jp 0:685224d2f66d 58 #define PT_WAITING 0
ban4jp 0:685224d2f66d 59 #define PT_EXITED 1
ban4jp 0:685224d2f66d 60 #define PT_ENDED 2
ban4jp 0:685224d2f66d 61 #define PT_YIELDED 3
ban4jp 0:685224d2f66d 62
ban4jp 0:685224d2f66d 63 /**
ban4jp 0:685224d2f66d 64 * \name Initialization
ban4jp 0:685224d2f66d 65 * @{
ban4jp 0:685224d2f66d 66 */
ban4jp 0:685224d2f66d 67
ban4jp 0:685224d2f66d 68 /**
ban4jp 0:685224d2f66d 69 * Initialize a protothread.
ban4jp 0:685224d2f66d 70 *
ban4jp 0:685224d2f66d 71 * Initializes a protothread. Initialization must be done prior to
ban4jp 0:685224d2f66d 72 * starting to execute the protothread.
ban4jp 0:685224d2f66d 73 *
ban4jp 0:685224d2f66d 74 * \param pt A pointer to the protothread control structure.
ban4jp 0:685224d2f66d 75 *
ban4jp 0:685224d2f66d 76 * \sa PT_SPAWN()
ban4jp 0:685224d2f66d 77 *
ban4jp 0:685224d2f66d 78 * \hideinitializer
ban4jp 0:685224d2f66d 79 */
ban4jp 0:685224d2f66d 80 #define PT_INIT(pt) LC_INIT((pt)->lc)
ban4jp 0:685224d2f66d 81
ban4jp 0:685224d2f66d 82 /** @} */
ban4jp 0:685224d2f66d 83
ban4jp 0:685224d2f66d 84 /**
ban4jp 0:685224d2f66d 85 * \name Declaration and definition
ban4jp 0:685224d2f66d 86 * @{
ban4jp 0:685224d2f66d 87 */
ban4jp 0:685224d2f66d 88
ban4jp 0:685224d2f66d 89 /**
ban4jp 0:685224d2f66d 90 * Declaration of a protothread.
ban4jp 0:685224d2f66d 91 *
ban4jp 0:685224d2f66d 92 * This macro is used to declare a protothread. All protothreads must
ban4jp 0:685224d2f66d 93 * be declared with this macro.
ban4jp 0:685224d2f66d 94 *
ban4jp 0:685224d2f66d 95 * \param name_args The name and arguments of the C function
ban4jp 0:685224d2f66d 96 * implementing the protothread.
ban4jp 0:685224d2f66d 97 *
ban4jp 0:685224d2f66d 98 * \hideinitializer
ban4jp 0:685224d2f66d 99 */
ban4jp 0:685224d2f66d 100 #define PT_THREAD(name_args) char name_args
ban4jp 0:685224d2f66d 101
ban4jp 0:685224d2f66d 102 /**
ban4jp 0:685224d2f66d 103 * Declare the start of a protothread inside the C function
ban4jp 0:685224d2f66d 104 * implementing the protothread.
ban4jp 0:685224d2f66d 105 *
ban4jp 0:685224d2f66d 106 * This macro is used to declare the starting point of a
ban4jp 0:685224d2f66d 107 * protothread. It should be placed at the start of the function in
ban4jp 0:685224d2f66d 108 * which the protothread runs. All C statements above the PT_BEGIN()
ban4jp 0:685224d2f66d 109 * invokation will be executed each time the protothread is scheduled.
ban4jp 0:685224d2f66d 110 *
ban4jp 0:685224d2f66d 111 * \param pt A pointer to the protothread control structure.
ban4jp 0:685224d2f66d 112 *
ban4jp 0:685224d2f66d 113 * \hideinitializer
ban4jp 0:685224d2f66d 114 */
ban4jp 0:685224d2f66d 115 #define PT_BEGIN(pt) { char PT_YIELD_FLAG = 1; LC_RESUME((pt)->lc)
ban4jp 0:685224d2f66d 116
ban4jp 0:685224d2f66d 117 /**
ban4jp 0:685224d2f66d 118 * Declare the end of a protothread.
ban4jp 0:685224d2f66d 119 *
ban4jp 0:685224d2f66d 120 * This macro is used for declaring that a protothread ends. It must
ban4jp 0:685224d2f66d 121 * always be used together with a matching PT_BEGIN() macro.
ban4jp 0:685224d2f66d 122 *
ban4jp 0:685224d2f66d 123 * \param pt A pointer to the protothread control structure.
ban4jp 0:685224d2f66d 124 *
ban4jp 0:685224d2f66d 125 * \hideinitializer
ban4jp 0:685224d2f66d 126 */
ban4jp 0:685224d2f66d 127 #define PT_END(pt) LC_END((pt)->lc); PT_YIELD_FLAG = 0; \
ban4jp 0:685224d2f66d 128 PT_INIT(pt); return PT_ENDED; }
ban4jp 0:685224d2f66d 129
ban4jp 0:685224d2f66d 130 /** @} */
ban4jp 0:685224d2f66d 131
ban4jp 0:685224d2f66d 132 /**
ban4jp 0:685224d2f66d 133 * \name Blocked wait
ban4jp 0:685224d2f66d 134 * @{
ban4jp 0:685224d2f66d 135 */
ban4jp 0:685224d2f66d 136
ban4jp 0:685224d2f66d 137 /**
ban4jp 0:685224d2f66d 138 * Block and wait until condition is true.
ban4jp 0:685224d2f66d 139 *
ban4jp 0:685224d2f66d 140 * This macro blocks the protothread until the specified condition is
ban4jp 0:685224d2f66d 141 * true.
ban4jp 0:685224d2f66d 142 *
ban4jp 0:685224d2f66d 143 * \param pt A pointer to the protothread control structure.
ban4jp 0:685224d2f66d 144 * \param condition The condition.
ban4jp 0:685224d2f66d 145 *
ban4jp 0:685224d2f66d 146 * \hideinitializer
ban4jp 0:685224d2f66d 147 */
ban4jp 0:685224d2f66d 148 #define PT_WAIT_UNTIL(pt, condition) \
ban4jp 0:685224d2f66d 149 do { \
ban4jp 0:685224d2f66d 150 LC_SET((pt)->lc); \
ban4jp 0:685224d2f66d 151 if(!(condition)) { \
ban4jp 0:685224d2f66d 152 return PT_WAITING; \
ban4jp 0:685224d2f66d 153 } \
ban4jp 0:685224d2f66d 154 } while(0)
ban4jp 0:685224d2f66d 155
ban4jp 0:685224d2f66d 156 /**
ban4jp 0:685224d2f66d 157 * Block and wait while condition is true.
ban4jp 0:685224d2f66d 158 *
ban4jp 0:685224d2f66d 159 * This function blocks and waits while condition is true. See
ban4jp 0:685224d2f66d 160 * PT_WAIT_UNTIL().
ban4jp 0:685224d2f66d 161 *
ban4jp 0:685224d2f66d 162 * \param pt A pointer to the protothread control structure.
ban4jp 0:685224d2f66d 163 * \param cond The condition.
ban4jp 0:685224d2f66d 164 *
ban4jp 0:685224d2f66d 165 * \hideinitializer
ban4jp 0:685224d2f66d 166 */
ban4jp 0:685224d2f66d 167 #define PT_WAIT_WHILE(pt, cond) PT_WAIT_UNTIL((pt), !(cond))
ban4jp 0:685224d2f66d 168
ban4jp 0:685224d2f66d 169 /** @} */
ban4jp 0:685224d2f66d 170
ban4jp 0:685224d2f66d 171 /**
ban4jp 0:685224d2f66d 172 * \name Hierarchical protothreads
ban4jp 0:685224d2f66d 173 * @{
ban4jp 0:685224d2f66d 174 */
ban4jp 0:685224d2f66d 175
ban4jp 0:685224d2f66d 176 /**
ban4jp 0:685224d2f66d 177 * Block and wait until a child protothread completes.
ban4jp 0:685224d2f66d 178 *
ban4jp 0:685224d2f66d 179 * This macro schedules a child protothread. The current protothread
ban4jp 0:685224d2f66d 180 * will block until the child protothread completes.
ban4jp 0:685224d2f66d 181 *
ban4jp 0:685224d2f66d 182 * \note The child protothread must be manually initialized with the
ban4jp 0:685224d2f66d 183 * PT_INIT() function before this function is used.
ban4jp 0:685224d2f66d 184 *
ban4jp 0:685224d2f66d 185 * \param pt A pointer to the protothread control structure.
ban4jp 0:685224d2f66d 186 * \param thread The child protothread with arguments
ban4jp 0:685224d2f66d 187 *
ban4jp 0:685224d2f66d 188 * \sa PT_SPAWN()
ban4jp 0:685224d2f66d 189 *
ban4jp 0:685224d2f66d 190 * \hideinitializer
ban4jp 0:685224d2f66d 191 */
ban4jp 0:685224d2f66d 192 #define PT_WAIT_THREAD(pt, thread) PT_WAIT_WHILE((pt), PT_SCHEDULE(thread))
ban4jp 0:685224d2f66d 193
ban4jp 0:685224d2f66d 194 /**
ban4jp 0:685224d2f66d 195 * Spawn a child protothread and wait until it exits.
ban4jp 0:685224d2f66d 196 *
ban4jp 0:685224d2f66d 197 * This macro spawns a child protothread and waits until it exits. The
ban4jp 0:685224d2f66d 198 * macro can only be used within a protothread.
ban4jp 0:685224d2f66d 199 *
ban4jp 0:685224d2f66d 200 * \param pt A pointer to the protothread control structure.
ban4jp 0:685224d2f66d 201 * \param child A pointer to the child protothread's control structure.
ban4jp 0:685224d2f66d 202 * \param thread The child protothread with arguments
ban4jp 0:685224d2f66d 203 *
ban4jp 0:685224d2f66d 204 * \hideinitializer
ban4jp 0:685224d2f66d 205 */
ban4jp 0:685224d2f66d 206 #define PT_SPAWN(pt, child, thread) \
ban4jp 0:685224d2f66d 207 do { \
ban4jp 0:685224d2f66d 208 PT_INIT((child)); \
ban4jp 0:685224d2f66d 209 PT_WAIT_THREAD((pt), (thread)); \
ban4jp 0:685224d2f66d 210 } while(0)
ban4jp 0:685224d2f66d 211
ban4jp 0:685224d2f66d 212 /** @} */
ban4jp 0:685224d2f66d 213
ban4jp 0:685224d2f66d 214 /**
ban4jp 0:685224d2f66d 215 * \name Exiting and restarting
ban4jp 0:685224d2f66d 216 * @{
ban4jp 0:685224d2f66d 217 */
ban4jp 0:685224d2f66d 218
ban4jp 0:685224d2f66d 219 /**
ban4jp 0:685224d2f66d 220 * Restart the protothread.
ban4jp 0:685224d2f66d 221 *
ban4jp 0:685224d2f66d 222 * This macro will block and cause the running protothread to restart
ban4jp 0:685224d2f66d 223 * its execution at the place of the PT_BEGIN() call.
ban4jp 0:685224d2f66d 224 *
ban4jp 0:685224d2f66d 225 * \param pt A pointer to the protothread control structure.
ban4jp 0:685224d2f66d 226 *
ban4jp 0:685224d2f66d 227 * \hideinitializer
ban4jp 0:685224d2f66d 228 */
ban4jp 0:685224d2f66d 229 #define PT_RESTART(pt) \
ban4jp 0:685224d2f66d 230 do { \
ban4jp 0:685224d2f66d 231 PT_INIT(pt); \
ban4jp 0:685224d2f66d 232 return PT_WAITING; \
ban4jp 0:685224d2f66d 233 } while(0)
ban4jp 0:685224d2f66d 234
ban4jp 0:685224d2f66d 235 /**
ban4jp 0:685224d2f66d 236 * Exit the protothread.
ban4jp 0:685224d2f66d 237 *
ban4jp 0:685224d2f66d 238 * This macro causes the protothread to exit. If the protothread was
ban4jp 0:685224d2f66d 239 * spawned by another protothread, the parent protothread will become
ban4jp 0:685224d2f66d 240 * unblocked and can continue to run.
ban4jp 0:685224d2f66d 241 *
ban4jp 0:685224d2f66d 242 * \param pt A pointer to the protothread control structure.
ban4jp 0:685224d2f66d 243 *
ban4jp 0:685224d2f66d 244 * \hideinitializer
ban4jp 0:685224d2f66d 245 */
ban4jp 0:685224d2f66d 246 #define PT_EXIT(pt) \
ban4jp 0:685224d2f66d 247 do { \
ban4jp 0:685224d2f66d 248 PT_INIT(pt); \
ban4jp 0:685224d2f66d 249 return PT_EXITED; \
ban4jp 0:685224d2f66d 250 } while(0)
ban4jp 0:685224d2f66d 251
ban4jp 0:685224d2f66d 252 /** @} */
ban4jp 0:685224d2f66d 253
ban4jp 0:685224d2f66d 254 /**
ban4jp 0:685224d2f66d 255 * \name Calling a protothread
ban4jp 0:685224d2f66d 256 * @{
ban4jp 0:685224d2f66d 257 */
ban4jp 0:685224d2f66d 258
ban4jp 0:685224d2f66d 259 /**
ban4jp 0:685224d2f66d 260 * Schedule a protothread.
ban4jp 0:685224d2f66d 261 *
ban4jp 0:685224d2f66d 262 * This function shedules a protothread. The return value of the
ban4jp 0:685224d2f66d 263 * function is non-zero if the protothread is running or zero if the
ban4jp 0:685224d2f66d 264 * protothread has exited.
ban4jp 0:685224d2f66d 265 *
ban4jp 0:685224d2f66d 266 * \param f The call to the C function implementing the protothread to
ban4jp 0:685224d2f66d 267 * be scheduled
ban4jp 0:685224d2f66d 268 *
ban4jp 0:685224d2f66d 269 * \hideinitializer
ban4jp 0:685224d2f66d 270 */
ban4jp 0:685224d2f66d 271 #define PT_SCHEDULE(f) ((f) == PT_WAITING)
ban4jp 0:685224d2f66d 272
ban4jp 0:685224d2f66d 273 /** @} */
ban4jp 0:685224d2f66d 274
ban4jp 0:685224d2f66d 275 /**
ban4jp 0:685224d2f66d 276 * \name Yielding from a protothread
ban4jp 0:685224d2f66d 277 * @{
ban4jp 0:685224d2f66d 278 */
ban4jp 0:685224d2f66d 279
ban4jp 0:685224d2f66d 280 /**
ban4jp 0:685224d2f66d 281 * Yield from the current protothread.
ban4jp 0:685224d2f66d 282 *
ban4jp 0:685224d2f66d 283 * This function will yield the protothread, thereby allowing other
ban4jp 0:685224d2f66d 284 * processing to take place in the system.
ban4jp 0:685224d2f66d 285 *
ban4jp 0:685224d2f66d 286 * \param pt A pointer to the protothread control structure.
ban4jp 0:685224d2f66d 287 *
ban4jp 0:685224d2f66d 288 * \hideinitializer
ban4jp 0:685224d2f66d 289 */
ban4jp 0:685224d2f66d 290 #define PT_YIELD(pt) \
ban4jp 0:685224d2f66d 291 do { \
ban4jp 0:685224d2f66d 292 PT_YIELD_FLAG = 0; \
ban4jp 0:685224d2f66d 293 LC_SET((pt)->lc); \
ban4jp 0:685224d2f66d 294 if(PT_YIELD_FLAG == 0) { \
ban4jp 0:685224d2f66d 295 return PT_YIELDED; \
ban4jp 0:685224d2f66d 296 } \
ban4jp 0:685224d2f66d 297 } while(0)
ban4jp 0:685224d2f66d 298
ban4jp 0:685224d2f66d 299 /**
ban4jp 0:685224d2f66d 300 * \brief Yield from the protothread until a condition occurs.
ban4jp 0:685224d2f66d 301 * \param pt A pointer to the protothread control structure.
ban4jp 0:685224d2f66d 302 * \param cond The condition.
ban4jp 0:685224d2f66d 303 *
ban4jp 0:685224d2f66d 304 * This function will yield the protothread, until the
ban4jp 0:685224d2f66d 305 * specified condition evaluates to true.
ban4jp 0:685224d2f66d 306 *
ban4jp 0:685224d2f66d 307 *
ban4jp 0:685224d2f66d 308 * \hideinitializer
ban4jp 0:685224d2f66d 309 */
ban4jp 0:685224d2f66d 310 #define PT_YIELD_UNTIL(pt, cond) \
ban4jp 0:685224d2f66d 311 do { \
ban4jp 0:685224d2f66d 312 PT_YIELD_FLAG = 0; \
ban4jp 0:685224d2f66d 313 LC_SET((pt)->lc); \
ban4jp 0:685224d2f66d 314 if((PT_YIELD_FLAG == 0) || !(cond)) { \
ban4jp 0:685224d2f66d 315 return PT_YIELDED; \
ban4jp 0:685224d2f66d 316 } \
ban4jp 0:685224d2f66d 317 } while(0)
ban4jp 0:685224d2f66d 318
ban4jp 0:685224d2f66d 319 /** @} */
ban4jp 0:685224d2f66d 320
ban4jp 0:685224d2f66d 321 #endif /* __PT_H__ */
ban4jp 0:685224d2f66d 322
ban4jp 0:685224d2f66d 323 /** @} */