static int app_dhcp_main(void) { uint32_t timeleft = 15000; struct dhcpc_state state; void *dhcp_handle; int ret; dhcp_handle = dhcpc_open(NET_DEVNAME); ret = dhcpc_request(dhcp_handle, &state); while (ret != OK) { usleep(10); //sys_msleep(10); timeleft -= 10; if (timeleft <= 0) break; } if (timeleft <= 0) { dhcpc_close(dhcp_handle); return -1; } netlib_set_ipv4addr(NET_DEVNAME, &state.ipaddr); netlib_set_ipv4netmask(NET_DEVNAME, &state.netmask); netlib_set_dripv4addr(NET_DEVNAME, &state.default_router); printf("IP address : %s ----\n", inet_ntoa(state.ipaddr)); return 1; }
void Network::init(void) { // two timers for tcp/ip timer_set(&periodic_timer, CLOCK_SECOND / 2); /* 0.5s */ timer_set(&arp_timer, CLOCK_SECOND * 10); /* 10s */ // Initialize the uIP TCP/IP stack. uip_init(); uip_setethaddr(mac_address); if (!use_dhcp) { // manual setup of ip uip_ipaddr_t tip; /* local IP address */ uip_ipaddr(tip, ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); uip_sethostaddr(tip); /* host IP address */ printf("IP Addr: %d.%d.%d.%d\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); uip_ipaddr(tip, ipgw[0], ipgw[1], ipgw[2], ipgw[3]); uip_setdraddr(tip); /* router IP address */ printf("IP GW: %d.%d.%d.%d\n", ipgw[0], ipgw[1], ipgw[2], ipgw[3]); uip_ipaddr(tip, ipmask[0], ipmask[1], ipmask[2], ipmask[3]); uip_setnetmask(tip); /* mask */ printf("IP mask: %d.%d.%d.%d\n", ipmask[0], ipmask[1], ipmask[2], ipmask[3]); setup_servers(); }else{ #if UIP_CONF_UDP dhcpc_init(mac_address, sizeof(mac_address)); dhcpc_request(); printf("Getting IP address....\n"); #endif } }
//配置网卡硬件,并设置MAC地址 //返回值:0,正常;1,失败; u8 tapdev_init(void) { u32 wait_count; u8 i, res = 0; #ifndef DHCP_ENABLE uip_ipaddr_t ipaddr; #endif res = ENC28J60_Init((u8*)Modbus.mac_addr); //初始化ENC28J60 //把IP地址和MAC地址写入缓存区 for (i = 0; i < 6; i++) { uip_ethaddr.addr[i] = Modbus.mac_addr[i]; } //指示灯状态:0x476 is PHLCON LEDA(绿)=links status, LEDB(红)=receive/transmit //PHLCON:PHY 模块LED 控制寄存器 ENC28J60_PHY_Write(PHLCON, 0x0476); uip_init(); //uIP初始化 if(Modbus.tcp_type == 0) { U8_T temp[4]; uip_ipaddr(ipaddr, Modbus.ip_addr[0], Modbus.ip_addr[1], Modbus.ip_addr[2], Modbus.ip_addr[3]); //设置本地设置IP地址 uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, Modbus.getway[0], Modbus.getway[1], Modbus.getway[2], Modbus.getway[3]); //设置网关IP地址(其实就是你路由器的IP地址) uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, Modbus.subnet[0], Modbus.subnet[1], Modbus.subnet[2], Modbus.subnet[3]); //设置网络掩码 uip_setnetmask(ipaddr); // flag_dhcp_configured = 2; temp[0] = Modbus.ip_addr[0]; temp[1] = Modbus.ip_addr[1]; temp[2] = Modbus.ip_addr[2]; temp[3] = Modbus.ip_addr[3]; temp[0] |= (255 - Modbus.subnet[0]); temp[1] |= (255 - Modbus.subnet[1]); temp[2] |= (255 - Modbus.subnet[2]); temp[3] |= (255 - Modbus.subnet[3]); uip_ipaddr(uip_hostaddr_submask,temp[0], temp[1],temp[2] ,temp[3]); delay_ms(1); } else // DHCP { // flag_dhcp_configured = 0; dhcpc_init(Modbus.mac_addr, 6); dhcpc_request(); } // printf("res=%u\n\r",res); return res; }
void protocol_switcher_app_init(void) { telnet_client_init(); siemensTCP_app_init(); specserver_app_init(); webclient_init(); httpd_init(); resolv_init(); #ifndef USE_STATIC_IP // Initialize the DHCP Client Application. static struct uip_eth_addr sTempAddr; uip_getethaddr(sTempAddr); dhcpc_init(&sTempAddr.addr[0], 6); dhcpc_request(); #else uip_ip4addr_t uip_dnsaddr; uip_ipaddr(uip_dnsaddr, DNS_IP1, DNS_IP2, DNS_IP3, DNS_IP4 ); resolv_conf(uip_dnsaddr); #endif }
// Init application void elua_uip_init( const struct uip_eth_addr *paddr ) { // Set hardware address uip_setethaddr( (*paddr) ); // Initialize the uIP TCP/IP stack. uip_init(); uip_arp_init(); #ifdef BUILD_DHCPC dhcpc_init( paddr->addr, sizeof( *paddr ) ); dhcpc_request(); #else elua_uip_conf_static(); #endif resolv_init(); #ifdef BUILD_CON_TCP uip_listen( HTONS( ELUA_NET_TELNET_PORT ) ); #endif }
/**************************************************************************** * Name: dhcp_client_start ****************************************************************************/ int dhcp_client_start(const char *intf) { struct dhcpc_state state; int ret; void *dhcp_hnd = NULL; ndbg("[DHCPC] External DHCPC application started\n"); dhcp_hnd = dhcpc_open(intf); if (dhcp_hnd) { ret = dhcpc_request(dhcp_hnd, &state); if (ret != OK) { ndbg("[DHCPC] get IP address fail\n"); dhcpc_close(dhcp_hnd); return -1; } DHCPC_SET_IP4ADDR(intf, state.ipaddr, state.netmask, state.default_router); ndbg("[DHCPC] IP address : %s ----\n", inet_ntoa(state.ipaddr)); dhcpc_close(dhcp_hnd); } else { ndbg("[DHCPC] Invalid dhcp handle\n"); return -1; } return OK; }
static int tcpecho_netsetup() { /* If this task is excecutated as an NSH built-in function, then the * network has already been configured by NSH's start-up logic. */ #ifndef CONFIG_NSH_BUILTIN_APPS struct in_addr addr; #if defined(CONFIG_EXAMPLES_TCPECHO_DHCPC) || defined(CONFIG_EXAMPLES_TCPECHO_NOMAC) uint8_t mac[IFHWADDRLEN]; #endif #ifdef CONFIG_EXAMPLES_TCPECHO_DHCPC struct dhcpc_state ds; void *handle; #endif /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLES_TCPECHO_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; netlib_setmacaddr("eth0", mac); #endif /* Set up our host address */ #ifdef CONFIG_EXAMPLES_TCPECHO_DHCPC addr.s_addr = 0; #else addr.s_addr = HTONL(CONFIG_EXAMPLES_TCPECHO_IPADDR); #endif netlib_set_ipv4addr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_TCPECHO_DRIPADDR); netlib_set_dripv4addr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_TCPECHO_NETMASK); netlib_set_ipv4netmask("eth0", &addr); #ifdef CONFIG_EXAMPLES_TCPECHO_DHCPC /* Get the MAC address of the NIC */ netlib_getmacaddr("eth0", mac); /* Set up the DHCPC modules */ handle = dhcpc_open(&mac, IFHWADDRLEN); /* Get an IP address. Note: there is no logic here for renewing the address in this * example. The address should be renewed in ds.lease_time/2 seconds. */ if (!handle) { return ERROR; } if (dhcpc_request(handle, &ds) != OK) { return ERROR; } netlib_set_ipv4addr("eth0", &ds.ipaddr); if (ds.netmask.s_addr != 0) { netlib_set_ipv4netmask("eth0", &ds.netmask); } if (ds.default_router.s_addr != 0) { netlib_set_dripv4addr("eth0", &ds.default_router); } if (ds.dnsaddr.s_addr != 0) { netlib_set_ipv4dnsaddr(&ds.dnsaddr); } dhcpc_close(handle); printf("IP: %s\n", inet_ntoa(ds.ipaddr)); #endif /* CONFIG_EXAMPLES_TCPECHO_DHCPC */ #endif /* CONFIG_NSH_BUILTIN_APPS */ return OK; }
/*-----------------------------------------------------------------------------------*/ PROCESS_THREAD(dhcp_process, ev, data) { PROCESS_BEGIN(); ctk_window_new(&window, 29, 14, "DHCP client"); CTK_WIDGET_ADD(&window, &requestbutton); CTK_WIDGET_ADD(&window, &statuslabel); CTK_WIDGET_ADD(&window, &ipaddrlabel); CTK_WIDGET_ADD(&window, &ipaddrtextentry); CTK_WIDGET_ADD(&window, &netmasklabel); CTK_WIDGET_ADD(&window, &netmasktextentry); CTK_WIDGET_ADD(&window, &gatewaylabel); CTK_WIDGET_ADD(&window, &gatewaytextentry); #if WITH_DNS CTK_WIDGET_ADD(&window, &dnsserverlabel); CTK_WIDGET_ADD(&window, &dnsservertextentry); #endif /* WITH_DNS */ CTK_WIDGET_ADD(&window, &savebutton); CTK_WIDGET_ADD(&window, &cancelbutton); CTK_WIDGET_FOCUS(&window, &requestbutton); ctk_window_open(&window); /* Allow resolver to set DNS server address. */ process_post(PROCESS_CURRENT(), PROCESS_EVENT_MSG, NULL); dhcpc_init(uip_ethaddr.addr, sizeof(uip_ethaddr.addr)); while(1) { PROCESS_WAIT_EVENT(); if(ev == PROCESS_EVENT_MSG) { makestrings(); ctk_window_redraw(&window); } else if(ev == tcpip_event) { dhcpc_appcall(ev, data); } else if(ev == ctk_signal_button_activate) { if(data == (process_data_t)&requestbutton) { dhcpc_request(); set_statustext("Requesting..."); } if(data == (process_data_t)&savebutton) { apply_tcpipconfig(); app_quit(); } if(data == (process_data_t)&cancelbutton) { app_quit(); } } else if( #if CTK_CONF_WINDOWCLOSE ev == ctk_signal_window_close || #endif ev == PROCESS_EVENT_EXIT) { app_quit(); } } PROCESS_END(); }
void main(void) { static struct uip_eth_addr eth_addr; uip_ipaddr_t ipaddr; cpu_init(); uart_init(); printf("Welcome\n"); spi_init(); enc28j60_comm_init(); printf("Welcome\n"); enc_init(mac_addr); // // Configure SysTick for a periodic interrupt. // MAP_SysTickPeriodSet(MAP_SysCtlClockGet() / SYSTICKHZ); MAP_SysTickEnable(); MAP_SysTickIntEnable(); //MAP_IntEnable(INT_GPIOA); MAP_IntEnable(INT_GPIOE); MAP_IntMasterEnable(); MAP_SysCtlPeripheralClockGating(false); printf("int enabled\n"); MAP_GPIOIntTypeSet(GPIO_PORTE_BASE, ENC_INT, GPIO_FALLING_EDGE); MAP_GPIOPinIntClear(GPIO_PORTE_BASE, ENC_INT); MAP_GPIOPinIntEnable(GPIO_PORTE_BASE, ENC_INT); uip_init(); eth_addr.addr[0] = mac_addr[0]; eth_addr.addr[1] = mac_addr[1]; eth_addr.addr[2] = mac_addr[2]; eth_addr.addr[3] = mac_addr[3]; eth_addr.addr[4] = mac_addr[4]; eth_addr.addr[5] = mac_addr[5]; uip_setethaddr(eth_addr); #define DEFAULT_IPADDR0 10 #define DEFAULT_IPADDR1 0 #define DEFAULT_IPADDR2 0 #define DEFAULT_IPADDR3 201 #define DEFAULT_NETMASK0 255 #define DEFAULT_NETMASK1 255 #define DEFAULT_NETMASK2 255 #define DEFAULT_NETMASK3 0 #undef STATIC_IP #ifdef STATIC_IP uip_ipaddr(ipaddr, DEFAULT_IPADDR0, DEFAULT_IPADDR1, DEFAULT_IPADDR2, DEFAULT_IPADDR3); uip_sethostaddr(ipaddr); printf("IP: %d.%d.%d.%d\n", DEFAULT_IPADDR0, DEFAULT_IPADDR1, DEFAULT_IPADDR2, DEFAULT_IPADDR3); uip_ipaddr(ipaddr, DEFAULT_NETMASK0, DEFAULT_NETMASK1, DEFAULT_NETMASK2, DEFAULT_NETMASK3); uip_setnetmask(ipaddr); #else uip_ipaddr(ipaddr, 0, 0, 0, 0); uip_sethostaddr(ipaddr); printf("Waiting for IP address...\n"); uip_ipaddr(ipaddr, 0, 0, 0, 0); uip_setnetmask(ipaddr); #endif httpd_init(); #ifndef STATIC_IP dhcpc_init(mac_addr, 6); dhcpc_request(); #endif long lPeriodicTimer, lARPTimer; lPeriodicTimer = lARPTimer = 0; int i; // = MAP_GPIOPinRead(GPIO_PORTA_BASE, ENC_INT) & ENC_INT; while(true) { //MAP_IntDisable(INT_UART0); MAP_SysCtlSleep(); //MAP_IntEnable(INT_UART0); //i = MAP_GPIOPinRead(GPIO_PORTA_BASE, ENC_INT) & ENC_INT; /*while(i != 0 && g_ulFlags == 0) { i = MAP_GPIOPinRead(GPIO_PORTA_BASE, ENC_INT) & ENC_INT; }*/ if( HWREGBITW(&g_ulFlags, FLAG_ENC_INT) == 1 ) { HWREGBITW(&g_ulFlags, FLAG_ENC_INT) = 0; enc_action(); } if(HWREGBITW(&g_ulFlags, FLAG_SYSTICK) == 1) { HWREGBITW(&g_ulFlags, FLAG_SYSTICK) = 0; lPeriodicTimer += SYSTICKMS; lARPTimer += SYSTICKMS; //printf("%d %d\n", lPeriodicTimer, lARPTimer); } if( lPeriodicTimer > UIP_PERIODIC_TIMER_MS ) { lPeriodicTimer = 0; int l; for(l = 0; l < UIP_CONNS; l++) { uip_periodic(l); // // If the above function invocation resulted in data that // should be sent out on the network, the global variable // uip_len is set to a value > 0. // if(uip_len > 0) { uip_arp_out(); enc_send_packet(uip_buf, uip_len); uip_len = 0; } } for(l = 0; l < UIP_UDP_CONNS; l++) { uip_udp_periodic(l); if( uip_len > 0) { uip_arp_out(); enc_send_packet(uip_buf, uip_len); uip_len = 0; } } } if( lARPTimer > UIP_ARP_TIMER_MS) { lARPTimer = 0; uip_arp_timer(); } } }
//***************************************************************************** // // This example demonstrates the use of the Ethernet Controller with the uIP // TCP/IP stack. // //***************************************************************************** int main(void) { uip_ipaddr_t ipaddr; static struct uip_eth_addr sTempAddr; long lPeriodicTimer, lARPTimer, lPacketLength; unsigned long ulUser0, ulUser1; unsigned long ulTemp; // // Set the clocking to run directly from the crystal. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // // Initialize the OLED display. // RIT128x96x4Init(1000000); RIT128x96x4StringDraw("Ethernet with uIP", 12, 0, 15); // // Enable and Reset the Ethernet Controller. // SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH); SysCtlPeripheralReset(SYSCTL_PERIPH_ETH); // // Enable Port F for Ethernet LEDs. // LED0 Bit 3 Output // LED1 Bit 2 Output // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3); // // Configure SysTick for a periodic interrupt. // SysTickPeriodSet(SysCtlClockGet() / SYSTICKHZ); SysTickEnable(); SysTickIntEnable(); // // Intialize the Ethernet Controller and disable all Ethernet Controller // interrupt sources. // EthernetIntDisable(ETH_BASE, (ETH_INT_PHY | ETH_INT_MDIO | ETH_INT_RXER | ETH_INT_RXOF | ETH_INT_TX | ETH_INT_TXER | ETH_INT_RX)); ulTemp = EthernetIntStatus(ETH_BASE, false); EthernetIntClear(ETH_BASE, ulTemp); // // Initialize the Ethernet Controller for operation. // EthernetInitExpClk(ETH_BASE, SysCtlClockGet()); // // Configure the Ethernet Controller for normal operation. // - Full Duplex // - TX CRC Auto Generation // - TX Padding Enabled // EthernetConfigSet(ETH_BASE, (ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN)); // // Wait for the link to become active. // RIT128x96x4StringDraw("Waiting for Link", 12, 8, 15); ulTemp = EthernetPHYRead(ETH_BASE, PHY_MR1); while((ulTemp & 0x0004) == 0) { ulTemp = EthernetPHYRead(ETH_BASE, PHY_MR1); } RIT128x96x4StringDraw("Link Established", 12, 16, 15); // // Enable the Ethernet Controller. // EthernetEnable(ETH_BASE); // // Enable the Ethernet interrupt. // IntEnable(INT_ETH); // // Enable the Ethernet RX Packet interrupt source. // EthernetIntEnable(ETH_BASE, ETH_INT_RX); // // Enable all processor interrupts. // IntMasterEnable(); // // Initialize the uIP TCP/IP stack. // uip_init(); #ifdef USE_STATIC_IP uip_ipaddr(ipaddr, DEFAULT_IPADDR0, DEFAULT_IPADDR1, DEFAULT_IPADDR2, DEFAULT_IPADDR3); uip_sethostaddr(ipaddr); DisplayIPAddress(ipaddr, 18, 24); uip_ipaddr(ipaddr, DEFAULT_NETMASK0, DEFAULT_NETMASK1, DEFAULT_NETMASK2, DEFAULT_NETMASK3); uip_setnetmask(ipaddr); #else uip_ipaddr(ipaddr, 0, 0, 0, 0); uip_sethostaddr(ipaddr); DisplayIPAddress(ipaddr, 18, 24); uip_ipaddr(ipaddr, 0, 0, 0, 0); uip_setnetmask(ipaddr); #endif // // Configure the hardware MAC address for Ethernet Controller filtering of // incoming packets. // // For the Ethernet Eval Kits, the MAC address will be stored in the // non-volatile USER0 and USER1 registers. These registers can be read // using the FlashUserGet function, as illustrated below. // FlashUserGet(&ulUser0, &ulUser1); if((ulUser0 == 0xffffffff) || (ulUser1 == 0xffffffff)) { // // We should never get here. This is an error if the MAC address has // not been programmed into the device. Exit the program. // RIT128x96x4StringDraw("MAC Address", 0, 16, 15); RIT128x96x4StringDraw("Not Programmed!", 0, 24, 15); while(1) { } } // // Convert the 24/24 split MAC address from NV ram into a 32/16 split MAC // address needed to program the hardware registers, then program the MAC // address into the Ethernet Controller registers. // sTempAddr.addr[0] = ((ulUser0 >> 0) & 0xff); sTempAddr.addr[1] = ((ulUser0 >> 8) & 0xff); sTempAddr.addr[2] = ((ulUser0 >> 16) & 0xff); sTempAddr.addr[3] = ((ulUser1 >> 0) & 0xff); sTempAddr.addr[4] = ((ulUser1 >> 8) & 0xff); sTempAddr.addr[5] = ((ulUser1 >> 16) & 0xff); // // Program the hardware with it's MAC address (for filtering). // EthernetMACAddrSet(ETH_BASE, (unsigned char *)&sTempAddr); uip_setethaddr(sTempAddr); // // Initialize the TCP/IP Application (e.g. web server). // httpd_init(); #ifndef USE_STATIC_IP // // Initialize the DHCP Client Application. // dhcpc_init(&sTempAddr.addr[0], 6); dhcpc_request(); #endif // // Main Application Loop. // lPeriodicTimer = 0; lARPTimer = 0; while(true) { // // Wait for an event to occur. This can be either a System Tick event, // or an RX Packet event. // while(!g_ulFlags) { } // // If SysTick, Clear the SysTick interrupt flag and increment the // timers. // if(HWREGBITW(&g_ulFlags, FLAG_SYSTICK) == 1) { HWREGBITW(&g_ulFlags, FLAG_SYSTICK) = 0; lPeriodicTimer += SYSTICKMS; lARPTimer += SYSTICKMS; } // // Check for an RX Packet and read it. // lPacketLength = EthernetPacketGetNonBlocking(ETH_BASE, uip_buf, sizeof(uip_buf)); if(lPacketLength > 0) { // // Set uip_len for uIP stack usage. // uip_len = (unsigned short)lPacketLength; // // Clear the RX Packet event and renable RX Packet interrupts. // if(HWREGBITW(&g_ulFlags, FLAG_RXPKT) == 1) { HWREGBITW(&g_ulFlags, FLAG_RXPKT) = 0; EthernetIntEnable(ETH_BASE, ETH_INT_RX); } // // Process incoming IP packets here. // if(BUF->type == htons(UIP_ETHTYPE_IP)) { uip_arp_ipin(); uip_input(); // // If the above function invocation resulted in data that // should be sent out on the network, the global variable // uip_len is set to a value > 0. // if(uip_len > 0) { uip_arp_out(); EthernetPacketPut(ETH_BASE, uip_buf, uip_len); uip_len = 0; } } // // Process incoming ARP packets here. // else if(BUF->type == htons(UIP_ETHTYPE_ARP)) { uip_arp_arpin(); // // If the above function invocation resulted in data that // should be sent out on the network, the global variable // uip_len is set to a value > 0. // if(uip_len > 0) { EthernetPacketPut(ETH_BASE, uip_buf, uip_len); uip_len = 0; } } } // // Process TCP/IP Periodic Timer here. // if(lPeriodicTimer > UIP_PERIODIC_TIMER_MS) { lPeriodicTimer = 0; for(ulTemp = 0; ulTemp < UIP_CONNS; ulTemp++) { uip_periodic(ulTemp); // // If the above function invocation resulted in data that // should be sent out on the network, the global variable // uip_len is set to a value > 0. // if(uip_len > 0) { uip_arp_out(); EthernetPacketPut(ETH_BASE, uip_buf, uip_len); uip_len = 0; } } #if UIP_UDP for(ulTemp = 0; ulTemp < UIP_UDP_CONNS; ulTemp++) { uip_udp_periodic(ulTemp); // // If the above function invocation resulted in data that // should be sent out on the network, the global variable // uip_len is set to a value > 0. // if(uip_len > 0) { uip_arp_out(); EthernetPacketPut(ETH_BASE, uip_buf, uip_len); uip_len = 0; } } #endif // UIP_UDP } // // Process ARP Timer here. // if(lARPTimer > UIP_ARP_TIMER_MS) { lARPTimer = 0; uip_arp_timer(); } } }
int main(void) { led_conf(); int i; uip_ipaddr_t ipaddr; struct timer periodic_timer, arp_timer; /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); WDTCSR |= _BV(_WD_CHANGE_BIT) | _BV(WDE); WDTCSR = 0; network_init(); clock_init(); timer_set(&periodic_timer, CLOCK_SECOND / 2); timer_set(&arp_timer, CLOCK_SECOND * 10); uip_init(); phys_init(); // must be done or sometimes arp doesn't work uip_arp_init(); _enable_dhcp=eeprom_read_byte(&ee_enable_dhcp); if ((_enable_dhcp != 1) && (_enable_dhcp != 0)) { // if the setting is invalid, enable by default _enable_dhcp = 1; eeprom_write_byte(&ee_enable_dhcp,_enable_dhcp); } eeprom_read_block ((void *)_eth_addr, (const void *)&ee_eth_addr,6); // if the mac address in eeprom looks good, use it. if((_eth_addr[0] != 255) && (_eth_addr[0] != 0)) { my_eth_addr.addr[0] = _eth_addr[0]; my_eth_addr.addr[1] = _eth_addr[1]; my_eth_addr.addr[2] = _eth_addr[2]; my_eth_addr.addr[3] = _eth_addr[3]; my_eth_addr.addr[4] = _eth_addr[4]; my_eth_addr.addr[5] = _eth_addr[5]; } uip_setethaddr(my_eth_addr); _enable_dhcp = 1; if (_enable_dhcp) { #ifdef __DHCPC_H__ // setup the dhcp renew timer the make the first request timer_set(&dhcp_timer, CLOCK_SECOND * 600); dhcpc_init(&my_eth_addr, 6); dhcpc_request(); #endif } else { eeprom_read_block ((void *)_ip_addr, (const void *)&ee_ip_addr,4); eeprom_read_block ((void *)_net_mask,(const void *)&ee_net_mask,4); eeprom_read_block ((void *)_gateway, (const void *)&ee_gateway,4); // if the IP looks good in flash, use it if ((_ip_addr[0] != 255) && (_ip_addr[0] != 0)) { uip_ipaddr(ipaddr, _ip_addr[0], _ip_addr[1], _ip_addr[2], _ip_addr[3]); uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, _gateway[0], _gateway[1], _gateway[2], _gateway[3]); uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, _net_mask[0], _net_mask[1], _net_mask[2], _net_mask[3]); uip_setnetmask(ipaddr); } else { // ip in flash didn't look good... use default uip_ipaddr(ipaddr, UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, UIP_IPADDR3); uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3); uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, UIP_NETMASK0, UIP_NETMASK1, UIP_NETMASK2, UIP_NETMASK3); uip_setnetmask(ipaddr); } } jsoncmd_init(); while (1) { uip_len = network_read(); if(uip_len > 0) { if(BUF->type == htons(UIP_ETHTYPE_IP)) { led_on(5); uip_arp_ipin(); // arp seems to have issues w/o this uip_input(); if(uip_len > 0) { uip_arp_out(); network_send(); } } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) { led_on(4); uip_arp_arpin(); // this is correct if(uip_len > 0) { network_send(); } } } else if(timer_expired(&periodic_timer)) { led_off(4); led_off(5); timer_reset(&periodic_timer); for(i = 0; i < UIP_CONNS; i++) { uip_periodic(i); if(uip_len > 0) { uip_arp_out(); network_send(); } } #if UIP_UDP for(i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i); if(uip_len > 0) { uip_arp_out(); network_send(); } } #endif /* UIP_UDP */ if(timer_expired(&arp_timer)) { timer_reset(&arp_timer); uip_arp_timer(); } } else if (_enable_dhcp && timer_expired(&dhcp_timer)) { #ifdef __DHCPC_H__ led_on(3); // for now turn off the led when we start the dhcp process dhcpc_renew(); timer_reset(&dhcp_timer); #endif } } return 0; }
int user_start(int argc, char *argv[]) { struct in_addr addr; #if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_EXAMPLE_UIP_NOMAC) uint8_t mac[IFHWADDRLEN]; #endif #ifdef CONFIG_EXAMPLE_UIP_DHCPC void *handle; #endif /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLE_UIP_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xb0; mac[3] = 0x0b; mac[4] = 0xba; mac[5] = 0xbe; uip_setmacaddr("eth0", mac); #endif /* Set up our host address */ #ifdef CONFIG_EXAMPLE_UIP_DHCPC addr.s_addr = 0; #else addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_IPADDR); #endif uip_sethostaddr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_DRIPADDR); uip_setdraddr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_NETMASK); uip_setnetmask("eth0", &addr); #ifdef CONFIG_EXAMPLE_UIP_DHCPC /* Set up the resolver */ resolv_init(); /* Get the MAC address of the NIC */ uip_getmacaddr("eth0", mac); /* Set up the DHCPC modules */ handle = dhcpc_open(&mac, IFHWADDRLEN); /* Get an IP address. Note: there is no logic here for renewing the address in this * example. The address should be renewed in ds.lease_time/2 seconds. */ printf("Getting IP address\n"); if (handle) { struct dhcpc_state ds; (void)dhcpc_request(handle, &ds); uip_sethostaddr("eth1", &ds.ipaddr); if (ds.netmask.s_addr != 0) { uip_setnetmask("eth0", &ds.netmask); } if (ds.default_router.s_addr != 0) { uip_setdraddr("eth0", &ds.default_router); } if (ds.dnsaddr.s_addr != 0) { resolv_conf(&ds.dnsaddr); } dhcpc_close(handle); printf("IP: %s\n", inet_ntoa(ds.ipaddr)); } #endif #ifdef CONFIG_NET_TCP printf("Starting webserver\n"); httpd_init(); httpd_listen(); #endif while(1) { sleep(3); printf("main: Still running\n"); #if CONFIG_NFILE_DESCRIPTORS > 0 fflush(stdout); #endif } return 0; }
int discover_main(int argc, char *argv[]) { /* If this task is excecutated as an NSH built-in function, then the * network has already been configured by NSH's start-up logic. */ #ifndef CONFIG_NSH_BUILTIN_APPS struct in_addr addr; #if defined(CONFIG_EXAMPLES_DISCOVER_DHCPC) || defined(CONFIG_EXAMPLES_DISCOVER_NOMAC) uint8_t mac[IFHWADDRLEN]; #endif #ifdef CONFIG_EXAMPLES_DISCOVER_DHCPC void *handle; #endif /* Many embedded network interfaces must have a software assigned MAC */ #ifdef CONFIG_EXAMPLES_DISCOVER_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; uip_setmacaddr("eth0", mac); #endif /* Set up our host address */ #ifdef CONFIG_EXAMPLES_DISCOVER_DHCPC addr.s_addr = 0; #else addr.s_addr = HTONL(CONFIG_EXAMPLES_DISCOVER_IPADDR); #endif uip_sethostaddr("eth0", &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_DISCOVER_DRIPADDR); uip_setdraddr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_DISCOVER_NETMASK); uip_setnetmask("eth0", &addr); #ifdef CONFIG_EXAMPLES_DISCOVER_DHCPC /* Set up the resolver */ dns_bind(); /* Get the MAC address of the NIC */ uip_getmacaddr("eth0", mac); /* Set up the DHCPC modules */ handle = dhcpc_open(&mac, IFHWADDRLEN); /* Get an IP address. Note: there is no logic here for renewing the address in this * example. The address should be renewed in ds.lease_time/2 seconds. */ printf("Getting IP address\n"); if (handle) { struct dhcpc_state ds; (void)dhcpc_request(handle, &ds); uip_sethostaddr("eth1", &ds.ipaddr); if (ds.netmask.s_addr != 0) { uip_setnetmask("eth0", &ds.netmask); } if (ds.default_router.s_addr != 0) { uip_setdraddr("eth0", &ds.default_router); } if (ds.dnsaddr.s_addr != 0) { dns_setserver(&ds.dnsaddr); } dhcpc_close(handle); printf("IP: %s\n", inet_ntoa(ds.ipaddr)); } #endif /* CONFIG_EXAMPLES_DISCOVER_DHCPC */ #endif /* CONFIG_NSH_BUILTIN_APPS */ if (discover_start(NULL) < 0) { ndbg("Could not start discover daemon.\n"); return ERROR; } return OK; }
static inline void wlan_bringup(void) { #if defined(CONFIG_EXAMPLES_WLAN_DHCPC) || defined(CONFIG_EXAMPLES_WLAN_NOMAC) uint8_t mac[IFHWADDRLEN]; #endif struct in_addr addr; #ifdef CONFIG_EXAMPLES_WLAN_DHCPC void *handle; #endif /* Many embedded network interfaces must have a software assigned * MAC */ #ifdef CONFIG_EXAMPLES_WLAN_NOMAC mac[0] = 0x00; mac[1] = 0xe0; mac[2] = 0xde; mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; uip_setmacaddr("eth0", mac); #endif /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_WLAN_DRIPADDR); uip_setdraddr("eth0", &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_WLAN_NETMASK); uip_setnetmask("eth0", &addr); /* Set up our host address */ #ifdef CONFIG_EXAMPLES_WLAN_DHCPC addr.s_addr = 0; #else addr.s_addr = HTONL(CONFIG_EXAMPLES_WLAN_IPADDR); #endif uip_sethostaddr("eth0", &addr); #ifdef CONFIG_EXAMPLES_WLAN_DHCPC /* Set up the resolver */ resolv_init(); /* Get the MAC address of the NIC */ uip_getmacaddr("eth0", mac); /* Set up the DHCPC modules */ handle = dhcpc_open(&mac, IFHWADDRLEN); /* Get an IP address. Note: there is no logic here for renewing * the address in this example. The address should be renewed in * ds.lease_time/2 seconds. */ printf("Getting IP address\n"); if (handle) { struct dhcpc_state ds; (void)dhcpc_request(handle, &ds); uip_sethostaddr("eth1", &ds.ipaddr); if (ds.netmask.s_addr != 0) { uip_setnetmask("eth0", &ds.netmask); } if (ds.default_router.s_addr != 0) { uip_setdraddr("eth0", &ds.default_router); } if (ds.dnsaddr.s_addr != 0) { resolv_conf(&ds.dnsaddr); } dhcpc_close(handle); printf("IP: %s\n", inet_ntoa(ds.ipaddr)); } #endif }