uIP 1.0 based webserver for LPC1114 + ENC28J60

Dependencies:   mbed TMP102

Committer:
ban4jp
Date:
Mon Jun 30 16:00:08 2014 +0000
Revision:
3:a2715e9c7737
Parent:
0:685224d2f66d
backported from Contiki 2.7

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