STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
tcpip.c
Go to the documentation of this file.
1 
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/tcpip.h"
44 #include "lwip/priv/tcpip_priv.h"
45 #include "lwip/sys.h"
46 #include "lwip/memp.h"
47 #include "lwip/mem.h"
48 #include "lwip/pbuf.h"
49 #include "lwip/init.h"
50 #include "lwip/ip.h"
51 #include "netif/etharp.h"
52 #include "netif/ppp/pppoe.h"
53 #include "netif/ppp/pppos.h"
54 
55 #define TCPIP_MSG_VAR_REF(name) API_VAR_REF(name)
56 #define TCPIP_MSG_VAR_DECLARE(name) API_VAR_DECLARE(struct tcpip_msg, name)
57 #define TCPIP_MSG_VAR_ALLOC(name) API_VAR_ALLOC(struct tcpip_msg, MEMP_TCPIP_MSG_API, name)
58 #define TCPIP_MSG_VAR_FREE(name) API_VAR_FREE(MEMP_TCPIP_MSG_API, name)
59 
60 /* global variables */
62 static void *tcpip_init_done_arg;
63 static sys_mbox_t mbox;
64 
65 #if LWIP_TCPIP_CORE_LOCKING
66 
67 sys_mutex_t lock_tcpip_core;
68 #endif /* LWIP_TCPIP_CORE_LOCKING */
69 
70 
81 static void
82 tcpip_thread(void *arg)
83 {
84  struct tcpip_msg *msg;
85  LWIP_UNUSED_ARG(arg);
86 
87  if (tcpip_init_done != NULL) {
88  tcpip_init_done(tcpip_init_done_arg);
89  }
90 
92  while (1) { /* MAIN Loop */
95  /* wait for a message, timeouts are processed while waiting */
96  sys_timeouts_mbox_fetch(&mbox, (void **)&msg);
98  if (msg == NULL) {
99  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: NULL\n"));
100  LWIP_ASSERT("tcpip_thread: invalid message", 0);
101  continue;
102  }
103  switch (msg->type) {
104 #if LWIP_NETCONN || LWIP_SOCKET
105  case TCPIP_MSG_API:
106  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
107  msg->msg.apimsg->function(&(msg->msg.apimsg->msg));
108  break;
109 #endif /* LWIP_NETCONN || LWIP_SOCKET */
110 
111 #if !LWIP_TCPIP_CORE_LOCKING_INPUT
112  case TCPIP_MSG_INPKT:
113  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
114 #if LWIP_ETHERNET
115  if (msg->msg.inp.netif->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
116  ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
117  } else
118 #endif /* LWIP_ETHERNET */
119  ip_input(msg->msg.inp.p, msg->msg.inp.netif);
120  memp_free(MEMP_TCPIP_MSG_INPKT, msg);
121  break;
122 
123 #if PPPOS_SUPPORT && !PPP_INPROC_IRQ_SAFE
124  case TCPIP_MSG_INPKT_PPPOS:
125  pppos_input_sys(msg->msg.inp.p, msg->msg.inp.netif);
126  memp_free(MEMP_TCPIP_MSG_INPKT, msg);
127  break;
128 #endif /* PPPOS_SUPPORT && !PPP_INPROC_IRQ_SAFE */
129 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
130 
131 #if LWIP_NETIF_API
132  case TCPIP_MSG_NETIFAPI:
133  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg));
134  msg->msg.netifapimsg->function(&(msg->msg.netifapimsg->msg));
135  break;
136 #endif /* LWIP_NETIF_API */
137 
138 #if LWIP_PPP_API
139  case TCPIP_MSG_PPPAPI:
140  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PPP API message %p\n", (void *)msg));
141  msg->msg.pppapimsg->function(&(msg->msg.pppapimsg->msg));
142  break;
143 #endif /* LWIP_PPP_API */
144 
145 #if LWIP_TCPIP_TIMEOUT
146  case TCPIP_MSG_TIMEOUT:
147  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
148  sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
149  memp_free(MEMP_TCPIP_MSG_API, msg);
150  break;
151  case TCPIP_MSG_UNTIMEOUT:
152  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
153  sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
154  memp_free(MEMP_TCPIP_MSG_API, msg);
155  break;
156 #endif /* LWIP_TCPIP_TIMEOUT */
157 
158  case TCPIP_MSG_CALLBACK:
159  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
160  msg->msg.cb.function(msg->msg.cb.ctx);
161  memp_free(MEMP_TCPIP_MSG_API, msg);
162  break;
163 
165  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK_STATIC %p\n", (void *)msg));
166  msg->msg.cb.function(msg->msg.cb.ctx);
167  break;
168 
169  default:
170  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: %d\n", msg->type));
171  LWIP_ASSERT("tcpip_thread: invalid message", 0);
172  break;
173  }
174  }
175 }
176 
185 err_t
186 tcpip_input(struct pbuf *p, struct netif *inp)
187 {
188 #if LWIP_TCPIP_CORE_LOCKING_INPUT
189  err_t ret;
190  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_input: PACKET %p/%p\n", (void *)p, (void *)inp));
191  LOCK_TCPIP_CORE();
192 #if LWIP_ETHERNET
194  ret = ethernet_input(p, inp);
195  } else
196 #endif /* LWIP_ETHERNET */
197  ret = ip_input(p, inp);
199  return ret;
200 #else /* LWIP_TCPIP_CORE_LOCKING_INPUT */
201  struct tcpip_msg *msg;
202 
203  if (!sys_mbox_valid_val(mbox)) {
204  return ERR_VAL;
205  }
206  msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT);
207  if (msg == NULL) {
208  return ERR_MEM;
209  }
210 
211  msg->type = TCPIP_MSG_INPKT;
212  msg->msg.inp.p = p;
213  msg->msg.inp.netif = inp;
214  if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
215  memp_free(MEMP_TCPIP_MSG_INPKT, msg);
216  return ERR_MEM;
217  }
218  return ERR_OK;
219 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
220 }
221 
222 #if PPPOS_SUPPORT && !PPP_INPROC_IRQ_SAFE
223 
231 err_t
232 tcpip_pppos_input(struct pbuf *p, struct netif *inp)
233 {
234 #if LWIP_TCPIP_CORE_LOCKING_INPUT
235  err_t ret;
236  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_pppos_input: PACKET %p/%p\n", (void *)p, (void *)inp));
237  LOCK_TCPIP_CORE();
238  ret = pppos_input_sys(p, inp);
240  return ret;
241 #else /* LWIP_TCPIP_CORE_LOCKING_INPUT */
242  struct tcpip_msg *msg;
243 
244  if (!sys_mbox_valid_val(mbox)) {
245  return ERR_VAL;
246  }
247  msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT);
248  if (msg == NULL) {
249  return ERR_MEM;
250  }
251 
252  msg->type = TCPIP_MSG_INPKT_PPPOS;
253  msg->msg.inp.p = p;
254  msg->msg.inp.netif = inp;
255  if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
256  memp_free(MEMP_TCPIP_MSG_INPKT, msg);
257  return ERR_MEM;
258  }
259  return ERR_OK;
260 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
261 }
262 #endif /* PPPOS_SUPPORT && !PPP_INPROC_IRQ_SAFE */
263 
275 err_t
277 {
278  struct tcpip_msg *msg;
279 
280  if (sys_mbox_valid_val(mbox)) {
281  msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
282  if (msg == NULL) {
283  return ERR_MEM;
284  }
285 
286  msg->type = TCPIP_MSG_CALLBACK;
287  msg->msg.cb.function = function;
288  msg->msg.cb.ctx = ctx;
289  if (block) {
290  sys_mbox_post(&mbox, msg);
291  } else {
292  if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
293  memp_free(MEMP_TCPIP_MSG_API, msg);
294  return ERR_MEM;
295  }
296  }
297  return ERR_OK;
298  }
299  return ERR_VAL;
300 }
301 
302 #if LWIP_TCPIP_TIMEOUT
303 
311 err_t
312 tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
313 {
314  struct tcpip_msg *msg;
315 
316  if (sys_mbox_valid_val(mbox)) {
317  msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
318  if (msg == NULL) {
319  return ERR_MEM;
320  }
321 
322  msg->type = TCPIP_MSG_TIMEOUT;
323  msg->msg.tmo.msecs = msecs;
324  msg->msg.tmo.h = h;
325  msg->msg.tmo.arg = arg;
326  sys_mbox_post(&mbox, msg);
327  return ERR_OK;
328  }
329  return ERR_VAL;
330 }
331 
340 err_t
341 tcpip_untimeout(sys_timeout_handler h, void *arg)
342 {
343  struct tcpip_msg *msg;
344 
345  if (sys_mbox_valid_val(mbox)) {
346  msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
347  if (msg == NULL) {
348  return ERR_MEM;
349  }
350 
351  msg->type = TCPIP_MSG_UNTIMEOUT;
352  msg->msg.tmo.h = h;
353  msg->msg.tmo.arg = arg;
354  sys_mbox_post(&mbox, msg);
355  return ERR_OK;
356  }
357  return ERR_VAL;
358 }
359 #endif /* LWIP_TCPIP_TIMEOUT */
360 
361 #if LWIP_NETCONN || LWIP_SOCKET
362 
370 err_t
371 tcpip_apimsg(struct api_msg *apimsg)
372 {
374 #ifdef LWIP_DEBUG
375  /* catch functions that don't set err */
376  apimsg->msg.err = ERR_VAL;
377 #endif
378 
379  if (sys_mbox_valid_val(mbox)) {
381  TCPIP_MSG_VAR_REF(msg).type = TCPIP_MSG_API;
382  TCPIP_MSG_VAR_REF(msg).msg.apimsg = apimsg;
383 #if LWIP_NETCONN_SEM_PER_THREAD
384  apimsg->msg.op_completed_sem = LWIP_NETCONN_THREAD_SEM_GET();
385  LWIP_ASSERT("netconn semaphore not initialized",
386  sys_sem_valid(apimsg->msg.op_completed_sem));
387 #endif
389  sys_arch_sem_wait(LWIP_API_MSG_SEM(&apimsg->msg), 0);
391  return apimsg->msg.err;
392  }
393  return ERR_VAL;
394 }
395 
396 #endif /* LWIP_NETCONN || LWIP_SOCKET */
397 
398 #if LWIP_NETIF_API
399 #if !LWIP_TCPIP_CORE_LOCKING
400 
407 err_t
408 tcpip_netifapi(struct netifapi_msg* netifapimsg)
409 {
411 
412  if (sys_mbox_valid_val(mbox)) {
413  err_t err;
415 
416  err = sys_sem_new(&netifapimsg->msg.sem, 0);
417  if (err != ERR_OK) {
418  netifapimsg->msg.err = err;
419  return err;
420  }
421 
422  TCPIP_MSG_VAR_REF(msg).type = TCPIP_MSG_NETIFAPI;
423  TCPIP_MSG_VAR_REF(msg).msg.netifapimsg = netifapimsg;
425  sys_sem_wait(&netifapimsg->msg.sem);
426  sys_sem_free(&netifapimsg->msg.sem);
428  return netifapimsg->msg.err;
429  }
430  return ERR_VAL;
431 }
432 #else /* !LWIP_TCPIP_CORE_LOCKING */
433 
441 err_t
442 tcpip_netifapi_lock(struct netifapi_msg* netifapimsg)
443 {
444  LOCK_TCPIP_CORE();
445  netifapimsg->function(&(netifapimsg->msg));
447  return netifapimsg->msg.err;
448 }
449 #endif /* !LWIP_TCPIP_CORE_LOCKING */
450 #endif /* LWIP_NETIF_API */
451 
452 #if LWIP_PPP_API
453 #if !LWIP_TCPIP_CORE_LOCKING
454 
461 err_t
462 tcpip_pppapi(struct pppapi_msg* pppapimsg)
463 {
464  struct tcpip_msg msg;
465 
466  if (sys_mbox_valid_val(mbox)) {
467  err_t err = sys_sem_new(&pppapimsg->msg.sem, 0);
468  if (err != ERR_OK) {
469  pppapimsg->msg.err = err;
470  return err;
471  }
472 
473  msg.type = TCPIP_MSG_PPPAPI;
474  msg.msg.pppapimsg = pppapimsg;
475  sys_mbox_post(&mbox, &msg);
476  sys_sem_wait(&pppapimsg->msg.sem);
477  sys_sem_free(&pppapimsg->msg.sem);
478  return pppapimsg->msg.err;
479  }
480  return ERR_VAL;
481 }
482 #else /* !LWIP_TCPIP_CORE_LOCKING */
483 
491 err_t
492 tcpip_pppapi_lock(struct pppapi_msg* pppapimsg)
493 {
494  LOCK_TCPIP_CORE();
495  pppapimsg->function(&(pppapimsg->msg));
497  return pppapimsg->msg.err;
498 }
499 #endif /* !LWIP_TCPIP_CORE_LOCKING */
500 #endif /* LWIP_PPP_API */
501 
510 struct tcpip_callback_msg*
512 {
513  struct tcpip_msg *msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
514  if (msg == NULL) {
515  return NULL;
516  }
518  msg->msg.cb.function = function;
519  msg->msg.cb.ctx = ctx;
520  return (struct tcpip_callback_msg*)msg;
521 }
522 
528 void
529 tcpip_callbackmsg_delete(struct tcpip_callback_msg* msg)
530 {
531  memp_free(MEMP_TCPIP_MSG_API, msg);
532 }
533 
541 err_t
542 tcpip_trycallback(struct tcpip_callback_msg* msg)
543 {
544  if (!sys_mbox_valid_val(mbox)) {
545  return ERR_VAL;
546  }
547  return sys_mbox_trypost(&mbox, msg);
548 }
549 
558 void
559 tcpip_init(tcpip_init_done_fn initfunc, void *arg)
560 {
561  lwip_init();
562 
563  tcpip_init_done = initfunc;
564  tcpip_init_done_arg = arg;
565  if (sys_mbox_new(&mbox, TCPIP_MBOX_SIZE) != ERR_OK) {
566  LWIP_ASSERT("failed to create tcpip_thread mbox", 0);
567  }
568 #if LWIP_TCPIP_CORE_LOCKING
569  if (sys_mutex_new(&lock_tcpip_core) != ERR_OK) {
570  LWIP_ASSERT("failed to create lock_tcpip_core", 0);
571  }
572 #endif /* LWIP_TCPIP_CORE_LOCKING */
573 
575 }
576 
583 static void
584 pbuf_free_int(void *p)
585 {
586  struct pbuf *q = (struct pbuf *)p;
587  pbuf_free(q);
588 }
589 
596 err_t
598 {
599  return tcpip_callback_with_block(pbuf_free_int, p, 0);
600 }
601 
609 err_t
611 {
612  return tcpip_callback_with_block(mem_free, m, 0);
613 }
614 
615 #endif /* !NO_SYS */
616 
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
Definition: sys_arch.c:391
u8_t flags
Definition: netif.h:269
err_t sys_mutex_new(sys_mutex_t *mutex)
Definition: sys_arch.c:339
void(* tcpip_init_done_fn)(void *arg)
Definition: tcpip.h:49
void mem_free(void *rmem)
Definition: mem.c:334
void(* tcpip_callback_fn)(void *ctx)
Definition: tcpip.h:51
#define TCPIP_MSG_VAR_ALLOC(name)
Definition: tcpip.c:57
#define TCPIP_MSG_VAR_REF(name)
Definition: tcpip.c:55
err_t mem_free_callback(void *m)
Definition: tcpip.c:610
#define LOCK_TCPIP_CORE()
Definition: tcpip_priv.h:91
#define ERR_VAL
Definition: err.h:58
err_t tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
Definition: tcpip.c:276
err_t tcpip_trycallback(struct tcpip_callback_msg *msg)
Definition: tcpip.c:542
#define LWIP_TCPIP_THREAD_ALIVE()
Definition: tcpip_priv.h:56
err_t ip_input(struct pbuf *p, struct netif *inp)
struct pbuf * p
Definition: tcpip_priv.h:195
err_t pbuf_free_callback(struct pbuf *p)
Definition: tcpip.c:597
osMessageQId sys_mbox_t
Definition: sys_arch.h:46
void memp_free(memp_t type, void *mem)
Definition: memp.c:399
#define TCPIP_THREAD_PRIO
Definition: lwipopts.h:244
void sys_sem_free(sys_sem_t *sem)
Definition: sys_arch.c:303
tcpip_callback_fn function
Definition: tcpip_priv.h:199
#define sys_mbox_valid_val(mbox)
Definition: sys.h:229
void lwip_init(void)
Definition: init.c:310
err_t sys_sem_new(sys_sem_t *sem, u8_t count)
Definition: sys_arch.c:221
int sys_sem_valid(sys_sem_t *sem)
Definition: sys_arch.c:312
#define TCPIP_MSG_VAR_FREE(name)
Definition: tcpip.c:58
#define sys_sem_wait(sem)
Definition: sys.h:157
void tcpip_init_done(void *)
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
err_t sys_mbox_new(sys_mbox_t *mbox, int size)
Definition: sys_arch.c:52
unsigned long u32_t
Definition: cc.h:42
#define NETIF_FLAG_ETHARP
Definition: netif.h:90
#define TCPIP_MSG_VAR_DECLARE(name)
Definition: tcpip.c:56
void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
#define TCPIP_MBOX_SIZE
Definition: lwipopts.h:239
err_t tcpip_input(struct pbuf *p, struct netif *inp)
Definition: tcpip.c:186
#define ERR_OK
Definition: err.h:52
Definition: pbuf.h:108
#define TCPIP_THREAD_STACKSIZE
Definition: lwipopts.h:238
union tcpip_msg::@48 msg
s8_t err_t
Definition: err.h:47
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
Definition: netif.h:182
#define NETIF_FLAG_ETHERNET
Definition: netif.h:94
#define UNLOCK_TCPIP_CORE()
Definition: tcpip_priv.h:92
void(* sys_timeout_handler)(void *arg)
Definition: lwip_timers.h:65
enum tcpip_msg_type type
Definition: tcpip_priv.h:182
#define TCPIP_THREAD_NAME
Definition: lwipopts.h:237
void tcpip_init(tcpip_init_done_fn initfunc, void *arg)
Definition: tcpip.c:559
struct tcpip_msg::@48::@49 inp
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
Definition: sys_arch.c:263
struct tcpip_msg::@48::@50 cb
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
Definition: sys_arch.c:106
unsigned char u8_t
Definition: cc.h:38
void sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
void sys_mbox_post(sys_mbox_t *mbox, void *msg)
Definition: sys_arch.c:98
void * ctx
Definition: tcpip_priv.h:200
void tcpip_callbackmsg_delete(struct tcpip_callback_msg *msg)
Definition: tcpip.c:529
void * memp_malloc(memp_t type)
Definition: memp.c:303
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:113
#define ERR_MEM
Definition: err.h:53
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
void sys_untimeout(sys_timeout_handler handler, void *arg)
struct tcpip_callback_msg * tcpip_callbackmsg_new(tcpip_callback_fn function, void *ctx)
Definition: tcpip.c:511
#define TCPIP_DEBUG
Definition: opt.h:2957
osSemaphoreId sys_mutex_t
Definition: sys_arch.h:45