V1

Dependents:   EthernetInterface

Fork of lwip by mbed official

Committer:
lemniskata
Date:
Thu Jun 13 20:00:31 2013 +0000
Revision:
11:c9233fe7de67
Parent:
0:51ac1d130fd4
V1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:51ac1d130fd4 1 /**
mbed_official 0:51ac1d130fd4 2 * @file
mbed_official 0:51ac1d130fd4 3 * Sequential API Main thread module
mbed_official 0:51ac1d130fd4 4 *
mbed_official 0:51ac1d130fd4 5 */
mbed_official 0:51ac1d130fd4 6
mbed_official 0:51ac1d130fd4 7 /*
mbed_official 0:51ac1d130fd4 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
mbed_official 0:51ac1d130fd4 9 * All rights reserved.
mbed_official 0:51ac1d130fd4 10 *
mbed_official 0:51ac1d130fd4 11 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 0:51ac1d130fd4 12 * are permitted provided that the following conditions are met:
mbed_official 0:51ac1d130fd4 13 *
mbed_official 0:51ac1d130fd4 14 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 0:51ac1d130fd4 15 * this list of conditions and the following disclaimer.
mbed_official 0:51ac1d130fd4 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 0:51ac1d130fd4 17 * this list of conditions and the following disclaimer in the documentation
mbed_official 0:51ac1d130fd4 18 * and/or other materials provided with the distribution.
mbed_official 0:51ac1d130fd4 19 * 3. The name of the author may not be used to endorse or promote products
mbed_official 0:51ac1d130fd4 20 * derived from this software without specific prior written permission.
mbed_official 0:51ac1d130fd4 21 *
mbed_official 0:51ac1d130fd4 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
mbed_official 0:51ac1d130fd4 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
mbed_official 0:51ac1d130fd4 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
mbed_official 0:51ac1d130fd4 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
mbed_official 0:51ac1d130fd4 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
mbed_official 0:51ac1d130fd4 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbed_official 0:51ac1d130fd4 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbed_official 0:51ac1d130fd4 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
mbed_official 0:51ac1d130fd4 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
mbed_official 0:51ac1d130fd4 31 * OF SUCH DAMAGE.
mbed_official 0:51ac1d130fd4 32 *
mbed_official 0:51ac1d130fd4 33 * This file is part of the lwIP TCP/IP stack.
mbed_official 0:51ac1d130fd4 34 *
mbed_official 0:51ac1d130fd4 35 * Author: Adam Dunkels <adam@sics.se>
mbed_official 0:51ac1d130fd4 36 *
mbed_official 0:51ac1d130fd4 37 */
mbed_official 0:51ac1d130fd4 38
mbed_official 0:51ac1d130fd4 39 #include "lwip/opt.h"
mbed_official 0:51ac1d130fd4 40
mbed_official 0:51ac1d130fd4 41 #if !NO_SYS /* don't build if not configured for use in lwipopts.h */
mbed_official 0:51ac1d130fd4 42
mbed_official 0:51ac1d130fd4 43 #include "lwip/sys.h"
mbed_official 0:51ac1d130fd4 44 #include "lwip/memp.h"
mbed_official 0:51ac1d130fd4 45 #include "lwip/mem.h"
mbed_official 0:51ac1d130fd4 46 #include "lwip/pbuf.h"
mbed_official 0:51ac1d130fd4 47 #include "lwip/tcpip.h"
mbed_official 0:51ac1d130fd4 48 #include "lwip/init.h"
mbed_official 0:51ac1d130fd4 49 #include "netif/etharp.h"
mbed_official 0:51ac1d130fd4 50 #include "netif/ppp_oe.h"
mbed_official 0:51ac1d130fd4 51
mbed_official 0:51ac1d130fd4 52 /* global variables */
mbed_official 0:51ac1d130fd4 53 static tcpip_init_done_fn tcpip_init_done;
mbed_official 0:51ac1d130fd4 54 static void *tcpip_init_done_arg;
mbed_official 0:51ac1d130fd4 55 static sys_mbox_t mbox;
mbed_official 0:51ac1d130fd4 56
mbed_official 0:51ac1d130fd4 57 #if LWIP_TCPIP_CORE_LOCKING
mbed_official 0:51ac1d130fd4 58 /** The global semaphore to lock the stack. */
mbed_official 0:51ac1d130fd4 59 sys_mutex_t lock_tcpip_core;
mbed_official 0:51ac1d130fd4 60 #endif /* LWIP_TCPIP_CORE_LOCKING */
mbed_official 0:51ac1d130fd4 61
mbed_official 0:51ac1d130fd4 62
mbed_official 0:51ac1d130fd4 63 /**
mbed_official 0:51ac1d130fd4 64 * The main lwIP thread. This thread has exclusive access to lwIP core functions
mbed_official 0:51ac1d130fd4 65 * (unless access to them is not locked). Other threads communicate with this
mbed_official 0:51ac1d130fd4 66 * thread using message boxes.
mbed_official 0:51ac1d130fd4 67 *
mbed_official 0:51ac1d130fd4 68 * It also starts all the timers to make sure they are running in the right
mbed_official 0:51ac1d130fd4 69 * thread context.
mbed_official 0:51ac1d130fd4 70 *
mbed_official 0:51ac1d130fd4 71 * @param arg unused argument
mbed_official 0:51ac1d130fd4 72 */
mbed_official 0:51ac1d130fd4 73 static void
mbed_official 0:51ac1d130fd4 74 tcpip_thread(void *arg)
mbed_official 0:51ac1d130fd4 75 {
mbed_official 0:51ac1d130fd4 76 struct tcpip_msg *msg;
mbed_official 0:51ac1d130fd4 77 LWIP_UNUSED_ARG(arg);
mbed_official 0:51ac1d130fd4 78
mbed_official 0:51ac1d130fd4 79 if (tcpip_init_done != NULL) {
mbed_official 0:51ac1d130fd4 80 tcpip_init_done(tcpip_init_done_arg);
mbed_official 0:51ac1d130fd4 81 }
mbed_official 0:51ac1d130fd4 82
mbed_official 0:51ac1d130fd4 83 LOCK_TCPIP_CORE();
mbed_official 0:51ac1d130fd4 84 while (1) { /* MAIN Loop */
mbed_official 0:51ac1d130fd4 85 UNLOCK_TCPIP_CORE();
mbed_official 0:51ac1d130fd4 86 LWIP_TCPIP_THREAD_ALIVE();
mbed_official 0:51ac1d130fd4 87 /* wait for a message, timeouts are processed while waiting */
mbed_official 0:51ac1d130fd4 88 sys_timeouts_mbox_fetch(&mbox, (void **)&msg);
mbed_official 0:51ac1d130fd4 89 LOCK_TCPIP_CORE();
mbed_official 0:51ac1d130fd4 90 switch (msg->type) {
mbed_official 0:51ac1d130fd4 91 #if LWIP_NETCONN
mbed_official 0:51ac1d130fd4 92 case TCPIP_MSG_API:
mbed_official 0:51ac1d130fd4 93 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
mbed_official 0:51ac1d130fd4 94 msg->msg.apimsg->function(&(msg->msg.apimsg->msg));
mbed_official 0:51ac1d130fd4 95 break;
mbed_official 0:51ac1d130fd4 96 #endif /* LWIP_NETCONN */
mbed_official 0:51ac1d130fd4 97
mbed_official 0:51ac1d130fd4 98 #if !LWIP_TCPIP_CORE_LOCKING_INPUT
mbed_official 0:51ac1d130fd4 99 case TCPIP_MSG_INPKT:
mbed_official 0:51ac1d130fd4 100 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
mbed_official 0:51ac1d130fd4 101 #if LWIP_ETHERNET
mbed_official 0:51ac1d130fd4 102 if (msg->msg.inp.netif->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
mbed_official 0:51ac1d130fd4 103 ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
mbed_official 0:51ac1d130fd4 104 } else
mbed_official 0:51ac1d130fd4 105 #endif /* LWIP_ETHERNET */
mbed_official 0:51ac1d130fd4 106 {
mbed_official 0:51ac1d130fd4 107 ip_input(msg->msg.inp.p, msg->msg.inp.netif);
mbed_official 0:51ac1d130fd4 108 }
mbed_official 0:51ac1d130fd4 109 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
mbed_official 0:51ac1d130fd4 110 break;
mbed_official 0:51ac1d130fd4 111 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
mbed_official 0:51ac1d130fd4 112
mbed_official 0:51ac1d130fd4 113 #if LWIP_NETIF_API
mbed_official 0:51ac1d130fd4 114 case TCPIP_MSG_NETIFAPI:
mbed_official 0:51ac1d130fd4 115 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg));
mbed_official 0:51ac1d130fd4 116 msg->msg.netifapimsg->function(&(msg->msg.netifapimsg->msg));
mbed_official 0:51ac1d130fd4 117 break;
mbed_official 0:51ac1d130fd4 118 #endif /* LWIP_NETIF_API */
mbed_official 0:51ac1d130fd4 119
mbed_official 0:51ac1d130fd4 120 case TCPIP_MSG_CALLBACK:
mbed_official 0:51ac1d130fd4 121 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
mbed_official 0:51ac1d130fd4 122 msg->msg.cb.function(msg->msg.cb.ctx);
mbed_official 0:51ac1d130fd4 123 memp_free(MEMP_TCPIP_MSG_API, msg);
mbed_official 0:51ac1d130fd4 124 break;
mbed_official 0:51ac1d130fd4 125
mbed_official 0:51ac1d130fd4 126 #if LWIP_TCPIP_TIMEOUT
mbed_official 0:51ac1d130fd4 127 case TCPIP_MSG_TIMEOUT:
mbed_official 0:51ac1d130fd4 128 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
mbed_official 0:51ac1d130fd4 129 sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
mbed_official 0:51ac1d130fd4 130 memp_free(MEMP_TCPIP_MSG_API, msg);
mbed_official 0:51ac1d130fd4 131 break;
mbed_official 0:51ac1d130fd4 132 case TCPIP_MSG_UNTIMEOUT:
mbed_official 0:51ac1d130fd4 133 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
mbed_official 0:51ac1d130fd4 134 sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
mbed_official 0:51ac1d130fd4 135 memp_free(MEMP_TCPIP_MSG_API, msg);
mbed_official 0:51ac1d130fd4 136 break;
mbed_official 0:51ac1d130fd4 137 #endif /* LWIP_TCPIP_TIMEOUT */
mbed_official 0:51ac1d130fd4 138
mbed_official 0:51ac1d130fd4 139 default:
mbed_official 0:51ac1d130fd4 140 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: %d\n", msg->type));
mbed_official 0:51ac1d130fd4 141 LWIP_ASSERT("tcpip_thread: invalid message", 0);
mbed_official 0:51ac1d130fd4 142 break;
mbed_official 0:51ac1d130fd4 143 }
mbed_official 0:51ac1d130fd4 144 }
mbed_official 0:51ac1d130fd4 145 }
mbed_official 0:51ac1d130fd4 146
mbed_official 0:51ac1d130fd4 147 /**
mbed_official 0:51ac1d130fd4 148 * Pass a received packet to tcpip_thread for input processing
mbed_official 0:51ac1d130fd4 149 *
mbed_official 0:51ac1d130fd4 150 * @param p the received packet, p->payload pointing to the Ethernet header or
mbed_official 0:51ac1d130fd4 151 * to an IP header (if inp doesn't have NETIF_FLAG_ETHARP or
mbed_official 0:51ac1d130fd4 152 * NETIF_FLAG_ETHERNET flags)
mbed_official 0:51ac1d130fd4 153 * @param inp the network interface on which the packet was received
mbed_official 0:51ac1d130fd4 154 */
mbed_official 0:51ac1d130fd4 155 err_t
mbed_official 0:51ac1d130fd4 156 tcpip_input(struct pbuf *p, struct netif *inp)
mbed_official 0:51ac1d130fd4 157 {
mbed_official 0:51ac1d130fd4 158 #if LWIP_TCPIP_CORE_LOCKING_INPUT
mbed_official 0:51ac1d130fd4 159 err_t ret;
mbed_official 0:51ac1d130fd4 160 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_input: PACKET %p/%p\n", (void *)p, (void *)inp));
mbed_official 0:51ac1d130fd4 161 LOCK_TCPIP_CORE();
mbed_official 0:51ac1d130fd4 162 #if LWIP_ETHERNET
mbed_official 0:51ac1d130fd4 163 if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
mbed_official 0:51ac1d130fd4 164 ret = ethernet_input(p, inp);
mbed_official 0:51ac1d130fd4 165 } else
mbed_official 0:51ac1d130fd4 166 #endif /* LWIP_ETHERNET */
mbed_official 0:51ac1d130fd4 167 {
mbed_official 0:51ac1d130fd4 168 ret = ip_input(p, inp);
mbed_official 0:51ac1d130fd4 169 }
mbed_official 0:51ac1d130fd4 170 UNLOCK_TCPIP_CORE();
mbed_official 0:51ac1d130fd4 171 return ret;
mbed_official 0:51ac1d130fd4 172 #else /* LWIP_TCPIP_CORE_LOCKING_INPUT */
mbed_official 0:51ac1d130fd4 173 struct tcpip_msg *msg;
mbed_official 0:51ac1d130fd4 174
mbed_official 0:51ac1d130fd4 175 if (sys_mbox_valid(&mbox)) {
mbed_official 0:51ac1d130fd4 176 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT);
mbed_official 0:51ac1d130fd4 177 if (msg == NULL) {
mbed_official 0:51ac1d130fd4 178 return ERR_MEM;
mbed_official 0:51ac1d130fd4 179 }
mbed_official 0:51ac1d130fd4 180
mbed_official 0:51ac1d130fd4 181 msg->type = TCPIP_MSG_INPKT;
mbed_official 0:51ac1d130fd4 182 msg->msg.inp.p = p;
mbed_official 0:51ac1d130fd4 183 msg->msg.inp.netif = inp;
mbed_official 0:51ac1d130fd4 184 if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
mbed_official 0:51ac1d130fd4 185 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
mbed_official 0:51ac1d130fd4 186 return ERR_MEM;
mbed_official 0:51ac1d130fd4 187 }
mbed_official 0:51ac1d130fd4 188 return ERR_OK;
mbed_official 0:51ac1d130fd4 189 }
mbed_official 0:51ac1d130fd4 190 return ERR_VAL;
mbed_official 0:51ac1d130fd4 191 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
mbed_official 0:51ac1d130fd4 192 }
mbed_official 0:51ac1d130fd4 193
mbed_official 0:51ac1d130fd4 194 /**
mbed_official 0:51ac1d130fd4 195 * Call a specific function in the thread context of
mbed_official 0:51ac1d130fd4 196 * tcpip_thread for easy access synchronization.
mbed_official 0:51ac1d130fd4 197 * A function called in that way may access lwIP core code
mbed_official 0:51ac1d130fd4 198 * without fearing concurrent access.
mbed_official 0:51ac1d130fd4 199 *
mbed_official 0:51ac1d130fd4 200 * @param f the function to call
mbed_official 0:51ac1d130fd4 201 * @param ctx parameter passed to f
mbed_official 0:51ac1d130fd4 202 * @param block 1 to block until the request is posted, 0 to non-blocking mode
mbed_official 0:51ac1d130fd4 203 * @return ERR_OK if the function was called, another err_t if not
mbed_official 0:51ac1d130fd4 204 */
mbed_official 0:51ac1d130fd4 205 err_t
mbed_official 0:51ac1d130fd4 206 tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
mbed_official 0:51ac1d130fd4 207 {
mbed_official 0:51ac1d130fd4 208 struct tcpip_msg *msg;
mbed_official 0:51ac1d130fd4 209
mbed_official 0:51ac1d130fd4 210 if (sys_mbox_valid(&mbox)) {
mbed_official 0:51ac1d130fd4 211 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
mbed_official 0:51ac1d130fd4 212 if (msg == NULL) {
mbed_official 0:51ac1d130fd4 213 return ERR_MEM;
mbed_official 0:51ac1d130fd4 214 }
mbed_official 0:51ac1d130fd4 215
mbed_official 0:51ac1d130fd4 216 msg->type = TCPIP_MSG_CALLBACK;
mbed_official 0:51ac1d130fd4 217 msg->msg.cb.function = function;
mbed_official 0:51ac1d130fd4 218 msg->msg.cb.ctx = ctx;
mbed_official 0:51ac1d130fd4 219 if (block) {
mbed_official 0:51ac1d130fd4 220 sys_mbox_post(&mbox, msg);
mbed_official 0:51ac1d130fd4 221 } else {
mbed_official 0:51ac1d130fd4 222 if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
mbed_official 0:51ac1d130fd4 223 memp_free(MEMP_TCPIP_MSG_API, msg);
mbed_official 0:51ac1d130fd4 224 return ERR_MEM;
mbed_official 0:51ac1d130fd4 225 }
mbed_official 0:51ac1d130fd4 226 }
mbed_official 0:51ac1d130fd4 227 return ERR_OK;
mbed_official 0:51ac1d130fd4 228 }
mbed_official 0:51ac1d130fd4 229 return ERR_VAL;
mbed_official 0:51ac1d130fd4 230 }
mbed_official 0:51ac1d130fd4 231
mbed_official 0:51ac1d130fd4 232 #if LWIP_TCPIP_TIMEOUT
mbed_official 0:51ac1d130fd4 233 /**
mbed_official 0:51ac1d130fd4 234 * call sys_timeout in tcpip_thread
mbed_official 0:51ac1d130fd4 235 *
mbed_official 0:51ac1d130fd4 236 * @param msec time in milliseconds for timeout
mbed_official 0:51ac1d130fd4 237 * @param h function to be called on timeout
mbed_official 0:51ac1d130fd4 238 * @param arg argument to pass to timeout function h
mbed_official 0:51ac1d130fd4 239 * @return ERR_MEM on memory error, ERR_OK otherwise
mbed_official 0:51ac1d130fd4 240 */
mbed_official 0:51ac1d130fd4 241 err_t
mbed_official 0:51ac1d130fd4 242 tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
mbed_official 0:51ac1d130fd4 243 {
mbed_official 0:51ac1d130fd4 244 struct tcpip_msg *msg;
mbed_official 0:51ac1d130fd4 245
mbed_official 0:51ac1d130fd4 246 if (sys_mbox_valid(&mbox)) {
mbed_official 0:51ac1d130fd4 247 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
mbed_official 0:51ac1d130fd4 248 if (msg == NULL) {
mbed_official 0:51ac1d130fd4 249 return ERR_MEM;
mbed_official 0:51ac1d130fd4 250 }
mbed_official 0:51ac1d130fd4 251
mbed_official 0:51ac1d130fd4 252 msg->type = TCPIP_MSG_TIMEOUT;
mbed_official 0:51ac1d130fd4 253 msg->msg.tmo.msecs = msecs;
mbed_official 0:51ac1d130fd4 254 msg->msg.tmo.h = h;
mbed_official 0:51ac1d130fd4 255 msg->msg.tmo.arg = arg;
mbed_official 0:51ac1d130fd4 256 sys_mbox_post(&mbox, msg);
mbed_official 0:51ac1d130fd4 257 return ERR_OK;
mbed_official 0:51ac1d130fd4 258 }
mbed_official 0:51ac1d130fd4 259 return ERR_VAL;
mbed_official 0:51ac1d130fd4 260 }
mbed_official 0:51ac1d130fd4 261
mbed_official 0:51ac1d130fd4 262 /**
mbed_official 0:51ac1d130fd4 263 * call sys_untimeout in tcpip_thread
mbed_official 0:51ac1d130fd4 264 *
mbed_official 0:51ac1d130fd4 265 * @param msec time in milliseconds for timeout
mbed_official 0:51ac1d130fd4 266 * @param h function to be called on timeout
mbed_official 0:51ac1d130fd4 267 * @param arg argument to pass to timeout function h
mbed_official 0:51ac1d130fd4 268 * @return ERR_MEM on memory error, ERR_OK otherwise
mbed_official 0:51ac1d130fd4 269 */
mbed_official 0:51ac1d130fd4 270 err_t
mbed_official 0:51ac1d130fd4 271 tcpip_untimeout(sys_timeout_handler h, void *arg)
mbed_official 0:51ac1d130fd4 272 {
mbed_official 0:51ac1d130fd4 273 struct tcpip_msg *msg;
mbed_official 0:51ac1d130fd4 274
mbed_official 0:51ac1d130fd4 275 if (sys_mbox_valid(&mbox)) {
mbed_official 0:51ac1d130fd4 276 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
mbed_official 0:51ac1d130fd4 277 if (msg == NULL) {
mbed_official 0:51ac1d130fd4 278 return ERR_MEM;
mbed_official 0:51ac1d130fd4 279 }
mbed_official 0:51ac1d130fd4 280
mbed_official 0:51ac1d130fd4 281 msg->type = TCPIP_MSG_UNTIMEOUT;
mbed_official 0:51ac1d130fd4 282 msg->msg.tmo.h = h;
mbed_official 0:51ac1d130fd4 283 msg->msg.tmo.arg = arg;
mbed_official 0:51ac1d130fd4 284 sys_mbox_post(&mbox, msg);
mbed_official 0:51ac1d130fd4 285 return ERR_OK;
mbed_official 0:51ac1d130fd4 286 }
mbed_official 0:51ac1d130fd4 287 return ERR_VAL;
mbed_official 0:51ac1d130fd4 288 }
mbed_official 0:51ac1d130fd4 289 #endif /* LWIP_TCPIP_TIMEOUT */
mbed_official 0:51ac1d130fd4 290
mbed_official 0:51ac1d130fd4 291 #if LWIP_NETCONN
mbed_official 0:51ac1d130fd4 292 /**
mbed_official 0:51ac1d130fd4 293 * Call the lower part of a netconn_* function
mbed_official 0:51ac1d130fd4 294 * This function is then running in the thread context
mbed_official 0:51ac1d130fd4 295 * of tcpip_thread and has exclusive access to lwIP core code.
mbed_official 0:51ac1d130fd4 296 *
mbed_official 0:51ac1d130fd4 297 * @param apimsg a struct containing the function to call and its parameters
mbed_official 0:51ac1d130fd4 298 * @return ERR_OK if the function was called, another err_t if not
mbed_official 0:51ac1d130fd4 299 */
mbed_official 0:51ac1d130fd4 300 err_t
mbed_official 0:51ac1d130fd4 301 tcpip_apimsg(struct api_msg *apimsg)
mbed_official 0:51ac1d130fd4 302 {
mbed_official 0:51ac1d130fd4 303 struct tcpip_msg msg;
mbed_official 0:51ac1d130fd4 304 #ifdef LWIP_DEBUG
mbed_official 0:51ac1d130fd4 305 /* catch functions that don't set err */
mbed_official 0:51ac1d130fd4 306 apimsg->msg.err = ERR_VAL;
mbed_official 0:51ac1d130fd4 307 #endif
mbed_official 0:51ac1d130fd4 308
mbed_official 0:51ac1d130fd4 309 if (sys_mbox_valid(&mbox)) {
mbed_official 0:51ac1d130fd4 310 msg.type = TCPIP_MSG_API;
mbed_official 0:51ac1d130fd4 311 msg.msg.apimsg = apimsg;
mbed_official 0:51ac1d130fd4 312 sys_mbox_post(&mbox, &msg);
mbed_official 0:51ac1d130fd4 313 sys_arch_sem_wait(&apimsg->msg.conn->op_completed, 0);
mbed_official 0:51ac1d130fd4 314 return apimsg->msg.err;
mbed_official 0:51ac1d130fd4 315 }
mbed_official 0:51ac1d130fd4 316 return ERR_VAL;
mbed_official 0:51ac1d130fd4 317 }
mbed_official 0:51ac1d130fd4 318
mbed_official 0:51ac1d130fd4 319 #if LWIP_TCPIP_CORE_LOCKING
mbed_official 0:51ac1d130fd4 320 /**
mbed_official 0:51ac1d130fd4 321 * Call the lower part of a netconn_* function
mbed_official 0:51ac1d130fd4 322 * This function has exclusive access to lwIP core code by locking it
mbed_official 0:51ac1d130fd4 323 * before the function is called.
mbed_official 0:51ac1d130fd4 324 *
mbed_official 0:51ac1d130fd4 325 * @param apimsg a struct containing the function to call and its parameters
mbed_official 0:51ac1d130fd4 326 * @return ERR_OK (only for compatibility fo tcpip_apimsg())
mbed_official 0:51ac1d130fd4 327 */
mbed_official 0:51ac1d130fd4 328 err_t
mbed_official 0:51ac1d130fd4 329 tcpip_apimsg_lock(struct api_msg *apimsg)
mbed_official 0:51ac1d130fd4 330 {
mbed_official 0:51ac1d130fd4 331 #ifdef LWIP_DEBUG
mbed_official 0:51ac1d130fd4 332 /* catch functions that don't set err */
mbed_official 0:51ac1d130fd4 333 apimsg->msg.err = ERR_VAL;
mbed_official 0:51ac1d130fd4 334 #endif
mbed_official 0:51ac1d130fd4 335
mbed_official 0:51ac1d130fd4 336 LOCK_TCPIP_CORE();
mbed_official 0:51ac1d130fd4 337 apimsg->function(&(apimsg->msg));
mbed_official 0:51ac1d130fd4 338 UNLOCK_TCPIP_CORE();
mbed_official 0:51ac1d130fd4 339 return apimsg->msg.err;
mbed_official 0:51ac1d130fd4 340
mbed_official 0:51ac1d130fd4 341 }
mbed_official 0:51ac1d130fd4 342 #endif /* LWIP_TCPIP_CORE_LOCKING */
mbed_official 0:51ac1d130fd4 343 #endif /* LWIP_NETCONN */
mbed_official 0:51ac1d130fd4 344
mbed_official 0:51ac1d130fd4 345 #if LWIP_NETIF_API
mbed_official 0:51ac1d130fd4 346 #if !LWIP_TCPIP_CORE_LOCKING
mbed_official 0:51ac1d130fd4 347 /**
mbed_official 0:51ac1d130fd4 348 * Much like tcpip_apimsg, but calls the lower part of a netifapi_*
mbed_official 0:51ac1d130fd4 349 * function.
mbed_official 0:51ac1d130fd4 350 *
mbed_official 0:51ac1d130fd4 351 * @param netifapimsg a struct containing the function to call and its parameters
mbed_official 0:51ac1d130fd4 352 * @return error code given back by the function that was called
mbed_official 0:51ac1d130fd4 353 */
mbed_official 0:51ac1d130fd4 354 err_t
mbed_official 0:51ac1d130fd4 355 tcpip_netifapi(struct netifapi_msg* netifapimsg)
mbed_official 0:51ac1d130fd4 356 {
mbed_official 0:51ac1d130fd4 357 struct tcpip_msg msg;
mbed_official 0:51ac1d130fd4 358
mbed_official 0:51ac1d130fd4 359 if (sys_mbox_valid(&mbox)) {
mbed_official 0:51ac1d130fd4 360 err_t err = sys_sem_new(&netifapimsg->msg.sem, 0);
mbed_official 0:51ac1d130fd4 361 if (err != ERR_OK) {
mbed_official 0:51ac1d130fd4 362 netifapimsg->msg.err = err;
mbed_official 0:51ac1d130fd4 363 return err;
mbed_official 0:51ac1d130fd4 364 }
mbed_official 0:51ac1d130fd4 365
mbed_official 0:51ac1d130fd4 366 msg.type = TCPIP_MSG_NETIFAPI;
mbed_official 0:51ac1d130fd4 367 msg.msg.netifapimsg = netifapimsg;
mbed_official 0:51ac1d130fd4 368 sys_mbox_post(&mbox, &msg);
mbed_official 0:51ac1d130fd4 369 sys_sem_wait(&netifapimsg->msg.sem);
mbed_official 0:51ac1d130fd4 370 sys_sem_free(&netifapimsg->msg.sem);
mbed_official 0:51ac1d130fd4 371 return netifapimsg->msg.err;
mbed_official 0:51ac1d130fd4 372 }
mbed_official 0:51ac1d130fd4 373 return ERR_VAL;
mbed_official 0:51ac1d130fd4 374 }
mbed_official 0:51ac1d130fd4 375 #else /* !LWIP_TCPIP_CORE_LOCKING */
mbed_official 0:51ac1d130fd4 376 /**
mbed_official 0:51ac1d130fd4 377 * Call the lower part of a netifapi_* function
mbed_official 0:51ac1d130fd4 378 * This function has exclusive access to lwIP core code by locking it
mbed_official 0:51ac1d130fd4 379 * before the function is called.
mbed_official 0:51ac1d130fd4 380 *
mbed_official 0:51ac1d130fd4 381 * @param netifapimsg a struct containing the function to call and its parameters
mbed_official 0:51ac1d130fd4 382 * @return ERR_OK (only for compatibility fo tcpip_netifapi())
mbed_official 0:51ac1d130fd4 383 */
mbed_official 0:51ac1d130fd4 384 err_t
mbed_official 0:51ac1d130fd4 385 tcpip_netifapi_lock(struct netifapi_msg* netifapimsg)
mbed_official 0:51ac1d130fd4 386 {
mbed_official 0:51ac1d130fd4 387 LOCK_TCPIP_CORE();
mbed_official 0:51ac1d130fd4 388 netifapimsg->function(&(netifapimsg->msg));
mbed_official 0:51ac1d130fd4 389 UNLOCK_TCPIP_CORE();
mbed_official 0:51ac1d130fd4 390 return netifapimsg->msg.err;
mbed_official 0:51ac1d130fd4 391 }
mbed_official 0:51ac1d130fd4 392 #endif /* !LWIP_TCPIP_CORE_LOCKING */
mbed_official 0:51ac1d130fd4 393 #endif /* LWIP_NETIF_API */
mbed_official 0:51ac1d130fd4 394
mbed_official 0:51ac1d130fd4 395 /**
mbed_official 0:51ac1d130fd4 396 * Initialize this module:
mbed_official 0:51ac1d130fd4 397 * - initialize all sub modules
mbed_official 0:51ac1d130fd4 398 * - start the tcpip_thread
mbed_official 0:51ac1d130fd4 399 *
mbed_official 0:51ac1d130fd4 400 * @param initfunc a function to call when tcpip_thread is running and finished initializing
mbed_official 0:51ac1d130fd4 401 * @param arg argument to pass to initfunc
mbed_official 0:51ac1d130fd4 402 */
mbed_official 0:51ac1d130fd4 403 void
mbed_official 0:51ac1d130fd4 404 tcpip_init(tcpip_init_done_fn initfunc, void *arg)
mbed_official 0:51ac1d130fd4 405 {
mbed_official 0:51ac1d130fd4 406 lwip_init();
mbed_official 0:51ac1d130fd4 407
mbed_official 0:51ac1d130fd4 408 tcpip_init_done = initfunc;
mbed_official 0:51ac1d130fd4 409 tcpip_init_done_arg = arg;
mbed_official 0:51ac1d130fd4 410 if(sys_mbox_new(&mbox, TCPIP_MBOX_SIZE) != ERR_OK) {
mbed_official 0:51ac1d130fd4 411 LWIP_ASSERT("failed to create tcpip_thread mbox", 0);
mbed_official 0:51ac1d130fd4 412 }
mbed_official 0:51ac1d130fd4 413 #if LWIP_TCPIP_CORE_LOCKING
mbed_official 0:51ac1d130fd4 414 if(sys_mutex_new(&lock_tcpip_core) != ERR_OK) {
mbed_official 0:51ac1d130fd4 415 LWIP_ASSERT("failed to create lock_tcpip_core", 0);
mbed_official 0:51ac1d130fd4 416 }
mbed_official 0:51ac1d130fd4 417 #endif /* LWIP_TCPIP_CORE_LOCKING */
mbed_official 0:51ac1d130fd4 418
mbed_official 0:51ac1d130fd4 419 sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
mbed_official 0:51ac1d130fd4 420 }
mbed_official 0:51ac1d130fd4 421
mbed_official 0:51ac1d130fd4 422 /**
mbed_official 0:51ac1d130fd4 423 * Simple callback function used with tcpip_callback to free a pbuf
mbed_official 0:51ac1d130fd4 424 * (pbuf_free has a wrong signature for tcpip_callback)
mbed_official 0:51ac1d130fd4 425 *
mbed_official 0:51ac1d130fd4 426 * @param p The pbuf (chain) to be dereferenced.
mbed_official 0:51ac1d130fd4 427 */
mbed_official 0:51ac1d130fd4 428 static void
mbed_official 0:51ac1d130fd4 429 pbuf_free_int(void *p)
mbed_official 0:51ac1d130fd4 430 {
mbed_official 0:51ac1d130fd4 431 struct pbuf *q = (struct pbuf *)p;
mbed_official 0:51ac1d130fd4 432 pbuf_free(q);
mbed_official 0:51ac1d130fd4 433 }
mbed_official 0:51ac1d130fd4 434
mbed_official 0:51ac1d130fd4 435 /**
mbed_official 0:51ac1d130fd4 436 * A simple wrapper function that allows you to free a pbuf from interrupt context.
mbed_official 0:51ac1d130fd4 437 *
mbed_official 0:51ac1d130fd4 438 * @param p The pbuf (chain) to be dereferenced.
mbed_official 0:51ac1d130fd4 439 * @return ERR_OK if callback could be enqueued, an err_t if not
mbed_official 0:51ac1d130fd4 440 */
mbed_official 0:51ac1d130fd4 441 err_t
mbed_official 0:51ac1d130fd4 442 pbuf_free_callback(struct pbuf *p)
mbed_official 0:51ac1d130fd4 443 {
mbed_official 0:51ac1d130fd4 444 return tcpip_callback_with_block(pbuf_free_int, p, 0);
mbed_official 0:51ac1d130fd4 445 }
mbed_official 0:51ac1d130fd4 446
mbed_official 0:51ac1d130fd4 447 /**
mbed_official 0:51ac1d130fd4 448 * A simple wrapper function that allows you to free heap memory from
mbed_official 0:51ac1d130fd4 449 * interrupt context.
mbed_official 0:51ac1d130fd4 450 *
mbed_official 0:51ac1d130fd4 451 * @param m the heap memory to free
mbed_official 0:51ac1d130fd4 452 * @return ERR_OK if callback could be enqueued, an err_t if not
mbed_official 0:51ac1d130fd4 453 */
mbed_official 0:51ac1d130fd4 454 err_t
mbed_official 0:51ac1d130fd4 455 mem_free_callback(void *m)
mbed_official 0:51ac1d130fd4 456 {
mbed_official 0:51ac1d130fd4 457 return tcpip_callback_with_block(mem_free, m, 0);
mbed_official 0:51ac1d130fd4 458 }
mbed_official 0:51ac1d130fd4 459
mbed_official 0:51ac1d130fd4 460 #endif /* !NO_SYS */