fdsf

Dependents:   sisk_proj_stat MQTT Hello_FXOS8700Q WireFSHandControl ... more

Committer:
grzemich
Date:
Wed Dec 07 23:47:50 2016 +0000
Revision:
0:d7bd7384a37c
dgd

Who changed what in which revision?

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