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