1 /**
2 * @file
3 * Sequential API Main thread module
4 *
5 */
6
7 /*
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 *
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.
21 *
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
31 * OF SUCH DAMAGE.
32 *
33 * This file is part of the lwIP TCP/IP stack.
34 *
35 * Author: Adam Dunkels <adam@sics.se>
36 *
37 */
38
39 #include "lwip/opt.h"
40
41 #if !NO_SYS /* don't build if not configured for use in lwipopts.h */
42
43 #include "lwip/priv/tcpip_priv.h"
44 #include "lwip/sys.h"
45 #include "lwip/memp.h"
46 #include "lwip/mem.h"
47 #include "lwip/init.h"
48 #include "lwip/ip.h"
49 #include "lwip/pbuf.h"
50 #include "lwip/etharp.h"
51 #include "netif/ethernet.h"
52 #if LWIP_LOWPOWER
53 #include "lwip/lowpower.h"
54 #endif
55
56 #define TCPIP_MSG_VAR_REF(name) API_VAR_REF(name)
57 #define TCPIP_MSG_VAR_DECLARE(name) API_VAR_DECLARE(struct tcpip_msg, name)
58 #define TCPIP_MSG_VAR_ALLOC(name) API_VAR_ALLOC(struct tcpip_msg, MEMP_TCPIP_MSG_API, name, ERR_MEM)
59 #define TCPIP_MSG_VAR_FREE(name) API_VAR_FREE(MEMP_TCPIP_MSG_API, name)
60
61 /* global variables */
62 static tcpip_init_done_fn tcpip_init_done;
63 static void *tcpip_init_done_arg;
64 static sys_mbox_t tcpip_mbox;
65
66 #if LWIP_TCPIP_CORE_LOCKING
67 /** The global semaphore to lock the stack. */
68 sys_mutex_t lock_tcpip_core;
69 #endif /* LWIP_TCPIP_CORE_LOCKING */
70
71 static void tcpip_thread_handle_msg(struct tcpip_msg *msg);
72
73 #if !LWIP_TIMERS
74
75 /** Wait for a message with timers disabled (e.g. pass a timer-check trigger into tcpip_thread) */
76 static void
tcpip_mbox_fetch(sys_mbox_t * mbox,void ** msg)77 tcpip_mbox_fetch(sys_mbox_t* mbox, void** msg)
78 {
79 LWIP_ASSERT_CORE_LOCKED();
80
81 UNLOCK_TCPIP_CORE();
82 sys_mbox_fetch(mbox, msg);
83 LOCK_TCPIP_CORE();
84 }
85
86 #else /* !LWIP_TIMERS */
87 #if !LWIP_LOWPOWER
88 /**
89 * Wait (forever) for a message to arrive in an mbox.
90 * While waiting, timeouts are processed.
91 *
92 * @param mbox the mbox to fetch the message from
93 * @param msg the place to store the message
94 */
95 static void
tcpip_mbox_fetch(sys_mbox_t * mbox,void ** msg)96 tcpip_mbox_fetch(sys_mbox_t *mbox, void **msg)
97 {
98 u32_t sleeptime, res;
99
100 again:
101 LWIP_ASSERT_CORE_LOCKED();
102
103 sleeptime = sys_timeouts_sleeptime();
104 if (sleeptime == SYS_TIMEOUTS_SLEEPTIME_INFINITE) {
105 UNLOCK_TCPIP_CORE();
106 sys_arch_mbox_fetch(mbox, msg, 0);
107 LOCK_TCPIP_CORE();
108 return;
109 } else if (sleeptime == 0) {
110 sys_check_timeouts();
111 /* We try again to fetch a message from the mbox. */
112 goto again;
113 }
114
115 UNLOCK_TCPIP_CORE();
116 res = sys_arch_mbox_fetch(mbox, msg, sleeptime);
117 LOCK_TCPIP_CORE();
118 if (res == SYS_ARCH_TIMEOUT) {
119 /* If a SYS_ARCH_TIMEOUT value is returned, a timeout occurred
120 before a message could be fetched. */
121 sys_check_timeouts();
122 /* We try again to fetch a message from the mbox. */
123 goto again;
124 }
125 }
126 #endif /* !LWIP_LOWPOWER */
127 #endif /* !LWIP_TIMERS */
128
129 /**
130 * The main lwIP thread. This thread has exclusive access to lwIP core functions
131 * (unless access to them is not locked). Other threads communicate with this
132 * thread using message boxes.
133 *
134 * It also starts all the timers to make sure they are running in the right
135 * thread context.
136 *
137 * @param arg unused argument
138 */
139 static void
tcpip_thread(void * arg)140 tcpip_thread(void *arg)
141 {
142 struct tcpip_msg *msg;
143 LWIP_UNUSED_ARG(arg);
144
145 LWIP_MARK_TCPIP_THREAD();
146
147 LOCK_TCPIP_CORE();
148 if (tcpip_init_done != NULL) {
149 tcpip_init_done(tcpip_init_done_arg);
150 }
151
152 while (1) { /* MAIN Loop */
153 LWIP_TCPIP_THREAD_ALIVE();
154 /* wait for a message, timeouts are processed while waiting */
155 tcpip_mbox_fetch(&tcpip_mbox, (void **)&msg);
156 if (msg == NULL) {
157 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: NULL\n"));
158 LWIP_ASSERT("tcpip_thread: invalid message", 0);
159 continue;
160 }
161 tcpip_thread_handle_msg(msg);
162 }
163 }
164
165 /* Handle a single tcpip_msg
166 * This is in its own function for access by tests only.
167 */
168 static void
tcpip_thread_handle_msg(struct tcpip_msg * msg)169 tcpip_thread_handle_msg(struct tcpip_msg *msg)
170 {
171 switch (msg->type) {
172 #if !LWIP_TCPIP_CORE_LOCKING
173 case TCPIP_MSG_API:
174 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
175 msg->msg.api_msg.function(msg->msg.api_msg.msg);
176 break;
177 case TCPIP_MSG_API_CALL:
178 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API CALL message %p\n", (void *)msg));
179 msg->msg.api_call.arg->err = msg->msg.api_call.function(msg->msg.api_call.arg);
180 sys_sem_signal(msg->msg.api_call.sem);
181 break;
182 case TCPIP_MSG_CALLBACK_STATIC_WAIT:
183 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK WAIT message %p\n", (void *)msg));
184 msg->msg.cb_wait.function(msg->msg.cb_wait.ctx);
185 sys_sem_signal(msg->msg.cb_wait.sem);
186 break;
187 #endif /* !LWIP_TCPIP_CORE_LOCKING */
188
189 #if !LWIP_TCPIP_CORE_LOCKING_INPUT
190 case TCPIP_MSG_INPKT:
191 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
192 if (msg->msg.inp.input_fn(msg->msg.inp.p, msg->msg.inp.netif) != ERR_OK) {
193 pbuf_free(msg->msg.inp.p);
194 }
195 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
196 break;
197 #endif /* !LWIP_TCPIP_CORE_LOCKING_INPUT */
198
199 #if LWIP_TCPIP_TIMEOUT && LWIP_TIMERS
200 case TCPIP_MSG_TIMEOUT:
201 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
202 sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
203 memp_free(MEMP_TCPIP_MSG_API, msg);
204 break;
205 case TCPIP_MSG_UNTIMEOUT:
206 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
207 sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
208 memp_free(MEMP_TCPIP_MSG_API, msg);
209 break;
210 #endif /* LWIP_TCPIP_TIMEOUT && LWIP_TIMERS */
211
212 case TCPIP_MSG_CALLBACK:
213 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
214 msg->msg.cb.function(msg->msg.cb.ctx);
215 memp_free(MEMP_TCPIP_MSG_API, msg);
216 break;
217
218 case TCPIP_MSG_CALLBACK_STATIC:
219 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK_STATIC %p\n", (void *)msg));
220 msg->msg.cb.function(msg->msg.cb.ctx);
221 break;
222 #if LWIP_LOWPOWER
223 /* just wake up thread do nothing */
224 case TCPIP_MSG_NA:
225 if (msg->msg.lowpower.type == LOW_BLOCK) {
226 LOWPOWER_SIGNAL(msg->msg.lowpower.wait_up);
227 } else {
228 memp_free(MEMP_TCPIP_MSG_LOWPOWER, msg);
229 }
230 sys_timeout_set_wake_time(LOW_TMR_DELAY);
231 break;
232 #endif
233 default:
234 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: %d\n", msg->type));
235 LWIP_ASSERT("tcpip_thread: invalid message", 0);
236 break;
237 }
238 }
239
240 #if LWIP_LOWPOWER
241 /* send a na msg to wake up tcpip_thread */
242 void
tcpip_send_msg_na(enum lowpower_msg_type type)243 tcpip_send_msg_na(enum lowpower_msg_type type)
244 {
245 struct tcpip_msg *msg = NULL;
246 err_t val;
247
248 /* is not used lowpower mode */
249 if ((type != LOW_FORCE_NON_BLOCK) && (get_lowpowper_mod() == LOW_TMR_NORMAL_MOD)) {
250 return;
251 }
252 if (sys_timeout_waiting_long() == 0) {
253 return;
254 }
255
256 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_LOWPOWER);
257 if (msg == NULL) {
258 LWIP_DEBUGF(LOWPOWER_DEBUG, ("tcpip_send_msg_na alloc faild\n"));
259 return;
260 }
261
262 /* just wake up thread if nonblock */
263 msg->type = TCPIP_MSG_NA;
264 msg->msg.lowpower.type = type;
265
266 if (type == LOW_BLOCK) {
267 LOWPOWER_SEM_NEW(msg->msg.lowpower.wait_up, val);
268 if (val != ERR_OK) {
269 LWIP_DEBUGF(LOWPOWER_DEBUG, ("alloc sem faild\n"));
270 memp_free(MEMP_TCPIP_MSG_LOWPOWER, msg);
271 return;
272 }
273 }
274
275 if (sys_mbox_trypost(&tcpip_mbox, msg) != ERR_OK) {
276 if (type == LOW_BLOCK) {
277 LOWPOWER_SEM_FREE(msg->msg.lowpower.wait_up);
278 }
279 memp_free(MEMP_TCPIP_MSG_LOWPOWER, msg);
280 LWIP_DEBUGF(LOWPOWER_DEBUG, ("tcpip_send_msg_na post faild\n"));
281 return;
282 }
283
284 if (type == LOW_BLOCK) {
285 LOWPOWER_SEM_WAIT(msg->msg.lowpower.wait_up);
286 LOWPOWER_SEM_FREE(msg->msg.lowpower.wait_up);
287 memp_free(MEMP_TCPIP_MSG_LOWPOWER, msg);
288 }
289 }
290 #endif /* LWIP_LOWPOWER */
291
292 #ifdef TCPIP_THREAD_TEST
293 /** Work on queued items in single-threaded test mode */
294 int
tcpip_thread_poll_one(void)295 tcpip_thread_poll_one(void)
296 {
297 int ret = 0;
298 struct tcpip_msg *msg;
299
300 if (sys_arch_mbox_tryfetch(&tcpip_mbox, (void **)&msg) != SYS_MBOX_EMPTY) {
301 LOCK_TCPIP_CORE();
302 if (msg != NULL) {
303 tcpip_thread_handle_msg(msg);
304 ret = 1;
305 }
306 UNLOCK_TCPIP_CORE();
307 }
308 return ret;
309 }
310 #endif
311
312 /**
313 * Pass a received packet to tcpip_thread for input processing
314 *
315 * @param p the received packet
316 * @param inp the network interface on which the packet was received
317 * @param input_fn input function to call
318 */
319 err_t
tcpip_inpkt(struct pbuf * p,struct netif * inp,netif_input_fn input_fn)320 tcpip_inpkt(struct pbuf *p, struct netif *inp, netif_input_fn input_fn)
321 {
322 #if LWIP_TCPIP_CORE_LOCKING_INPUT
323 err_t ret;
324 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_inpkt: PACKET %p/%p\n", (void *)p, (void *)inp));
325 #if LWIP_LOWPOWER
326 tcpip_send_msg_na(LOW_BLOCK);
327 #endif
328 LOCK_TCPIP_CORE();
329 ret = input_fn(p, inp);
330 UNLOCK_TCPIP_CORE();
331 return ret;
332 #else /* LWIP_TCPIP_CORE_LOCKING_INPUT */
333 struct tcpip_msg *msg;
334
335 LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
336
337 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT);
338 if (msg == NULL) {
339 return ERR_MEM;
340 }
341
342 msg->type = TCPIP_MSG_INPKT;
343 msg->msg.inp.p = p;
344 msg->msg.inp.netif = inp;
345 msg->msg.inp.input_fn = input_fn;
346 if (sys_mbox_trypost(&tcpip_mbox, msg) != ERR_OK) {
347 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
348 return ERR_MEM;
349 }
350 return ERR_OK;
351 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
352 }
353
354 /**
355 * @ingroup lwip_os
356 * Pass a received packet to tcpip_thread for input processing with
357 * ethernet_input or ip_input. Don't call directly, pass to netif_add()
358 * and call netif->input().
359 *
360 * @param p the received packet, p->payload pointing to the Ethernet header or
361 * to an IP header (if inp doesn't have NETIF_FLAG_ETHARP or
362 * NETIF_FLAG_ETHERNET flags)
363 * @param inp the network interface on which the packet was received
364 */
365 err_t
tcpip_input(struct pbuf * p,struct netif * inp)366 tcpip_input(struct pbuf *p, struct netif *inp)
367 {
368 #if LWIP_ETHERNET
369 if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
370 return tcpip_inpkt(p, inp, ethernet_input);
371 } else
372 #endif /* LWIP_ETHERNET */
373 return tcpip_inpkt(p, inp, ip_input);
374 }
375
376 /**
377 * @ingroup lwip_os
378 * Call a specific function in the thread context of
379 * tcpip_thread for easy access synchronization.
380 * A function called in that way may access lwIP core code
381 * without fearing concurrent access.
382 * Blocks until the request is posted.
383 * Must not be called from interrupt context!
384 *
385 * @param function the function to call
386 * @param ctx parameter passed to f
387 * @return ERR_OK if the function was called, another err_t if not
388 *
389 * @see tcpip_try_callback
390 */
391 err_t
tcpip_callback(tcpip_callback_fn function,void * ctx)392 tcpip_callback(tcpip_callback_fn function, void *ctx)
393 {
394 struct tcpip_msg *msg;
395
396 LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
397
398 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
399 if (msg == NULL) {
400 return ERR_MEM;
401 }
402
403 msg->type = TCPIP_MSG_CALLBACK;
404 msg->msg.cb.function = function;
405 msg->msg.cb.ctx = ctx;
406
407 sys_mbox_post(&tcpip_mbox, msg);
408 return ERR_OK;
409 }
410
411 /**
412 * @ingroup lwip_os
413 * Call a specific function in the thread context of
414 * tcpip_thread for easy access synchronization.
415 * A function called in that way may access lwIP core code
416 * without fearing concurrent access.
417 * Does NOT block when the request cannot be posted because the
418 * tcpip_mbox is full, but returns ERR_MEM instead.
419 * Can be called from interrupt context.
420 *
421 * @param function the function to call
422 * @param ctx parameter passed to f
423 * @return ERR_OK if the function was called, another err_t if not
424 *
425 * @see tcpip_callback
426 */
427 err_t
tcpip_try_callback(tcpip_callback_fn function,void * ctx)428 tcpip_try_callback(tcpip_callback_fn function, void *ctx)
429 {
430 struct tcpip_msg *msg;
431
432 LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
433
434 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
435 if (msg == NULL) {
436 return ERR_MEM;
437 }
438
439 msg->type = TCPIP_MSG_CALLBACK;
440 msg->msg.cb.function = function;
441 msg->msg.cb.ctx = ctx;
442
443 if (sys_mbox_trypost(&tcpip_mbox, msg) != ERR_OK) {
444 memp_free(MEMP_TCPIP_MSG_API, msg);
445 return ERR_MEM;
446 }
447 return ERR_OK;
448 }
449
450 #if LWIP_TCPIP_TIMEOUT && LWIP_TIMERS
451 /**
452 * call sys_timeout in tcpip_thread
453 *
454 * @param msecs time in milliseconds for timeout
455 * @param h function to be called on timeout
456 * @param arg argument to pass to timeout function h
457 * @return ERR_MEM on memory error, ERR_OK otherwise
458 */
459 err_t
tcpip_timeout(u32_t msecs,sys_timeout_handler h,void * arg)460 tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
461 {
462 struct tcpip_msg *msg;
463
464 LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
465
466 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
467 if (msg == NULL) {
468 return ERR_MEM;
469 }
470
471 msg->type = TCPIP_MSG_TIMEOUT;
472 msg->msg.tmo.msecs = msecs;
473 msg->msg.tmo.h = h;
474 msg->msg.tmo.arg = arg;
475 sys_mbox_post(&tcpip_mbox, msg);
476 return ERR_OK;
477 }
478
479 /**
480 * call sys_untimeout in tcpip_thread
481 *
482 * @param h function to be called on timeout
483 * @param arg argument to pass to timeout function h
484 * @return ERR_MEM on memory error, ERR_OK otherwise
485 */
486 err_t
tcpip_untimeout(sys_timeout_handler h,void * arg)487 tcpip_untimeout(sys_timeout_handler h, void *arg)
488 {
489 struct tcpip_msg *msg;
490
491 LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
492
493 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
494 if (msg == NULL) {
495 return ERR_MEM;
496 }
497
498 msg->type = TCPIP_MSG_UNTIMEOUT;
499 msg->msg.tmo.h = h;
500 msg->msg.tmo.arg = arg;
501 sys_mbox_post(&tcpip_mbox, msg);
502 return ERR_OK;
503 }
504 #endif /* LWIP_TCPIP_TIMEOUT && LWIP_TIMERS */
505
506
507 /**
508 * Sends a message to TCPIP thread to call a function. Caller thread blocks on
509 * on a provided semaphore, which is NOT automatically signalled by TCPIP thread,
510 * this has to be done by the user.
511 * It is recommended to use LWIP_TCPIP_CORE_LOCKING since this is the way
512 * with least runtime overhead.
513 *
514 * @param fn function to be called from TCPIP thread
515 * @param apimsg argument to API function
516 * @param sem semaphore to wait on
517 * @return ERR_OK if the function was called, another err_t if not
518 */
519 err_t
tcpip_send_msg_wait_sem(tcpip_callback_fn fn,void * apimsg,sys_sem_t * sem)520 tcpip_send_msg_wait_sem(tcpip_callback_fn fn, void *apimsg, sys_sem_t *sem)
521 {
522 #if LWIP_TCPIP_CORE_LOCKING
523 LWIP_UNUSED_ARG(sem);
524 #if LWIP_LOWPOWER
525 tcpip_send_msg_na(LOW_BLOCK);
526 #endif
527 LOCK_TCPIP_CORE();
528 fn(apimsg);
529 UNLOCK_TCPIP_CORE();
530 return ERR_OK;
531 #else /* LWIP_TCPIP_CORE_LOCKING */
532 TCPIP_MSG_VAR_DECLARE(msg);
533
534 LWIP_ASSERT("semaphore not initialized", sys_sem_valid(sem));
535 LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
536
537 TCPIP_MSG_VAR_ALLOC(msg);
538 TCPIP_MSG_VAR_REF(msg).type = TCPIP_MSG_API;
539 TCPIP_MSG_VAR_REF(msg).msg.api_msg.function = fn;
540 TCPIP_MSG_VAR_REF(msg).msg.api_msg.msg = apimsg;
541 sys_mbox_post(&tcpip_mbox, &TCPIP_MSG_VAR_REF(msg));
542 sys_arch_sem_wait(sem, 0);
543 TCPIP_MSG_VAR_FREE(msg);
544 return ERR_OK;
545 #endif /* LWIP_TCPIP_CORE_LOCKING */
546 }
547
548 /**
549 * Synchronously calls function in TCPIP thread and waits for its completion.
550 * It is recommended to use LWIP_TCPIP_CORE_LOCKING (preferred) or
551 * LWIP_NETCONN_SEM_PER_THREAD.
552 * If not, a semaphore is created and destroyed on every call which is usually
553 * an expensive/slow operation.
554 * @param fn Function to call
555 * @param call Call parameters
556 * @return Return value from tcpip_api_call_fn
557 */
558 err_t
tcpip_api_call(tcpip_api_call_fn fn,struct tcpip_api_call_data * call)559 tcpip_api_call(tcpip_api_call_fn fn, struct tcpip_api_call_data *call)
560 {
561 #if LWIP_TCPIP_CORE_LOCKING
562 err_t err;
563 #if LWIP_LOWPOWER
564 tcpip_send_msg_na(LOW_BLOCK);
565 #endif
566 LOCK_TCPIP_CORE();
567 err = fn(call);
568 UNLOCK_TCPIP_CORE();
569 return err;
570 #else /* LWIP_TCPIP_CORE_LOCKING */
571 TCPIP_MSG_VAR_DECLARE(msg);
572
573 #if !LWIP_NETCONN_SEM_PER_THREAD
574 err_t err = sys_sem_new(&call->sem, 0);
575 if (err != ERR_OK) {
576 return err;
577 }
578 #endif /* LWIP_NETCONN_SEM_PER_THREAD */
579
580 LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
581
582 TCPIP_MSG_VAR_ALLOC(msg);
583 TCPIP_MSG_VAR_REF(msg).type = TCPIP_MSG_API_CALL;
584 TCPIP_MSG_VAR_REF(msg).msg.api_call.arg = call;
585 TCPIP_MSG_VAR_REF(msg).msg.api_call.function = fn;
586 #if LWIP_NETCONN_SEM_PER_THREAD
587 TCPIP_MSG_VAR_REF(msg).msg.api_call.sem = LWIP_NETCONN_THREAD_SEM_GET();
588 #else /* LWIP_NETCONN_SEM_PER_THREAD */
589 TCPIP_MSG_VAR_REF(msg).msg.api_call.sem = &call->sem;
590 #endif /* LWIP_NETCONN_SEM_PER_THREAD */
591 sys_mbox_post(&tcpip_mbox, &TCPIP_MSG_VAR_REF(msg));
592 sys_arch_sem_wait(TCPIP_MSG_VAR_REF(msg).msg.api_call.sem, 0);
593 TCPIP_MSG_VAR_FREE(msg);
594
595 #if !LWIP_NETCONN_SEM_PER_THREAD
596 sys_sem_free(&call->sem);
597 #endif /* LWIP_NETCONN_SEM_PER_THREAD */
598
599 return call->err;
600 #endif /* LWIP_TCPIP_CORE_LOCKING */
601 }
602
603 /**
604 * @ingroup lwip_os
605 * Allocate a structure for a static callback message and initialize it.
606 * The message has a special type such that lwIP never frees it.
607 * This is intended to be used to send "static" messages from interrupt context,
608 * e.g. the message is allocated once and posted several times from an IRQ
609 * using tcpip_callbackmsg_trycallback().
610 * Example usage: Trigger execution of an ethernet IRQ DPC routine in lwIP thread context.
611 *
612 * @param function the function to call
613 * @param ctx parameter passed to function
614 * @return a struct pointer to pass to tcpip_callbackmsg_trycallback().
615 *
616 * @see tcpip_callbackmsg_trycallback()
617 * @see tcpip_callbackmsg_delete()
618 */
619 struct tcpip_callback_msg *
tcpip_callbackmsg_new(tcpip_callback_fn function,void * ctx)620 tcpip_callbackmsg_new(tcpip_callback_fn function, void *ctx)
621 {
622 struct tcpip_msg *msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
623 if (msg == NULL) {
624 return NULL;
625 }
626 msg->type = TCPIP_MSG_CALLBACK_STATIC;
627 msg->msg.cb.function = function;
628 msg->msg.cb.ctx = ctx;
629 return (struct tcpip_callback_msg *)msg;
630 }
631
632 /**
633 * @ingroup lwip_os
634 * Free a callback message allocated by tcpip_callbackmsg_new().
635 *
636 * @param msg the message to free
637 *
638 * @see tcpip_callbackmsg_new()
639 */
640 void
tcpip_callbackmsg_delete(struct tcpip_callback_msg * msg)641 tcpip_callbackmsg_delete(struct tcpip_callback_msg *msg)
642 {
643 memp_free(MEMP_TCPIP_MSG_API, msg);
644 }
645
646 /**
647 * @ingroup lwip_os
648 * Try to post a callback-message to the tcpip_thread tcpip_mbox.
649 *
650 * @param msg pointer to the message to post
651 * @return sys_mbox_trypost() return code
652 *
653 * @see tcpip_callbackmsg_new()
654 */
655 err_t
tcpip_callbackmsg_trycallback(struct tcpip_callback_msg * msg)656 tcpip_callbackmsg_trycallback(struct tcpip_callback_msg *msg)
657 {
658 LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
659 return sys_mbox_trypost(&tcpip_mbox, msg);
660 }
661
662 /**
663 * @ingroup lwip_os
664 * Try to post a callback-message to the tcpip_thread mbox.
665 * Same as @ref tcpip_callbackmsg_trycallback but calls sys_mbox_trypost_fromisr(),
666 * mainly to help FreeRTOS, where calls differ between task level and ISR level.
667 *
668 * @param msg pointer to the message to post
669 * @return sys_mbox_trypost_fromisr() return code (without change, so this
670 * knowledge can be used to e.g. propagate "bool needs_scheduling")
671 *
672 * @see tcpip_callbackmsg_new()
673 */
674 err_t
tcpip_callbackmsg_trycallback_fromisr(struct tcpip_callback_msg * msg)675 tcpip_callbackmsg_trycallback_fromisr(struct tcpip_callback_msg *msg)
676 {
677 LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
678 return sys_mbox_trypost_fromisr(&tcpip_mbox, msg);
679 }
680
681 /**
682 * Sends a message to TCPIP thread to call a function. Caller thread blocks
683 * until the function returns.
684 * It is recommended to use LWIP_TCPIP_CORE_LOCKING (preferred) or
685 * LWIP_NETCONN_SEM_PER_THREAD.
686 * If not, a semaphore is created and destroyed on every call which is usually
687 * an expensive/slow operation.
688 *
689 * @param function the function to call
690 * @param ctx parameter passed to f
691 * @return ERR_OK if the function was called, another err_t if not
692 */
693 err_t
tcpip_callback_wait(tcpip_callback_fn function,void * ctx)694 tcpip_callback_wait(tcpip_callback_fn function, void *ctx)
695 {
696 #if LWIP_TCPIP_CORE_LOCKING
697 LOCK_TCPIP_CORE();
698 function(ctx);
699 UNLOCK_TCPIP_CORE();
700 return ERR_OK;
701 #else /* LWIP_TCPIP_CORE_LOCKING */
702 err_t err;
703 sys_sem_t sem;
704 struct tcpip_msg msg;
705
706 LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(tcpip_mbox));
707
708 err = sys_sem_new(&sem, 0);
709 if (err != ERR_OK) {
710 return err;
711 }
712
713 msg.type = TCPIP_MSG_CALLBACK_STATIC_WAIT;
714 msg.msg.cb_wait.function = function;
715 msg.msg.cb_wait.ctx = ctx;
716 msg.msg.cb_wait.sem = &sem;
717 sys_mbox_post(&tcpip_mbox, &msg);
718 sys_arch_sem_wait(&sem, 0);
719 sys_sem_free(&sem);
720 return ERR_OK;
721 #endif /* LWIP_TCPIP_CORE_LOCKING */
722 }
723
724 /**
725 * @ingroup lwip_os
726 * Initialize this module:
727 * - initialize all sub modules
728 * - start the tcpip_thread
729 *
730 * @param initfunc a function to call when tcpip_thread is running and finished initializing
731 * @param arg argument to pass to initfunc
732 */
733 void
tcpip_init(tcpip_init_done_fn initfunc,void * arg)734 tcpip_init(tcpip_init_done_fn initfunc, void *arg)
735 {
736 lwip_init();
737
738 tcpip_init_done = initfunc;
739 tcpip_init_done_arg = arg;
740 if (sys_mbox_new(&tcpip_mbox, TCPIP_MBOX_SIZE) != ERR_OK) {
741 LWIP_ASSERT("failed to create tcpip_thread mbox", 0);
742 }
743 #if LWIP_TCPIP_CORE_LOCKING
744 if (sys_mutex_new(&lock_tcpip_core) != ERR_OK) {
745 LWIP_ASSERT("failed to create lock_tcpip_core", 0);
746 }
747 #endif /* LWIP_TCPIP_CORE_LOCKING */
748
749 sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
750 }
751
752 /**
753 * Simple callback function used with tcpip_callback to free a pbuf
754 * (pbuf_free has a wrong signature for tcpip_callback)
755 *
756 * @param p The pbuf (chain) to be dereferenced.
757 */
758 static void
pbuf_free_int(void * p)759 pbuf_free_int(void *p)
760 {
761 struct pbuf *q = (struct pbuf *)p;
762 pbuf_free(q);
763 }
764
765 /**
766 * A simple wrapper function that allows you to free a pbuf from interrupt context.
767 *
768 * @param p The pbuf (chain) to be dereferenced.
769 * @return ERR_OK if callback could be enqueued, an err_t if not
770 */
771 err_t
pbuf_free_callback(struct pbuf * p)772 pbuf_free_callback(struct pbuf *p)
773 {
774 return tcpip_try_callback(pbuf_free_int, p);
775 }
776
777 /**
778 * A simple wrapper function that allows you to free heap memory from
779 * interrupt context.
780 *
781 * @param m the heap memory to free
782 * @return ERR_OK if callback could be enqueued, an err_t if not
783 */
784 err_t
mem_free_callback(void * m)785 mem_free_callback(void *m)
786 {
787 return tcpip_try_callback(mem_free, m);
788 }
789
790 #endif /* !NO_SYS */
791