/** * @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; switch (DHCP_state) { case DHCP_START: { dhcp_start(&netif); IPaddress = 0; DHCP_state = DHCP_WAIT_ADDRESS; LwIP_DHCP_begin(); } 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); /* DHCP complete */ LwIP_DHCP_done(IPaddress); } else { /* DHCP timeout */ if (netif.dhcp->tries > MAX_DHCP_TRIES) { DHCP_state = DHCP_TIMEOUT; /* Stop DHCP */ dhcp_stop(&netif); /* Deliver timeout call */ LwIP_DHCP_timeout(); /* 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); /* Deliver static IP complete call */ LwIP_static_done((IP_ADDR0 << 24) | (IP_ADDR1 << 16) | (IP_ADDR2 << 8) | IP_ADDR3); } } } break; default: break; } }
/** * @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; switch (DHCP_state) { case DHCP_START: { dhcp_start(&netif); IPaddress = 0; DHCP_state = DHCP_WAIT_ADDRESS; } 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); STM_EVAL_LEDOn(LED1); } 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); STM_EVAL_LEDOn(LED1); } } } break; default: break; } }
void lwip_bringdown(void) { // Disconnect from the network dhcp_release(&lwip_netif); dhcp_stop(&lwip_netif); lwip_ip_addr[0] = '\0'; }
/********************************************************************************************************* ** 函数名称: netif_remove_hook ** 功能描述: 删除网络接口回调函数. (网络上下文中调用) ** 输 入 : pvNetif 网络接口 ** 输 出 : ** 全局变量: ** 调用模块: *********************************************************************************************************/ VOID netif_remove_hook (PVOID pvNetif) { struct netif *netif = (struct netif *)pvNetif; INT iNum = (INT)netif->num; rt_netif_remove_hook(netif); /* 更新路由表有效标志 */ if (iNum < __LW_NETIF_MAX_NUM) { LWIP_NETIF_LOCK(); /* 进入临界区 */ _G_pnetifArray[iNum] = LW_NULL; _G_uiNetifNum--; LWIP_NETIF_UNLOCK(); /* 退出临界区 */ } #if LW_CFG_NET_NPF_EN > 0 __npfNetifRemoveHook(netif); #endif /* LW_CFG_NET_NPF_EN > 0 */ #if LW_CFG_LWIP_DHCP > 0 if (netif->dhcp) { dhcp_stop(netif); /* 关闭 DHCP 回收 UDP 控制块 */ dhcp_cleanup(netif); /* 回收 DHCP 内存 */ } #endif /* LW_CFG_LWIP_DHCP > 0 */ #if LW_CFG_LWIP_AUTOIP > 0 if (netif->autoip) { mem_free(netif->autoip); /* 回收 AUTOIP 内存 */ netif->autoip = LW_NULL; } #endif /* LW_CFG_LWIP_AUTOIP > 0 */ }
static int runDHCP(const char* ifname) { char ipaddr[PROPERTY_VALUE_MAX]; uint32_t prefixLength; char gateway[PROPERTY_VALUE_MAX]; char dns1[PROPERTY_VALUE_MAX]; char dns2[PROPERTY_VALUE_MAX]; char dns3[PROPERTY_VALUE_MAX]; char dns4[PROPERTY_VALUE_MAX]; const char *dns[5] = {dns1, dns2, dns3, dns4, NULL}; char server[PROPERTY_VALUE_MAX]; uint32_t lease; char vendorInfo[PROPERTY_VALUE_MAX]; char domains[PROPERTY_VALUE_MAX]; ALOGD("stop dhcp for wlan0..."); dhcp_stop("wlan0"); ifc_clear_addresses("wlan0"); //dhcp_do_request: start dhcpcd service to get ip address if(dhcp_do_request(ifname, ipaddr, gateway, &prefixLength, dns, server, &lease, vendorInfo, domains)) { ALOGE("dhcp_do_request for wlan0 failed!"); return -1; } //ifc_configure: use the dhcp info to configure interface if(ifc_configure(ifname, inet_addr(ipaddr), prefixLength, inet_addr(gateway), inet_addr(dns1), inet_addr(dns2))) { ALOGE("ifc_configure for wlan0 failed!"); return -1; } return 0; }
void disable_wifi() { wifi_state = DISABLING; ALOGD("stop dhcp for wlan0..."); dhcp_stop("wlan0"); ifc_disable("wlan0"); ifc_clear_addresses("wlan0"); ALOGD("stop supplicant..."); if(wifi_stop_supplicant(1) != 0) { ALOGE("stop supplicant failed!"); } ALOGD("unload wlan driver..."); wifi_unload_driver(); wifi_state = DISABLED; WIFI_MSG_INFO_CALLBACK("Wifi diabled."); //disable wifi also a event automanager should check. sem_post(&sem); return ; }
// This sets the IP configuration on-the-fly void ethernet_set_configuration(IPAddress ipAddress, IPAddress netMask, IPAddress gateWay) { if ((gs_net_if.flags & NETIF_FLAG_DHCP) != 0) { // stop DHCP if it was used before dhcp_stop(&gs_net_if); } struct ip_addr x_ip_addr, x_net_mask, x_gateway; x_ip_addr.addr = ipAddress.GetV4LittleEndian(); x_net_mask.addr = netMask.GetV4LittleEndian(); x_gateway.addr = gateWay.GetV4LittleEndian(); if (x_ip_addr.addr == 0) { // start DHCP and request a dynamic IP address dhcp_start(&gs_net_if); } else { // use static IP address netif_set_ipaddr(&gs_net_if, &x_ip_addr); netif_set_netmask(&gs_net_if, &x_net_mask); netif_set_gw(&gs_net_if, &x_gateway); // don't forget to set it up again netif_set_up(&gs_net_if); } }
// This sets the IP configuration on-the-fly void ethernet_set_configuration(const uint8_t ipAddress[], const uint8_t netMask[], const uint8_t gateWay[]) { if ((gs_net_if.flags & NETIF_FLAG_DHCP) != 0) { // stop DHCP if it was used before dhcp_stop(&gs_net_if); } struct ip_addr x_ip_addr, x_net_mask, x_gateway; IP4_ADDR(&x_ip_addr, ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3]); IP4_ADDR(&x_net_mask, netMask[0], netMask[1], netMask[2], netMask[3]); IP4_ADDR(&x_gateway, gateWay[0], gateWay[1], gateWay[2], gateWay[3]); if (x_ip_addr.addr == 0) { // start DHCP and request a dynamic IP address dhcp_start(&gs_net_if); } else { // use static IP address netif_set_ipaddr(&gs_net_if, &x_ip_addr); netif_set_netmask(&gs_net_if, &x_net_mask); netif_set_gw(&gs_net_if, &x_gateway); // don't forget to set it up again netif_set_up(&gs_net_if); } }
static void _lwip_sys_init(void) { struct ip_addr ipaddr, netmask, gw; unsigned char *ip; ip = getCfgIP(); IP4_ADDR(&ipaddr,ip[0],ip[1],ip[2],ip[3]); ip = getCfgGateWay(); IP4_ADDR(&gw, ip[0],ip[1],ip[2],ip[3]); ip = getCfgNetMask(); IP4_ADDR(&netmask,ip[0],ip[1],ip[2],ip[3]); tcpip_init(RT_NULL, RT_NULL); netif_set_addr(netif_default, &ipaddr, &netmask, &gw); // We SHOULD define an IP address (udp setup should work any time). // So, it may send DHCP packets of src_addr not all zero, tested ok. /* if( isDHCPEnabled() ) { // when DHCP is enabled, ip/netmask/gateway should be set to all 0. IP4_ADDR(&ipaddr,0,0,0,0); netif_set_addr(netif_default, &ipaddr, &ipaddr, &ipaddr); } else { netif_set_addr(netif_default, &ipaddr, &netmask, &gw); }*/ netif_set_up(netif_default); if( isDHCPEnabled() ) dhcp_start(netif_default); else dhcp_stop(netif_default); }
void net_shutdown(void) { printf("Releasing DHCP lease...\n"); dhcp_release(ð); dhcp_stop(ð); printf("Shutting down network...\n"); netif_remove(ð); gelicif_shutdown(ð); }
mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_obj_t *args) { if (n_args == 0) { // Get IP addresses const ip_addr_t *dns = dns_getserver(0); mp_obj_t tuple[4] = { netutils_format_ipv4_addr((uint8_t*)&netif->ip_addr, NETUTILS_BIG), netutils_format_ipv4_addr((uint8_t*)&netif->netmask, NETUTILS_BIG), netutils_format_ipv4_addr((uint8_t*)&netif->gw, NETUTILS_BIG), netutils_format_ipv4_addr((uint8_t*)dns, NETUTILS_BIG), }; return mp_obj_new_tuple(4, tuple); } else if (args[0] == MP_OBJ_NEW_QSTR(MP_QSTR_dhcp)) { // Start the DHCP client if (dhcp_supplied_address(netif)) { dhcp_renew(netif); } else { dhcp_stop(netif); dhcp_start(netif); } // Wait for DHCP to get IP address uint32_t start = mp_hal_ticks_ms(); while (!dhcp_supplied_address(netif)) { if (mp_hal_ticks_ms() - start > 10000) { mp_raise_msg(&mp_type_OSError, "timeout waiting for DHCP to get IP address"); } mp_hal_delay_ms(100); } return mp_const_none; } else { // Release and stop any existing DHCP dhcp_release(netif); dhcp_stop(netif); // Set static IP addresses mp_obj_t *items; mp_obj_get_array_fixed_n(args[0], 4, &items); netutils_parse_ipv4_addr(items[0], (uint8_t*)&netif->ip_addr, NETUTILS_BIG); netutils_parse_ipv4_addr(items[1], (uint8_t*)&netif->netmask, NETUTILS_BIG); netutils_parse_ipv4_addr(items[2], (uint8_t*)&netif->gw, NETUTILS_BIG); ip_addr_t dns; netutils_parse_ipv4_addr(items[3], (uint8_t*)&dns, NETUTILS_BIG); dns_setserver(0, &dns); return mp_const_none; } }
void cbIP_removePanInterface(void) { LWIP_PRINT("Interface down\n"); dhcp_stop(&panIf.hInterface); netif_remove(&panIf.hInterface); dhcp_cleanup(&panIf.hInterface); }
//Stop DHCP protocol on a specific layer3 interface. BOOL lwipStopDHCP(LPVOID pL3Interface) { struct netif* pif = (struct netif*)pL3Interface; if (NULL == pif) { BUG(); } dhcp_stop(pif); return TRUE; }
int EthernetInterface::disconnect() { if (use_dhcp) { dhcp_release(&netif); dhcp_stop(&netif); } else { netif_set_down(&netif); } eth_arch_disable_interrupts(); return 0; }
int EthernetInterface::disconnect() { if (use_dhcp) { dhcp_release(&lpcNetif); dhcp_stop(&lpcNetif); } else { netif_set_down(&lpcNetif); } NVIC_DisableIRQ(ENET_IRQn); return 0; }
nsapi_error_t mbed_lwip_bringdown_2(bool ppp) { // Check if we've connected if (lwip_connected == NSAPI_STATUS_DISCONNECTED) { return NSAPI_ERROR_PARAMETER; } #if LWIP_DHCP // Disconnect from the network if (lwip_dhcp) { dhcp_release(&lwip_netif); dhcp_stop(&lwip_netif); lwip_dhcp = false; lwip_dhcp_has_to_be_set = false; } #endif if (ppp) { /* this is a blocking call, returns when PPP is properly closed */ err_t err = ppp_lwip_disconnect(); if (err) { return mbed_lwip_err_remap(err); } MBED_ASSERT(!netif_is_link_up(&lwip_netif)); /*if (netif_is_link_up(&lwip_netif)) { if (sys_arch_sem_wait(&lwip_netif_unlinked, 15000) == SYS_ARCH_TIMEOUT) { return NSAPI_ERROR_DEVICE_ERROR; } }*/ } else { netif_set_down(&lwip_netif); } #if LWIP_IPV6 mbed_lwip_clear_ipv6_addresses(&lwip_netif); #endif sys_sem_free(&lwip_netif_has_any_addr); sys_sem_new(&lwip_netif_has_any_addr, 0); #if PREF_ADDR_TIMEOUT sys_sem_free(&lwip_netif_has_pref_addr); sys_sem_new(&lwip_netif_has_pref_addr, 0); #endif #if BOTH_ADDR_TIMEOUT sys_sem_free(&lwip_netif_has_both_addr); sys_sem_new(&lwip_netif_has_both_addr, 0); #endif lwip_has_addr_state = 0; lwip_connected = NSAPI_STATUS_DISCONNECTED; return 0; }
void lwip_dhcp_stop(void *pParameter) { struct netif *pNetif = (struct netif *)pParameter; struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; if(pNetif->ip_addr.addr != 0) { /* Stop DHCP */ dhcp_stop(pNetif); uprintf(UPRINT_INFO, UPRINT_BLK_NET, "LWIP DHCP get IP addr: 0x%08x\n", pNetif->ip_addr.addr); } else { /* DHCP timeout */ if (pNetif->dhcp->tries > MAX_DHCP_TRIES) { /* Stop DHCP */ dhcp_stop(pNetif); /* 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(pNetif, &ipaddr , &netmask, &gw); uprintf(UPRINT_INFO, UPRINT_BLK_NET, "LWIP DHCP use static IP addr: 0x%08x\n", pNetif->ip_addr.addr); } else { /* continue to monitor */ sys_timeout(2000, lwip_dhcp_stop, pNetif); } } }
int lwip_bringdown(void) { // Check if we've connected if (!lwip_get_ip_address()) { return NSAPI_ERROR_PARAMETER; } // Disconnect from the network dhcp_release(&lwip_netif); dhcp_stop(&lwip_netif); lwip_ip_addr[0] = '\0'; return 0; }
int ps2ip_setconfig(t_ip_info* pInfo) { NetIF* pNetIF=netif_find(pInfo->netif_name); if (pNetIF==NULL) { return 0; } netif_set_ipaddr(pNetIF,(IPAddr*)&pInfo->ipaddr); netif_set_netmask(pNetIF,(IPAddr*)&pInfo->netmask); netif_set_gw(pNetIF,(IPAddr*)&pInfo->gw); #if LWIP_DHCP //Enable dhcp here if (pInfo->dhcp_enabled) { if (!pNetIF->dhcp) { //Start dhcp client dhcp_start(pNetIF); } } else { if (pNetIF->dhcp) { //Stop dhcp client dhcp_stop(pNetIF); } } #endif return 1; }
static void wl_cm_disconn_cb(void* ctx) { struct ctx_server* hs = ctx; LINK_LED_OFF(); INFO_INIT("Disconnection cb...\n"); if (hs->net_cfg.dhcp_running) { printk("link down, release dhcp\n"); dhcp_release(hs->net_cfg.netif); dhcp_stop(hs->net_cfg.netif); hs->net_cfg.dhcp_running = 0; } else { printk("link down\n"); netif_set_down(hs->net_cfg.netif); } set_result_cmd(WL_FAILURE); }
/** * @brief Stop lwip service in a certain operation mode. * @param[in] uint8_t opmode: the current operation mode. * @retval None */ void lwip_net_stop(uint8_t opmode) { struct netif *sta_if; struct netif *ap_if; sta_if = netif_find_by_type(NETIF_TYPE_STA); ap_if = netif_find_by_type(NETIF_TYPE_AP); switch (opmode) { case WIFI_MODE_AP_ONLY: dhcpd_stop(); netif_set_link_down(ap_if); break; case WIFI_MODE_STA_ONLY: case WIFI_MODE_REPEATER: netif_set_status_callback(sta_if, NULL); dhcp_release(sta_if); dhcp_stop(sta_if); netif_set_link_down(sta_if); break; } }
nsapi_error_t mbed_lwip_bringdown(void) { // Check if we've connected if (!lwip_connected) { return NSAPI_ERROR_PARAMETER; } #if LWIP_IPV4 // Disconnect from the network if (lwip_dhcp) { dhcp_release(&lwip_netif); dhcp_stop(&lwip_netif); lwip_dhcp = false; } else { netif_set_down(&lwip_netif); } #endif lwip_connected = false; // TO DO - actually remove addresses from stack, and shut down properly return 0; }
/** * @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 BSP_LCD_ClearStringLine(7); BSP_LCD_ClearStringLine(8); BSP_LCD_ClearStringLine(9); BSP_LCD_DisplayStringAtLine(8,(uint8_t *) " Looking for"); BSP_LCD_DisplayStringAtLine(9,(uint8_t *) " DHCP sever ..."); #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]); BSP_LCD_ClearStringLine(7); BSP_LCD_ClearStringLine(8); BSP_LCD_ClearStringLine(9); BSP_LCD_DisplayStringAtLine(7,(uint8_t *) " IP address assigned"); BSP_LCD_DisplayStringAtLine(8,(uint8_t *) " by a DHCP server:"); BSP_LCD_DisplayStringAtLine(9,(uint8_t *) 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); BSP_LCD_ClearStringLine(7); BSP_LCD_ClearStringLine(8); BSP_LCD_ClearStringLine(9); BSP_LCD_DisplayStringAtLine(7,(uint8_t *) " DHCP timeout !!"); BSP_LCD_DisplayStringAtLine(8,(uint8_t *) " Static IP address :"); BSP_LCD_DisplayStringAtLine(9,(uint8_t *) iptxt); #else BSP_LED_On(LED1); #endif } } } break; default: break; }
/** * @brief LwIP_DHCP_Process_Handle * @param None * @retval None */ void LwIP_DHCP_task(void * pvParameters) { struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; uint32_t IPaddress; uint8_t iptab[4]; uint8_t iptxt[20]; uint8_t DHCP_state; DHCP_state = DHCP_START; for (;;) { switch (DHCP_state) { case DHCP_START: { dhcp_start(&xnetif); 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 = xnetif.ip_addr.addr; if (IPaddress!=0) { DHCP_state = DHCP_ADDRESS_ASSIGNED; /* Stop DHCP */ dhcp_stop(&xnetif); #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 /* end of DHCP process: LED1 stays ON*/ STM_EVAL_LEDOn(LED1); vTaskDelete(NULL); } else { /* DHCP timeout */ if (xnetif.dhcp->tries > MAX_DHCP_TRIES) { DHCP_state = DHCP_TIMEOUT; /* Stop DHCP */ dhcp_stop(&xnetif); /* 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(&xnetif, &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 /* end of DHCP process: LED1 stays ON*/ STM_EVAL_LEDOn(LED1); vTaskDelete(NULL); } } } break; default: break; }
/** * @brief Link callback function, this function is called on change of link status. * @param The network interface * @retval None */ void ETH_link_callback(struct netif *netif) { __IO uint32_t timeout = 0; uint32_t tmpreg; uint16_t RegValue; struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; #ifndef USE_DHCP uint8_t iptab[4] = {0}; uint8_t iptxt[20]; #endif /* USE_DHCP */ /* Clear LCD */ // LCD_ClearLine(Line4); // LCD_ClearLine(Line5); // LCD_ClearLine(Line6); // LCD_ClearLine(Line7); // LCD_ClearLine(Line8); // LCD_ClearLine(Line9); if(netif_is_link_up(netif)) { /* Restart the auto-negotiation */ if(ETH_InitStructure.ETH_AutoNegotiation != ETH_AutoNegotiation_Disable) { /* Reset Timeout counter */ timeout = 0; /* Enable auto-negotiation */ ETH_WritePHYRegister(DP83848_PHY_ADDRESS, PHY_BCR, PHY_AutoNegotiation); /* Wait until the auto-negotiation will be completed */ do { timeout++; } while (!(ETH_ReadPHYRegister(DP83848_PHY_ADDRESS, PHY_BSR) & PHY_AutoNego_Complete) && (timeout < (uint32_t)PHY_READ_TO)); /* Reset Timeout counter */ timeout = 0; /* Read the result of the auto-negotiation */ RegValue = ETH_ReadPHYRegister(DP83848_PHY_ADDRESS, PHY_SR); /* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */ if((RegValue & PHY_DUPLEX_STATUS) != (uint16_t)RESET) { /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */ ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex; } else { /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */ ETH_InitStructure.ETH_Mode = ETH_Mode_HalfDuplex; } /* Configure the MAC with the speed fixed by the auto-negotiation process */ if(RegValue & PHY_SPEED_STATUS) { /* Set Ethernet speed to 10M following the auto-negotiation */ ETH_InitStructure.ETH_Speed = ETH_Speed_10M; } else { /* Set Ethernet speed to 100M following the auto-negotiation */ ETH_InitStructure.ETH_Speed = ETH_Speed_100M; } /*------------------------ ETHERNET MACCR Re-Configuration --------------------*/ /* Get the ETHERNET MACCR value */ tmpreg = ETH->MACCR; /* Set the FES bit according to ETH_Speed value */ /* Set the DM bit according to ETH_Mode value */ tmpreg |= (uint32_t)(ETH_InitStructure.ETH_Speed | ETH_InitStructure.ETH_Mode); /* Write to ETHERNET MACCR */ ETH->MACCR = (uint32_t)tmpreg; _eth_delay_(ETH_REG_WRITE_DELAY); tmpreg = ETH->MACCR; ETH->MACCR = tmpreg; } /* Restart MAC interface */ ETH_Start(); #ifdef USE_DHCP ipaddr.addr = 0; netmask.addr = 0; gw.addr = 0; DHCP_state = DHCP_START; #else 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); #endif /* USE_DHCP */ netif_set_addr(&gnetif, &ipaddr , &netmask, &gw); /* When the netif is fully configured this function must be called.*/ netif_set_up(&gnetif); #ifdef USE_LCD /* Set the LCD Text Color */ LCD_SetTextColor(Green); /* Display message on the LCD */ LCD_DisplayStringLine(Line5, (uint8_t*)" Network Cable is "); LCD_DisplayStringLine(Line6, (uint8_t*)" now connected "); /* Set the LCD Text Color */ LCD_SetTextColor(White); #ifndef USE_DHCP /* Display static IP address */ 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_DisplayStringLine(Line8, (uint8_t*)" Static IP address "); LCD_DisplayStringLine(Line9, iptxt); /* Clear LCD */ LCD_ClearLine(Line5); LCD_ClearLine(Line6); #endif /* USE_DHCP */ #endif /* USE_LCD */ EthLinkStatus = 0; } else { ETH_Stop(); #ifdef USE_DHCP DHCP_state = DHCP_LINK_DOWN; dhcp_stop(netif); #endif /* USE_DHCP */ /* When the netif link is down this function must be called.*/ netif_set_down(&gnetif); #ifdef USE_LCD /* Set the LCD Text Color */ LCD_SetTextColor(Red); /* Display message on the LCD */ LCD_DisplayStringLine(Line5, (uint8_t*)" Network Cable is "); LCD_DisplayStringLine(Line6, (uint8_t*)" unplugged "); /* Set the LCD Text Color */ LCD_SetTextColor(White); #endif /* USE_LCD */ } }
void ethProcess() { static uint32_t lastCheck = 0; if (ticks - lastCheck >= 1000) { lastCheck = ticks; TByteBuffer b; if (ethPrepareBuffer(&b, 2)) { uint16_t type = 0x0000; BYTEBUFFER_APPEND(&b, type); ethSendPacket(&b); ethFreeBuffer(&b); } if (dodump) enc28j60Dump(); } static uint32_t lastARPTime = 0; if (ticks - lastARPTime >= ARP_TMR_INTERVAL) { lastARPTime = ticks; etharp_tmr(); } static uint32_t lastDHCPTime1 = 0; if (ticks - lastDHCPTime1 >= DHCP_COARSE_TIMER_MSECS) { lastDHCPTime1 = ticks; dhcp_coarse_tmr(); } static uint32_t lastDHCPTime2 = 0; if (ticks - lastDHCPTime2 >= DHCP_FINE_TIMER_MSECS) { lastDHCPTime2 = ticks; dhcp_fine_tmr(); } if (IO_IS_LOW(INT_ETH)) { uint8_t eir = enc28j60ReadControl(EIR); myprintf("eir 0x%02x\r\n", eir); if (eir & EIR_LINKIF) { uint16_t phir = enc28j60ReadPhyWord(PHIR); if (phir & PHIR_PLNKIF) { uint16_t stat = enc28j60ReadPhyWord(PHSTAT2); if (stat & PHSTAT2_LSTAT) // link is up { myprintf("LINK UP\r\n"); netif_set_up(ð_netif); dhcp_start(ð_netif); } else // link is down or was for a period { myprintf("LINK DOWN\r\n"); dhcp_stop(ð_netif); netif_set_down(ð_netif); } } myprintf("phir: 0x%04x\r\n", phir); } enc28j60_if_input(ð_netif); } }
//***************************************************************************** // //! Change the configuration of the lwIP network interface. //! //! \param ulIPAddr is the new IP address to be used (static). //! \param ulNetMask is the new network mask to be used (static). //! \param ulGWAddr is the new Gateway address to be used (static). //! \param ulIPMode is the IP Address Mode. \b IPADDR_USE_STATIC 0 will force //! static IP addressing to be used, \b IPADDR_USE_DHCP will force DHCP with //! fallback to Link Local (Auto IP), while \b IPADDR_USE_AUTOIP will force //! Link Local only. //! //! This function will evaluate the new configuration data. If necessary, the //! interface will be brought down, reconfigured, and then brought back up //! with the new configuration. //! //! \return None. // //***************************************************************************** void lwIPNetworkConfigChange(struct netif *netif, IP_CONFIG * ipCfg) { struct ip_addr ip_addr; struct ip_addr net_mask; struct ip_addr gw_addr; IP_CONFIG currentIPConfig; // Check the parameters. #if LWIP_DHCP && LWIP_AUTOIP ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) || (ipCfg->IPMode == IPADDR_USE_DHCP) || (ipCfg->IPMode == IPADDR_USE_AUTOIP)) #elif LWIP_DHCP ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) || (ipCfg->IPMode == IPADDR_USE_DHCP)) #elif LWIP_AUTOIP ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) || (ipCfg->IPMode == IPADDR_USE_AUTOIP)) #else ASSERT(ipCfg->IPMode == IPADDR_USE_STATIC) #endif // Setup the network address values. if (ipCfg->IPMode == IPADDR_USE_STATIC) { ip_addr.addr = htonl(ipCfg->IPAddr); net_mask.addr = htonl(ipCfg->NetMask); gw_addr.addr = htonl(ipCfg->GWAddr); } #if LWIP_DHCP || LWIP_AUTOIP else { ip_addr.addr = 0; net_mask.addr = 0; gw_addr.addr = 0; } #endif // Switch on the current IP Address Aquisition mode. currentIPConfig.IPMode = IPADDR_USE_DHCP; LWIPServiceTaskIPConfigGet(netif, ¤tIPConfig); switch (currentIPConfig.IPMode) { // Static IP case IPADDR_USE_STATIC: { // Set the new address parameters. This will change the address // configuration in lwIP, and if necessary, will reset any links // that are active. This is valid for all three modes. // netif_set_addr(netif, &ip_addr, &net_mask, &gw_addr); // If we are going to DHCP mode, then start the DHCP server now. #if LWIP_DHCP if (ipCfg->IPMode == IPADDR_USE_DHCP) { dhcp_start(netif); } #endif // If we are going to AutoIP mode, then start the AutoIP process // now. #if LWIP_AUTOIP if (ipCfg->IPMode == IPADDR_USE_AUTOIP) { autoip_start(netif); } #endif // And we're done. break; } // DHCP (with AutoIP fallback). #if LWIP_DHCP case IPADDR_USE_DHCP: { // // If we are going to static IP addressing, then disable DHCP and // force the new static IP address. // if (ipCfg->IPMode == IPADDR_USE_STATIC) { dhcp_stop(netif); // SEE bug http://savannah.nongnu.org/bugs/?22804 netif->flags &= ~NETIF_FLAG_DHCP; netif_set_addr(netif, &ip_addr, &net_mask, &gw_addr); } // If we are going to AUTO IP addressing, then disable DHCP, set // the default addresses, and start AutoIP. #if LWIP_AUTOIP else if (ipCfg->IPMode == IPADDR_USE_AUTOIP) { dhcp_stop(netif); netif_set_addr(netif, &ip_addr, &net_mask, &gw_addr); autoip_start(netif); } #endif break; } #endif // AUTOIP #if LWIP_AUTOIP case IPADDR_USE_AUTOIP: { // // If we are going to static IP addressing, then disable AutoIP and // force the new static IP address. // if (ulIPMode == IPADDR_USE_STATIC) { autoip_stop(netif); netif_set_addr(netif, &ip_addr, &net_mask, &gw_addr); } // // If we are going to DHCP addressing, then disable AutoIP, set the // default addresses, and start dhcp. // #if LWIP_DHCP else if (ulIPMode == IPADDR_USE_AUTOIP) { autoip_stop(netif); netif_set_addr(netif, &ip_addr, &net_mask, &gw_addr); dhcp_start(netif); } #endif break; } #endif } }
/** * @brief DHCP_Process_Handle * @param None * @retval None */ void DHCP_Process(struct netif *netif) { struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; uint32_t IPaddress = 0; switch (DHCP_state) { case DHCP_START: { netif->ip_addr.addr = 0; netif->netmask.addr = 0; netif->gw.addr = 0; IPaddress = 0; DHCP_state = DHCP_WAIT_ADDRESS; dhcp_start(netif); #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 } } }
/** * @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] = {0}; uint8_t iptxt[20]; switch (DHCP_state) { case DHCP_START: { DHCP_state = DHCP_WAIT_ADDRESS; dhcp_start(&gnetif); /* IP address should be set to 0 every time we want to assign a new DHCP address */ IPaddress = 0; #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 = gnetif.ip_addr.addr; if (IPaddress!=0) { DHCP_state = DHCP_ADDRESS_ASSIGNED; /* Stop DHCP */ dhcp_stop(&gnetif); #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 STM_EVAL_LEDOn(LED1); } else { /* DHCP timeout */ if (gnetif.dhcp->tries > MAX_DHCP_TRIES) { DHCP_state = DHCP_TIMEOUT; /* Stop DHCP */ dhcp_stop(&gnetif); /* 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(&gnetif, &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 STM_EVAL_LEDOn(LED1); } } }
/** * @brief VNC Thread * @param argument: network interface * @retval None */ void VNC_Thread(void const * argument) { for (;;) { switch (VNC_State) { case VNC_LINK_UP: { gnetif.ip_addr.addr = 0; gnetif.netmask.addr = 0; gnetif.gw.addr = 0; dhcp_start(&gnetif); VNC_State = VNC_WAIT_FOR_ADDRESS; VNC_SERVER_LogMessage ("Waiting for DHCP server...\n"); VNC_SERVER_StatusChanged(VNC_WAIT_FOR_ADDRESS); } break; case VNC_WAIT_FOR_ADDRESS: { if (gnetif.ip_addr.addr!=0) { dhcp_stop(&gnetif); VNC_State = VNC_START; VNC_SERVER_StatusChanged(VNC_START); } else { /* DHCP timeout */ if (gnetif.dhcp->tries > MAX_DHCP_TRIES) { VNC_State = VNC_ERROR; dhcp_stop(&gnetif); VNC_SERVER_LogMessage ("No reply from DHCP Server!\n"); } } } break; case VNC_START: sprintf((char*)iptxt, "IP address : %d.%d.%d.%d\n", (uint8_t)(gnetif.ip_addr.addr), (uint8_t)((gnetif.ip_addr.addr) >> 8), (uint8_t)((gnetif.ip_addr.addr) >> 16), (uint8_t)((gnetif.ip_addr.addr) >> 24)); VNC_SERVER_LogMessage ((char *)iptxt); /* Init VNC context and attach to layer (so context is updated if the display-layer-contents change */ GUI_VNC_AttachToLayer(&_Context, 0); _Context.ServerIndex = 0; GUI_VNC_SetProgName ("STM32 VNC Server"); if(VNC_LockState) { GUI_VNC_SetPassword((U8 *)"STM32"); } else { GUI_VNC_SetAuthentication(NULL); } VNC_State = VNC_PROCESS; break; case VNC_PROCESS: VNC_Process(); break; case VNC_IDLE: break; default: break; } osDelay(250); } }