00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "lwip/opt.h"
00040
00041 #if !NO_SYS
00042
00043 #include "lwip/sys.h"
00044 #include "lwip/memp.h"
00045 #include "lwip/pbuf.h"
00046 #include "lwip/ip_frag.h"
00047 #include "lwip/tcp.h"
00048 #include "lwip/autoip.h"
00049 #include "lwip/dhcp.h"
00050 #include "lwip/igmp.h"
00051 #include "lwip/dns.h"
00052 #include "lwip/tcpip.h"
00053 #include "lwip/init.h"
00054 #include "netif/etharp.h"
00055 #include "netif/ppp_oe.h"
00056
00057
00058 static void (* tcpip_init_done)(void *arg);
00059 static void *tcpip_init_done_arg;
00060 static sys_mbox_t mbox = SYS_MBOX_NULL;
00061
00062 #if LWIP_TCPIP_CORE_LOCKING
00063
00064 sys_sem_t lock_tcpip_core;
00065 #endif
00066
00067 #if LWIP_TCP
00068
00069 static int tcpip_tcp_timer_active;
00070
00071
00072
00073
00074
00075
00076 static void
00077 tcpip_tcp_timer(void *arg)
00078 {
00079 LWIP_UNUSED_ARG(arg);
00080
00081
00082 tcp_tmr();
00083
00084 if (tcp_active_pcbs || tcp_tw_pcbs) {
00085
00086 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
00087 } else {
00088
00089 tcpip_tcp_timer_active = 0;
00090 }
00091 }
00092
00093 #if !NO_SYS
00094
00095
00096
00097
00098
00099 void
00100 tcp_timer_needed(void)
00101 {
00102
00103 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
00104
00105 tcpip_tcp_timer_active = 1;
00106 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
00107 }
00108 }
00109 #endif
00110 #endif
00111
00112 #if IP_REASSEMBLY
00113
00114
00115
00116
00117
00118 static void
00119 ip_reass_timer(void *arg)
00120 {
00121 LWIP_UNUSED_ARG(arg);
00122 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: ip_reass_tmr()\n"));
00123 ip_reass_tmr();
00124 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
00125 }
00126 #endif
00127
00128 #if LWIP_ARP
00129
00130
00131
00132
00133
00134 static void
00135 arp_timer(void *arg)
00136 {
00137 LWIP_UNUSED_ARG(arg);
00138 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: etharp_tmr()\n"));
00139 etharp_tmr();
00140 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
00141 }
00142 #endif
00143
00144 #if LWIP_DHCP
00145
00146
00147
00148
00149
00150 static void
00151 dhcp_timer_coarse(void *arg)
00152 {
00153 LWIP_UNUSED_ARG(arg);
00154 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
00155 dhcp_coarse_tmr();
00156 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
00157 }
00158
00159
00160
00161
00162
00163
00164 static void
00165 dhcp_timer_fine(void *arg)
00166 {
00167 LWIP_UNUSED_ARG(arg);
00168 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
00169 dhcp_fine_tmr();
00170 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
00171 }
00172 #endif
00173
00174 #if LWIP_AUTOIP
00175
00176
00177
00178
00179
00180 static void
00181 autoip_timer(void *arg)
00182 {
00183 LWIP_UNUSED_ARG(arg);
00184 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: autoip_tmr()\n"));
00185 autoip_tmr();
00186 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
00187 }
00188 #endif
00189
00190 #if LWIP_IGMP
00191
00192
00193
00194
00195
00196 static void
00197 igmp_timer(void *arg)
00198 {
00199 LWIP_UNUSED_ARG(arg);
00200 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: igmp_tmr()\n"));
00201 igmp_tmr();
00202 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
00203 }
00204 #endif
00205
00206 #if LWIP_DNS
00207
00208
00209
00210
00211
00212 static void
00213 dns_timer(void *arg)
00214 {
00215 LWIP_UNUSED_ARG(arg);
00216 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dns_tmr()\n"));
00217 dns_tmr();
00218 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
00219 }
00220 #endif
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 static void
00233 tcpip_thread(void *arg)
00234 {
00235 struct tcpip_msg *msg;
00236 LWIP_UNUSED_ARG(arg);
00237
00238 #if IP_REASSEMBLY
00239 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
00240 #endif
00241 #if LWIP_ARP
00242 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
00243 #endif
00244 #if LWIP_DHCP
00245 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
00246 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
00247 #endif
00248 #if LWIP_AUTOIP
00249 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
00250 #endif
00251 #if LWIP_IGMP
00252 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
00253 #endif
00254 #if LWIP_DNS
00255 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
00256 #endif
00257
00258 if (tcpip_init_done != NULL) {
00259 tcpip_init_done(tcpip_init_done_arg);
00260 }
00261
00262 LOCK_TCPIP_CORE();
00263 while (1) {
00264 sys_mbox_fetch(mbox, (void *)&msg);
00265 switch (msg->type) {
00266 #if LWIP_NETCONN
00267 case TCPIP_MSG_API:
00268 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
00269 msg->msg.apimsg->function(&(msg->msg.apimsg->msg));
00270 break;
00271 #endif
00272
00273 case TCPIP_MSG_INPKT:
00274 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
00275 #if LWIP_ARP
00276 if (msg->msg.inp.netif->flags & NETIF_FLAG_ETHARP) {
00277 ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
00278 } else
00279 #endif
00280 { ip_input(msg->msg.inp.p, msg->msg.inp.netif);
00281 }
00282 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
00283 break;
00284
00285 #if LWIP_NETIF_API
00286 case TCPIP_MSG_NETIFAPI:
00287 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg));
00288 msg->msg.netifapimsg->function(&(msg->msg.netifapimsg->msg));
00289 break;
00290 #endif
00291
00292 case TCPIP_MSG_CALLBACK:
00293 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
00294 msg->msg.cb.f(msg->msg.cb.ctx);
00295 memp_free(MEMP_TCPIP_MSG_API, msg);
00296 break;
00297
00298 case TCPIP_MSG_TIMEOUT:
00299 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
00300 sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
00301 memp_free(MEMP_TCPIP_MSG_API, msg);
00302 break;
00303 case TCPIP_MSG_UNTIMEOUT:
00304 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
00305 sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
00306 memp_free(MEMP_TCPIP_MSG_API, msg);
00307 break;
00308
00309 default:
00310 break;
00311 }
00312 }
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322 err_t
00323 tcpip_input(struct pbuf *p, struct netif *inp)
00324 {
00325 struct tcpip_msg *msg;
00326
00327 if (mbox != SYS_MBOX_NULL) {
00328 msg = memp_malloc(MEMP_TCPIP_MSG_INPKT);
00329 if (msg == NULL) {
00330 return ERR_MEM;
00331 }
00332
00333 msg->type = TCPIP_MSG_INPKT;
00334 msg->msg.inp.p = p;
00335 msg->msg.inp.netif = inp;
00336 if (sys_mbox_trypost(mbox, msg) != ERR_OK) {
00337 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
00338 return ERR_MEM;
00339 }
00340 return ERR_OK;
00341 }
00342 return ERR_VAL;
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 err_t
00357 tcpip_callback_with_block(void (*f)(void *ctx), void *ctx, u8_t block)
00358 {
00359 struct tcpip_msg *msg;
00360
00361 if (mbox != SYS_MBOX_NULL) {
00362 msg = memp_malloc(MEMP_TCPIP_MSG_API);
00363 if (msg == NULL) {
00364 return ERR_MEM;
00365 }
00366
00367 msg->type = TCPIP_MSG_CALLBACK;
00368 msg->msg.cb.f = f;
00369 msg->msg.cb.ctx = ctx;
00370 if (block) {
00371 sys_mbox_post(mbox, msg);
00372 } else {
00373 if (sys_mbox_trypost(mbox, msg) != ERR_OK) {
00374 memp_free(MEMP_TCPIP_MSG_API, msg);
00375 return ERR_MEM;
00376 }
00377 }
00378 return ERR_OK;
00379 }
00380 return ERR_VAL;
00381 }
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 err_t
00392 tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
00393 {
00394 struct tcpip_msg *msg;
00395
00396 if (mbox != SYS_MBOX_NULL) {
00397 msg = memp_malloc(MEMP_TCPIP_MSG_API);
00398 if (msg == NULL) {
00399 return ERR_MEM;
00400 }
00401
00402 msg->type = TCPIP_MSG_TIMEOUT;
00403 msg->msg.tmo.msecs = msecs;
00404 msg->msg.tmo.h = h;
00405 msg->msg.tmo.arg = arg;
00406 sys_mbox_post(mbox, msg);
00407 return ERR_OK;
00408 }
00409 return ERR_VAL;
00410 }
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 err_t
00421 tcpip_untimeout(sys_timeout_handler h, void *arg)
00422 {
00423 struct tcpip_msg *msg;
00424
00425 if (mbox != SYS_MBOX_NULL) {
00426 msg = memp_malloc(MEMP_TCPIP_MSG_API);
00427 if (msg == NULL) {
00428 return ERR_MEM;
00429 }
00430
00431 msg->type = TCPIP_MSG_UNTIMEOUT;
00432 msg->msg.tmo.h = h;
00433 msg->msg.tmo.arg = arg;
00434 sys_mbox_post(mbox, msg);
00435 return ERR_OK;
00436 }
00437 return ERR_VAL;
00438 }
00439
00440 #if LWIP_NETCONN
00441
00442
00443
00444
00445
00446
00447
00448
00449 err_t
00450 tcpip_apimsg(struct api_msg *apimsg)
00451 {
00452 struct tcpip_msg msg;
00453
00454 if (mbox != SYS_MBOX_NULL) {
00455 msg.type = TCPIP_MSG_API;
00456 msg.msg.apimsg = apimsg;
00457 sys_mbox_post(mbox, &msg);
00458 sys_arch_sem_wait(apimsg->msg.conn->op_completed, 0);
00459 return ERR_OK;
00460 }
00461 return ERR_VAL;
00462 }
00463
00464 #if LWIP_TCPIP_CORE_LOCKING
00465
00466
00467
00468
00469
00470
00471
00472
00473 err_t
00474 tcpip_apimsg_lock(struct api_msg *apimsg)
00475 {
00476 LOCK_TCPIP_CORE();
00477 apimsg->function(&(apimsg->msg));
00478 UNLOCK_TCPIP_CORE();
00479 return ERR_OK;
00480
00481 }
00482 #endif
00483 #endif
00484
00485 #if LWIP_NETIF_API
00486 #if !LWIP_TCPIP_CORE_LOCKING
00487
00488
00489
00490
00491
00492
00493
00494 err_t
00495 tcpip_netifapi(struct netifapi_msg* netifapimsg)
00496 {
00497 struct tcpip_msg msg;
00498
00499 if (mbox != SYS_MBOX_NULL) {
00500 netifapimsg->msg.sem = sys_sem_new(0);
00501 if (netifapimsg->msg.sem == SYS_SEM_NULL) {
00502 netifapimsg->msg.err = ERR_MEM;
00503 return netifapimsg->msg.err;
00504 }
00505
00506 msg.type = TCPIP_MSG_NETIFAPI;
00507 msg.msg.netifapimsg = netifapimsg;
00508 sys_mbox_post(mbox, &msg);
00509 sys_sem_wait(netifapimsg->msg.sem);
00510 sys_sem_free(netifapimsg->msg.sem);
00511 return netifapimsg->msg.err;
00512 }
00513 return ERR_VAL;
00514 }
00515 #else
00516
00517
00518
00519
00520
00521
00522
00523
00524 err_t
00525 tcpip_netifapi_lock(struct netifapi_msg* netifapimsg)
00526 {
00527 LOCK_TCPIP_CORE();
00528 netifapimsg->function(&(netifapimsg->msg));
00529 UNLOCK_TCPIP_CORE();
00530 return netifapimsg->msg.err;
00531 }
00532 #endif
00533 #endif
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 void
00544 tcpip_init(void (* initfunc)(void *), void *arg)
00545 {
00546 lwip_init();
00547
00548 tcpip_init_done = initfunc;
00549 tcpip_init_done_arg = arg;
00550 mbox = sys_mbox_new(TCPIP_MBOX_SIZE);
00551 #if LWIP_TCPIP_CORE_LOCKING
00552 lock_tcpip_core = sys_sem_new(1);
00553 #endif
00554
00555 sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
00556 }
00557
00558
00559
00560
00561
00562
00563
00564 static void
00565 pbuf_free_int(void *p)
00566 {
00567 struct pbuf *q = p;
00568 pbuf_free(q);
00569 }
00570
00571
00572
00573
00574
00575
00576
00577 err_t
00578 pbuf_free_callback(struct pbuf *p)
00579 {
00580 return tcpip_callback_with_block(pbuf_free_int, p, 0);
00581 }
00582
00583
00584
00585
00586
00587
00588
00589
00590 err_t
00591 mem_free_callback(void *m)
00592 {
00593 return tcpip_callback_with_block(mem_free, m, 0);
00594 }
00595
00596 #endif