Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed
Core/lwIP/include/lwip/sys.h@0:a548a085de55, 2010-03-14 (annotated)
- Committer:
- pehrhovey
- Date:
- Sun Mar 14 00:54:12 2010 +0000
- Revision:
- 0:a548a085de55
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pehrhovey | 0:a548a085de55 | 1 | /* |
pehrhovey | 0:a548a085de55 | 2 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
pehrhovey | 0:a548a085de55 | 3 | * All rights reserved. |
pehrhovey | 0:a548a085de55 | 4 | * |
pehrhovey | 0:a548a085de55 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
pehrhovey | 0:a548a085de55 | 6 | * are permitted provided that the following conditions are met: |
pehrhovey | 0:a548a085de55 | 7 | * |
pehrhovey | 0:a548a085de55 | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
pehrhovey | 0:a548a085de55 | 9 | * this list of conditions and the following disclaimer. |
pehrhovey | 0:a548a085de55 | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
pehrhovey | 0:a548a085de55 | 11 | * this list of conditions and the following disclaimer in the documentation |
pehrhovey | 0:a548a085de55 | 12 | * and/or other materials provided with the distribution. |
pehrhovey | 0:a548a085de55 | 13 | * 3. The name of the author may not be used to endorse or promote products |
pehrhovey | 0:a548a085de55 | 14 | * derived from this software without specific prior written permission. |
pehrhovey | 0:a548a085de55 | 15 | * |
pehrhovey | 0:a548a085de55 | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
pehrhovey | 0:a548a085de55 | 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
pehrhovey | 0:a548a085de55 | 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
pehrhovey | 0:a548a085de55 | 19 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
pehrhovey | 0:a548a085de55 | 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
pehrhovey | 0:a548a085de55 | 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
pehrhovey | 0:a548a085de55 | 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
pehrhovey | 0:a548a085de55 | 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
pehrhovey | 0:a548a085de55 | 24 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
pehrhovey | 0:a548a085de55 | 25 | * OF SUCH DAMAGE. |
pehrhovey | 0:a548a085de55 | 26 | * |
pehrhovey | 0:a548a085de55 | 27 | * This file is part of the lwIP TCP/IP stack. |
pehrhovey | 0:a548a085de55 | 28 | * |
pehrhovey | 0:a548a085de55 | 29 | * Author: Adam Dunkels <adam@sics.se> |
pehrhovey | 0:a548a085de55 | 30 | * |
pehrhovey | 0:a548a085de55 | 31 | */ |
pehrhovey | 0:a548a085de55 | 32 | #ifndef __LWIP_SYS_H__ |
pehrhovey | 0:a548a085de55 | 33 | #define __LWIP_SYS_H__ |
pehrhovey | 0:a548a085de55 | 34 | |
pehrhovey | 0:a548a085de55 | 35 | #include "lwip/opt.h" |
pehrhovey | 0:a548a085de55 | 36 | |
pehrhovey | 0:a548a085de55 | 37 | #ifdef __cplusplus |
pehrhovey | 0:a548a085de55 | 38 | extern "C" { |
pehrhovey | 0:a548a085de55 | 39 | #endif |
pehrhovey | 0:a548a085de55 | 40 | |
pehrhovey | 0:a548a085de55 | 41 | #if NO_SYS |
pehrhovey | 0:a548a085de55 | 42 | |
pehrhovey | 0:a548a085de55 | 43 | /* For a totally minimal and standalone system, we provide null |
pehrhovey | 0:a548a085de55 | 44 | definitions of the sys_ functions. */ |
pehrhovey | 0:a548a085de55 | 45 | typedef u8_t sys_sem_t; |
pehrhovey | 0:a548a085de55 | 46 | typedef u8_t sys_mbox_t; |
pehrhovey | 0:a548a085de55 | 47 | struct sys_timeo {u8_t dummy;}; |
pehrhovey | 0:a548a085de55 | 48 | |
pehrhovey | 0:a548a085de55 | 49 | #define sys_init() |
pehrhovey | 0:a548a085de55 | 50 | #define sys_timeout(m,h,a) |
pehrhovey | 0:a548a085de55 | 51 | #define sys_untimeout(m,a) |
pehrhovey | 0:a548a085de55 | 52 | #define sys_sem_new(c) c |
pehrhovey | 0:a548a085de55 | 53 | #define sys_sem_signal(s) |
pehrhovey | 0:a548a085de55 | 54 | #define sys_sem_wait(s) |
pehrhovey | 0:a548a085de55 | 55 | #define sys_sem_wait_timeout(s,t) |
pehrhovey | 0:a548a085de55 | 56 | #define sys_arch_sem_wait(s,t) |
pehrhovey | 0:a548a085de55 | 57 | #define sys_sem_free(s) |
pehrhovey | 0:a548a085de55 | 58 | #define sys_mbox_new(s) 0 |
pehrhovey | 0:a548a085de55 | 59 | #define sys_mbox_fetch(m,d) |
pehrhovey | 0:a548a085de55 | 60 | #define sys_mbox_tryfetch(m,d) |
pehrhovey | 0:a548a085de55 | 61 | #define sys_mbox_post(m,d) |
pehrhovey | 0:a548a085de55 | 62 | #define sys_mbox_trypost(m,d) |
pehrhovey | 0:a548a085de55 | 63 | #define sys_mbox_free(m) |
pehrhovey | 0:a548a085de55 | 64 | |
pehrhovey | 0:a548a085de55 | 65 | #define sys_thread_new(n,t,a,s,p) |
pehrhovey | 0:a548a085de55 | 66 | |
pehrhovey | 0:a548a085de55 | 67 | #else /* NO_SYS */ |
pehrhovey | 0:a548a085de55 | 68 | |
pehrhovey | 0:a548a085de55 | 69 | /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */ |
pehrhovey | 0:a548a085de55 | 70 | #define SYS_ARCH_TIMEOUT 0xffffffffUL |
pehrhovey | 0:a548a085de55 | 71 | |
pehrhovey | 0:a548a085de55 | 72 | /* sys_mbox_tryfetch returns SYS_MBOX_EMPTY if appropriate. |
pehrhovey | 0:a548a085de55 | 73 | * For now we use the same magic value, but we allow this to change in future. |
pehrhovey | 0:a548a085de55 | 74 | */ |
pehrhovey | 0:a548a085de55 | 75 | #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT |
pehrhovey | 0:a548a085de55 | 76 | |
pehrhovey | 0:a548a085de55 | 77 | #include "lwip/err.h" |
pehrhovey | 0:a548a085de55 | 78 | #include "arch/sys_arch.h" |
pehrhovey | 0:a548a085de55 | 79 | |
pehrhovey | 0:a548a085de55 | 80 | typedef void (* sys_timeout_handler)(void *arg); |
pehrhovey | 0:a548a085de55 | 81 | |
pehrhovey | 0:a548a085de55 | 82 | struct sys_timeo { |
pehrhovey | 0:a548a085de55 | 83 | struct sys_timeo *next; |
pehrhovey | 0:a548a085de55 | 84 | u32_t time; |
pehrhovey | 0:a548a085de55 | 85 | sys_timeout_handler h; |
pehrhovey | 0:a548a085de55 | 86 | void *arg; |
pehrhovey | 0:a548a085de55 | 87 | }; |
pehrhovey | 0:a548a085de55 | 88 | |
pehrhovey | 0:a548a085de55 | 89 | struct sys_timeouts { |
pehrhovey | 0:a548a085de55 | 90 | struct sys_timeo *next; |
pehrhovey | 0:a548a085de55 | 91 | }; |
pehrhovey | 0:a548a085de55 | 92 | |
pehrhovey | 0:a548a085de55 | 93 | /* sys_init() must be called before anthing else. */ |
pehrhovey | 0:a548a085de55 | 94 | void sys_init(void); |
pehrhovey | 0:a548a085de55 | 95 | |
pehrhovey | 0:a548a085de55 | 96 | /* |
pehrhovey | 0:a548a085de55 | 97 | * sys_timeout(): |
pehrhovey | 0:a548a085de55 | 98 | * |
pehrhovey | 0:a548a085de55 | 99 | * Schedule a timeout a specified amount of milliseconds in the |
pehrhovey | 0:a548a085de55 | 100 | * future. When the timeout occurs, the specified timeout handler will |
pehrhovey | 0:a548a085de55 | 101 | * be called. The handler will be passed the "arg" argument when |
pehrhovey | 0:a548a085de55 | 102 | * called. |
pehrhovey | 0:a548a085de55 | 103 | * |
pehrhovey | 0:a548a085de55 | 104 | */ |
pehrhovey | 0:a548a085de55 | 105 | void sys_timeout(u32_t msecs, sys_timeout_handler h, void *arg); |
pehrhovey | 0:a548a085de55 | 106 | void sys_untimeout(sys_timeout_handler h, void *arg); |
pehrhovey | 0:a548a085de55 | 107 | struct sys_timeouts *sys_arch_timeouts(void); |
pehrhovey | 0:a548a085de55 | 108 | |
pehrhovey | 0:a548a085de55 | 109 | /* Semaphore functions. */ |
pehrhovey | 0:a548a085de55 | 110 | sys_sem_t sys_sem_new(u8_t count); |
pehrhovey | 0:a548a085de55 | 111 | void sys_sem_signal(sys_sem_t sem); |
pehrhovey | 0:a548a085de55 | 112 | u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout); |
pehrhovey | 0:a548a085de55 | 113 | void sys_sem_free(sys_sem_t sem); |
pehrhovey | 0:a548a085de55 | 114 | void sys_sem_wait(sys_sem_t sem); |
pehrhovey | 0:a548a085de55 | 115 | int sys_sem_wait_timeout(sys_sem_t sem, u32_t timeout); |
pehrhovey | 0:a548a085de55 | 116 | |
pehrhovey | 0:a548a085de55 | 117 | /* Time functions. */ |
pehrhovey | 0:a548a085de55 | 118 | #ifndef sys_msleep |
pehrhovey | 0:a548a085de55 | 119 | void sys_msleep(u32_t ms); /* only has a (close to) 1 jiffy resolution. */ |
pehrhovey | 0:a548a085de55 | 120 | #endif |
pehrhovey | 0:a548a085de55 | 121 | #ifndef sys_jiffies |
pehrhovey | 0:a548a085de55 | 122 | u32_t sys_jiffies(void); /* since power up. */ |
pehrhovey | 0:a548a085de55 | 123 | #endif |
pehrhovey | 0:a548a085de55 | 124 | |
pehrhovey | 0:a548a085de55 | 125 | /* Mailbox functions. */ |
pehrhovey | 0:a548a085de55 | 126 | sys_mbox_t sys_mbox_new(int size); |
pehrhovey | 0:a548a085de55 | 127 | void sys_mbox_post(sys_mbox_t mbox, void *msg); |
pehrhovey | 0:a548a085de55 | 128 | err_t sys_mbox_trypost(sys_mbox_t mbox, void *msg); |
pehrhovey | 0:a548a085de55 | 129 | u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout); |
pehrhovey | 0:a548a085de55 | 130 | #ifndef sys_arch_mbox_tryfetch /* Allow port to override with a macro */ |
pehrhovey | 0:a548a085de55 | 131 | u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg); |
pehrhovey | 0:a548a085de55 | 132 | #endif |
pehrhovey | 0:a548a085de55 | 133 | /* For now, we map straight to sys_arch implementation. */ |
pehrhovey | 0:a548a085de55 | 134 | #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg) |
pehrhovey | 0:a548a085de55 | 135 | void sys_mbox_free(sys_mbox_t mbox); |
pehrhovey | 0:a548a085de55 | 136 | void sys_mbox_fetch(sys_mbox_t mbox, void **msg); |
pehrhovey | 0:a548a085de55 | 137 | |
pehrhovey | 0:a548a085de55 | 138 | /* Thread functions. */ |
pehrhovey | 0:a548a085de55 | 139 | sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio); |
pehrhovey | 0:a548a085de55 | 140 | |
pehrhovey | 0:a548a085de55 | 141 | #endif /* NO_SYS */ |
pehrhovey | 0:a548a085de55 | 142 | |
pehrhovey | 0:a548a085de55 | 143 | /** Returns the current time in milliseconds. */ |
pehrhovey | 0:a548a085de55 | 144 | u32_t sys_now(void); |
pehrhovey | 0:a548a085de55 | 145 | |
pehrhovey | 0:a548a085de55 | 146 | /* Critical Region Protection */ |
pehrhovey | 0:a548a085de55 | 147 | /* These functions must be implemented in the sys_arch.c file. |
pehrhovey | 0:a548a085de55 | 148 | In some implementations they can provide a more light-weight protection |
pehrhovey | 0:a548a085de55 | 149 | mechanism than using semaphores. Otherwise semaphores can be used for |
pehrhovey | 0:a548a085de55 | 150 | implementation */ |
pehrhovey | 0:a548a085de55 | 151 | #ifndef SYS_ARCH_PROTECT |
pehrhovey | 0:a548a085de55 | 152 | /** SYS_LIGHTWEIGHT_PROT |
pehrhovey | 0:a548a085de55 | 153 | * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection |
pehrhovey | 0:a548a085de55 | 154 | * for certain critical regions during buffer allocation, deallocation and memory |
pehrhovey | 0:a548a085de55 | 155 | * allocation and deallocation. |
pehrhovey | 0:a548a085de55 | 156 | */ |
pehrhovey | 0:a548a085de55 | 157 | #if SYS_LIGHTWEIGHT_PROT |
pehrhovey | 0:a548a085de55 | 158 | |
pehrhovey | 0:a548a085de55 | 159 | /** SYS_ARCH_DECL_PROTECT |
pehrhovey | 0:a548a085de55 | 160 | * declare a protection variable. This macro will default to defining a variable of |
pehrhovey | 0:a548a085de55 | 161 | * type sys_prot_t. If a particular port needs a different implementation, then |
pehrhovey | 0:a548a085de55 | 162 | * this macro may be defined in sys_arch.h. |
pehrhovey | 0:a548a085de55 | 163 | */ |
pehrhovey | 0:a548a085de55 | 164 | #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev |
pehrhovey | 0:a548a085de55 | 165 | /** SYS_ARCH_PROTECT |
pehrhovey | 0:a548a085de55 | 166 | * Perform a "fast" protect. This could be implemented by |
pehrhovey | 0:a548a085de55 | 167 | * disabling interrupts for an embedded system or by using a semaphore or |
pehrhovey | 0:a548a085de55 | 168 | * mutex. The implementation should allow calling SYS_ARCH_PROTECT when |
pehrhovey | 0:a548a085de55 | 169 | * already protected. The old protection level is returned in the variable |
pehrhovey | 0:a548a085de55 | 170 | * "lev". This macro will default to calling the sys_arch_protect() function |
pehrhovey | 0:a548a085de55 | 171 | * which should be implemented in sys_arch.c. If a particular port needs a |
pehrhovey | 0:a548a085de55 | 172 | * different implementation, then this macro may be defined in sys_arch.h |
pehrhovey | 0:a548a085de55 | 173 | */ |
pehrhovey | 0:a548a085de55 | 174 | #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect() |
pehrhovey | 0:a548a085de55 | 175 | /** SYS_ARCH_UNPROTECT |
pehrhovey | 0:a548a085de55 | 176 | * Perform a "fast" set of the protection level to "lev". This could be |
pehrhovey | 0:a548a085de55 | 177 | * implemented by setting the interrupt level to "lev" within the MACRO or by |
pehrhovey | 0:a548a085de55 | 178 | * using a semaphore or mutex. This macro will default to calling the |
pehrhovey | 0:a548a085de55 | 179 | * sys_arch_unprotect() function which should be implemented in |
pehrhovey | 0:a548a085de55 | 180 | * sys_arch.c. If a particular port needs a different implementation, then |
pehrhovey | 0:a548a085de55 | 181 | * this macro may be defined in sys_arch.h |
pehrhovey | 0:a548a085de55 | 182 | */ |
pehrhovey | 0:a548a085de55 | 183 | #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev) |
pehrhovey | 0:a548a085de55 | 184 | sys_prot_t sys_arch_protect(void); |
pehrhovey | 0:a548a085de55 | 185 | void sys_arch_unprotect(sys_prot_t pval); |
pehrhovey | 0:a548a085de55 | 186 | |
pehrhovey | 0:a548a085de55 | 187 | #else |
pehrhovey | 0:a548a085de55 | 188 | |
pehrhovey | 0:a548a085de55 | 189 | #define SYS_ARCH_DECL_PROTECT(lev) |
pehrhovey | 0:a548a085de55 | 190 | #define SYS_ARCH_PROTECT(lev) |
pehrhovey | 0:a548a085de55 | 191 | #define SYS_ARCH_UNPROTECT(lev) |
pehrhovey | 0:a548a085de55 | 192 | |
pehrhovey | 0:a548a085de55 | 193 | #endif /* SYS_LIGHTWEIGHT_PROT */ |
pehrhovey | 0:a548a085de55 | 194 | |
pehrhovey | 0:a548a085de55 | 195 | #endif /* SYS_ARCH_PROTECT */ |
pehrhovey | 0:a548a085de55 | 196 | |
pehrhovey | 0:a548a085de55 | 197 | /* |
pehrhovey | 0:a548a085de55 | 198 | * Macros to set/get and increase/decrease variables in a thread-safe way. |
pehrhovey | 0:a548a085de55 | 199 | * Use these for accessing variable that are used from more than one thread. |
pehrhovey | 0:a548a085de55 | 200 | */ |
pehrhovey | 0:a548a085de55 | 201 | |
pehrhovey | 0:a548a085de55 | 202 | #ifndef SYS_ARCH_INC |
pehrhovey | 0:a548a085de55 | 203 | #define SYS_ARCH_INC(var, val) do { \ |
pehrhovey | 0:a548a085de55 | 204 | SYS_ARCH_DECL_PROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 205 | SYS_ARCH_PROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 206 | var += val; \ |
pehrhovey | 0:a548a085de55 | 207 | SYS_ARCH_UNPROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 208 | } while(0) |
pehrhovey | 0:a548a085de55 | 209 | #endif /* SYS_ARCH_INC */ |
pehrhovey | 0:a548a085de55 | 210 | |
pehrhovey | 0:a548a085de55 | 211 | #ifndef SYS_ARCH_DEC |
pehrhovey | 0:a548a085de55 | 212 | #define SYS_ARCH_DEC(var, val) do { \ |
pehrhovey | 0:a548a085de55 | 213 | SYS_ARCH_DECL_PROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 214 | SYS_ARCH_PROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 215 | var -= val; \ |
pehrhovey | 0:a548a085de55 | 216 | SYS_ARCH_UNPROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 217 | } while(0) |
pehrhovey | 0:a548a085de55 | 218 | #endif /* SYS_ARCH_DEC */ |
pehrhovey | 0:a548a085de55 | 219 | |
pehrhovey | 0:a548a085de55 | 220 | #ifndef SYS_ARCH_GET |
pehrhovey | 0:a548a085de55 | 221 | #define SYS_ARCH_GET(var, ret) do { \ |
pehrhovey | 0:a548a085de55 | 222 | SYS_ARCH_DECL_PROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 223 | SYS_ARCH_PROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 224 | ret = var; \ |
pehrhovey | 0:a548a085de55 | 225 | SYS_ARCH_UNPROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 226 | } while(0) |
pehrhovey | 0:a548a085de55 | 227 | #endif /* SYS_ARCH_GET */ |
pehrhovey | 0:a548a085de55 | 228 | |
pehrhovey | 0:a548a085de55 | 229 | #ifndef SYS_ARCH_SET |
pehrhovey | 0:a548a085de55 | 230 | #define SYS_ARCH_SET(var, val) do { \ |
pehrhovey | 0:a548a085de55 | 231 | SYS_ARCH_DECL_PROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 232 | SYS_ARCH_PROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 233 | var = val; \ |
pehrhovey | 0:a548a085de55 | 234 | SYS_ARCH_UNPROTECT(old_level); \ |
pehrhovey | 0:a548a085de55 | 235 | } while(0) |
pehrhovey | 0:a548a085de55 | 236 | #endif /* SYS_ARCH_SET */ |
pehrhovey | 0:a548a085de55 | 237 | |
pehrhovey | 0:a548a085de55 | 238 | |
pehrhovey | 0:a548a085de55 | 239 | #ifdef __cplusplus |
pehrhovey | 0:a548a085de55 | 240 | } |
pehrhovey | 0:a548a085de55 | 241 | #endif |
pehrhovey | 0:a548a085de55 | 242 | |
pehrhovey | 0:a548a085de55 | 243 | #endif /* __LWIP_SYS_H__ */ |