//***************************************************************************** // // Required by lwIP library to support any host-related timer functions. // //***************************************************************************** void lwIPHostTimerHandler(void) { unsigned long ulIPAddress; // // Get the local IP address. // ulIPAddress = lwIPLocalIPAddrGet(); // // See if an IP address has been assigned. // if(ulIPAddress == 0) { // // Draw a spinning line to indicate that the IP address is being // discoverd. // UARTprintf("\b%c", g_pcTwirl[g_ulTwirlPos]); // // Update the index into the twirl. // g_ulTwirlPos = (g_ulTwirlPos + 1) & 3; } // // Check if IP address has changed, and display if it has. // else if(ulIPAddress != g_ulLastIPAddr) { // // Display the new IP address. // UARTprintf("\rIP: %d.%d.%d.%d \n", ulIPAddress & 0xff, (ulIPAddress >> 8) & 0xff, (ulIPAddress >> 16) & 0xff, (ulIPAddress >> 24) & 0xff); // // Save the new IP address. // g_ulLastIPAddr = ulIPAddress; // // Display the new network mask. // ulIPAddress = lwIPLocalNetMaskGet(); UARTprintf("Netmask: %d.%d.%d.%d\n", ulIPAddress & 0xff, (ulIPAddress >> 8) & 0xff, (ulIPAddress >> 16) & 0xff, (ulIPAddress >> 24) & 0xff); // // Display the new gateway address. // ulIPAddress = lwIPLocalGWAddrGet(); UARTprintf("Gateway: %d.%d.%d.%d\n", ulIPAddress & 0xff, (ulIPAddress >> 8) & 0xff, (ulIPAddress >> 16) & 0xff, (ulIPAddress >> 24) & 0xff); }
//***************************************************************************** // // Returns the IP address for this interface. // // This function will read and return the currently assigned IP address for // the Tiva Ethernet interface. // // \return Returns the assigned IP address for this interface. // //***************************************************************************** uint32_t EthClientAddrGet(void) { // // Return IP. // return(lwIPLocalIPAddrGet()); }
/* ------------------------------------------------------------------------------------------------------ * sockex_nonblocking_connect() * * Description : Handing socket receive data. * * Argument(s) : none. * */ void sockex_testrecv(void *arg) { // int ret; struct sockaddr_in servaddr, cliaddr; // struct timeval tv; unsigned long cliaddr_len; LWIP_UNUSED_ARG(arg); memset(&servaddr, 0, sizeof(servaddr)); /* set up address to connect to */ servaddr.sin_len = sizeof(servaddr); servaddr.sin_family = AF_INET; servaddr.sin_port = PP_HTONS(SOCK_HOSR_PORT); servaddr.sin_addr.s_addr = lwIPLocalIPAddrGet(); /* Set local IP address.*/ listenfd = lwip_socket(AF_INET, SOCK_STREAM, 0); lwip_bind(listenfd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)); lwip_listen(listenfd, SOCK_HOSR_PORT); RS232printf("Accepting connections ...\n"); cliaddr_len = sizeof(cliaddr); for(;;) { connfd = lwip_accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len); if(connfd <= 0) { OSTimeDly(2); continue; } else { connfd = connfd; RS232printf("cli is ok!"); } // lwip_select(); OSTimeDly(2); } }
/* ------------------------------------------------------------------------------------------------------ * TcpClientTask() * * Description : main function. * * Argument(s) : none. * */ static void TcpClientMainProc(void) { struct in_addr g_sClientIP; switch(g_bNetStatus) { case NETS_INIT: do { g_sClientIP.s_addr = lwIPLocalIPAddrGet(); OSTimeDly(10); }while(0 == g_sClientIP.s_addr); OSMboxPost(App_LcdMbox, (void *)&g_sClientIP.s_addr); /* Send lcd txt.*/ g_bNetStatus = NETS_LOCIP; /* Net mode charge LOCIP.*/ TaskSocket_Create(); /* Create Socket task and init.*/ break; case NETS_LOCIP: // TcpGetLocalIp(); break; case NETS_SRVIP: // TcpGetServerIp(); break; case NETS_LOGIN: // TcpClientRelogin(); break; case NETS_NORMAL: // TcpNormalProc(); break; default: break; } }
//***************************************************************************** // // Required by lwIP library to support any host-related timer functions. // //***************************************************************************** void lwIPHostTimerHandler(void) { uint32_t ui32Idx, ui32NewIPAddress; // // Get the current IP address. // ui32NewIPAddress = lwIPLocalIPAddrGet(); // // See if the IP address has changed. // if(ui32NewIPAddress != g_ui32IPAddress) { // // See if there is an IP address assigned. // if(ui32NewIPAddress == 0xffffffff) { // // Indicate that there is no link. // UARTprintf("Waiting for link.\n"); } else if(ui32NewIPAddress == 0) { // // There is no IP address, so indicate that the DHCP process is // running. // UARTprintf("Waiting for IP address.\n"); } else { // // Display the new IP address. // //UARTprintf("IP Address: "); //DisplayIPAddress(ui32NewIPAddress); //UARTprintf("\nOpen a browser and enter the IP address.\n"); } // // Save the new IP address. // g_ui32IPAddress = ui32NewIPAddress; // // Turn GPIO off. // MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1, ~GPIO_PIN_1); } // // If there is not an IP address. // if((ui32NewIPAddress == 0) || (ui32NewIPAddress == 0xffffffff)) { // // Loop through the LED animation. // for(ui32Idx = 1; ui32Idx < 17; ui32Idx++) { // // Toggle the GPIO // MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1, (MAP_GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_1) ^ GPIO_PIN_1)); SysCtlDelay(120000000/(ui32Idx << 1)); } } }
/* ------------------------------------------------------------------------------------------------------ * sockex_nonblocking_connect() * * Description : tests socket blocking and nonblocking connect. * * Argument(s) : none. * */ void sockex_nonblocking_connect(void *arg) { int ret; struct sockaddr_in addr; fd_set readset; fd_set writeset; fd_set errset; struct timeval tv; LWIP_UNUSED_ARG(arg); memset(&addr, 0, sizeof(addr)); /* set up address to connect to */ addr.sin_len = sizeof(addr); addr.sin_family = AF_INET; addr.sin_port = PP_HTONS(SOCK_TARGET_PORT); addr.sin_addr.s_addr = inet_addr(SOCK_TARGET_HOST); socket_state = SOCKET_NEW; for(;;) { if(g_bNetStatus == NETS_LOCIP) /* IP is setted.*/ { switch(socket_state) { case SOCKET_NEW: { s = lwip_socket(AF_INET, SOCK_STREAM, 0); socket_state = SOCKET_CON; break; } case SOCKET_CON: { unsigned int ip; ret = lwip_connect(s, (struct sockaddr*)&addr, sizeof(addr)); LWIP_ASSERT("ret == 0", ret == 0); if(ret < 0) { lwip_close(s); socket_state = SOCKET_NEW; OSTimeDly(2); RS232printf("socket connect failed.\n"); break; } ip = lwIPLocalIPAddrGet(); NetDisplayIPAddress(ip); /* Socket updata IP address.*/ socket_state = SOCKET_IDIE; } case SOCKET_IDIE: { INT8U *msg; INT8U err; msg = (INT8U *)OSMboxPend(NET_RfMbox, 0, &err); /* Waiting socket writing data.*/ if(err != OS_ERR_NONE) break; ret = lwip_write(s, msg, 6); if(ret < 0) { lwip_close(s); socket_state = SOCKET_CON; } } break; case SOCKET_CHECK: // TODO: Check socket connecting. FD_ZERO(&readset); FD_SET(s, &readset); FD_ZERO(&writeset); FD_SET(s, &writeset); FD_ZERO(&errset); FD_SET(s, &errset); tv.tv_sec = 3; tv.tv_usec = 0; /* Set time out 3s, 函数立即返回*/ ret = lwip_select(s+1, &readset, &writeset, &errset, &tv); if(ret == 0) { RS232printf("socket check timeout.\n"); lwip_close(s); socket_state = SOCKET_CON; /* Reconnect socket.*/ } if(FD_ISSET(s, &writeset) == 0) /* If socket couldn't write.*/ { RS232printf("socket write test error.\n"); lwip_close(s); socket_state = SOCKET_CON; /* Reconnect socket.*/ } ret = lwip_write(s, "test", 6); if(ret < 0) { lwip_close(s); socket_state = SOCKET_CON; } OSTimeDly(2000); break; default: break; } } OSTimeDly(2); } }
/* ------------------------------------------------------------------------------------------------------ * sockex_selects() * * Description : socket selects test. * * Argument(s) : none. * */ void sockex_selects(void *arg) { int sock_fd, new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; socklen_t sin_size; int yes; INT8U buf[BUF_SIZE]; int ret; int i; fd_set fdsr; /* Create file descriptor.*/ int maxsock; struct timeval tv; conn_amount = 0; LWIP_UNUSED_ARG(arg); sock_fd = lwip_socket(AF_INET, SOCK_STREAM, 0); yes = 1; ret = lwip_setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); if(ret == -1) { return; } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_len = sizeof(server_addr); server_addr.sin_port = PP_HTONS(SOCK_HOSR_PORT); server_addr.sin_addr.s_addr = lwIPLocalIPAddrGet(); /* IP_ADDR_ANY is '0.0.0.0'.*/ lwip_bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); lwip_listen(sock_fd, BACKLOG + 1); /* MAX TCP client is BACKLOG.*/ sin_size = sizeof(client_addr); maxsock = sock_fd; while(1) { FD_ZERO(&fdsr); /* Initialize file descriptor set.*/ FD_SET(sock_fd, &fdsr); tv.tv_sec = 10; /* Timeout setting.*/ tv.tv_usec = 0; for (i = 0; i < BACKLOG; i++) /* Add active connection to fd set.*/ { if (fd_A[i] != 0) { FD_SET(fd_A[i], &fdsr); } } ret = lwip_select(maxsock + 1, &fdsr, NULL, NULL, &tv); if(ret < 0) { break; } else if(ret == 0) { continue; } for (i = 0; i < conn_amount; i++) /* Check every fd in the set.*/ { if (FD_ISSET(fd_A[i], &fdsr)) { int opt = 100; /* set recv timeout (100 ms) */ lwip_setsockopt(fd_A[i], SOL_SOCKET, SO_RCVTIMEO, &opt, sizeof(int)); ret = lwip_read(fd_A[i], buf, 8); if (ret <= 0) { // lwip_close(fd_A[i]); // FD_CLR(fd_A[i], &fdsr); // fd_A[i] = 0; } else /* receive data.*/ { if((buf[0] == 'C')&&(buf[1] == 'o')) { // address_t addr; // INT8U mac[8] = {0x00, 0x12, 0x4B, 0x00, 0x01, 0xC0, 0xB7, 0xE0}; // addr.mode = LONG_ADDR; // utilReverseBuf(mac, 8); // memcpy(addr.long_addr, mac, 8); // mac_tx_handle(&addr, &buf[7], 1, MAC_DATA); } if (ret < BUF_SIZE) memset(&buf[ret], '\0', 1); } } } if(FD_ISSET(sock_fd, &fdsr)) /* Check whether a new connection comes.*/ { new_fd = lwip_accept(sock_fd, (struct sockaddr *)&client_addr, &sin_size); if(new_fd <= 0) { continue; } // lwip_send(new_fd, "con", 4, 0); if(conn_amount < BACKLOG) /* Add to fd queue.*/ { fd_A[conn_amount++] = new_fd; if(new_fd > maxsock) maxsock = new_fd; } else { // conn_amount = 0; lwip_close(fd_A[conn_amount-1]); fd_A[conn_amount-1] = new_fd; if(new_fd > maxsock) maxsock = new_fd; // lwip_send(new_fd, "bye", 4, 0); // lwip_close(new_fd); /* Close larger than 5 socket.*/ } } // for (i = 0; i < BACKLOG; i++) /* Close other connections.*/ // { // if (fd_A[i] != 0) { // lwip_close(fd_A[i]); // } // } } }
//***************************************************************************** // // Required by lwIP library to support any host-related timer functions. // //***************************************************************************** void lwIPHostTimerHandler(void) { #ifdef ewarm volatile unsigned long ulIPAddress; unsigned long ulTemp; #else unsigned long ulIPAddress, ulTemp; #endif // // Get the local IP address. // ulIPAddress = lwIPLocalIPAddrGet(); // // If IP Address has not yet been assigned, update the display accordingly // if(ulIPAddress == 0) { // // Draw a spinning line to indicate that the IP address is being // discoverd. // UARTprintf("\b%c", g_pcTwirl[g_ulTwirlPos]); // // Update the index into the twirl. // g_ulTwirlPos = (g_ulTwirlPos + 1) & 3; } // // Check if IP address has changed, and display if it has. // else if(g_ulLastIPAddr != ulIPAddress) { // // Display the new IP address. // ulTemp = ulIPAddress; UARTprintf("\rIP: %d.%d.%d.%d \n", ulTemp & 0xff, (ulTemp >> 8) & 0xff, (ulTemp >> 16) & 0xff, (ulTemp >> 24) & 0xff); // // Save the new IP address. // g_ulLastIPAddr = ulIPAddress; // // Display the new network mask. // ulTemp = lwIPLocalNetMaskGet(); UARTprintf("Netmask: %d.%d.%d.%d\n", ulTemp & 0xff, (ulTemp >> 8) & 0xff, (ulTemp >> 16) & 0xff, (ulTemp >> 24) & 0xff); // // Display the new gateway address. // ulTemp = lwIPLocalGWAddrGet(); UARTprintf("Gateway: %d.%d.%d.%d\n", ulTemp & 0xff, (ulTemp >> 8) & 0xff, (ulTemp >> 16) & 0xff, (ulTemp >> 24) & 0xff); }
// This task simply toggles the user LED at a 1 Hz rate. static void LEDTask(void *pvParameters) { portTickType ui32LastTime; uint32_t ui32Temp; // Get the current tick count. ui32LastTime = xTaskGetTickCount(); // Loop forever. while (1) { ui32Temp = lwIPLocalIPAddrGet(); /* No IP acquired */ if (ui32Temp == IPADDR_NONE || ui32Temp == IPADDR_ANY) { // Turn off the user LED. #ifdef DEVKIT ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5, GPIO_PIN_5); #else ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, GPIO_PIN_1); #endif // Wait for the required amount of time. vTaskDelayUntil(&ui32LastTime, LED_DELAY_OFF / portTICK_RATE_MS); // Turn on the user LED. #ifdef DEVKIT ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5, 0); #else ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, 0); #endif // Wait for the required amount of time. vTaskDelayUntil(&ui32LastTime, LED_DELAY_OFF / portTICK_RATE_MS); /* Auto IP acquired */ } else if ( (ui32Temp & 0xFFFF) == 0xFEA9 ) { // Turn off the user LED. #ifdef DEVKIT ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5, GPIO_PIN_5); #else ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, GPIO_PIN_1); #endif // Wait for the required amount of time. vTaskDelayUntil(&ui32LastTime, LED_DELAY_ON / portTICK_RATE_MS); // Turn on the user LED. #ifdef DEVKIT ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5, 0); #else ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, 0); #endif // Wait for the required amount of time. vTaskDelayUntil(&ui32LastTime, LED_DELAY_OFF / portTICK_RATE_MS); /* DHCP IP acquired */ } else if ( (ui32Temp & 0xFFFF) == 0xA8C0 ) { // Turn off the user LED. #ifdef DEVKIT ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5, GPIO_PIN_5); #else ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, GPIO_PIN_1); #endif // Wait for the required amount of time. vTaskDelayUntil(&ui32LastTime, LED_DELAY_OFF / portTICK_RATE_MS); // Turn on the user LED. #ifdef DEVKIT ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5, 0); #else ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, 0); #endif // Wait for the required amount of time. vTaskDelayUntil(&ui32LastTime, LED_DELAY_ON / portTICK_RATE_MS); } else { vTaskDelayUntil(&ui32LastTime, 2000 / portTICK_RATE_MS); } } }
//***************************************************************************** // // Periodic Tick for the Ethernet client // // This function is the needed periodic tick for the Ethernet client. It needs // to be called periodically through the use of a timer or systick. // // \return None. // //***************************************************************************** void EthClientTick(uint32_t ui32TickMS) { uint32_t ui32IPAddr; int32_t i32Ret; if(HWREGBITW(&g_sEnet.ui32Flags, FLAG_TIMER_DHCP_EN)) { lwIPTimer(ui32TickMS); } if(HWREGBITW(&g_sEnet.ui32Flags, FLAG_TIMER_DNS_EN)) { dns_tmr(); } if(HWREGBITW(&g_sEnet.ui32Flags, FLAG_TIMER_TCP_EN)) { tcp_tmr(); } // // Check for loss of link. // if((g_sEnet.eState != iEthNoConnection) && (lwIPLocalIPAddrGet() == 0xffffffff)) { // // Reset the connection due to a loss of link. // EthClientReset(); // // Signal a disconnect event. // g_sEnet.pfnEvent(ETH_EVENT_DISCONNECT, 0, 0); } else if(g_sEnet.eState == iEthNoConnection) { // // Once link is detected start DHCP. // if(lwIPLocalIPAddrGet() != 0xffffffff) { EthClientDHCPConnect(); g_sEnet.eState = iEthDHCPWait; } } else if(g_sEnet.eState == iEthDHCPWait) { // // Get IP address. // ui32IPAddr = lwIPLocalIPAddrGet(); // // If IP Address has not yet been assigned, update the display // accordingly. // if((ui32IPAddr != 0xffffffff) && (ui32IPAddr != 0)) { // // Update the DHCP IP address. // g_sEnet.sLocalIP.addr = ui32IPAddr; g_sEnet.eState = iEthDHCPComplete; // // Stop DHCP timer since an address has been provided. // HWREGBITW(&g_sEnet.ui32Flags, FLAG_DHCP_STARTED) = 0; } } else if(g_sEnet.eState == iEthDHCPComplete) { if(g_sEnet.pcProxyName == 0) { // // Resolve the host by name. // i32Ret = EthClientDNSResolve("api.openweathermap.org"); } else { // // Resolve the proxy by name. // i32Ret = EthClientDNSResolve(g_sEnet.pcProxyName); } if(i32Ret == ERR_OK) { // // If the address was already returned then go to idle. // g_sEnet.eState = iEthIdle; // // Notify the main routine of the new Ethernet connection. // g_sEnet.pfnEvent(ETH_EVENT_CONNECT, &g_sEnet.sLocalIP.addr, 4); } else if(i32Ret == ERR_INPROGRESS) { // // If the request is pending the go to the iEthDNSWait state. // g_sEnet.eState = iEthDNSWait; } } else if(g_sEnet.eState == iEthDNSWait) { // // Check if the host name was resolved. // if(HWREGBITW(&g_sEnet.ui32Flags, FLAG_DNS_ADDRFOUND)) { // // Stop calling the DNS timer function. // HWREGBITW(&g_sEnet.ui32Flags, FLAG_TIMER_DNS_EN) = 0; g_sEnet.eState = iEthIdle; // // Notify the main routine of the new Ethernet connection. // g_sEnet.pfnEvent(ETH_EVENT_CONNECT, &g_sEnet.sLocalIP.addr, 4); } } else if(g_sEnet.eState == iEthTCPConnectWait) { } else if(g_sEnet.eState == iEthTCPConnectComplete) { err_t eError; g_sEnet.eState = iEthTCPOpen; eError = EthClientSend(g_sWeather.pcRequest, g_sWeather.ui32RequestSize); if(eError == ERR_OK) { // // Waiting on a query response. // g_sEnet.eState = iEthQueryWait; } else { g_sEnet.eState = iEthIdle; } } }