/** * @brief Initializes the lwIP stack * @param None * @retval None */ void LwIP_Init(void) { struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; uint8_t macaddress[6]={0,0,0,0,0,1}; /* Initializes the dynamic memory heap defined by MEM_SIZE.*/ mem_init(); /* Initializes the memory pools defined by MEMP_NUM_x.*/ memp_init(); #if LWIP_DHCP ipaddr.addr = 0; netmask.addr = 0; gw.addr = 0; /* Configure the board opeartin mode: Client/Server */ LCD_DisplayStringLine(Line5, " Keep Key button "); LCD_DisplayStringLine(Line6, "pressed to activate "); LCD_DisplayStringLine(Line7, " the server "); Delay(KEY_DELAY); if(!STM_EVAL_PBGetState(Button_KEY)) { Server = SELECTED; LCD_DisplayStringLine(Line5, " "); LCD_DisplayStringLine(Line6, " Server selected "); LCD_DisplayStringLine(Line7, " "); Delay(LCD_DELAY); } else { macaddress[5]=CLIENTMAC6; Server = NOT_SELECTED; LCD_DisplayStringLine(Line5, " "); LCD_DisplayStringLine(Line6, " Client selected "); LCD_DisplayStringLine(Line7, " "); Delay(LCD_DELAY); } #else IP4_ADDR(&ipaddr, 192, 168, 0, 8); IP4_ADDR(&netmask, 255, 255, 255, 0); IP4_ADDR(&gw, 192, 168, 0, 1); #endif Set_MAC_Address(macaddress); /* - netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask, struct ip_addr *gw, void *state, err_t (* init)(struct netif *netif), err_t (* input)(struct pbuf *p, struct netif *netif)) Adds your network interface to the netif_list. Allocate a struct netif and pass a pointer to this structure as the first argument. Give pointers to cleared ip_addr structures when using DHCP, or fill them with sane numbers otherwise. The state pointer may be NULL. The init function pointer must point to a initialization function for your ethernet netif interface. The following code illustrates it's use.*/ netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input); /* Registers the default network interface.*/ netif_set_default(&netif); #if LWIP_DHCP /* Creates a new DHCP client for this interface on the first call. Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at the predefined regular intervals after starting the client. You can peek in the netif->dhcp struct for the actual DHCP status.*/ dhcp_start(&netif); #endif /* When the netif is fully configured this function must be called.*/ netif_set_up(&netif); }
/* This function initializes all network interfaces */ static void msvc_netif_init(void) { #if LWIP_IPV4 && USE_ETHERNET ip4_addr_t ipaddr, netmask, gw; #endif /* LWIP_IPV4 && USE_ETHERNET */ #if USE_SLIPIF u8_t num_slip1 = 0; #if LWIP_IPV4 ip4_addr_t ipaddr_slip1, netmask_slip1, gw_slip1; #endif #if USE_SLIPIF > 1 u8_t num_slip2 = 1; #if LWIP_IPV4 ip4_addr_t ipaddr_slip2, netmask_slip2, gw_slip2; #endif #endif /* USE_SLIPIF > 1 */ #endif /* USE_SLIPIF */ #if USE_DHCP || USE_AUTOIP err_t err; #endif #if USE_PPP const char *username = NULL, *password = NULL; #ifdef PPP_USERNAME username = PPP_USERNAME; #endif #ifdef PPP_PASSWORD password = PPP_PASSWORD; #endif printf("ppp_connect: COM%d\n", (int)sio_idx); #if PPPOS_SUPPORT ppp_sio = sio_open(sio_idx); if (ppp_sio == NULL) { printf("sio_open error\n"); } else { ppp = pppos_create(&ppp_netif, ppp_output_cb, pppLinkStatusCallback, NULL); if (ppp == NULL) { printf("pppos_create error\n"); } else { ppp_set_auth(ppp, PPPAUTHTYPE_ANY, username, password); ppp_connect(ppp, 0); } } #endif /* PPPOS_SUPPORT */ #endif /* USE_PPP */ #if USE_ETHERNET #if LWIP_IPV4 #define NETIF_ADDRS &ipaddr, &netmask, &gw, ip4_addr_set_zero(&gw); ip4_addr_set_zero(&ipaddr); ip4_addr_set_zero(&netmask); #if USE_ETHERNET_TCPIP #if USE_DHCP printf("Starting lwIP, local interface IP is dhcp-enabled\n"); #elif USE_AUTOIP printf("Starting lwIP, local interface IP is autoip-enabled\n"); #else /* USE_DHCP */ LWIP_PORT_INIT_GW(&gw); LWIP_PORT_INIT_IPADDR(&ipaddr); LWIP_PORT_INIT_NETMASK(&netmask); printf("Starting lwIP, local interface IP is %s\n", ip4addr_ntoa(&ipaddr)); #endif /* USE_DHCP */ #endif /* USE_ETHERNET_TCPIP */ #else /* LWIP_IPV4 */ #define NETIF_ADDRS printf("Starting lwIP, IPv4 disable\n"); #endif /* LWIP_IPV4 */ #if NO_SYS netif_set_default(netif_add(&netif, NETIF_ADDRS NULL, pcapif_init, netif_input)); #else /* NO_SYS */ netif_set_default(netif_add(&netif, NETIF_ADDRS NULL, pcapif_init, tcpip_input)); #endif /* NO_SYS */ #if LWIP_IPV6 netif_create_ip6_linklocal_address(&netif, 1); printf("ip6 linklocal address: "); ip6_addr_debug_print(0xFFFFFFFF & ~LWIP_DBG_HALT, netif_ip6_addr(&netif, 0)); printf("\n"); #endif /* LWIP_IPV6 */ #if LWIP_NETIF_STATUS_CALLBACK netif_set_status_callback(&netif, status_callback); #endif /* LWIP_NETIF_STATUS_CALLBACK */ #if LWIP_NETIF_LINK_CALLBACK netif_set_link_callback(&netif, link_callback); #endif /* LWIP_NETIF_LINK_CALLBACK */ #if USE_ETHERNET_TCPIP #if LWIP_AUTOIP autoip_set_struct(&netif, &netif_autoip); #endif /* LWIP_AUTOIP */ #if LWIP_DHCP dhcp_set_struct(&netif, &netif_dhcp); #endif /* LWIP_DHCP */ netif_set_up(&netif); #if USE_DHCP err = dhcp_start(&netif); LWIP_ASSERT("dhcp_start failed", err == ERR_OK); #elif USE_AUTOIP err = autoip_start(&netif); LWIP_ASSERT("autoip_start failed", err == ERR_OK); #endif /* USE_DHCP */ #else /* USE_ETHERNET_TCPIP */ /* Use ethernet for PPPoE only */ netif.flags &= ~(NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP); /* no ARP */ netif.flags |= NETIF_FLAG_ETHERNET; /* but pure ethernet */ #endif /* USE_ETHERNET_TCPIP */ #if USE_PPP && PPPOE_SUPPORT /* start PPPoE after ethernet netif is added! */ ppp = pppoe_create(&ppp_netif, &netif, NULL, NULL, pppLinkStatusCallback, NULL); if (ppp == NULL) { printf("pppos_create error\n"); } else { ppp_set_auth(ppp, PPPAUTHTYPE_ANY, username, password); ppp_connect(ppp, 0); } #endif /* USE_PPP && PPPOE_SUPPORT */ #endif /* USE_ETHERNET */ #if USE_SLIPIF #if LWIP_IPV4 #define SLIP1_ADDRS &ipaddr_slip1, &netmask_slip1, &gw_slip1, LWIP_PORT_INIT_SLIP1_IPADDR(&ipaddr_slip1); LWIP_PORT_INIT_SLIP1_GW(&gw_slip1); LWIP_PORT_INIT_SLIP1_NETMASK(&netmask_slip1); printf("Starting lwIP slipif, local interface IP is %s\n", ip4addr_ntoa(&ipaddr_slip1)); #else #define SLIP1_ADDRS printf("Starting lwIP slipif\n"); #endif #if defined(SIO_USE_COMPORT) && SIO_USE_COMPORT num_slip1++; /* COM ports cannot be 0-based */ #endif netif_add(&slipif1, SLIP1_ADDRS &num_slip1, slipif_init, ip_input); #if !USE_ETHERNET netif_set_default(&slipif1); #endif /* !USE_ETHERNET */ #if LWIP_IPV6 netif_create_ip6_linklocal_address(&slipif1, 1); printf("SLIP ip6 linklocal address: "); ip6_addr_debug_print(0xFFFFFFFF & ~LWIP_DBG_HALT, netif_ip6_addr(&slipif1, 0)); printf("\n"); #endif /* LWIP_IPV6 */ #if LWIP_NETIF_STATUS_CALLBACK netif_set_status_callback(&slipif1, status_callback); #endif /* LWIP_NETIF_STATUS_CALLBACK */ #if LWIP_NETIF_LINK_CALLBACK netif_set_link_callback(&slipif1, link_callback); #endif /* LWIP_NETIF_LINK_CALLBACK */ netif_set_up(&slipif1); #if USE_SLIPIF > 1 #if LWIP_IPV4 #define SLIP2_ADDRS &ipaddr_slip2, &netmask_slip2, &gw_slip2, LWIP_PORT_INIT_SLIP2_IPADDR(&ipaddr_slip2); LWIP_PORT_INIT_SLIP2_GW(&gw_slip2); LWIP_PORT_INIT_SLIP2_NETMASK(&netmask_slip2); printf("Starting lwIP SLIP if #2, local interface IP is %s\n", ip4addr_ntoa(&ipaddr_slip2)); #else #define SLIP2_ADDRS printf("Starting lwIP SLIP if #2\n"); #endif #if defined(SIO_USE_COMPORT) && SIO_USE_COMPORT num_slip2++; /* COM ports cannot be 0-based */ #endif netif_add(&slipif2, SLIP2_ADDRS &num_slip2, slipif_init, ip_input); #if LWIP_IPV6 netif_create_ip6_linklocal_address(&slipif1, 1); printf("SLIP2 ip6 linklocal address: "); ip6_addr_debug_print(0xFFFFFFFF & ~LWIP_DBG_HALT, netif_ip6_addr(&slipif2, 0)); printf("\n"); #endif /* LWIP_IPV6 */ #if LWIP_NETIF_STATUS_CALLBACK netif_set_status_callback(&slipif2, status_callback); #endif /* LWIP_NETIF_STATUS_CALLBACK */ #if LWIP_NETIF_LINK_CALLBACK netif_set_link_callback(&slipif2, link_callback); #endif /* LWIP_NETIF_LINK_CALLBACK */ netif_set_up(&slipif2); #endif /* USE_SLIPIF > 1*/ #endif /* USE_SLIPIF */ }
msg_t LwIpThreadFunc(void* arg) { (void) arg; bool started_phy_read = false; bool link_status = false; rcc::enable_gpio<pins::red, pins::yellow, pins::green, pins::button>(); gpio::configure<pins::red, pins::yellow, pins::green>(gpio::output()); gpio::configure<pins::button>(gpio::input, gpio::pull_up()); netif_init(); etharp_init(); udp_init(); tcpip_init(NULL,NULL); std::copy(mac_addr, mac_addr+6, interface.hwaddr); std::copy(mac_addr, mac_addr+6, cfg.mac_addr.begin()); interface.hwaddr_len = 6; netif_add(&interface, &ip.addr, &netmask.addr, &gateway.addr, NULL, lwip_funcs::ethernetif_init, ::ethernet_input ); netif_set_default(&interface); netif_set_up(&interface); bool dhcp_bound = false; unsigned int history = 0; bool enabled = false; while(true) { auto eventmask = chEvtWaitAnyTimeout( RxAvailableMask | TxCompletedMask, 100 ); if( eventmask & RxAvailableMask ) { rx_walk_descriptors(&interface); } if( eventmask & TxCompletedMask ) { tx_walk_descriptors(); } unsigned int val = pins::button::get(); history = (history << 1) | val; if((history&0xF) == 12 /*~3&0xF*/ ) { if(enabled) { ptp_port.post_thread_command(uptp::SystemPort::ThreadCommands::DisableClock); pins::green::reset(); } else { ptp_port.post_thread_command(uptp::SystemPort::ThreadCommands::EnableClock); pins::green::set(); } enabled = !enabled; } if(started_phy_read) { auto phystats = eth::phy_read_register_finish(); if(!link_status && (phystats & 1)) { start_mac_dma(phystats, interface); netif_set_link_up(&interface); dhcp_start(&interface); ptp_port.start(); link_status = true; } else if(link_status && !(phystats&1)){ //link_status = false; //netif_set_link_down(&interface); } started_phy_read = false; } else { eth::phy_read_register_start(0x10, 1); started_phy_read = true; } if(link_status && interface.dhcp->state == DHCP_STATE_BOUND && !dhcp_bound) { ptp_port.ip_addr_changed(interface.ip_addr); dhcp_bound = true; } } return 0; }
/** * @brief LwIP_DHCP_Process_Handle * @param None * @retval None */ void LwIP_DHCP_Process_Handle() { struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; uint8_t iptab[4]; uint8_t iptxt[20]; switch (DHCP_state) { case DHCP_START: { dhcp_start(&netif); IPaddress = 0; DHCP_state = DHCP_WAIT_ADDRESS; #ifdef USE_LCD LCD_DisplayStringLine(Line4, (uint8_t*)" Looking for "); LCD_DisplayStringLine(Line5, (uint8_t*)" DHCP server "); LCD_DisplayStringLine(Line6, (uint8_t*)" please wait... "); #endif } break; case DHCP_WAIT_ADDRESS: { /* Read the new IP address */ IPaddress = netif.ip_addr.addr; if (IPaddress!=0) { DHCP_state = DHCP_ADDRESS_ASSIGNED; /* Stop DHCP */ dhcp_stop(&netif); #ifdef USE_LCD iptab[0] = (uint8_t)(IPaddress >> 24); iptab[1] = (uint8_t)(IPaddress >> 16); iptab[2] = (uint8_t)(IPaddress >> 8); iptab[3] = (uint8_t)(IPaddress); sprintf((char*)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]); LCD_ClearLine(Line4); LCD_ClearLine(Line5); LCD_ClearLine(Line6); /* Display the IP address */ LCD_DisplayStringLine(Line7, (uint8_t*)"IP address assigned "); LCD_DisplayStringLine(Line8, (uint8_t*)" by a DHCP server "); LCD_DisplayStringLine(Line9, iptxt); #endif } else { /* DHCP timeout */ if (netif.dhcp->tries > MAX_DHCP_TRIES) { DHCP_state = DHCP_TIMEOUT; /* Stop DHCP */ dhcp_stop(&netif); /* Static address used */ IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 ); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); netif_set_addr(&netif, &ipaddr , &netmask, &gw); #ifdef USE_LCD LCD_DisplayStringLine(Line7, (uint8_t*)" DHCP timeout "); iptab[0] = IP_ADDR3; iptab[1] = IP_ADDR2; iptab[2] = IP_ADDR1; iptab[3] = IP_ADDR0; sprintf((char*)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]); LCD_ClearLine(Line4); LCD_ClearLine(Line5); LCD_ClearLine(Line6); LCD_DisplayStringLine(Line8, (uint8_t*)" Static IP address "); LCD_DisplayStringLine(Line9, iptxt); #endif } } }
/** * @brief LWIP handling thread. * * @param[in] p pointer to a @p lwipthread_opts structure or @p NULL * @return The function does not return. */ static THD_FUNCTION(lwip_thread, 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); /* Resumes the caller and goes to the final priority.*/ chThdResume(&lwip_trp, MSG_OK); chThdSetPriority(LWIP_THREAD_PRIORITY); while (true) { eventmask_t mask = chEvtWaitAny(ALL_EVENTS); if (mask & PERIODIC_TIMER_ID) { bool 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); tcpip_callback_with_block(netif_set_link_up, &thisif, 0); #if LWIP_DHCP dhcp_start(&thisif); #endif } else { // tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down, // &thisif, 0); tcpip_callback_with_block( netif_set_link_down, &thisif, 0); #if LWIP_DHCP dhcp_stop(&thisif); #endif } } } 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); } } } } }
/** * @brief DHCP Process * @param argument: network interface * @retval None */ void DHCP_thread(void const * argument) { struct netif *netif = (struct netif *) argument; struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; uint32_t IPaddress; for (;;) { switch (DHCP_state) { case DHCP_START: { netif->ip_addr.addr = 0; netif->netmask.addr = 0; netif->gw.addr = 0; IPaddress = 0; dhcp_start(netif); DHCP_state = DHCP_WAIT_ADDRESS; #ifdef USE_LCD LCD_UsrLog (" State: Looking for DHCP sever ...\n"); #endif } break; case DHCP_WAIT_ADDRESS: { /* Read the new IP address */ IPaddress = netif->ip_addr.addr; if (IPaddress!=0) { DHCP_state = DHCP_ADDRESS_ASSIGNED; /* Stop DHCP */ dhcp_stop(netif); #ifdef USE_LCD uint8_t iptab[4]; uint8_t iptxt[20]; iptab[0] = (uint8_t)(IPaddress >> 24); iptab[1] = (uint8_t)(IPaddress >> 16); iptab[2] = (uint8_t)(IPaddress >> 8); iptab[3] = (uint8_t)(IPaddress); sprintf((char*)iptxt, "%d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]); LCD_UsrLog ("IP address assigned by a DHCP server: %s\n", iptxt); #else BSP_LED_On(LED1); #endif } else { /* DHCP timeout */ if (netif->dhcp->tries > MAX_DHCP_TRIES) { DHCP_state = DHCP_TIMEOUT; /* Stop DHCP */ dhcp_stop(netif); /* Static address used */ IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 ); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); netif_set_addr(netif, &ipaddr , &netmask, &gw); #ifdef USE_LCD uint8_t iptxt[20]; sprintf((char*)iptxt, "%d.%d.%d.%d", IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3); LCD_UsrLog ("DHCP timeout !!\n"); LCD_UsrLog ("Static IP address : %s\n", iptxt); #else BSP_LED_On(LED1); #endif } } } break; default: break; }
void start_interface(void *arg) { struct interface *ifp = arg; struct if_options *ifo = ifp->options; int nolease; size_t i; handle_carrier(LINK_UNKNOWN, 0, ifp->name); if (ifp->carrier == LINK_DOWN) { syslog(LOG_INFO, "%s: waiting for carrier", ifp->name); return; } if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6)) { /* Report client DUID */ if (duid == NULL) { if (duid_init(ifp) == 0) return; syslog(LOG_INFO, "DUID %s", hwaddr_ntoa(duid, duid_len)); } /* Report IAIDs */ syslog(LOG_INFO, "%s: IAID %s", ifp->name, hwaddr_ntoa(ifo->iaid, sizeof(ifo->iaid))); warn_iaid_conflict(ifp, ifo->iaid); for (i = 0; i < ifo->ia_len; i++) { if (memcmp(ifo->iaid, ifo->ia[i].iaid, sizeof(ifo->iaid))) { syslog(LOG_INFO, "%s: IAID %s", ifp->name, hwaddr_ntoa(ifo->ia[i].iaid, sizeof(ifo->ia[i].iaid))); warn_iaid_conflict(ifp, ifo->ia[i].iaid); } } } if (ifo->options & DHCPCD_IPV6) { if (ifo->options & DHCPCD_IPV6RS && !(ifo->options & DHCPCD_INFORM)) ipv6nd_startrs(ifp); if (!(ifo->options & DHCPCD_IPV6RS)) { if (ifo->options & DHCPCD_IA_FORCED) nolease = dhcp6_start(ifp, DH6S_INIT); else { nolease = dhcp6_find_delegates(ifp); /* Enabling the below doesn't really make * sense as there is currently no standard * to push routes via DHCPv6. * (There is an expired working draft, * maybe abandoned?) * You can also get it to work by forcing * an IA as shown above. */ #if 0 /* With no RS or delegates we might * as well try and solicit a DHCPv6 address */ if (nolease == 0) nolease = dhcp6_start(ifp, DH6S_INIT); #endif } if (nolease == -1) syslog(LOG_ERR, "%s: dhcp6_start: %m", ifp->name); } } if (ifo->options & DHCPCD_IPV4) dhcp_start(ifp); }
void EtherShield::ES_dhcp_start(uint8_t *buf, uint8_t *macaddrin, uint8_t *ipaddrin, uint8_t *maskin, uint8_t *gwipin, uint8_t *dhcpsvrin, uint8_t *dnssvrin ) { dhcp_start(buf, macaddrin, ipaddrin, maskin, gwipin, dhcpsvrin, dnssvrin ); }
/** * @brief DHCP Process * @param argument: network interface * @retval None */ void DHCP_thread(void const * argument) { struct netif *netif = (struct netif *) argument; struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; uint32_t IPaddress; for (;;) { switch (DHCP_state) { case DHCP_START: { _VNCServer_Notify(NOTIFY_SERVER_DHCP_START); netif->ip_addr.addr = 0; netif->netmask.addr = 0; netif->gw.addr = 0; IPaddress = 0; dhcp_start(netif); DHCP_state = DHCP_WAIT_ADDRESS; _VNCServer_Notify(NOTIFY_SERVER_DHCP_WAIT_ADDRESS); } break; case DHCP_WAIT_ADDRESS: { /* Read the new IP address */ IPaddress = netif->ip_addr.addr; if (IPaddress!=0) { /* Stop DHCP */ dhcp_stop(netif); DHCP_state = DHCP_ADDRESS_ASSIGNED; _VNCServer_GetAssignedAddress(IP_ADDRESS, (uint8_t)(IPaddress), (uint8_t)(IPaddress >> 8), (uint8_t)(IPaddress >> 16), (uint8_t)(IPaddress >> 24)); IPaddress = netif->netmask.addr; _VNCServer_GetAssignedAddress(SUBNET_MASK, (uint8_t)(IPaddress), (uint8_t)(IPaddress >> 8), (uint8_t)(IPaddress >> 16), (uint8_t)(IPaddress >> 24)); IPaddress = netif->gw.addr; _VNCServer_GetAssignedAddress(GW_ADDRESS, (uint8_t)(IPaddress), (uint8_t)(IPaddress >> 8), (uint8_t)(IPaddress >> 16), (uint8_t)(IPaddress >> 24)); _VNCServer_Notify(NOTIFY_SERVER_DHCP_ADDRESS_ASSIGNED); } else { /* DHCP timeout */ if (netif->dhcp->tries > MAX_DHCP_TRIES) { DHCP_state = DHCP_TIMEOUT; /* Stop DHCP */ dhcp_stop(netif); /* Static address used */ IP4_ADDR(&ipaddr, IP_ADDR3 ,IP_ADDR2 , IP_ADDR1 , IP_ADDR0 ); IP4_ADDR(&netmask, NETMASK_ADDR3, NETMASK_ADDR2, NETMASK_ADDR1, NETMASK_ADDR0); IP4_ADDR(&gw, GW_ADDR3, GW_ADDR2, GW_ADDR1, GW_ADDR0); netif_set_addr(netif, &ipaddr , &netmask, &gw); _VNCServer_GetAssignedAddress(IP_ADDRESS, IP_ADDR3, IP_ADDR2, IP_ADDR1, IP_ADDR0); _VNCServer_GetAssignedAddress(SUBNET_MASK, NETMASK_ADDR3, NETMASK_ADDR2, NETMASK_ADDR1, NETMASK_ADDR0); _VNCServer_GetAssignedAddress(GW_ADDRESS, GW_ADDR3, GW_ADDR2, GW_ADDR1, GW_ADDR0); _VNCServer_Notify(NOTIFY_SERVER_DHCP_TIMEOUT); } } } break; default: break; }
void dhcp_init(cyg_addrword_t arg) { uint no_timeout = 1; int need_LinkLocal = 1; while( Network_TCPIP_ON == 0 ) ppause(100); //ZOTIPS dhcp_start(WLanface); dhcp_start(Lanface); //ZOTIPS ppause(2000); if ( !strcmp(EEPROM_Data.WLESSID, "") || !strcmp(EEPROM_Data.WLESSID, "< ANY >")) dhcp_serch(25); else dhcp_serch(13); while(1) { cyg_semaphore_init( &dhcp_sem, 0); no_timeout = 1; #ifdef LINKLOCAL_IP // Ron Add 11/28/04 if ( (mib_DHCP_p->IPAddr == 0x0) && need_LinkLocal ) //ZOTIPS Give_ip_by_myself(WLanface); Give_ip_by_myself(Lanface); //ZOTIPS else { if(EEPROM_Data.RENVEnable == 1) { if( rendezvous_TaskHdl == 0) cyg_semaphore_post( &rendezvous_sem); else Need_Rendezous_Reload = 1; } } #else if( mib_DHCP_p->IPAddr == 0x0 ) set_factory_ip(); #endif if( (mib_DHCP_p->IPAddr == 0x0 ) || ((NGET32(EEPROM_Data.BoxIPAddress) & 0x0000FFFF)==0x0000FEA9) ) no_timeout = cyg_semaphore_timed_wait( &dhcp_sem, cyg_current_time() + 90000); else cyg_semaphore_wait( &dhcp_sem); if( no_timeout == 0 ) need_LinkLocal = 0; else need_LinkLocal = 1; erase_netif_ipaddr(); delete_dhcp_time(); ppause(500); dhcp_serch(3); } }
UINT32 WiFiService(void) { static uint APScanStartTime; uint32 ret = -1; int temp; //////////////////////////////////////////////////////////////////////////////////////////////// //WIFI Scan if (GetMsg(MSG_WIFI_SCAN_START)) { if (wl_do_escan()) { SendMsg(MSG_WIFI_SCAN_ERR); } else { SendMsg(MSG_WIFI_SCAN); APScanStartTime = SysTickCounter;//SysTickCounter unit is 0.5ms printf("APScanStartTime:%d\n", APScanStartTime); } } if (CheckMsg(MSG_WIFI_SCAN)) { if (GetScanEventOpen()) { if (SDC_GetSdioEvent(0) == SDC_SUCCESS) { process_sdio_pending_irqs(); } /* check the scan process..*/ temp = rk_scan_work(); if ((temp > 0) || /* check the scan timeout.*/ (SysTickCounter > APScanStartTime + AP_SCAN_TIMEOUT)) { printf("scan tout\n"); ClearMsg(MSG_WIFI_SCAN); SendMsg(MSG_WIFI_SCAN_OK); } } else { ClearMsg(MSG_WIFI_SCAN); } } //WIFI Scan end //////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////// if (GetMsg(MSG_WIFI_CONNECT)) { int APScanclrTail; ClearMsg(MSG_WIFI_APPW_JUMP); ClearMsg(MSG_WIFI_SCAN); ModuleOverlay(MODULE_ID_WIFI_WPA, MODULE_OVERLAY_DATA | MODULE_OVERLAY_BSS); //goto scan code rk_printf_save_ap(); if (0 != rk_wifi_connect_pro(gSysConfig.WiFiConfig.ssid, gSysConfig.WiFiConfig.password, strlen(gSysConfig.WiFiConfig.password))) { SendMsg(MSG_WIFI_CONN_FAILURE); } else { NETIF *netif; uint32 timeout; IP_ADDR3 * pIp; int i; if (GetMsg(MSG_WIFI_NO_ENCODE)) { MSG_SET_WPA_SH(); } ModuleOverlay(MODULE_ID_WIFI_LWIP, MODULE_OVERLAY_ALL); ModuleOverlay(MODULE_ID_WIFI_DHCP, MODULE_OVERLAY_ALL); for (i=0; i<3; i++) { tcp_pcb_init(); udp_pcb_init(); arp_list_init(); netif = netif_add(NULL, NULL, NULL); eth_tickinit(); dhcp_start(netif); udp_user_use(); dns_init(); timeout = SysTickCounter; while(1) { if (netif_is_up()) { netif->dhcp = NULL; SendMsg(MSG_WIFI_CONN_SUCCESS); return TRUE; } if(SysTickCounter > (timeout + 2000)) { break; } /* Ethernet data packet received procession....*/ //printf("fle-->eth service\n"); eth_service(FALSE); } } SendMsg(MSG_WIFI_CONN_FAILURE); } } if (CheckMsg(MSG_WIFI_TCP_CONNECTING)) { UserIsrDisable(); eth_service(0); UserIsrEnable(1); } if(CheckMsg(MSG_QPW_ENABLE)) { QpwNumber++; if(QpwNumber > 0x780000) { // 1 min QpwNumber = 0; upload_connect(C_QPW); } } return ret; }
int network_config(void) { struct ifnet * ifn; in_addr_t ip_addr; in_addr_t netmask = INADDR_ANY; in_addr_t gw_addr = INADDR_ANY; char s[64]; char s1[16]; char s2[16]; char * env; uint8_t * ethaddr; uint64_t esn; int dhcp; /* Initialize MAC address with the MCU's UID */ ethaddr = (uint8_t *)&esn; esn = *((uint64_t *)STM32F_UID); DCC_LOG2(LOG_TRACE, "ESN=0x%08x%08x", esn >> 32, esn); ethaddr[0] = (ethaddr[0] & 0xfc) | 0x02; /* Locally administered MAC */ DCC_LOG(LOG_TRACE, "tcpip_init()."); tcpip_init(); if ((env = getenv("IPCFG")) == NULL) { tracef("IPCFG not set, using defaults!\n"); /* default configuration */ strcpy(s, "192.168.0.128 255.255.255.0 192.168.0.1 0"); /* set the default configuration */ setenv("IPCFG", s, 1); } else { strcpy(s, env); } if ((env = getenv("ETHADDR")) != NULL) { eth_strtomac(ethaddr, env); } else { trace("Ethernet MAC address not set, using defaults!"); DCC_LOG(LOG_WARNING, "Ethernet MAC address not set."); } tracef("* mac addr: %02x-%02x-%02x-%02x-%02x-%02x", ethaddr[0], ethaddr[1], ethaddr[2], ethaddr[3], ethaddr[4], ethaddr[5]); if (!inet_aton(strtok(s, " ,"), (struct in_addr *)&ip_addr)) { DCC_LOG(LOG_WARNING, "inet_aton() failed."); return -1; } if (inet_aton(strtok(NULL, " ,"), (struct in_addr *)&netmask)) { if (inet_aton(strtok(NULL, " ,"), (struct in_addr *)&gw_addr)) { dhcp = strtoul(strtok(NULL, ""), NULL, 0); } } /* initialize the Ethernet interface */ /* configure the ip address */ ifn = ethif_init(ethaddr, ip_addr, netmask); // ifn = loopif_init(ip_addr, netmask); ifn_getname(ifn, s); ifn_ipv4_get(ifn, &ip_addr, &netmask); tracef("* netif %s: %s, %s", s, inet_ntop(AF_INET, (void *)&ip_addr, s1, 16), inet_ntop(AF_INET, (void *)&netmask, s2, 16)); if (gw_addr != INADDR_ANY) { /* add the default route (gateway) to ethif */ ipv4_route_add(INADDR_ANY, INADDR_ANY, gw_addr, ifn); tracef("* default route gw: %s", inet_ntop(AF_INET, (void *)&gw_addr, s1, 16)); } if (dhcp) { #if 0 /* configure the initial ip address */ dhcp_start(); /* schedule the interface to be configured through dhcp */ dhcp_ifconfig(ethif, dhcp_callback); tracef("DHCP started.\n"); #endif } return 0; }
static void tcpip_init_done_callback(void *arg) { rt_device_t device; struct eth_device *ethif; struct ip_addr ipaddr, netmask, gw; struct rt_list_node* node; struct rt_object* object; struct rt_object_information *information; extern struct rt_object_information rt_object_container[]; LWIP_ASSERT("invalid arg.\n",arg); IP4_ADDR(&gw, 0,0,0,0); IP4_ADDR(&ipaddr, 0,0,0,0); IP4_ADDR(&netmask, 0,0,0,0); /* enter critical */ rt_enter_critical(); /* for each network interfaces */ information = &rt_object_container[RT_Object_Class_Device]; for (node = information->object_list.next; node != &(information->object_list); node = node->next) { object = rt_list_entry(node, struct rt_object, list); device = (rt_device_t)object; if (device->type == RT_Device_Class_NetIf) { ethif = (struct eth_device *)device; /* leave critical */ rt_exit_critical(); netif_add(ethif->netif, &ipaddr, &netmask, &gw, ethif, netif_device_init, tcpip_input); if (netif_default == NULL) netif_set_default(ethif->netif); #ifdef LWIP_IPV6 ethif->netif->output_ip6 = ethip6_output; netif_create_ip6_linklocal_address(ethif->netif, 1); #ifdef LWIP_IPV6_AUTOCONFIG ethif->netif->ip6_autoconfig_enabled = 1; #endif #ifdef LWIP_IPV6_MLD ethif->netif->mld_mac_filter = NULL; #endif #endif #if LWIP_DHCP if (ethif->flags & NETIF_FLAG_DHCP) { /* if this interface uses DHCP, start the DHCP client */ dhcp_start(ethif->netif); } else #endif { /* set interface up */ netif_set_up(ethif->netif); } #ifdef LWIP_NETIF_LINK_CALLBACK netif_set_link_up(ethif->netif); #endif /* enter critical */ rt_enter_critical(); } } /* leave critical */ rt_exit_critical(); rt_sem_release((rt_sem_t)arg); }
/*-----------------------------------------------------------------------------------*/ static void main_thread(void *arg) { struct ip_addr ipaddr, netmask, gw; sys_sem_t sem; netif_init(); sem = sys_sem_new(0); tcpip_init(tcpip_init_done, &sem); sys_sem_wait(sem); sys_sem_free(sem); printf("TCP/IP initialized.\n"); #if LWIP_DHCP { struct netif *netif; IP4_ADDR(&gw, 0,0,0,0); IP4_ADDR(&ipaddr, 0,0,0,0); IP4_ADDR(&netmask, 0,0,0,0); netif = netif_add(&ipaddr, &netmask, &gw, tapif_init, tcpip_input); netif_set_default(netif); dhcp_init(); dhcp_start(netif); } #else IP4_ADDR(&gw, 192,168,0,1); IP4_ADDR(&ipaddr, 192,168,0,2); IP4_ADDR(&netmask, 255,255,255,0); /* netif_set_default(netif_add(&ipaddr, &netmask, &gw, tapif_init, tcpip_input));*/ netif_set_default(netif_add(&ipaddr, &netmask, &gw, tapif_init, tcpip_input)); #endif /* Only used for testing purposes: */ /* IP4_ADDR(&gw, 193,10,66,1); IP4_ADDR(&ipaddr, 193,10,66,107); IP4_ADDR(&netmask, 255,255,252,0); netif_add(&ipaddr, &netmask, &gw, pcapif_init, tcpip_input);*/ IP4_ADDR(&gw, 127,0,0,1); IP4_ADDR(&ipaddr, 127,0,0,1); IP4_ADDR(&netmask, 255,0,0,0); netif_add(&ipaddr, &netmask, &gw, loopif_init, tcpip_input); tcpecho_init(); shell_init(); httpd_init(); udpecho_init(); printf("Applications started.\n"); /* sys_timeout(5000, tcp_timeout, NULL);*/ #ifdef MEM_PERF mem_perf_init("/tmp/memstats.client"); #endif /* MEM_PERF */ /* Block for ever. */ sem = sys_sem_new(0); sys_sem_wait(sem); }
int main() { struct ip_addr ipaddr, netmask, gw; /* the mac address of the board. this should be unique per board */ unsigned char mac_ethernet_address[] = { 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 }; echo_netif = &server_netif; #ifdef __arm__ #if XPAR_GIGE_PCS_PMA_SGMII_CORE_PRESENT == 1 || XPAR_GIGE_PCS_PMA_1000BASEX_CORE_PRESENT == 1 ProgramSi5324(); ProgramSfpPhy(); #endif #endif init_platform(); #ifdef XLWIP_CONFIG_INCLUDE_AXIETH_ON_ZYNQ /* PHY Autoneg and EMAC configuration */ EthFMC_init_axiemac(EMAC_BASEADDR,mac_ethernet_address); #endif #if LWIP_DHCP==1 ipaddr.addr = 0; gw.addr = 0; netmask.addr = 0; #else /* initliaze IP addresses to be used */ IP4_ADDR(&ipaddr, 192, 168, 1, 10); IP4_ADDR(&netmask, 255, 255, 255, 0); IP4_ADDR(&gw, 192, 168, 1, 1); #endif print_app_header(); lwip_init(); /* Add network interface to the netif_list, and set it as default */ if (!xemac_add(echo_netif, &ipaddr, &netmask, &gw, mac_ethernet_address, EMAC_BASEADDR)) { xil_printf("Error adding N/W interface\n\r"); return -1; } netif_set_default(echo_netif); /* now enable interrupts */ platform_enable_interrupts(); /* specify that the network if is up */ netif_set_up(echo_netif); #if (LWIP_DHCP==1) /* Create a new DHCP client for this interface. * Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at * the predefined regular intervals after starting the client. */ dhcp_start(echo_netif); dhcp_timoutcntr = 24; while(((echo_netif->ip_addr.addr) == 0) && (dhcp_timoutcntr > 0)) xemacif_input(echo_netif); if (dhcp_timoutcntr <= 0) { if ((echo_netif->ip_addr.addr) == 0) { xil_printf("DHCP Timeout\r\n"); xil_printf("Configuring default IP of 192.168.1.10\r\n"); IP4_ADDR(&(echo_netif->ip_addr), 192, 168, 1, 10); IP4_ADDR(&(echo_netif->netmask), 255, 255, 255, 0); IP4_ADDR(&(echo_netif->gw), 192, 168, 1, 1); } } ipaddr.addr = echo_netif->ip_addr.addr; gw.addr = echo_netif->gw.addr; netmask.addr = echo_netif->netmask.addr; #endif print_ip_settings(&ipaddr, &netmask, &gw); /* start the application (web server, rxtest, txtest, etc..) */ start_application(); /* receive and process packets */ while (1) { xemacif_input(echo_netif); transfer_data(); } /* never reached */ cleanup_platform(); return 0; }
int netif_ioctl_cfg(void *data, size_t size) { struct netif *netif; struct ifcfg *ifcfg; struct dhcp_state *state; if (!data) return -EFAULT; if (size != sizeof(struct ifcfg)) return -EINVAL; ifcfg = (struct ifcfg *) data; netif = netif_find(ifcfg->name); if (!netif) return -ENXIO; // Check for interface down if ((ifcfg->flags & IFCFG_UP) == 0 && (netif->flags & NETIF_UP) == 1) { // Release DHCP lease dhcp_stop(netif); netif->flags &= ~NETIF_UP; } // Update network interface configuration if (ifcfg->flags & IFCFG_DHCP) { netif->flags |= NETIF_DHCP; } else { netif->flags &= ~NETIF_DHCP; } netif->ipaddr.addr = ((struct sockaddr_in *) &ifcfg->addr)->sin_addr.s_addr; netif->netmask.addr = ((struct sockaddr_in *) &ifcfg->netmask)->sin_addr.s_addr; netif->gw.addr = ((struct sockaddr_in *) &ifcfg->gw)->sin_addr.s_addr; netif->broadcast.addr = ((struct sockaddr_in *) &ifcfg->broadcast)->sin_addr.s_addr; if (netif->broadcast.addr == IP_ADDR_ANY) { netif->broadcast.addr = (netif->ipaddr.addr & netif->netmask.addr) | ~(netif->netmask.addr); } if (ifcfg->flags & IFCFG_DEFAULT) { netif_default = netif; } else if (netif == netif_default) { netif_default = NULL; } // Copy hwaddr into ifcfg as info memcpy(ifcfg->hwaddr, &netif->hwaddr, sizeof(struct eth_addr)); // Check for interface up if ((ifcfg->flags & IFCFG_UP) == 1 && (netif->flags & NETIF_UP) == 0) { netif->flags |= NETIF_UP; if (netif->flags & NETIF_DHCP) { // Obtain network parameters using DHCP state = dhcp_start(netif); if (state) { if (wait_for_object(&state->binding_complete, 30000) < 0) return -ETIMEDOUT; ((struct sockaddr_in *) &ifcfg->addr)->sin_addr.s_addr = netif->ipaddr.addr; ((struct sockaddr_in *) &ifcfg->netmask)->sin_addr.s_addr = netif->netmask.addr; ((struct sockaddr_in *) &ifcfg->gw)->sin_addr.s_addr = netif->gw.addr; ((struct sockaddr_in *) &ifcfg->broadcast)->sin_addr.s_addr = netif->broadcast.addr; } } } return 0; }