BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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 | |||
53 | #define TCPIP_MSG_VAR_REF(name) API_VAR_REF(name) |
||
54 | #define TCPIP_MSG_VAR_DECLARE(name) API_VAR_DECLARE(struct tcpip_msg, name) |
||
55 | #define TCPIP_MSG_VAR_ALLOC(name) API_VAR_ALLOC(struct tcpip_msg, MEMP_TCPIP_MSG_API, name, ERR_MEM) |
||
56 | #define TCPIP_MSG_VAR_FREE(name) API_VAR_FREE(MEMP_TCPIP_MSG_API, name) |
||
57 | |||
58 | /* global variables */ |
||
59 | static tcpip_init_done_fn tcpip_init_done; |
||
60 | static void *tcpip_init_done_arg; |
||
61 | static sys_mbox_t mbox; |
||
62 | |||
63 | #if LWIP_TCPIP_CORE_LOCKING |
||
64 | /** The global semaphore to lock the stack. */ |
||
65 | sys_mutex_t lock_tcpip_core; |
||
66 | #endif /* LWIP_TCPIP_CORE_LOCKING */ |
||
67 | |||
68 | #if LWIP_TIMERS |
||
69 | /* wait for a message, timeouts are processed while waiting */ |
||
70 | #define TCPIP_MBOX_FETCH(mbox, msg) sys_timeouts_mbox_fetch(mbox, msg) |
||
71 | #else /* LWIP_TIMERS */ |
||
72 | /* wait for a message with timers disabled (e.g. pass a timer-check trigger into tcpip_thread) */ |
||
73 | #define TCPIP_MBOX_FETCH(mbox, msg) sys_mbox_fetch(mbox, msg) |
||
74 | #endif /* LWIP_TIMERS */ |
||
75 | |||
76 | static void tcpip_thread_handle_msg(struct tcpip_msg *msg); |
||
77 | |||
78 | /** |
||
79 | * The main lwIP thread. This thread has exclusive access to lwIP core functions |
||
80 | * (unless access to them is not locked). Other threads communicate with this |
||
81 | * thread using message boxes. |
||
82 | * |
||
83 | * It also starts all the timers to make sure they are running in the right |
||
84 | * thread context. |
||
85 | * |
||
86 | * @param arg unused argument |
||
87 | */ |
||
88 | static void |
||
89 | tcpip_thread(void *arg) |
||
90 | { |
||
91 | struct tcpip_msg *msg; |
||
92 | LWIP_UNUSED_ARG(arg); |
||
93 | |||
94 | if (tcpip_init_done != NULL) { |
||
95 | tcpip_init_done(tcpip_init_done_arg); |
||
96 | } |
||
97 | |||
98 | LOCK_TCPIP_CORE(); |
||
99 | while (1) { /* MAIN Loop */ |
||
100 | UNLOCK_TCPIP_CORE(); |
||
101 | LWIP_TCPIP_THREAD_ALIVE(); |
||
102 | /* wait for a message, timeouts are processed while waiting */ |
||
103 | TCPIP_MBOX_FETCH(&mbox, (void **)&msg); |
||
104 | LOCK_TCPIP_CORE(); |
||
105 | if (msg == NULL) { |
||
106 | LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: NULL\n")); |
||
107 | LWIP_ASSERT("tcpip_thread: invalid message", 0); |
||
108 | continue; |
||
109 | } |
||
110 | tcpip_thread_handle_msg(msg); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | /* Handle a single tcpip_msg |
||
115 | * This is in its own function for access by tests only. |
||
116 | */ |
||
117 | static void |
||
118 | tcpip_thread_handle_msg(struct tcpip_msg *msg) |
||
119 | { |
||
120 | switch (msg->type) { |
||
121 | #if !LWIP_TCPIP_CORE_LOCKING |
||
122 | case TCPIP_MSG_API: |
||
123 | LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg)); |
||
124 | msg->msg.api_msg.function(msg->msg.api_msg.msg); |
||
125 | break; |
||
126 | case TCPIP_MSG_API_CALL: |
||
127 | LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API CALL message %p\n", (void *)msg)); |
||
128 | msg->msg.api_call.arg->err = msg->msg.api_call.function(msg->msg.api_call.arg); |
||
129 | sys_sem_signal(msg->msg.api_call.sem); |
||
130 | break; |
||
131 | #endif /* !LWIP_TCPIP_CORE_LOCKING */ |
||
132 | |||
133 | #if !LWIP_TCPIP_CORE_LOCKING_INPUT |
||
134 | case TCPIP_MSG_INPKT: |
||
135 | LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg)); |
||
136 | msg->msg.inp.input_fn(msg->msg.inp.p, msg->msg.inp.netif); |
||
137 | memp_free(MEMP_TCPIP_MSG_INPKT, msg); |
||
138 | break; |
||
139 | #endif /* !LWIP_TCPIP_CORE_LOCKING_INPUT */ |
||
140 | |||
141 | #if LWIP_TCPIP_TIMEOUT && LWIP_TIMERS |
||
142 | case TCPIP_MSG_TIMEOUT: |
||
143 | LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg)); |
||
144 | sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg); |
||
145 | memp_free(MEMP_TCPIP_MSG_API, msg); |
||
146 | break; |
||
147 | case TCPIP_MSG_UNTIMEOUT: |
||
148 | LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg)); |
||
149 | sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg); |
||
150 | memp_free(MEMP_TCPIP_MSG_API, msg); |
||
151 | break; |
||
152 | #endif /* LWIP_TCPIP_TIMEOUT && LWIP_TIMERS */ |
||
153 | |||
154 | case TCPIP_MSG_CALLBACK: |
||
155 | LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg)); |
||
156 | msg->msg.cb.function(msg->msg.cb.ctx); |
||
157 | memp_free(MEMP_TCPIP_MSG_API, msg); |
||
158 | break; |
||
159 | |||
160 | case TCPIP_MSG_CALLBACK_STATIC: |
||
161 | LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK_STATIC %p\n", (void *)msg)); |
||
162 | msg->msg.cb.function(msg->msg.cb.ctx); |
||
163 | break; |
||
164 | |||
165 | default: |
||
166 | LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: %d\n", msg->type)); |
||
167 | LWIP_ASSERT("tcpip_thread: invalid message", 0); |
||
168 | break; |
||
169 | } |
||
170 | } |
||
171 | |||
172 | #ifdef TCPIP_THREAD_TEST |
||
173 | /** Work on queued items in single-threaded test mode */ |
||
174 | int |
||
175 | tcpip_thread_poll_one(void) |
||
176 | { |
||
177 | int ret = 0; |
||
178 | struct tcpip_msg *msg; |
||
179 | |||
180 | /* wait for a message, timeouts are processed while waiting */ |
||
181 | if (sys_arch_mbox_tryfetch(&mbox, (void **)&msg) != SYS_ARCH_TIMEOUT) { |
||
182 | LOCK_TCPIP_CORE(); |
||
183 | if (msg != NULL) { |
||
184 | tcpip_thread_handle_msg(msg); |
||
185 | ret = 1; |
||
186 | } |
||
187 | UNLOCK_TCPIP_CORE(); |
||
188 | } |
||
189 | return ret; |
||
190 | } |
||
191 | #endif |
||
192 | |||
193 | /** |
||
194 | * Pass a received packet to tcpip_thread for input processing |
||
195 | * |
||
196 | * @param p the received packet |
||
197 | * @param inp the network interface on which the packet was received |
||
198 | * @param input_fn input function to call |
||
199 | */ |
||
200 | err_t |
||
201 | tcpip_inpkt(struct pbuf *p, struct netif *inp, netif_input_fn input_fn) |
||
202 | { |
||
203 | #if LWIP_TCPIP_CORE_LOCKING_INPUT |
||
204 | err_t ret; |
||
205 | LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_inpkt: PACKET %p/%p\n", (void *)p, (void *)inp)); |
||
206 | LOCK_TCPIP_CORE(); |
||
207 | ret = input_fn(p, inp); |
||
208 | UNLOCK_TCPIP_CORE(); |
||
209 | return ret; |
||
210 | #else /* LWIP_TCPIP_CORE_LOCKING_INPUT */ |
||
211 | struct tcpip_msg *msg; |
||
212 | |||
213 | LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox)); |
||
214 | |||
215 | msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT); |
||
216 | if (msg == NULL) { |
||
217 | return ERR_MEM; |
||
218 | } |
||
219 | |||
220 | msg->type = TCPIP_MSG_INPKT; |
||
221 | msg->msg.inp.p = p; |
||
222 | msg->msg.inp.netif = inp; |
||
223 | msg->msg.inp.input_fn = input_fn; |
||
224 | if (sys_mbox_trypost(&mbox, msg) != ERR_OK) { |
||
225 | memp_free(MEMP_TCPIP_MSG_INPKT, msg); |
||
226 | return ERR_MEM; |
||
227 | } |
||
228 | return ERR_OK; |
||
229 | #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */ |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @ingroup lwip_os |
||
234 | * Pass a received packet to tcpip_thread for input processing with |
||
235 | * ethernet_input or ip_input. Don't call directly, pass to netif_add() |
||
236 | * and call netif->input(). |
||
237 | * |
||
238 | * @param p the received packet, p->payload pointing to the Ethernet header or |
||
239 | * to an IP header (if inp doesn't have NETIF_FLAG_ETHARP or |
||
240 | * NETIF_FLAG_ETHERNET flags) |
||
241 | * @param inp the network interface on which the packet was received |
||
242 | */ |
||
243 | err_t |
||
244 | tcpip_input(struct pbuf *p, struct netif *inp) |
||
245 | { |
||
246 | #if LWIP_ETHERNET |
||
247 | if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) { |
||
248 | return tcpip_inpkt(p, inp, ethernet_input); |
||
249 | } else |
||
250 | #endif /* LWIP_ETHERNET */ |
||
251 | return tcpip_inpkt(p, inp, ip_input); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @ingroup lwip_os |
||
256 | * Call a specific function in the thread context of |
||
257 | * tcpip_thread for easy access synchronization. |
||
258 | * A function called in that way may access lwIP core code |
||
259 | * without fearing concurrent access. |
||
260 | * Blocks until the request is posted. |
||
261 | * Must not be called from interrupt context! |
||
262 | * |
||
263 | * @param function the function to call |
||
264 | * @param ctx parameter passed to f |
||
265 | * @return ERR_OK if the function was called, another err_t if not |
||
266 | * |
||
267 | * @see tcpip_try_callback |
||
268 | */ |
||
269 | err_t |
||
270 | tcpip_callback(tcpip_callback_fn function, void *ctx) |
||
271 | { |
||
272 | struct tcpip_msg *msg; |
||
273 | |||
274 | LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox)); |
||
275 | |||
276 | msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API); |
||
277 | if (msg == NULL) { |
||
278 | return ERR_MEM; |
||
279 | } |
||
280 | |||
281 | msg->type = TCPIP_MSG_CALLBACK; |
||
282 | msg->msg.cb.function = function; |
||
283 | msg->msg.cb.ctx = ctx; |
||
284 | |||
285 | sys_mbox_post(&mbox, msg); |
||
286 | return ERR_OK; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * @ingroup lwip_os |
||
291 | * Call a specific function in the thread context of |
||
292 | * tcpip_thread for easy access synchronization. |
||
293 | * A function called in that way may access lwIP core code |
||
294 | * without fearing concurrent access. |
||
295 | * Does NOT block when the request cannot be posted because the |
||
296 | * mbox is full, but returns ERR_MEM instead. |
||
297 | * Can be called from interrupt context. |
||
298 | * |
||
299 | * @param function the function to call |
||
300 | * @param ctx parameter passed to f |
||
301 | * @return ERR_OK if the function was called, another err_t if not |
||
302 | * |
||
303 | * @see tcpip_callback |
||
304 | */ |
||
305 | err_t |
||
306 | tcpip_try_callback(tcpip_callback_fn function, void *ctx) |
||
307 | { |
||
308 | struct tcpip_msg *msg; |
||
309 | |||
310 | LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox)); |
||
311 | |||
312 | msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API); |
||
313 | if (msg == NULL) { |
||
314 | return ERR_MEM; |
||
315 | } |
||
316 | |||
317 | msg->type = TCPIP_MSG_CALLBACK; |
||
318 | msg->msg.cb.function = function; |
||
319 | msg->msg.cb.ctx = ctx; |
||
320 | |||
321 | if (sys_mbox_trypost(&mbox, msg) != ERR_OK) { |
||
322 | memp_free(MEMP_TCPIP_MSG_API, msg); |
||
323 | return ERR_MEM; |
||
324 | } |
||
325 | return ERR_OK; |
||
326 | } |
||
327 | |||
328 | #if LWIP_TCPIP_TIMEOUT && LWIP_TIMERS |
||
329 | /** |
||
330 | * call sys_timeout in tcpip_thread |
||
331 | * |
||
332 | * @param msecs time in milliseconds for timeout |
||
333 | * @param h function to be called on timeout |
||
334 | * @param arg argument to pass to timeout function h |
||
335 | * @return ERR_MEM on memory error, ERR_OK otherwise |
||
336 | */ |
||
337 | err_t |
||
338 | tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg) |
||
339 | { |
||
340 | struct tcpip_msg *msg; |
||
341 | |||
342 | LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox)); |
||
343 | |||
344 | msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API); |
||
345 | if (msg == NULL) { |
||
346 | return ERR_MEM; |
||
347 | } |
||
348 | |||
349 | msg->type = TCPIP_MSG_TIMEOUT; |
||
350 | msg->msg.tmo.msecs = msecs; |
||
351 | msg->msg.tmo.h = h; |
||
352 | msg->msg.tmo.arg = arg; |
||
353 | sys_mbox_post(&mbox, msg); |
||
354 | return ERR_OK; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * call sys_untimeout in tcpip_thread |
||
359 | * |
||
360 | * @param h function to be called on timeout |
||
361 | * @param arg argument to pass to timeout function h |
||
362 | * @return ERR_MEM on memory error, ERR_OK otherwise |
||
363 | */ |
||
364 | err_t |
||
365 | tcpip_untimeout(sys_timeout_handler h, void *arg) |
||
366 | { |
||
367 | struct tcpip_msg *msg; |
||
368 | |||
369 | LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox)); |
||
370 | |||
371 | msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API); |
||
372 | if (msg == NULL) { |
||
373 | return ERR_MEM; |
||
374 | } |
||
375 | |||
376 | msg->type = TCPIP_MSG_UNTIMEOUT; |
||
377 | msg->msg.tmo.h = h; |
||
378 | msg->msg.tmo.arg = arg; |
||
379 | sys_mbox_post(&mbox, msg); |
||
380 | return ERR_OK; |
||
381 | } |
||
382 | #endif /* LWIP_TCPIP_TIMEOUT && LWIP_TIMERS */ |
||
383 | |||
384 | |||
385 | /** |
||
386 | * Sends a message to TCPIP thread to call a function. Caller thread blocks on |
||
387 | * on a provided semaphore, which ist NOT automatically signalled by TCPIP thread, |
||
388 | * this has to be done by the user. |
||
389 | * It is recommended to use LWIP_TCPIP_CORE_LOCKING since this is the way |
||
390 | * with least runtime overhead. |
||
391 | * |
||
392 | * @param fn function to be called from TCPIP thread |
||
393 | * @param apimsg argument to API function |
||
394 | * @param sem semaphore to wait on |
||
395 | * @return ERR_OK if the function was called, another err_t if not |
||
396 | */ |
||
397 | err_t |
||
398 | tcpip_send_msg_wait_sem(tcpip_callback_fn fn, void *apimsg, sys_sem_t *sem) |
||
399 | { |
||
400 | #if LWIP_TCPIP_CORE_LOCKING |
||
401 | LWIP_UNUSED_ARG(sem); |
||
402 | LOCK_TCPIP_CORE(); |
||
403 | fn(apimsg); |
||
404 | UNLOCK_TCPIP_CORE(); |
||
405 | return ERR_OK; |
||
406 | #else /* LWIP_TCPIP_CORE_LOCKING */ |
||
407 | TCPIP_MSG_VAR_DECLARE(msg); |
||
408 | |||
409 | LWIP_ASSERT("semaphore not initialized", sys_sem_valid(sem)); |
||
410 | LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox)); |
||
411 | |||
412 | TCPIP_MSG_VAR_ALLOC(msg); |
||
413 | TCPIP_MSG_VAR_REF(msg).type = TCPIP_MSG_API; |
||
414 | TCPIP_MSG_VAR_REF(msg).msg.api_msg.function = fn; |
||
415 | TCPIP_MSG_VAR_REF(msg).msg.api_msg.msg = apimsg; |
||
416 | sys_mbox_post(&mbox, &TCPIP_MSG_VAR_REF(msg)); |
||
417 | sys_arch_sem_wait(sem, 0); |
||
418 | TCPIP_MSG_VAR_FREE(msg); |
||
419 | return ERR_OK; |
||
420 | #endif /* LWIP_TCPIP_CORE_LOCKING */ |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Synchronously calls function in TCPIP thread and waits for its completion. |
||
425 | * It is recommended to use LWIP_TCPIP_CORE_LOCKING (preferred) or |
||
426 | * LWIP_NETCONN_SEM_PER_THREAD. |
||
427 | * If not, a semaphore is created and destroyed on every call which is usually |
||
428 | * an expensive/slow operation. |
||
429 | * @param fn Function to call |
||
430 | * @param call Call parameters |
||
431 | * @return Return value from tcpip_api_call_fn |
||
432 | */ |
||
433 | err_t |
||
434 | tcpip_api_call(tcpip_api_call_fn fn, struct tcpip_api_call_data *call) |
||
435 | { |
||
436 | #if LWIP_TCPIP_CORE_LOCKING |
||
437 | err_t err; |
||
438 | LOCK_TCPIP_CORE(); |
||
439 | err = fn(call); |
||
440 | UNLOCK_TCPIP_CORE(); |
||
441 | return err; |
||
442 | #else /* LWIP_TCPIP_CORE_LOCKING */ |
||
443 | TCPIP_MSG_VAR_DECLARE(msg); |
||
444 | |||
445 | #if !LWIP_NETCONN_SEM_PER_THREAD |
||
446 | err_t err = sys_sem_new(&call->sem, 0); |
||
447 | if (err != ERR_OK) { |
||
448 | return err; |
||
449 | } |
||
450 | #endif /* LWIP_NETCONN_SEM_PER_THREAD */ |
||
451 | |||
452 | LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox)); |
||
453 | |||
454 | TCPIP_MSG_VAR_ALLOC(msg); |
||
455 | TCPIP_MSG_VAR_REF(msg).type = TCPIP_MSG_API_CALL; |
||
456 | TCPIP_MSG_VAR_REF(msg).msg.api_call.arg = call; |
||
457 | TCPIP_MSG_VAR_REF(msg).msg.api_call.function = fn; |
||
458 | #if LWIP_NETCONN_SEM_PER_THREAD |
||
459 | TCPIP_MSG_VAR_REF(msg).msg.api_call.sem = LWIP_NETCONN_THREAD_SEM_GET(); |
||
460 | #else /* LWIP_NETCONN_SEM_PER_THREAD */ |
||
461 | TCPIP_MSG_VAR_REF(msg).msg.api_call.sem = &call->sem; |
||
462 | #endif /* LWIP_NETCONN_SEM_PER_THREAD */ |
||
463 | sys_mbox_post(&mbox, &TCPIP_MSG_VAR_REF(msg)); |
||
464 | sys_arch_sem_wait(TCPIP_MSG_VAR_REF(msg).msg.api_call.sem, 0); |
||
465 | TCPIP_MSG_VAR_FREE(msg); |
||
466 | |||
467 | #if !LWIP_NETCONN_SEM_PER_THREAD |
||
468 | sys_sem_free(&call->sem); |
||
469 | #endif /* LWIP_NETCONN_SEM_PER_THREAD */ |
||
470 | |||
471 | return call->err; |
||
472 | #endif /* LWIP_TCPIP_CORE_LOCKING */ |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Allocate a structure for a static callback message and initialize it. |
||
477 | * This is intended to be used to send "static" messages from interrupt context. |
||
478 | * |
||
479 | * @param function the function to call |
||
480 | * @param ctx parameter passed to function |
||
481 | * @return a struct pointer to pass to tcpip_trycallback(). |
||
482 | */ |
||
483 | struct tcpip_callback_msg * |
||
484 | tcpip_callbackmsg_new(tcpip_callback_fn function, void *ctx) |
||
485 | { |
||
486 | struct tcpip_msg *msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API); |
||
487 | if (msg == NULL) { |
||
488 | return NULL; |
||
489 | } |
||
490 | msg->type = TCPIP_MSG_CALLBACK_STATIC; |
||
491 | msg->msg.cb.function = function; |
||
492 | msg->msg.cb.ctx = ctx; |
||
493 | return (struct tcpip_callback_msg *)msg; |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Free a callback message allocated by tcpip_callbackmsg_new(). |
||
498 | * |
||
499 | * @param msg the message to free |
||
500 | */ |
||
501 | void |
||
502 | tcpip_callbackmsg_delete(struct tcpip_callback_msg *msg) |
||
503 | { |
||
504 | memp_free(MEMP_TCPIP_MSG_API, msg); |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * Try to post a callback-message to the tcpip_thread mbox |
||
509 | * This is intended to be used to send "static" messages from interrupt context. |
||
510 | * |
||
511 | * @param msg pointer to the message to post |
||
512 | * @return sys_mbox_trypost() return code |
||
513 | */ |
||
514 | err_t |
||
515 | tcpip_trycallback(struct tcpip_callback_msg *msg) |
||
516 | { |
||
517 | LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox)); |
||
518 | return sys_mbox_trypost(&mbox, msg); |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * @ingroup lwip_os |
||
523 | * Initialize this module: |
||
524 | * - initialize all sub modules |
||
525 | * - start the tcpip_thread |
||
526 | * |
||
527 | * @param initfunc a function to call when tcpip_thread is running and finished initializing |
||
528 | * @param arg argument to pass to initfunc |
||
529 | */ |
||
530 | void |
||
531 | tcpip_init(tcpip_init_done_fn initfunc, void *arg) |
||
532 | { |
||
533 | lwip_init(); |
||
534 | |||
535 | tcpip_init_done = initfunc; |
||
536 | tcpip_init_done_arg = arg; |
||
537 | if (sys_mbox_new(&mbox, TCPIP_MBOX_SIZE) != ERR_OK) { |
||
538 | LWIP_ASSERT("failed to create tcpip_thread mbox", 0); |
||
539 | } |
||
540 | #if LWIP_TCPIP_CORE_LOCKING |
||
541 | if (sys_mutex_new(&lock_tcpip_core) != ERR_OK) { |
||
542 | LWIP_ASSERT("failed to create lock_tcpip_core", 0); |
||
543 | } |
||
544 | #endif /* LWIP_TCPIP_CORE_LOCKING */ |
||
545 | |||
546 | sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO); |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * Simple callback function used with tcpip_callback to free a pbuf |
||
551 | * (pbuf_free has a wrong signature for tcpip_callback) |
||
552 | * |
||
553 | * @param p The pbuf (chain) to be dereferenced. |
||
554 | */ |
||
555 | static void |
||
556 | pbuf_free_int(void *p) |
||
557 | { |
||
558 | struct pbuf *q = (struct pbuf *)p; |
||
559 | pbuf_free(q); |
||
560 | } |
||
561 | |||
562 | /** |
||
563 | * A simple wrapper function that allows you to free a pbuf from interrupt context. |
||
564 | * |
||
565 | * @param p The pbuf (chain) to be dereferenced. |
||
566 | * @return ERR_OK if callback could be enqueued, an err_t if not |
||
567 | */ |
||
568 | err_t |
||
569 | pbuf_free_callback(struct pbuf *p) |
||
570 | { |
||
571 | return tcpip_try_callback(pbuf_free_int, p); |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * A simple wrapper function that allows you to free heap memory from |
||
576 | * interrupt context. |
||
577 | * |
||
578 | * @param m the heap memory to free |
||
579 | * @return ERR_OK if callback could be enqueued, an err_t if not |
||
580 | */ |
||
581 | err_t |
||
582 | mem_free_callback(void *m) |
||
583 | { |
||
584 | return tcpip_try_callback(mem_free, m); |
||
585 | } |
||
586 | |||
587 | #endif /* !NO_SYS */ |