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