int netstack_set_ipaddr(u8 *addr) { IP4_ADDR(&lns.ipaddr, addr[0],addr[1],addr[2],addr[3]); netif_set_ipaddr(&lns.nif, &lns.ipaddr); return VMM_OK; }
int netstack_send_echo(u8 *ripaddr, u16 size, u16 seqno, struct netstack_echo_reply *reply) { int i, rc; u64 timeout = PING_DELAY_NS; struct pbuf *p; struct icmp_echo_hdr *iecho; size_t len = sizeof(struct icmp_echo_hdr) + size; LWIP_ASSERT("ping_size <= 0xffff", len <= 0xffff); /* Lock ping context for atomicity */ vmm_mutex_lock(&lns.ping_lock); /* Alloc ping pbuf */ p = pbuf_alloc(PBUF_IP, (u16_t)len, PBUF_RAM); if (!p) { vmm_mutex_unlock(&lns.ping_lock); return VMM_ENOMEM; } if ((p->len != p->tot_len) || (p->next != NULL)) { pbuf_free(p); vmm_mutex_unlock(&lns.ping_lock); return VMM_EFAIL; } /* Prepare ECHO request */ iecho = (struct icmp_echo_hdr *)p->payload; ICMPH_TYPE_SET(iecho, ICMP_ECHO); ICMPH_CODE_SET(iecho, 0); iecho->chksum = 0; iecho->id = PING_ID; iecho->seqno = htons(seqno); for (i = 0; i < size; i++) { ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; } iecho->chksum = inet_chksum(iecho, len); /* Prepare target address */ IP4_ADDR(&lns.ping_addr, ripaddr[0],ripaddr[1],ripaddr[2],ripaddr[3]); /* Save ping info */ lns.ping_seq_num = seqno; lns.ping_reply = reply; lns.ping_recv_tstamp = 0; lns.ping_send_tstamp = vmm_timer_timestamp(); lns.ping_recv_tstamp = lns.ping_send_tstamp + PING_DELAY_NS; /* Send ping packet */ raw_sendto(lns.ping_pcb, p, &lns.ping_addr); /* Wait for ping to complete with timeout */ timeout = lns.ping_recv_tstamp - lns.ping_send_tstamp; rc = vmm_completion_wait_timeout(&lns.ping_done, &timeout); timeout = lns.ping_recv_tstamp - lns.ping_send_tstamp; lns.ping_reply->rtt = udiv64(timeout, 1000); /* Free ping pbuf */ pbuf_free(p); /* Clear ping reply pointer */ lns.ping_reply = NULL; /* Unloack ping context */ vmm_mutex_unlock(&lns.ping_lock); return rc; }
static int __init lwip_netstack_init(void) { int rc; struct vmm_netswitch *nsw; struct vmm_devtree_node *node; const char *attrval; u8 ip[] = {192, 168, 0, 1}; u8 mask[] = {255, 255, 255, 0}; /* Clear lwIP state */ memset(&lns, 0, sizeof(lns)); /* Get netstack device tree node if available */ node = vmm_devtree_getnode(VMM_DEVTREE_PATH_SEPARATOR_STRING VMM_DEVTREE_VMMINFO_NODE_NAME VMM_DEVTREE_PATH_SEPARATOR_STRING VMM_DEVTREE_VMMNET_NODE_NAME VMM_DEVTREE_PATH_SEPARATOR_STRING VMM_DEVTREE_NETSTACK_NODE_NAME); /* Retrive preferred IP address */ attrval = vmm_devtree_attrval(node, "ipaddr"); if (attrval) { /* Read ip address from netstack node */ str2ipaddr(ip, attrval); } /* Retrive preferred IP address */ attrval = vmm_devtree_attrval(node, "netmask"); if (attrval) { /* Read network mask from netstack node */ str2ipaddr(mask, attrval); } /* Retrive preferred netswitch */ attrval = vmm_devtree_attrval(node, "netswitch"); if (attrval) { /* Find netswitch with given name */ nsw = vmm_netswitch_find(attrval); } else { /* Get the first netswitch */ nsw = vmm_netswitch_get(0); } if (!nsw) { vmm_panic("No netswitch found\n"); } /* Allocate a netport */ lns.port = vmm_netport_alloc("lwip-netport", VMM_NETPORT_DEF_QUEUE_SIZE); if (!lns.port) { vmm_printf("lwIP netport_alloc() failed\n"); rc = VMM_EFAIL; goto fail; } /* Setup a netport */ lns.port->mtu = 1500; lns.port->link_changed = lwip_set_link; lns.port->can_receive = lwip_can_receive; lns.port->switch2port_xfer = lwip_switch2port_xfer; lns.port->priv = &lns; /* Register a netport */ rc = vmm_netport_register(lns.port); if (rc) { goto fail1; } /* Initialize lwIP + TCP/IP APIs */ tcpip_init(NULL, NULL); /* Add netif */ IP4_ADDR(&lns.ipaddr, ip[0],ip[1],ip[2],ip[3]); IP4_ADDR(&lns.netmask, mask[0],mask[1],mask[2],mask[3]); IP4_ADDR(&lns.gw, ip[0],ip[1],ip[2],ip[3]); netif_add(&lns.nif, &lns.ipaddr, &lns.netmask, &lns.gw, &lns, lwip_netstack_netif_init, ethernet_input); /* Set default netif */ netif_set_default(&lns.nif); /* Attach netport with netswitch * Note: This will cause netport link_change() */ rc = vmm_netswitch_port_add(nsw, lns.port); if (rc) { goto fail2; } #if !defined(PING_USE_SOCKETS) /* Initalize RAW PCB for ping */ ping_raw_init(); #endif return VMM_OK; fail2: vmm_netport_unregister(lns.port); fail1: vmm_netport_free(lns.port); fail: return rc; }
/** * @brief This function notify user about link status changement. * @param netif: the network interface * @retval None */ void ethernetif_notify_conn_changed(struct netif *netif) { struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; if(netif_is_link_up(netif)) { #ifdef USE_DHCP #ifndef USE_LCD BSP_LED_Off(LED2); BSP_LED_On(LED1); #endif /* USE_LCD */ /* Update DHCP state machine */ 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); #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(8,(uint8_t *) " Static IP address:"); BSP_LCD_DisplayStringAtLine(9,(uint8_t *) iptxt); #else BSP_LED_Off(LED2); BSP_LED_On(LED1); #endif /* USE_LCD */ #endif /* USE_DHCP */ netif_set_addr(netif, &ipaddr , &netmask, &gw); /* When the netif is fully configured this function must be called.*/ netif_set_up(netif); } else { #ifdef USE_DHCP /* Update DHCP state machine */ DHCP_state = DHCP_LINK_DOWN; #endif /* USE_DHCP */ /* When the netif link is down this function must be called.*/ netif_set_down(netif); #ifdef USE_LCD BSP_LCD_ClearStringLine(7); BSP_LCD_ClearStringLine(8); BSP_LCD_ClearStringLine(9); BSP_LCD_DisplayStringAtLine(8,(uint8_t *) " The network cable"); BSP_LCD_DisplayStringAtLine(9,(uint8_t *) " is not connected"); #else BSP_LED_Off(LED1); BSP_LED_On(LED2); #endif /* USE_LCD */ } }
int netstack_send_echo(u8 *ripaddr, u16 size, u16 seqno, struct netstack_echo_reply *reply) { u64 ts; int s, i, err; char buf[64]; size_t fromlen, off, len = sizeof(struct icmp_echo_hdr) + size; ip_addr_t to_addr, from_addr; struct sockaddr_in sock; struct ip_hdr *iphdr; struct icmp_echo_hdr *iecho; LWIP_ASSERT("ping_size is too big\n", len <= 0xffff); /* Prepare target address */ IP4_ADDR(&to_addr, ripaddr[0],ripaddr[1],ripaddr[2],ripaddr[3]); /* Open RAW socket */ if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) { vmm_printf("%s: failed to open ICMP socket\n", __func__); return VMM_EFAIL; } /* Set socket option */ i = PING_RCV_TIMEO; lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &i, sizeof(i)); /* Prepare socket address */ sock.sin_len = sizeof(sock); sock.sin_family = AF_INET; inet_addr_from_ipaddr(&sock.sin_addr, &to_addr); /* Prepare ECHO request */ iecho = (struct icmp_echo_hdr *)vmm_zalloc(len); if (!iecho) { return VMM_ENOMEM; } ICMPH_TYPE_SET(iecho, ICMP_ECHO); ICMPH_CODE_SET(iecho, 0); iecho->chksum = 0; iecho->id = PING_ID; iecho->seqno = htons(seqno); for (i = 0; i < size; i++) { ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; } iecho->chksum = inet_chksum(iecho, len); /* Send ECHO request */ err = lwip_sendto(s, iecho, len, 0, (struct sockaddr*)&sock, sizeof(sock)); vmm_free(iecho); if (!err) { return VMM_EFAIL; } /* Get reference timestamp */ ts = vmm_timer_timestamp(); /* Wait for ECHO reply */ err = VMM_EFAIL; off = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&sock, (socklen_t*)&fromlen); if (off >= (sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr))) { inet_addr_to_ipaddr(&from_addr, &sock.sin_addr); iphdr = (struct ip_hdr *)buf; iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4)); if ((iecho->id == PING_ID) && (iecho->seqno == htons(seqno))) { reply->ripaddr[0] = ip4_addr1(&from_addr); reply->ripaddr[1] = ip4_addr2(&from_addr); reply->ripaddr[2] = ip4_addr3(&from_addr); reply->ripaddr[3] = ip4_addr4(&from_addr); reply->ttl = IPH_TTL(iphdr); reply->len = len; reply->seqno = seqno; reply->rtt = udiv64(vmm_timer_timestamp() - ts, 1000); err = VMM_OK; } } while (off < len) { off = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&sock, (socklen_t*)&fromlen); } /* Close RAW socket */ lwip_close(s); return err; }
/** * @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, 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 */ if (netif_is_link_up(netif)) { /* Restart the autonegotiation */ 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 = 0U; /* 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) != (uint32_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 DEBUG printf("Network cable is now connected\n\r"); #endif /* DEBUG */ #ifndef USE_DHCP /* Display static IP address */ iptab[0] = IP_ADDR3; iptab[1] = IP_ADDR2; iptab[2] = IP_ADDR1; iptab[3] = IP_ADDR0; #ifdef DEBUG printf("Static IP address: %d.%d.%d.%d\n\r", iptab[3], iptab[2], iptab[1], iptab[0]); #endif /* DEBUG */ #endif /* USE_DHCP */ } 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 DEBUG printf("Network cable is now disconnected\n\r"); #endif /* DEBUG */ } }
/** * @brief Network configuration * @param use_dhcp: indicates if DHCP client is activated or not * @retval None */ void NetworkInit(uint8_t use_dhcp) { struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; /* Create tcp_ip stack thread */ tcpip_init( NULL, NULL ); /* IP address setting */ if (use_dhcp) { ipaddr.addr = 0; netmask.addr = 0; gw.addr = 0; } else { IP4_ADDR(&ipaddr, ip_address[3], ip_address[2], ip_address[1], ip_address[0]); IP4_ADDR(&netmask, sn_mask[3], sn_mask[2] , sn_mask[1], sn_mask[0]); IP4_ADDR(&gw, gw_address[3], gw_address[2], gw_address[1], gw_address[0]); } /* Add the network interface */ netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input); /* Registers the default network interface. */ netif_set_default(&gnetif); if (netif_is_link_up(&gnetif)) { /* When the netif is fully configured this function must be called.*/ netif_set_up(&gnetif); _VNCServer_Notify(NOTIFY_SERVER_NETIF_UP); if (use_dhcp) { DHCP_state = DHCP_START; } } else { /* When the netif link is down this function must be called */ netif_set_down(&gnetif); _VNCServer_Notify(NOTIFY_SERVER_NETIF_DOWN); } if (use_dhcp) { /* Start DHCPClient */ osThreadDef(DHCP, DHCP_thread, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE); _DHCPClinet_TCB = osThreadCreate (osThread(DHCP), &gnetif); } /* Cable management BSP Configuration ***************************************/ /* Init IO Expander */ BSP_IO_Init(); /* Enable IO Expander interrupt for ETH MII pin */ BSP_IO_ConfigPin(MII_INT_PIN, IO_MODE_IT_FALLING_EDGE); }
/** * Conversion from oid to lwIP ip_addr * @param ident points to s32_t ident[4] input * @param ip points to output struct */ void snmp_oidtoip(s32_t *ident, ip_addr_t *ip) { IP4_ADDR(ip, ident[0], ident[1], ident[2], ident[3]); }
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); }
void user_init(void) { uart_set_baud(0, 921600); printf("\n\nESP8266 UMAC\n\n"); bridge_init(); umac_cfg um_cfg = { .timer_fn = umac_impl_request_future_tick, .cancel_timer_fn = umac_impl_cancel_future_tick, .now_fn = umac_impl_now_tick, .tx_byte_fn = umac_impl_tx_byte, .tx_buf_fn = umac_impl_tx_buf, .rx_pkt_fn = umac_impl_rx_pkt, .rx_pkt_ack_fn = bridge_pkt_acked, .timeout_fn = bridge_timeout, .nonprotocol_data_fn = NULL }; umac_init(&um, &um_cfg, rx_buf); um_tim_hdl = xTimerCreate( (signed char *)"umac_tim", 10, false, (void *)um_tim_id, um_tim_cb); char ap_cred[128]; struct sdk_station_config config; bool setup_ap = true; systask_init(); device_id = 0; fs_init(); if (fs_mount() >= 0) { #if 0 spiffs_DIR d; struct spiffs_dirent e; struct spiffs_dirent *pe = &e; fs_opendir("/", &d); while ((pe = fs_readdir(&d, pe))) { printf("%s [%04x] size:%i\n", pe->name, pe->obj_id, pe->size); } fs_closedir(&d); #endif // read preferred ssid spiffs_file fd_ssid = fs_open(".ssid", SPIFFS_RDONLY, 0); if (fd_ssid > 0) { if (fs_read(fd_ssid, (uint8_t *)ap_cred, sizeof(ap_cred)) > 0) { fs_close(fd_ssid); char *nl_ix = strchr(ap_cred, '\n'); if (nl_ix > 0) { memset(&config, 0, sizeof(struct sdk_station_config)); strncpy((char *)&config.ssid, ap_cred, nl_ix - ap_cred); char *nl_ix2 = strchr(nl_ix + 1, '\n'); if (nl_ix2 > 0) { strncpy((char *)&config.password, nl_ix + 1, nl_ix2 - (nl_ix + 1)); setup_ap = false; } } printf("ssid:%s\n", config.ssid); } // if read else { printf("could not read .ssid\n"); } } // if fs_ssid else { printf("no .ssid found, running softAP\n"); } // find device id or create one spiffs_file fd_devid = fs_open(".devid", SPIFFS_RDONLY, 0); if (fd_devid < 0) { device_id = hwrand(); fd_devid = fs_open(".devid", SPIFFS_O_CREAT | SPIFFS_O_TRUNC | SPIFFS_O_WRONLY | SPIFFS_O_APPEND, 0); fs_write(fd_devid, &device_id, 4); printf("create devid\n"); } else { fs_read(fd_devid, &device_id, 4); } fs_close(fd_devid); printf("devid %08x\n", device_id); // remove previous scan results fs_remove(SYSTASK_AP_SCAN_FILENAME); } // if mount if (setup_ap) { sdk_wifi_set_opmode(SOFTAP_MODE); struct ip_info ap_ip; IP4_ADDR(&ap_ip.ip, 192, 169, 1, 1); IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0); IP4_ADDR(&ap_ip.netmask, 255, 255, 255, 0); sdk_wifi_set_ip_info(1, &ap_ip); struct sdk_softap_config ap_cfg = { .ssid = "WISLEEP", .password = "", .ssid_len = 7, .channel = 1, .authmode = AUTH_OPEN, .ssid_hidden = false, .max_connection = 255, .beacon_interval = 100 }; sdk_wifi_softap_set_config(&ap_cfg); ip_addr_t first_client_ip; IP4_ADDR(&first_client_ip, 192, 169, 1, 100); dhcpserver_start(&first_client_ip, 4); } else { // required to call wifi_set_opmode before station_set_config sdk_wifi_set_opmode(STATION_MODE); sdk_wifi_station_set_config(&config); } server_init(server_actions); um_mutex = xSemaphoreCreateMutex(); xTaskCreate(uart_task, (signed char * )"uart_task", 512, NULL, 2, NULL); xTaskCreate(server_task, (signed char *)"server_task", 1024, NULL, 2, NULL); }
/*-----------------------------------------------------------------------------------*/ 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; }
/** * @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 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); #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); } } }
void ICACHE_FLASH_ATTR settings_apply( userSettings_t *settings ) { //Description if (!((settings->magic==0x42)&&(settings->version==SETTINGS_VERSION))) { os_printf("<ERROR: CAN NOT APPLY CORRUPT SETTINGS!>\n\r"); return; } if (settings->connection_successful) { ap_stop(); } else { ap_start(); } //Device information wifi_station_set_hostname(settings->name); //Network struct station_config stationConf; wifi_station_get_config( &stationConf ); stationConf.bssid_set = 0; os_memcpy( &stationConf.ssid, settings->ssid, 32 ); os_memcpy( &stationConf.password, settings->password, 64 ); wifi_station_set_config_current( &stationConf ); os_printf("########################## Wifi config set to SSID '%s' and PASSWORD '%s'.\n\r", settings->ssid, settings->password); if (settings->enable_dhcp) { wifi_station_dhcpc_start(); } else { wifi_station_dhcpc_stop(); struct ip_info dhcpc_ip_info; IP4_ADDR(&dhcpc_ip_info.ip, settings->static_ip_1, settings->static_ip_2, settings->static_ip_3, settings->static_ip_4); IP4_ADDR(&dhcpc_ip_info.gw, settings->static_gateway_1, settings->static_gateway_2, settings->static_gateway_3, settings->static_gateway_4); IP4_ADDR(&dhcpc_ip_info.netmask, settings->static_netmask_1, settings->static_netmask_2, settings->static_netmask_3, settings->static_netmask_4); wifi_set_ip_info(STATION_IF, &dhcpc_ip_info); } wifi_station_disconnect(); //MDNS if (settings->enable_mdns) { /*wifi_set_broadcast_if(STATIONAP_MODE); struct mdns_info *info = (struct mdns_info *) os_zalloc(sizeof(struct mdns_info)); info->host_name = settings->mdns_hostname; info->ipAddr = station_ipconfig.ip.addr; //ESP8266 station IP info->server_name = "FirmwaRe MDNS"; info->server_port = 80; info->txt_data[0] = “version = now”; info->txt_data[1] = “user1 = data1”; info->txt_data[2] = “user2 = data2”; espconn_mdns_init(info);*/ } //NTP if (settings->enable_ntp) { //settings->ntpserver //settings->timezone //settings->enable_summertime } //Security /* settings->password; */ //Digital outputs #if OUTPUT1>-1 if (settings->bootstate&1){ board_setOutput(OUTPUT1, true); } else { board_setOutput(OUTPUT1, false); } #endif #if OUTPUT2>-1 if (settings->bootstate&2) { board_setOutput(OUTPUT2, true); } else { board_setOutput(OUTPUT2, false); } #endif #if OUTPUT3>-1 if (settings->bootstate&4) { board_setOutput(OUTPUT3, true); } else { board_setOutput(OUTPUT3, false); } #endif #if OUTPUT4>-1 if (settings->bootstate&8) { board_setOutput(OUTPUT4, true); } else { board_setOutput(OUTPUT4, false); } #endif #if OUTPUT5>-1 if (settings->bootstate&16) { board_setOutput(OUTPUT5, true); } else { board_setOutput(OUTPUT5, false); } #endif #if OUTPUT6>-1 if (settings->bootstate&32) { board_setOutput(OUTPUT6, true); } else { board_setOutput(OUTPUT6, false); } #endif #if OUTPUT7>-1 if (settings->bootstate&64) { board_setOutput(OUTPUT7, true); } else { board_setOutput(OUTPUT7, false); } #endif #if OUTPUT8>-1 if (settings->bootstate&128) { board_setOutput(OUTPUT8, true); } else { board_setOutput(OUTPUT8, false); } #endif //ESPLight setPWM(settings->bootstate_R, settings->bootstate_G, settings->bootstate_B); //settings->ledstrip_type; //settings->ledstrip_length; //PKA /* settings->pka_wb settings->pka_wb_time */ }
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_LWIP_Init(); MX_USART1_UART_Init(); MX_USB_DEVICE_Init(); /* USER CODE BEGIN 2 */ printf("Application startup successful....\n"); httpd_init(); ip4_addr_t ip_addr; IP4_ADDR(&ip_addr, 192, 168, 1, 1); err_t err; // Setup an empty client info structure struct mqtt_connect_client_info_t ci; memset(&ci, 0, sizeof(ci)); ci.client_id = "lwip_test"; err = mqtt_client_connect(&client, &ip_addr, MQTT_PORT, mqtt_connection_cb, 0, &ci); // For now just print the result code if something goes wrong if(err != ERR_OK) { printf("mqtt_connect return %d\n", err); } uint8_t published=0; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { MX_LWIP_Process(); if(mqtt_client_is_connected(&client) && !published) { example_publish(&client, "Hallo Welt!"); published=1; } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }
/** * Ethernet to PERF task * * @param none * @return none */ void BRIDGE_PERF_Task( void *pvParameters ) { struct ip_addr xIpAddr, xNetMast, xGateway; extern err_t ethernetif_init( struct netif *netif ); SIM_SCGC6 |= SIM_SCGC6_PIT_MASK;//enable PIT /* Parameters are not used - suppress compiler error */ ( void )pvParameters; tcpip_init( NULL, NULL ); /* Create and configure the FEC interface. */ IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 ); IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 ); IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 ); netif_add( &ENET_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input ); /* make it the default interface */ netif_set_default( &ENET_if ); /* bring it up */ netif_set_up( &ENET_if ); #if BENCHMARK_PROTOCOL //**********************************UDP**********************************// { struct udp_pcb *remote_pcb; remote_pcb = udp_new(); if(!remote_pcb) vTaskDelete(NULL); /*FSL: error during buffer creation*/ #if BENCHMARK_SEND_TO_PC //**********************************TX***********************************// { uint16 i; /*client mode: connect to this server address*/ struct ip_addr ServerIPAddr; struct pbuf *p; /*Fill the array*/ for(i=0;i<sizeof(array);i++) array[i] = i%256;//dummy sequence /*Server IP address to connect*/ IP4_ADDR(&ServerIPAddr,192,168,0,1); printf("Starting UDP TX Client\n"); if( udp_connect(remote_pcb, &ServerIPAddr, (u16_t)BENCHMARK_PORT) != ERR_OK ) vTaskDelete(NULL); /*FSL: error during socket creation*/ if( (p = pbuf_alloc(PBUF_TRANSPORT,0,PBUF_REF)) == NULL ) vTaskDelete(NULL); /*FSL: error during buffer creation*/ p->payload = &array[0]; p->len = p->tot_len = BENCHMARK_PACKET_LENGTH; /*FSL: send selected number of packets*/ for(pkt_counter=0;pkt_counter<BENCHMARK_NUMBER_OF_PACKETS;pkt_counter++) { if( udp_send(remote_pcb, p) != ERR_OK ) break;//error: abort } /*FSL: send ending frames*/ p->len = p->tot_len = 1; /*FSL: send 10 times*/ udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); //FSL: remove connection pbuf_free(p); udp_remove(remote_pcb); printf("Leaving UDP Tx Benchmark Test\n"); vTaskDelete(NULL); /*FSL: task no longer needed*/ } #else //**********************************RX***********************************// { printf("Starting UDP RX Server\n"); PIT_TCTRL0 = PIT_TCTRL_TEN_MASK;//enable timer PIT_MCR &= ~PIT_MCR_MDIS_MASK; PIT_LDVAL0 = 0xFFFFFFFF;//max value load_value = PIT_LDVAL0; PIT_TCTRL0 = 0; if( udp_bind(remote_pcb, IP_ADDR_ANY, BENCHMARK_PORT) != ERR_OK ) printf("Wrong bind to UDP port\n"); udp_recv(remote_pcb, udp_receive_fnc, NULL); /*Task no longer needed*/ vTaskDelete(NULL); } #endif } #else //**********************************TCP**********************************// #if BENCHMARK_SEND_TO_PC //**********************************TX***********************************// { struct netconn *conn; uint16 i; /*client mode: connect to this server address*/ struct ip_addr ServerIPAddr; /*Fill the array*/ for(i=0;i<sizeof(array);i++) array[i] = i%256;//dummy sequence /*Server IP address to connect*/ IP4_ADDR(&ServerIPAddr,192,168,0,1); printf("Starting TCP Tx Benchmark Test\n"); /*FSL: wait forever until getting a connection to a valid server*/ do { printf("Trying to connect to server...\n"); /* Create a new TCP PCB. */ conn = netconn_new(NETCONN_TCP); /*client connection*/ if( netconn_connect(conn, &ServerIPAddr, (uint16)BENCHMARK_PORT) != ERR_OK ) { /*close connection for a new SYN process*/ netconn_close(conn); netconn_delete(conn); vTaskDelay(2000); } else break; } while(1);/*infinite loop until getting a valid SYN/SYN-ACK/ACK*/ printf("Connected to server\n"); /*FSL: send selected number of packets*/ pkt_counter = BENCHMARK_NUMBER_OF_PACKETS;//will send in a moment /*FSL: only send when connection is established, otherwise close*/ while( pkt_counter-- ) { netconn_write(conn, &array[0], sizeof(array), NETCONN_NOCOPY); } /*FSL: no more packets*/ netconn_close(conn); netconn_delete(conn); printf("Leaving TCP Tx Benchmark Test\n"); /*FSL: good bye*/ vTaskDelete(NULL); } #else //**********************************RX***********************************// for(;;) { struct netconn *conn, *newconn; struct netbuf *inbuf; uint32 total_length_received = 0; double final_result; /* Create a new TCP PCB. */ conn = netconn_new(NETCONN_TCP); if( netconn_bind(conn,IP_ADDR_ANY,(uint16)BENCHMARK_PORT) != ERR_OK ) { /*close connection for a new SYN process*/ netconn_close(conn); netconn_delete(conn); /*FSL: good bye*/ vTaskDelete(NULL); } printf("Starting TCP Rx Benchmark Test\n"); /* Put the connection into LISTEN state. */ netconn_listen(conn); PIT_TCTRL0 = PIT_TCTRL_TEN_MASK;//enable timer PIT_MCR &= ~PIT_MCR_MDIS_MASK; PIT_LDVAL0 = 0xFFFFFFFF;//max value load_value = PIT_LDVAL0; PIT_TCTRL0 = 0; /* waiting for connection from client */ newconn = netconn_accept(conn); PIT_TCTRL0 = PIT_TCTRL_TEN_MASK;//enable timer for(;;) { if( (inbuf = netconn_recv(newconn)) != NULL ) { /*data is not touch: inbuf->ptr and inbuf->ptr->tot_len*/ total_length_received += inbuf->ptr->tot_len; /*free pbuf memory*/ netbuf_delete(inbuf); } else { current_timer = PIT_CVAL0;//get last time break;/*connection closed*/ } } /*FSL: no more packets*/ netconn_close(newconn); netconn_delete(newconn); netconn_close(conn); netconn_delete(conn); /*calculate*/ time_sfw = (load_value + 1 - current_timer); PIT_TCTRL0 = 0;//stop the timer printf("Benchmark results for TCP Rx:\n\n"); printf("Number of bytes received = %d\n",total_length_received); printf("Number of ticks = %d\n",time_sfw); printf("Frequency of ticks = %d\n",(configCPU_CLOCK_HZ/2)); printf("Calculate benchmark: Bytes/sec\n\n"); final_result = (double)total_length_received * (configCPU_CLOCK_HZ/2) /(double)time_sfw; printf("Measured throughput is %9.2f Bytes/sec\n",final_result); } #endif #endif }
void fATWA(void *arg){ #if CONFIG_LWIP_LAYER struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; struct netif * pnetif = &xnetif[0]; #endif int timeout = 20; printf("[ATWA]: _AT_WLAN_AP_ACTIVATE_\n\r"); if(ap.ssid.val[0] == 0){ printf("[ATWA]Error: SSID can't be empty\n\r"); return; } if(ap.password == NULL){ ap.security_type = RTW_SECURITY_OPEN; } else{ ap.security_type = RTW_SECURITY_WPA2_AES_PSK; } #if CONFIG_WEBSERVER //store into flash memset(wifi_setting.ssid, 0, sizeof(wifi_setting.ssid));; memcpy(wifi_setting.ssid, ap.ssid.val, strlen((char*)ap.ssid.val)); wifi_setting.ssid[ap.ssid.len] = '\0'; wifi_setting.security_type = ap.security_type; if(ap.security_type !=0) wifi_setting.security_type = 1; else wifi_setting.security_type = 0; if (ap.password) memcpy(wifi_setting.password, ap.password, strlen((char*)ap.password)); else memset(wifi_setting.password, 0, sizeof(wifi_setting.password));; wifi_setting.channel = ap.channel; #if CONFIG_READ_FLASH StoreApInfo(); #endif #endif #if CONFIG_LWIP_LAYER dhcps_deinit(); IP4_ADDR(&ipaddr, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_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); #ifdef CONFIG_DONT_CARE_TP pnetif->flags |= NETIF_FLAG_IPSWITCH; #endif #endif wifi_off(); vTaskDelay(20); if (wifi_on(RTW_MODE_AP) < 0){ printf("\n\rERROR: Wifi on failed!"); return; } printf("\n\rStarting AP ..."); #ifdef CONFIG_WPS_AP wpas_wps_dev_config(pnetif->hwaddr, 1); #endif if(wifi_start_ap((char*)ap.ssid.val, ap.security_type, (char*)ap.password, ap.ssid.len, ap.password_len, ap.channel) < 0) { printf("\n\rERROR: Operation failed!"); return; } while(1) { char essid[33]; if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) > 0) { if(strcmp((const char *) essid, (const char *)ap.ssid.val) == 0) { printf("\n\r%s started\n", ap.ssid.val); break; } } if(timeout == 0) { printf("\n\rERROR: Start AP timeout!"); break; } vTaskDelay(1 * configTICK_RATE_HZ); timeout --; } #if CONFIG_LWIP_LAYER //LwIP_UseStaticIP(pnetif); dhcps_init(pnetif); #endif init_wifi_struct( ); }
int netstack_set_ipmask(u8 *addr) { IP4_ADDR(&lns.netmask, addr[0],addr[1],addr[2],addr[3]); netif_set_netmask(&lns.nif, &lns.netmask); return VMM_OK; }
/** * @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 } } }
int netstack_set_gatewayip(u8 *addr) { IP4_ADDR(&lns.gw, addr[0],addr[1],addr[2],addr[3]); netif_set_gw(&lns.nif, &lns.gw); return VMM_OK; }
/** * @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; }
/** * @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; }