3 * Sequential API Main thread module
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
41 #if !NO_SYS /* don't build if not configured for use in lwipopts.h */
44 #include "lwip/memp.h"
46 #include "lwip/pbuf.h"
47 #include "lwip/tcpip.h"
48 #include "lwip/init.h"
49 #include "netif/etharp.h"
50 #include "netif/ppp_oe.h"
52 /* global variables */
53 static tcpip_init_done_fn tcpip_init_done;
54 static void *tcpip_init_done_arg;
55 static sys_mbox_t mbox;
57 #if LWIP_TCPIP_CORE_LOCKING
58 /** The global semaphore to lock the stack. */
59 sys_mutex_t lock_tcpip_core;
60 #endif /* LWIP_TCPIP_CORE_LOCKING */
64 * The main lwIP thread. This thread has exclusive access to lwIP core functions
65 * (unless access to them is not locked). Other threads communicate with this
66 * thread using message boxes.
68 * It also starts all the timers to make sure they are running in the right
71 * @param arg unused argument
74 tcpip_thread(void *arg)
76 struct tcpip_msg *msg;
79 if (tcpip_init_done != NULL) {
80 tcpip_init_done(tcpip_init_done_arg);
84 while (1) { /* MAIN Loop */
86 /* wait for a message, timeouts are processed while waiting */
87 sys_timeouts_mbox_fetch(&mbox, (void **)&msg);
92 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
93 msg->msg.apimsg->function(&(msg->msg.apimsg->msg));
95 #endif /* LWIP_NETCONN */
98 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
100 if (msg->msg.inp.netif->flags & NETIF_FLAG_ETHARP) {
101 ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
103 #endif /* LWIP_ETHERNET */
104 { ip_input(msg->msg.inp.p, msg->msg.inp.netif);
106 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
110 case TCPIP_MSG_NETIFAPI:
111 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg));
112 msg->msg.netifapimsg->function(&(msg->msg.netifapimsg->msg));
114 #endif /* LWIP_NETIF_API */
116 case TCPIP_MSG_CALLBACK:
117 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
118 msg->msg.cb.function(msg->msg.cb.ctx);
119 memp_free(MEMP_TCPIP_MSG_API, msg);
122 case TCPIP_MSG_TIMEOUT:
123 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
124 sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
125 memp_free(MEMP_TCPIP_MSG_API, msg);
127 case TCPIP_MSG_UNTIMEOUT:
128 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
129 sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
130 memp_free(MEMP_TCPIP_MSG_API, msg);
140 * Pass a received packet to tcpip_thread for input processing
142 * @param p the received packet, p->payload pointing to the Ethernet header or
143 * to an IP header (if netif doesn't got NETIF_FLAG_ETHARP flag)
144 * @param inp the network interface on which the packet was received
147 tcpip_input(struct pbuf *p, struct netif *inp)
149 struct tcpip_msg *msg;
151 if (sys_mbox_valid(&mbox)) {
152 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT);
157 msg->type = TCPIP_MSG_INPKT;
159 msg->msg.inp.netif = inp;
160 if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
161 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
170 * Call a specific function in the thread context of
171 * tcpip_thread for easy access synchronization.
172 * A function called in that way may access lwIP core code
173 * without fearing concurrent access.
175 * @param f the function to call
176 * @param ctx parameter passed to f
177 * @param block 1 to block until the request is posted, 0 to non-blocking mode
178 * @return ERR_OK if the function was called, another err_t if not
181 tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
183 struct tcpip_msg *msg;
185 if (sys_mbox_valid(&mbox)) {
186 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
191 msg->type = TCPIP_MSG_CALLBACK;
192 msg->msg.cb.function = function;
193 msg->msg.cb.ctx = ctx;
195 sys_mbox_post(&mbox, msg);
197 if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
198 memp_free(MEMP_TCPIP_MSG_API, msg);
208 * call sys_timeout in tcpip_thread
210 * @param msec time in miliseconds for timeout
211 * @param h function to be called on timeout
212 * @param arg argument to pass to timeout function h
213 * @return ERR_MEM on memory error, ERR_OK otherwise
216 tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
218 struct tcpip_msg *msg;
220 if (sys_mbox_valid(&mbox)) {
221 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
226 msg->type = TCPIP_MSG_TIMEOUT;
227 msg->msg.tmo.msecs = msecs;
229 msg->msg.tmo.arg = arg;
230 sys_mbox_post(&mbox, msg);
237 * call sys_untimeout in tcpip_thread
239 * @param msec time in miliseconds for timeout
240 * @param h function to be called on timeout
241 * @param arg argument to pass to timeout function h
242 * @return ERR_MEM on memory error, ERR_OK otherwise
245 tcpip_untimeout(sys_timeout_handler h, void *arg)
247 struct tcpip_msg *msg;
249 if (sys_mbox_valid(&mbox)) {
250 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
255 msg->type = TCPIP_MSG_UNTIMEOUT;
257 msg->msg.tmo.arg = arg;
258 sys_mbox_post(&mbox, msg);
266 * Call the lower part of a netconn_* function
267 * This function is then running in the thread context
268 * of tcpip_thread and has exclusive access to lwIP core code.
270 * @param apimsg a struct containing the function to call and its parameters
271 * @return ERR_OK if the function was called, another err_t if not
274 tcpip_apimsg(struct api_msg *apimsg)
276 struct tcpip_msg msg;
278 /* catch functions that don't set err */
279 apimsg->msg.err = ERR_VAL;
282 if (sys_mbox_valid(&mbox)) {
283 msg.type = TCPIP_MSG_API;
284 msg.msg.apimsg = apimsg;
285 sys_mbox_post(&mbox, &msg);
286 sys_arch_sem_wait(&apimsg->msg.conn->op_completed, 0);
287 return apimsg->msg.err;
292 #if LWIP_TCPIP_CORE_LOCKING
294 * Call the lower part of a netconn_* function
295 * This function has exclusive access to lwIP core code by locking it
296 * before the function is called.
298 * @param apimsg a struct containing the function to call and its parameters
299 * @return ERR_OK (only for compatibility fo tcpip_apimsg())
302 tcpip_apimsg_lock(struct api_msg *apimsg)
305 /* catch functions that don't set err */
306 apimsg->msg.err = ERR_VAL;
310 apimsg->function(&(apimsg->msg));
312 return apimsg->msg.err;
315 #endif /* LWIP_TCPIP_CORE_LOCKING */
316 #endif /* LWIP_NETCONN */
319 #if !LWIP_TCPIP_CORE_LOCKING
321 * Much like tcpip_apimsg, but calls the lower part of a netifapi_*
324 * @param netifapimsg a struct containing the function to call and its parameters
325 * @return error code given back by the function that was called
328 tcpip_netifapi(struct netifapi_msg* netifapimsg)
330 struct tcpip_msg msg;
332 if (sys_mbox_valid(&mbox)) {
333 err_t err = sys_sem_new(&netifapimsg->msg.sem, 0);
335 netifapimsg->msg.err = err;
339 msg.type = TCPIP_MSG_NETIFAPI;
340 msg.msg.netifapimsg = netifapimsg;
341 sys_mbox_post(&mbox, &msg);
342 sys_sem_wait(&netifapimsg->msg.sem);
343 sys_sem_free(&netifapimsg->msg.sem);
344 return netifapimsg->msg.err;
348 #else /* !LWIP_TCPIP_CORE_LOCKING */
350 * Call the lower part of a netifapi_* function
351 * This function has exclusive access to lwIP core code by locking it
352 * before the function is called.
354 * @param netifapimsg a struct containing the function to call and its parameters
355 * @return ERR_OK (only for compatibility fo tcpip_netifapi())
358 tcpip_netifapi_lock(struct netifapi_msg* netifapimsg)
361 netifapimsg->function(&(netifapimsg->msg));
363 return netifapimsg->msg.err;
365 #endif /* !LWIP_TCPIP_CORE_LOCKING */
366 #endif /* LWIP_NETIF_API */
369 * Initialize this module:
370 * - initialize all sub modules
371 * - start the tcpip_thread
373 * @param initfunc a function to call when tcpip_thread is running and finished initializing
374 * @param arg argument to pass to initfunc
377 tcpip_init(tcpip_init_done_fn initfunc, void *arg)
381 tcpip_init_done = initfunc;
382 tcpip_init_done_arg = arg;
383 if(sys_mbox_new(&mbox, TCPIP_MBOX_SIZE) != ERR_OK) {
384 LWIP_ASSERT("failed to create tcpip_thread mbox", 0);
386 #if LWIP_TCPIP_CORE_LOCKING
387 if(sys_mutex_new(&lock_tcpip_core) != ERR_OK) {
388 LWIP_ASSERT("failed to create lock_tcpip_core", 0);
390 #endif /* LWIP_TCPIP_CORE_LOCKING */
392 sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
396 * Simple callback function used with tcpip_callback to free a pbuf
397 * (pbuf_free has a wrong signature for tcpip_callback)
399 * @param p The pbuf (chain) to be dereferenced.
402 pbuf_free_int(void *p)
404 struct pbuf *q = (struct pbuf *)p;
409 * A simple wrapper function that allows you to free a pbuf from interrupt context.
411 * @param p The pbuf (chain) to be dereferenced.
412 * @return ERR_OK if callback could be enqueued, an err_t if not
415 pbuf_free_callback(struct pbuf *p)
417 return tcpip_callback_with_block(pbuf_free_int, p, 0);
421 * A simple wrapper function that allows you to free heap memory from
424 * @param m the heap memory to free
425 * @return ERR_OK if callback could be enqueued, an err_t if not
428 mem_free_callback(void *m)
430 return tcpip_callback_with_block(mem_free, m, 0);