Philippe Bazot / Mbed 2 deprecated DU4SmartCities

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
icraggs
Date:
Wed Oct 01 13:27:35 2014 +0000
Revision:
8:80d49dd91542
Parent:
6:37b6d0d56190
Remove conditional compilation for IBM IoT settings

Who changed what in which revision?

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