msg_t WebThread(void *p) { EvTimer evt1, evt2; EventListener el0, el1, el2; uip_ipaddr_t ipaddr; (void)p; /* * Event sources setup. */ chEvtRegister(macGetReceiveEventSource(ÐD1), &el0, FRAME_RECEIVED_ID); chEvtAddFlags(EVENT_MASK(FRAME_RECEIVED_ID)); /* In case some frames are already buffered */ evtInit(&evt1, MS2ST(500)); evtStart(&evt1); chEvtRegister(&evt1.et_es, &el1, PERIODIC_TIMER_ID); evtInit(&evt2, S2ST(10)); evtStart(&evt2); chEvtRegister(&evt2.et_es, &el2, ARP_TIMER_ID); /* * EMAC driver start. */ macStart(ÐD1, &mac_config); (void)macPollLinkStatus(ÐD1); /* * uIP initialization. */ uip_init(); uip_setethaddr(macaddr); uip_ipaddr(ipaddr, IPADDR0, IPADDR1, IPADDR2, IPADDR3); uip_sethostaddr(ipaddr); httpd_init(); while (TRUE) { chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS)); } return 0; }
/** * @brief LWIP handling thread. * * @param[in] p pointer to a @p lwipthread_opts structure or @p NULL * @return The function does not return. */ msg_t lwip_thread(void *p) { event_timer_t evt; event_listener_t el0, el1; struct ip_addr ip, gateway, netmask; static struct netif thisif; static const MACConfig mac_config = {thisif.hwaddr}; chRegSetThreadName("lwipthread"); /* Initializes the thing.*/ tcpip_init(NULL, NULL); /* TCP/IP parameters, runtime or compile time.*/ if (p) { struct lwipthread_opts *opts = p; unsigned i; for (i = 0; i < 6; i++) thisif.hwaddr[i] = opts->macaddress[i]; ip.addr = opts->address; gateway.addr = opts->gateway; netmask.addr = opts->netmask; } else { thisif.hwaddr[0] = LWIP_ETHADDR_0; thisif.hwaddr[1] = LWIP_ETHADDR_1; thisif.hwaddr[2] = LWIP_ETHADDR_2; thisif.hwaddr[3] = LWIP_ETHADDR_3; thisif.hwaddr[4] = LWIP_ETHADDR_4; thisif.hwaddr[5] = LWIP_ETHADDR_5; LWIP_IPADDR(&ip); LWIP_GATEWAY(&gateway); LWIP_NETMASK(&netmask); } macStart(ÐD1, &mac_config); netif_add(&thisif, &ip, &netmask, &gateway, NULL, ethernetif_init, tcpip_input); netif_set_default(&thisif); netif_set_up(&thisif); /* Setup event sources.*/ evtObjectInit(&evt, LWIP_LINK_POLL_INTERVAL); evtStart(&evt); chEvtRegisterMask(&evt.et_es, &el0, PERIODIC_TIMER_ID); chEvtRegisterMask(macGetReceiveEventSource(ÐD1), &el1, FRAME_RECEIVED_ID); chEvtAddEvents(PERIODIC_TIMER_ID | FRAME_RECEIVED_ID); /* Goes to the final priority after initialization.*/ chThdSetPriority(LWIP_THREAD_PRIORITY); while (TRUE) { eventmask_t mask = chEvtWaitAny(ALL_EVENTS); if (mask & PERIODIC_TIMER_ID) { bool_t current_link_status = macPollLinkStatus(ÐD1); if (current_link_status != netif_is_link_up(&thisif)) { if (current_link_status) tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up, &thisif, 0); else tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down, &thisif, 0); } } if (mask & FRAME_RECEIVED_ID) { struct pbuf *p; while ((p = low_level_input(&thisif)) != NULL) { struct eth_hdr *ethhdr = p->payload; switch (htons(ethhdr->type)) { /* IP or ARP packet? */ case ETHTYPE_IP: case ETHTYPE_ARP: #if PPPOE_SUPPORT /* PPPoE packet? */ case ETHTYPE_PPPOEDISC: case ETHTYPE_PPPOE: #endif /* PPPOE_SUPPORT */ /* full packet send to tcpip_thread to process */ if (thisif.input(p, &thisif) == ERR_OK) break; LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); default: pbuf_free(p); } } } } return 0; }
/* * ARP periodic timer. */ static void ARPTimerHandler(eventid_t id) { (void)id; (void)macPollLinkStatus(ÐD1); uip_arp_timer(); }