Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

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