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