1*10465441SEvalZero /**
2*10465441SEvalZero * @file
3*10465441SEvalZero * Sequential API Main thread module
4*10465441SEvalZero *
5*10465441SEvalZero */
6*10465441SEvalZero
7*10465441SEvalZero /*
8*10465441SEvalZero * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9*10465441SEvalZero * All rights reserved.
10*10465441SEvalZero *
11*10465441SEvalZero * Redistribution and use in source and binary forms, with or without modification,
12*10465441SEvalZero * are permitted provided that the following conditions are met:
13*10465441SEvalZero *
14*10465441SEvalZero * 1. Redistributions of source code must retain the above copyright notice,
15*10465441SEvalZero * this list of conditions and the following disclaimer.
16*10465441SEvalZero * 2. Redistributions in binary form must reproduce the above copyright notice,
17*10465441SEvalZero * this list of conditions and the following disclaimer in the documentation
18*10465441SEvalZero * and/or other materials provided with the distribution.
19*10465441SEvalZero * 3. The name of the author may not be used to endorse or promote products
20*10465441SEvalZero * derived from this software without specific prior written permission.
21*10465441SEvalZero *
22*10465441SEvalZero * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23*10465441SEvalZero * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24*10465441SEvalZero * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25*10465441SEvalZero * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26*10465441SEvalZero * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27*10465441SEvalZero * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*10465441SEvalZero * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29*10465441SEvalZero * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30*10465441SEvalZero * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31*10465441SEvalZero * OF SUCH DAMAGE.
32*10465441SEvalZero *
33*10465441SEvalZero * This file is part of the lwIP TCP/IP stack.
34*10465441SEvalZero *
35*10465441SEvalZero * Author: Adam Dunkels <[email protected]>
36*10465441SEvalZero *
37*10465441SEvalZero */
38*10465441SEvalZero
39*10465441SEvalZero #include "lwip/opt.h"
40*10465441SEvalZero
41*10465441SEvalZero #if !NO_SYS /* don't build if not configured for use in lwipopts.h */
42*10465441SEvalZero
43*10465441SEvalZero #include "lwip/priv/tcpip_priv.h"
44*10465441SEvalZero #include "lwip/sys.h"
45*10465441SEvalZero #include "lwip/memp.h"
46*10465441SEvalZero #include "lwip/mem.h"
47*10465441SEvalZero #include "lwip/init.h"
48*10465441SEvalZero #include "lwip/ip.h"
49*10465441SEvalZero #include "lwip/pbuf.h"
50*10465441SEvalZero #include "lwip/etharp.h"
51*10465441SEvalZero #include "netif/ethernet.h"
52*10465441SEvalZero
53*10465441SEvalZero #define TCPIP_MSG_VAR_REF(name) API_VAR_REF(name)
54*10465441SEvalZero #define TCPIP_MSG_VAR_DECLARE(name) API_VAR_DECLARE(struct tcpip_msg, name)
55*10465441SEvalZero #define TCPIP_MSG_VAR_ALLOC(name) API_VAR_ALLOC(struct tcpip_msg, MEMP_TCPIP_MSG_API, name, ERR_MEM)
56*10465441SEvalZero #define TCPIP_MSG_VAR_FREE(name) API_VAR_FREE(MEMP_TCPIP_MSG_API, name)
57*10465441SEvalZero
58*10465441SEvalZero /* global variables */
59*10465441SEvalZero static tcpip_init_done_fn tcpip_init_done;
60*10465441SEvalZero static void *tcpip_init_done_arg;
61*10465441SEvalZero static sys_mbox_t tcpip_mbox;
62*10465441SEvalZero
63*10465441SEvalZero #if LWIP_TCPIP_CORE_LOCKING
64*10465441SEvalZero /** The global semaphore to lock the stack. */
65*10465441SEvalZero sys_mutex_t lock_tcpip_core;
66*10465441SEvalZero #endif /* LWIP_TCPIP_CORE_LOCKING */
67*10465441SEvalZero
68*10465441SEvalZero static void tcpip_thread_handle_msg(struct tcpip_msg *msg);
69*10465441SEvalZero
70*10465441SEvalZero #if !LWIP_TIMERS
71*10465441SEvalZero /* wait for a message with timers disabled (e.g. pass a timer-check trigger into tcpip_thread) */
72*10465441SEvalZero #define TCPIP_MBOX_FETCH(mbox, msg) sys_mbox_fetch(mbox, msg)
73*10465441SEvalZero #else /* !LWIP_TIMERS */
74*10465441SEvalZero /* wait for a message, timeouts are processed while waiting */
75*10465441SEvalZero #define TCPIP_MBOX_FETCH(mbox, msg) tcpip_timeouts_mbox_fetch(mbox, msg)
76*10465441SEvalZero /**
77*10465441SEvalZero * Wait (forever) for a message to arrive in an mbox.
78*10465441SEvalZero * While waiting, timeouts are processed.
79*10465441SEvalZero *
80*10465441SEvalZero * @param mbox the mbox to fetch the message from
81*10465441SEvalZero * @param msg the place to store the message
82*10465441SEvalZero */
83*10465441SEvalZero static void
tcpip_timeouts_mbox_fetch(sys_mbox_t * mbox,void ** msg)84*10465441SEvalZero tcpip_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
85*10465441SEvalZero {
86*10465441SEvalZero u32_t sleeptime, res;
87*10465441SEvalZero
88*10465441SEvalZero again:
89*10465441SEvalZero LWIP_ASSERT_CORE_LOCKED();
90*10465441SEvalZero
91*10465441SEvalZero sleeptime = sys_timeouts_sleeptime();
92*10465441SEvalZero if (sleeptime == SYS_TIMEOUTS_SLEEPTIME_INFINITE) {
93*10465441SEvalZero UNLOCK_TCPIP_CORE();
94*10465441SEvalZero sys_arch_mbox_fetch(mbox, msg, 0);
95*10465441SEvalZero LOCK_TCPIP_CORE();
96*10465441SEvalZero return;
97*10465441SEvalZero } else if (sleeptime == 0) {
98*10465441SEvalZero sys_check_timeouts();
99*10465441SEvalZero /* We try again to fetch a message from the mbox. */
100*10465441SEvalZero goto again;
101*10465441SEvalZero }
102*10465441SEvalZero
103*10465441SEvalZero UNLOCK_TCPIP_CORE();
104*10465441SEvalZero res = sys_arch_mbox_fetch(mbox, msg, sleeptime);
105*10465441SEvalZero LOCK_TCPIP_CORE();
106*10465441SEvalZero if (res == SYS_ARCH_TIMEOUT) {
107*10465441SEvalZero /* If a SYS_ARCH_TIMEOUT value is returned, a timeout occurred
108*10465441SEvalZero before a message could be fetched. */
109*10465441SEvalZero sys_check_timeouts();
110*10465441SEvalZero /* We try again to fetch a message from the mbox. */
111*10465441SEvalZero goto again;
112*10465441SEvalZero }
113*10465441SEvalZero }
114*10465441SEvalZero #endif /* !LWIP_TIMERS */
115*10465441SEvalZero
116*10465441SEvalZero /**
117*10465441SEvalZero * The main lwIP thread. This thread has exclusive access to lwIP core functions
118*10465441SEvalZero * (unless access to them is not locked). Other threads communicate with this
119*10465441SEvalZero * thread using message boxes.
120*10465441SEvalZero *
121*10465441SEvalZero * It also starts all the timers to make sure they are running in the right
122*10465441SEvalZero * thread context.
123*10465441SEvalZero *
124*10465441SEvalZero * @param arg unused argument
125*10465441SEvalZero */
126*10465441SEvalZero static void
tcpip_thread(void * arg)127*10465441SEvalZero tcpip_thread(void *arg)
128*10465441SEvalZero {
129*10465441SEvalZero struct tcpip_msg *msg;
130*10465441SEvalZero LWIP_UNUSED_ARG(arg);
131*10465441SEvalZero
132*10465441SEvalZero LWIP_MARK_TCPIP_THREAD();
133*10465441SEvalZero
134*10465441SEvalZero LOCK_TCPIP_CORE();
135*10465441SEvalZero if (tcpip_init_done != NULL) {
136*10465441SEvalZero tcpip_init_done(tcpip_init_done_arg);
137*10465441SEvalZero }
138*10465441SEvalZero
139*10465441SEvalZero while (1) { /* MAIN Loop */
140*10465441SEvalZero LWIP_TCPIP_THREAD_ALIVE();
141*10465441SEvalZero /* wait for a message, timeouts are processed while waiting */
142*10465441SEvalZero TCPIP_MBOX_FETCH(&tcpip_mbox, (void **)&msg);
143*10465441SEvalZero if (msg == NULL) {
144*10465441SEvalZero LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: NULL\n"));
145*10465441SEvalZero LWIP_ASSERT("tcpip_thread: invalid message", 0);
146*10465441SEvalZero continue;
147*10465441SEvalZero }
148*10465441SEvalZero tcpip_thread_handle_msg(msg);
149*10465441SEvalZero }
150*10465441SEvalZero }
151*10465441SEvalZero
152*10465441SEvalZero /* Handle a single tcpip_msg
153*10465441SEvalZero * This is in its own function for access by tests only.
154*10465441SEvalZero */
155*10465441SEvalZero static void
tcpip_thread_handle_msg(struct tcpip_msg * msg)156*10465441SEvalZero tcpip_thread_handle_msg(struct tcpip_msg *msg)
157*10465441SEvalZero {
158*10465441SEvalZero switch (msg->type) {
159*10465441SEvalZero #if !LWIP_TCPIP_CORE_LOCKING
160*10465441SEvalZero case TCPIP_MSG_API:
161*10465441SEvalZero LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
162*10465441SEvalZero msg->msg.api_msg.function(msg->msg.api_msg.msg);
163*10465441SEvalZero break;
164*10465441SEvalZero case TCPIP_MSG_API_CALL:
165*10465441SEvalZero LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API CALL message %p\n", (void *)msg));
166*10465441SEvalZero msg->msg.api_call.arg->err = msg->msg.api_call.function(msg->msg.api_call.arg);
167*10465441SEvalZero sys_sem_signal(msg->msg.api_call.sem);
168*10465441SEvalZero break;
169*10465441SEvalZero #endif /* !LWIP_TCPIP_CORE_LOCKING */
170*10465441SEvalZero
171*10465441SEvalZero #if !LWIP_TCPIP_CORE_LOCKING_INPUT
172*10465441SEvalZero case TCPIP_MSG_INPKT:
173*10465441SEvalZero LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
174*10465441SEvalZero if (msg->msg.inp.input_fn(msg->msg.inp.p, msg->msg.inp.netif) != ERR_OK) {
175*10465441SEvalZero pbuf_free(msg->msg.inp.p);
176*10465441SEvalZero }
177*10465441SEvalZero memp_free(MEMP_TCPIP_MSG_INPKT, msg);
178*10465441SEvalZero break;
179*10465441SEvalZero #endif /* !LWIP_TCPIP_CORE_LOCKING_INPUT */
180*10465441SEvalZero
181*10465441SEvalZero #if LWIP_TCPIP_TIMEOUT && LWIP_TIMERS
182*10465441SEvalZero case TCPIP_MSG_TIMEOUT:
183*10465441SEvalZero LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
184*10465441SEvalZero sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
185*10465441SEvalZero memp_free(MEMP_TCPIP_MSG_API, msg);
186*10465441SEvalZero break;
187*10465441SEvalZero case TCPIP_MSG_UNTIMEOUT:
188*10465441SEvalZero LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
189*10465441SEvalZero sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
190*10465441SEvalZero memp_free(MEMP_TCPIP_MSG_API, msg);
191*10465441SEvalZero break;
192*10465441SEvalZero #endif /* LWIP_TCPIP_TIMEOUT && LWIP_TIMERS */
193*10465441SEvalZero
194*10465441SEvalZero case TCPIP_MSG_CALLBACK:
195*10465441SEvalZero LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
196*10465441SEvalZero msg->msg.cb.function(msg->msg.cb.ctx);
197*10465441SEvalZero memp_free(MEMP_TCPIP_MSG_API, msg);
198*10465441SEvalZero break;
199*10465441SEvalZero
200*10465441SEvalZero case TCPIP_MSG_CALLBACK_STATIC:
201*10465441SEvalZero LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK_STATIC %p\n", (void *)msg));
202*10465441SEvalZero msg->msg.cb.function(msg->msg.cb.ctx);
203*10465441SEvalZero break;
204*10465441SEvalZero
205*10465441SEvalZero default:
206*10465441SEvalZero LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: %d\n", msg->type));
207*10465441SEvalZero LWIP_ASSERT("tcpip_thread: invalid message", 0);
208*10465441SEvalZero break;
209*10465441SEvalZero }
210*10465441SEvalZero }
211*10465441SEvalZero
212*10465441SEvalZero #ifdef TCPIP_THREAD_TEST
213*10465441SEvalZero /** Work on queued items in single-threaded test mode */
214*10465441SEvalZero int
tcpip_thread_poll_one(void)215*10465441SEvalZero tcpip_thread_poll_one(void)
216*10465441SEvalZero {
217*10465441SEvalZero int ret = 0;
218*10465441SEvalZero struct tcpip_msg *msg;
219*10465441SEvalZero
220*10465441SEvalZero if (sys_arch_mbox_tryfetch(&tcpip_mbox, (void **)&msg) != SYS_ARCH_TIMEOUT) {
221*10465441SEvalZero LOCK_TCPIP_CORE();
222*10465441SEvalZero if (msg != NULL) {
223*10465441SEvalZero tcpip_thread_handle_msg(msg);
224*10465441SEvalZero ret = 1;
225*10465441SEvalZero }
226*10465441SEvalZero UNLOCK_TCPIP_CORE();
227*10465441SEvalZero }
228*10465441SEvalZero return ret;
229*10465441SEvalZero }
230*10465441SEvalZero #endif
231*10465441SEvalZero
232*10465441SEvalZero /**
233*10465441SEvalZero * Pass a received packet to tcpip_thread for input processing
234*10465441SEvalZero *
235*10465441SEvalZero * @param p the received packet
236*10465441SEvalZero * @param inp the network interface on which the packet was received
237*10465441SEvalZero * @param input_fn input function to call
238*10465441SEvalZero */
239*10465441SEvalZero err_t
tcpip_inpkt(struct pbuf * p,struct netif * inp,netif_input_fn input_fn)240*10465441SEvalZero tcpip_inpkt(struct pbuf *p, struct netif *inp, netif_input_fn input_fn)
241*10465441SEvalZero {
242*10465441SEvalZero #if LWIP_TCPIP_CORE_LOCKING_INPUT
243*10465441SEvalZero err_t ret;
244*10465441SEvalZero LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_inpkt: PACKET %p/%p\n", (void *)p, (void *)inp));
245*10465441SEvalZero LOCK_TCPIP_CORE();
246*10465441SEvalZero ret = input_fn(p, inp);
247*10465441SEvalZero UNLOCK_TCPIP_CORE();
248*10465441SEvalZero return ret;
249*10465441SEvalZero #else /* LWIP_TCPIP_CORE_LOCKING_INPUT */
250*10465441SEvalZero struct tcpip_msg *msg;
251*10465441SEvalZero
252*10465441SEvalZero LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
253*10465441SEvalZero
254*10465441SEvalZero msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT);
255*10465441SEvalZero if (msg == NULL) {
256*10465441SEvalZero return ERR_MEM;
257*10465441SEvalZero }
258*10465441SEvalZero
259*10465441SEvalZero msg->type = TCPIP_MSG_INPKT;
260*10465441SEvalZero msg->msg.inp.p = p;
261*10465441SEvalZero msg->msg.inp.netif = inp;
262*10465441SEvalZero msg->msg.inp.input_fn = input_fn;
263*10465441SEvalZero if (sys_mbox_trypost(&tcpip_mbox, msg) != ERR_OK) {
264*10465441SEvalZero memp_free(MEMP_TCPIP_MSG_INPKT, msg);
265*10465441SEvalZero return ERR_MEM;
266*10465441SEvalZero }
267*10465441SEvalZero return ERR_OK;
268*10465441SEvalZero #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
269*10465441SEvalZero }
270*10465441SEvalZero
271*10465441SEvalZero /**
272*10465441SEvalZero * @ingroup lwip_os
273*10465441SEvalZero * Pass a received packet to tcpip_thread for input processing with
274*10465441SEvalZero * ethernet_input or ip_input. Don't call directly, pass to netif_add()
275*10465441SEvalZero * and call netif->input().
276*10465441SEvalZero *
277*10465441SEvalZero * @param p the received packet, p->payload pointing to the Ethernet header or
278*10465441SEvalZero * to an IP header (if inp doesn't have NETIF_FLAG_ETHARP or
279*10465441SEvalZero * NETIF_FLAG_ETHERNET flags)
280*10465441SEvalZero * @param inp the network interface on which the packet was received
281*10465441SEvalZero */
282*10465441SEvalZero err_t
tcpip_input(struct pbuf * p,struct netif * inp)283*10465441SEvalZero tcpip_input(struct pbuf *p, struct netif *inp)
284*10465441SEvalZero {
285*10465441SEvalZero #if LWIP_ETHERNET
286*10465441SEvalZero if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
287*10465441SEvalZero return tcpip_inpkt(p, inp, ethernet_input);
288*10465441SEvalZero } else
289*10465441SEvalZero #endif /* LWIP_ETHERNET */
290*10465441SEvalZero return tcpip_inpkt(p, inp, ip_input);
291*10465441SEvalZero }
292*10465441SEvalZero
293*10465441SEvalZero /**
294*10465441SEvalZero * @ingroup lwip_os
295*10465441SEvalZero * Call a specific function in the thread context of
296*10465441SEvalZero * tcpip_thread for easy access synchronization.
297*10465441SEvalZero * A function called in that way may access lwIP core code
298*10465441SEvalZero * without fearing concurrent access.
299*10465441SEvalZero * Blocks until the request is posted.
300*10465441SEvalZero * Must not be called from interrupt context!
301*10465441SEvalZero *
302*10465441SEvalZero * @param function the function to call
303*10465441SEvalZero * @param ctx parameter passed to f
304*10465441SEvalZero * @return ERR_OK if the function was called, another err_t if not
305*10465441SEvalZero *
306*10465441SEvalZero * @see tcpip_try_callback
307*10465441SEvalZero */
308*10465441SEvalZero err_t
tcpip_callback(tcpip_callback_fn function,void * ctx)309*10465441SEvalZero tcpip_callback(tcpip_callback_fn function, void *ctx)
310*10465441SEvalZero {
311*10465441SEvalZero struct tcpip_msg *msg;
312*10465441SEvalZero
313*10465441SEvalZero LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
314*10465441SEvalZero
315*10465441SEvalZero msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
316*10465441SEvalZero if (msg == NULL) {
317*10465441SEvalZero return ERR_MEM;
318*10465441SEvalZero }
319*10465441SEvalZero
320*10465441SEvalZero msg->type = TCPIP_MSG_CALLBACK;
321*10465441SEvalZero msg->msg.cb.function = function;
322*10465441SEvalZero msg->msg.cb.ctx = ctx;
323*10465441SEvalZero
324*10465441SEvalZero sys_mbox_post(&tcpip_mbox, msg);
325*10465441SEvalZero return ERR_OK;
326*10465441SEvalZero }
327*10465441SEvalZero
328*10465441SEvalZero /**
329*10465441SEvalZero * @ingroup lwip_os
330*10465441SEvalZero * Call a specific function in the thread context of
331*10465441SEvalZero * tcpip_thread for easy access synchronization.
332*10465441SEvalZero * A function called in that way may access lwIP core code
333*10465441SEvalZero * without fearing concurrent access.
334*10465441SEvalZero * Does NOT block when the request cannot be posted because the
335*10465441SEvalZero * tcpip_mbox is full, but returns ERR_MEM instead.
336*10465441SEvalZero * Can be called from interrupt context.
337*10465441SEvalZero *
338*10465441SEvalZero * @param function the function to call
339*10465441SEvalZero * @param ctx parameter passed to f
340*10465441SEvalZero * @return ERR_OK if the function was called, another err_t if not
341*10465441SEvalZero *
342*10465441SEvalZero * @see tcpip_callback
343*10465441SEvalZero */
344*10465441SEvalZero err_t
tcpip_try_callback(tcpip_callback_fn function,void * ctx)345*10465441SEvalZero tcpip_try_callback(tcpip_callback_fn function, void *ctx)
346*10465441SEvalZero {
347*10465441SEvalZero struct tcpip_msg *msg;
348*10465441SEvalZero
349*10465441SEvalZero LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
350*10465441SEvalZero
351*10465441SEvalZero msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
352*10465441SEvalZero if (msg == NULL) {
353*10465441SEvalZero return ERR_MEM;
354*10465441SEvalZero }
355*10465441SEvalZero
356*10465441SEvalZero msg->type = TCPIP_MSG_CALLBACK;
357*10465441SEvalZero msg->msg.cb.function = function;
358*10465441SEvalZero msg->msg.cb.ctx = ctx;
359*10465441SEvalZero
360*10465441SEvalZero if (sys_mbox_trypost(&tcpip_mbox, msg) != ERR_OK) {
361*10465441SEvalZero memp_free(MEMP_TCPIP_MSG_API, msg);
362*10465441SEvalZero return ERR_MEM;
363*10465441SEvalZero }
364*10465441SEvalZero return ERR_OK;
365*10465441SEvalZero }
366*10465441SEvalZero
367*10465441SEvalZero #if LWIP_TCPIP_TIMEOUT && LWIP_TIMERS
368*10465441SEvalZero /**
369*10465441SEvalZero * call sys_timeout in tcpip_thread
370*10465441SEvalZero *
371*10465441SEvalZero * @param msecs time in milliseconds for timeout
372*10465441SEvalZero * @param h function to be called on timeout
373*10465441SEvalZero * @param arg argument to pass to timeout function h
374*10465441SEvalZero * @return ERR_MEM on memory error, ERR_OK otherwise
375*10465441SEvalZero */
376*10465441SEvalZero err_t
tcpip_timeout(u32_t msecs,sys_timeout_handler h,void * arg)377*10465441SEvalZero tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
378*10465441SEvalZero {
379*10465441SEvalZero struct tcpip_msg *msg;
380*10465441SEvalZero
381*10465441SEvalZero LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
382*10465441SEvalZero
383*10465441SEvalZero msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
384*10465441SEvalZero if (msg == NULL) {
385*10465441SEvalZero return ERR_MEM;
386*10465441SEvalZero }
387*10465441SEvalZero
388*10465441SEvalZero msg->type = TCPIP_MSG_TIMEOUT;
389*10465441SEvalZero msg->msg.tmo.msecs = msecs;
390*10465441SEvalZero msg->msg.tmo.h = h;
391*10465441SEvalZero msg->msg.tmo.arg = arg;
392*10465441SEvalZero sys_mbox_post(&tcpip_mbox, msg);
393*10465441SEvalZero return ERR_OK;
394*10465441SEvalZero }
395*10465441SEvalZero
396*10465441SEvalZero /**
397*10465441SEvalZero * call sys_untimeout in tcpip_thread
398*10465441SEvalZero *
399*10465441SEvalZero * @param h function to be called on timeout
400*10465441SEvalZero * @param arg argument to pass to timeout function h
401*10465441SEvalZero * @return ERR_MEM on memory error, ERR_OK otherwise
402*10465441SEvalZero */
403*10465441SEvalZero err_t
tcpip_untimeout(sys_timeout_handler h,void * arg)404*10465441SEvalZero tcpip_untimeout(sys_timeout_handler h, void *arg)
405*10465441SEvalZero {
406*10465441SEvalZero struct tcpip_msg *msg;
407*10465441SEvalZero
408*10465441SEvalZero LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
409*10465441SEvalZero
410*10465441SEvalZero msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
411*10465441SEvalZero if (msg == NULL) {
412*10465441SEvalZero return ERR_MEM;
413*10465441SEvalZero }
414*10465441SEvalZero
415*10465441SEvalZero msg->type = TCPIP_MSG_UNTIMEOUT;
416*10465441SEvalZero msg->msg.tmo.h = h;
417*10465441SEvalZero msg->msg.tmo.arg = arg;
418*10465441SEvalZero sys_mbox_post(&tcpip_mbox, msg);
419*10465441SEvalZero return ERR_OK;
420*10465441SEvalZero }
421*10465441SEvalZero #endif /* LWIP_TCPIP_TIMEOUT && LWIP_TIMERS */
422*10465441SEvalZero
423*10465441SEvalZero
424*10465441SEvalZero /**
425*10465441SEvalZero * Sends a message to TCPIP thread to call a function. Caller thread blocks on
426*10465441SEvalZero * on a provided semaphore, which ist NOT automatically signalled by TCPIP thread,
427*10465441SEvalZero * this has to be done by the user.
428*10465441SEvalZero * It is recommended to use LWIP_TCPIP_CORE_LOCKING since this is the way
429*10465441SEvalZero * with least runtime overhead.
430*10465441SEvalZero *
431*10465441SEvalZero * @param fn function to be called from TCPIP thread
432*10465441SEvalZero * @param apimsg argument to API function
433*10465441SEvalZero * @param sem semaphore to wait on
434*10465441SEvalZero * @return ERR_OK if the function was called, another err_t if not
435*10465441SEvalZero */
436*10465441SEvalZero err_t
tcpip_send_msg_wait_sem(tcpip_callback_fn fn,void * apimsg,sys_sem_t * sem)437*10465441SEvalZero tcpip_send_msg_wait_sem(tcpip_callback_fn fn, void *apimsg, sys_sem_t *sem)
438*10465441SEvalZero {
439*10465441SEvalZero #if LWIP_TCPIP_CORE_LOCKING
440*10465441SEvalZero LWIP_UNUSED_ARG(sem);
441*10465441SEvalZero LOCK_TCPIP_CORE();
442*10465441SEvalZero fn(apimsg);
443*10465441SEvalZero UNLOCK_TCPIP_CORE();
444*10465441SEvalZero return ERR_OK;
445*10465441SEvalZero #else /* LWIP_TCPIP_CORE_LOCKING */
446*10465441SEvalZero TCPIP_MSG_VAR_DECLARE(msg);
447*10465441SEvalZero
448*10465441SEvalZero LWIP_ASSERT("semaphore not initialized", sys_sem_valid(sem));
449*10465441SEvalZero LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
450*10465441SEvalZero
451*10465441SEvalZero TCPIP_MSG_VAR_ALLOC(msg);
452*10465441SEvalZero TCPIP_MSG_VAR_REF(msg).type = TCPIP_MSG_API;
453*10465441SEvalZero TCPIP_MSG_VAR_REF(msg).msg.api_msg.function = fn;
454*10465441SEvalZero TCPIP_MSG_VAR_REF(msg).msg.api_msg.msg = apimsg;
455*10465441SEvalZero sys_mbox_post(&tcpip_mbox, &TCPIP_MSG_VAR_REF(msg));
456*10465441SEvalZero sys_arch_sem_wait(sem, 0);
457*10465441SEvalZero TCPIP_MSG_VAR_FREE(msg);
458*10465441SEvalZero return ERR_OK;
459*10465441SEvalZero #endif /* LWIP_TCPIP_CORE_LOCKING */
460*10465441SEvalZero }
461*10465441SEvalZero
462*10465441SEvalZero /**
463*10465441SEvalZero * Synchronously calls function in TCPIP thread and waits for its completion.
464*10465441SEvalZero * It is recommended to use LWIP_TCPIP_CORE_LOCKING (preferred) or
465*10465441SEvalZero * LWIP_NETCONN_SEM_PER_THREAD.
466*10465441SEvalZero * If not, a semaphore is created and destroyed on every call which is usually
467*10465441SEvalZero * an expensive/slow operation.
468*10465441SEvalZero * @param fn Function to call
469*10465441SEvalZero * @param call Call parameters
470*10465441SEvalZero * @return Return value from tcpip_api_call_fn
471*10465441SEvalZero */
472*10465441SEvalZero err_t
tcpip_api_call(tcpip_api_call_fn fn,struct tcpip_api_call_data * call)473*10465441SEvalZero tcpip_api_call(tcpip_api_call_fn fn, struct tcpip_api_call_data *call)
474*10465441SEvalZero {
475*10465441SEvalZero #if LWIP_TCPIP_CORE_LOCKING
476*10465441SEvalZero err_t err;
477*10465441SEvalZero LOCK_TCPIP_CORE();
478*10465441SEvalZero err = fn(call);
479*10465441SEvalZero UNLOCK_TCPIP_CORE();
480*10465441SEvalZero return err;
481*10465441SEvalZero #else /* LWIP_TCPIP_CORE_LOCKING */
482*10465441SEvalZero TCPIP_MSG_VAR_DECLARE(msg);
483*10465441SEvalZero
484*10465441SEvalZero #if !LWIP_NETCONN_SEM_PER_THREAD
485*10465441SEvalZero err_t err = sys_sem_new(&call->sem, 0);
486*10465441SEvalZero if (err != ERR_OK) {
487*10465441SEvalZero return err;
488*10465441SEvalZero }
489*10465441SEvalZero #endif /* LWIP_NETCONN_SEM_PER_THREAD */
490*10465441SEvalZero
491*10465441SEvalZero LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
492*10465441SEvalZero
493*10465441SEvalZero TCPIP_MSG_VAR_ALLOC(msg);
494*10465441SEvalZero TCPIP_MSG_VAR_REF(msg).type = TCPIP_MSG_API_CALL;
495*10465441SEvalZero TCPIP_MSG_VAR_REF(msg).msg.api_call.arg = call;
496*10465441SEvalZero TCPIP_MSG_VAR_REF(msg).msg.api_call.function = fn;
497*10465441SEvalZero #if LWIP_NETCONN_SEM_PER_THREAD
498*10465441SEvalZero TCPIP_MSG_VAR_REF(msg).msg.api_call.sem = LWIP_NETCONN_THREAD_SEM_GET();
499*10465441SEvalZero #else /* LWIP_NETCONN_SEM_PER_THREAD */
500*10465441SEvalZero TCPIP_MSG_VAR_REF(msg).msg.api_call.sem = &call->sem;
501*10465441SEvalZero #endif /* LWIP_NETCONN_SEM_PER_THREAD */
502*10465441SEvalZero sys_mbox_post(&tcpip_mbox, &TCPIP_MSG_VAR_REF(msg));
503*10465441SEvalZero sys_arch_sem_wait(TCPIP_MSG_VAR_REF(msg).msg.api_call.sem, 0);
504*10465441SEvalZero TCPIP_MSG_VAR_FREE(msg);
505*10465441SEvalZero
506*10465441SEvalZero #if !LWIP_NETCONN_SEM_PER_THREAD
507*10465441SEvalZero sys_sem_free(&call->sem);
508*10465441SEvalZero #endif /* LWIP_NETCONN_SEM_PER_THREAD */
509*10465441SEvalZero
510*10465441SEvalZero return call->err;
511*10465441SEvalZero #endif /* LWIP_TCPIP_CORE_LOCKING */
512*10465441SEvalZero }
513*10465441SEvalZero
514*10465441SEvalZero /**
515*10465441SEvalZero * @ingroup lwip_os
516*10465441SEvalZero * Allocate a structure for a static callback message and initialize it.
517*10465441SEvalZero * The message has a special type such that lwIP never frees it.
518*10465441SEvalZero * This is intended to be used to send "static" messages from interrupt context,
519*10465441SEvalZero * e.g. the message is allocated once and posted several times from an IRQ
520*10465441SEvalZero * using tcpip_callbackmsg_trycallback().
521*10465441SEvalZero * Example usage: Trigger execution of an ethernet IRQ DPC routine in lwIP thread context.
522*10465441SEvalZero *
523*10465441SEvalZero * @param function the function to call
524*10465441SEvalZero * @param ctx parameter passed to function
525*10465441SEvalZero * @return a struct pointer to pass to tcpip_callbackmsg_trycallback().
526*10465441SEvalZero *
527*10465441SEvalZero * @see tcpip_callbackmsg_trycallback()
528*10465441SEvalZero * @see tcpip_callbackmsg_delete()
529*10465441SEvalZero */
530*10465441SEvalZero struct tcpip_callback_msg *
tcpip_callbackmsg_new(tcpip_callback_fn function,void * ctx)531*10465441SEvalZero tcpip_callbackmsg_new(tcpip_callback_fn function, void *ctx)
532*10465441SEvalZero {
533*10465441SEvalZero struct tcpip_msg *msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
534*10465441SEvalZero if (msg == NULL) {
535*10465441SEvalZero return NULL;
536*10465441SEvalZero }
537*10465441SEvalZero msg->type = TCPIP_MSG_CALLBACK_STATIC;
538*10465441SEvalZero msg->msg.cb.function = function;
539*10465441SEvalZero msg->msg.cb.ctx = ctx;
540*10465441SEvalZero return (struct tcpip_callback_msg *)msg;
541*10465441SEvalZero }
542*10465441SEvalZero
543*10465441SEvalZero /**
544*10465441SEvalZero * @ingroup lwip_os
545*10465441SEvalZero * Free a callback message allocated by tcpip_callbackmsg_new().
546*10465441SEvalZero *
547*10465441SEvalZero * @param msg the message to free
548*10465441SEvalZero *
549*10465441SEvalZero * @see tcpip_callbackmsg_new()
550*10465441SEvalZero */
551*10465441SEvalZero void
tcpip_callbackmsg_delete(struct tcpip_callback_msg * msg)552*10465441SEvalZero tcpip_callbackmsg_delete(struct tcpip_callback_msg *msg)
553*10465441SEvalZero {
554*10465441SEvalZero memp_free(MEMP_TCPIP_MSG_API, msg);
555*10465441SEvalZero }
556*10465441SEvalZero
557*10465441SEvalZero /**
558*10465441SEvalZero * @ingroup lwip_os
559*10465441SEvalZero * Try to post a callback-message to the tcpip_thread tcpip_mbox.
560*10465441SEvalZero *
561*10465441SEvalZero * @param msg pointer to the message to post
562*10465441SEvalZero * @return sys_mbox_trypost() return code
563*10465441SEvalZero *
564*10465441SEvalZero * @see tcpip_callbackmsg_new()
565*10465441SEvalZero */
566*10465441SEvalZero err_t
tcpip_callbackmsg_trycallback(struct tcpip_callback_msg * msg)567*10465441SEvalZero tcpip_callbackmsg_trycallback(struct tcpip_callback_msg *msg)
568*10465441SEvalZero {
569*10465441SEvalZero LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
570*10465441SEvalZero return sys_mbox_trypost(&tcpip_mbox, msg);
571*10465441SEvalZero }
572*10465441SEvalZero
573*10465441SEvalZero /**
574*10465441SEvalZero * @ingroup lwip_os
575*10465441SEvalZero * Try to post a callback-message to the tcpip_thread mbox.
576*10465441SEvalZero * Same as @ref tcpip_callbackmsg_trycallback but calls sys_mbox_trypost_fromisr(),
577*10465441SEvalZero * mainly to help FreeRTOS, where calls differ between task level and ISR level.
578*10465441SEvalZero *
579*10465441SEvalZero * @param msg pointer to the message to post
580*10465441SEvalZero * @return sys_mbox_trypost_fromisr() return code (without change, so this
581*10465441SEvalZero * knowledge can be used to e.g. propagate "bool needs_scheduling")
582*10465441SEvalZero *
583*10465441SEvalZero * @see tcpip_callbackmsg_new()
584*10465441SEvalZero */
585*10465441SEvalZero err_t
tcpip_callbackmsg_trycallback_fromisr(struct tcpip_callback_msg * msg)586*10465441SEvalZero tcpip_callbackmsg_trycallback_fromisr(struct tcpip_callback_msg *msg)
587*10465441SEvalZero {
588*10465441SEvalZero LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
589*10465441SEvalZero return sys_mbox_trypost_fromisr(&tcpip_mbox, msg);
590*10465441SEvalZero }
591*10465441SEvalZero
592*10465441SEvalZero /**
593*10465441SEvalZero * @ingroup lwip_os
594*10465441SEvalZero * Initialize this module:
595*10465441SEvalZero * - initialize all sub modules
596*10465441SEvalZero * - start the tcpip_thread
597*10465441SEvalZero *
598*10465441SEvalZero * @param initfunc a function to call when tcpip_thread is running and finished initializing
599*10465441SEvalZero * @param arg argument to pass to initfunc
600*10465441SEvalZero */
601*10465441SEvalZero void
tcpip_init(tcpip_init_done_fn initfunc,void * arg)602*10465441SEvalZero tcpip_init(tcpip_init_done_fn initfunc, void *arg)
603*10465441SEvalZero {
604*10465441SEvalZero lwip_init();
605*10465441SEvalZero
606*10465441SEvalZero tcpip_init_done = initfunc;
607*10465441SEvalZero tcpip_init_done_arg = arg;
608*10465441SEvalZero if (sys_mbox_new(&tcpip_mbox, TCPIP_MBOX_SIZE) != ERR_OK) {
609*10465441SEvalZero LWIP_ASSERT("failed to create tcpip_thread mbox", 0);
610*10465441SEvalZero }
611*10465441SEvalZero #if LWIP_TCPIP_CORE_LOCKING
612*10465441SEvalZero if (sys_mutex_new(&lock_tcpip_core) != ERR_OK) {
613*10465441SEvalZero LWIP_ASSERT("failed to create lock_tcpip_core", 0);
614*10465441SEvalZero }
615*10465441SEvalZero #endif /* LWIP_TCPIP_CORE_LOCKING */
616*10465441SEvalZero
617*10465441SEvalZero sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
618*10465441SEvalZero }
619*10465441SEvalZero
620*10465441SEvalZero /**
621*10465441SEvalZero * Simple callback function used with tcpip_callback to free a pbuf
622*10465441SEvalZero * (pbuf_free has a wrong signature for tcpip_callback)
623*10465441SEvalZero *
624*10465441SEvalZero * @param p The pbuf (chain) to be dereferenced.
625*10465441SEvalZero */
626*10465441SEvalZero static void
pbuf_free_int(void * p)627*10465441SEvalZero pbuf_free_int(void *p)
628*10465441SEvalZero {
629*10465441SEvalZero struct pbuf *q = (struct pbuf *)p;
630*10465441SEvalZero pbuf_free(q);
631*10465441SEvalZero }
632*10465441SEvalZero
633*10465441SEvalZero /**
634*10465441SEvalZero * A simple wrapper function that allows you to free a pbuf from interrupt context.
635*10465441SEvalZero *
636*10465441SEvalZero * @param p The pbuf (chain) to be dereferenced.
637*10465441SEvalZero * @return ERR_OK if callback could be enqueued, an err_t if not
638*10465441SEvalZero */
639*10465441SEvalZero err_t
pbuf_free_callback(struct pbuf * p)640*10465441SEvalZero pbuf_free_callback(struct pbuf *p)
641*10465441SEvalZero {
642*10465441SEvalZero return tcpip_try_callback(pbuf_free_int, p);
643*10465441SEvalZero }
644*10465441SEvalZero
645*10465441SEvalZero /**
646*10465441SEvalZero * A simple wrapper function that allows you to free heap memory from
647*10465441SEvalZero * interrupt context.
648*10465441SEvalZero *
649*10465441SEvalZero * @param m the heap memory to free
650*10465441SEvalZero * @return ERR_OK if callback could be enqueued, an err_t if not
651*10465441SEvalZero */
652*10465441SEvalZero err_t
mem_free_callback(void * m)653*10465441SEvalZero mem_free_callback(void *m)
654*10465441SEvalZero {
655*10465441SEvalZero return tcpip_try_callback(mem_free, m);
656*10465441SEvalZero }
657*10465441SEvalZero
658*10465441SEvalZero #endif /* !NO_SYS */
659