Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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