/*! \brief This function checks the LAN connection by pinging the AP's gateway \param[in] None \return 0 on success, negative error-code on error */ static _i32 checkLanConnection() { SlPingStartCommand_t pingParams = {0}; SlPingReport_t pingReport = {0}; _i32 retVal = -1; CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE); g_PingPacketsRecv = 0; /* Set the ping parameters */ pingParams.PingIntervalTime = PING_INTERVAL; pingParams.PingSize = PING_PKT_SIZE; pingParams.PingRequestTimeout = PING_TIMEOUT; pingParams.TotalNumberOfAttempts = NO_OF_ATTEMPTS; pingParams.Flags = 0; pingParams.Ip = g_GatewayIP; /* Check for LAN connection */ retVal = sl_NetAppPingStart( (SlPingStartCommand_t*)&pingParams, SL_AF_INET, (SlPingReport_t*)&pingReport, SimpleLinkPingReport); ASSERT_ON_ERROR(retVal); /* Wait */ while(!IS_PING_DONE(g_Status)) { _SlNonOsMainLoopTask(); } if(0 == g_PingPacketsRecv) { /* Problem with LAN connection */ ASSERT_ON_ERROR(LAN_CONNECTION_FAILED); } /* LAN connection is successful */ return SUCCESS; }
//**************************************************************************** // //! \brief device will try to ping the machine that has just connected to the //! device. //! //! \param ulIpAddr is the ip address of the station which has connected to //! device //! //! \return 0 if ping is successful, -1 for error // //**************************************************************************** static int PingTest(unsigned long ulIpAddr) { signed long retVal = -1; SlPingStartCommand_t PingParams; SlPingReport_t PingReport; PingParams.PingIntervalTime = PING_INTERVAL; PingParams.PingSize = PING_PKT_SIZE; PingParams.PingRequestTimeout = PING_TIMEOUT; PingParams.TotalNumberOfAttempts = NO_OF_ATTEMPTS; PingParams.Flags = PING_FLAG; PingParams.Ip = ulIpAddr; /* Cleint's ip address */ UART_PRINT("Running Ping Test...\n\r"); /* Check for LAN connection */ retVal = sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&PingReport, SimpleLinkPingReport); ASSERT_ON_ERROR(__LINE__, retVal); while(!IS_PING_DONE(g_ulStatus)) { // Wait for Ping Event } if (g_ulPingPacketsRecv) { // LAN connection is successful UART_PRINT("Ping Test successful\n\r"); return SUCCESS; } else { // Problem with LAN connection return LAN_CONNECTION_FAILED; } }
static void net_ping(const char *host) { SlPingStartCommand_t pingParams = {0}; SlPingReport_t pingReport = {0}; unsigned long ulIpAddr = 0; CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_PING_DONE); // Set the ping parameters pingParams.PingIntervalTime = 1000; pingParams.PingSize = 20; pingParams.PingRequestTimeout = 3000; pingParams.TotalNumberOfAttempts = 3; pingParams.Flags = 0; pingParams.Ip = g_ulGatewayIP; UART_PRINT("ping host: %s\r\n", host); sl_NetAppDnsGetHostByName((signed char*)host, strlen(host), &ulIpAddr, SL_AF_INET); UART_PRINT("host ip: 0x%08X\r\n", host); pingParams.Ip = ulIpAddr; sl_NetAppPingStart((SlPingStartCommand_t*)&pingParams, SL_AF_INET, (SlPingReport_t*)&pingReport, SimpleLinkPingReport); while (!IS_PING_DONE(g_ulStatus)) _SlNonOsMainLoopTask(); }
//**************************************************************************** // //! \brief device will try to ping the machine that has just connected to the //! device. //! //! \param ulIpAddr is the ip address of the station which has connected to //! device //! //! \return 0 if ping is successful, -1 for error // //**************************************************************************** int PingTest(unsigned long ulIpAddr) { SlPingStartCommand_t PingParams; SlPingReport_t PingReport; PingParams.PingIntervalTime = 1000; PingParams.PingSize = 20; PingParams.PingRequestTimeout = 3000; PingParams.TotalNumberOfAttempts = 3; PingParams.Flags = 0; PingParams.Ip = ulIpAddr; /* STA's ip address which is our AP address */ /* Check for LAN connection */ sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&PingReport, SimpleLinkPingReport); while (!g_uiPingDone) { } if (g_uiPingPacketsRecv) { /* LAN connection is successful */ return(0); } else { return(-1); } }
//***************************************************************************** //! \brief This function checks the internet connection by pinging //! the external-host (HOST_NAME) //! //! \param None //! //! \return 0 on success, negative error-code on error //! //***************************************************************************** static long CheckInternetConnection() { SlPingStartCommand_t pingParams = {0}; SlPingReport_t pingReport = {0}; unsigned long ulIpAddr = 0; long lRetVal = -1; CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_PING_DONE); g_ulPingPacketsRecv = 0; // Set the ping parameters pingParams.PingIntervalTime = PING_INTERVAL; pingParams.PingSize = PING_PKT_SIZE; pingParams.PingRequestTimeout = PING_TIMEOUT; pingParams.TotalNumberOfAttempts = NO_OF_ATTEMPTS; pingParams.Flags = 0; pingParams.Ip = g_ulGatewayIP; // Get external host IP address lRetVal = sl_NetAppDnsGetHostByName((signed char*)HOST_NAME, sizeof(HOST_NAME), &ulIpAddr, SL_AF_INET); ASSERT_ON_ERROR(lRetVal); // Replace the ping address to match HOST_NAME's IP address pingParams.Ip = ulIpAddr; // Try to ping HOST_NAME lRetVal = sl_NetAppPingStart((SlPingStartCommand_t*)&pingParams, SL_AF_INET, (SlPingReport_t*)&pingReport, SimpleLinkPingReport); ASSERT_ON_ERROR(lRetVal); // Wait while(!IS_PING_DONE(g_ulStatus)) { // Wait for Ping Event #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } if (0 == g_ulPingPacketsRecv) { // Problem with internet connection ASSERT_ON_ERROR(INTERNET_CONNECTION_FAILED); } // Internet connection is successful return SUCCESS; }
/*! \brief This function checks the internet connection by pinging the external-host (HOST_NAME) \param[in] None \return 0 on success, negative error-code on error */ static _i32 checkInternetConnection() { SlPingStartCommand_t pingParams = {0}; SlPingReport_t pingReport = {0}; _u32 ipAddr = 0; _i32 retVal = -1; CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE); g_PingPacketsRecv = 0; /* Set the ping parameters */ pingParams.PingIntervalTime = PING_INTERVAL; pingParams.PingSize = PING_PKT_SIZE; pingParams.PingRequestTimeout = PING_TIMEOUT; pingParams.TotalNumberOfAttempts = NO_OF_ATTEMPTS; pingParams.Flags = 0; pingParams.Ip = g_GatewayIP; /* Check for Internet connection */ retVal = sl_NetAppDnsGetHostByName((_i8 *)HOST_NAME, pal_Strlen(HOST_NAME), &ipAddr, SL_AF_INET); ASSERT_ON_ERROR(retVal); /* Replace the ping address to match HOST_NAME's IP address */ pingParams.Ip = ipAddr; /* Try to ping HOST_NAME */ retVal = sl_NetAppPingStart( (SlPingStartCommand_t*)&pingParams, SL_AF_INET, (SlPingReport_t*)&pingReport, SimpleLinkPingReport); ASSERT_ON_ERROR(retVal); /* Wait */ while(!IS_PING_DONE(g_Status)) { _SlNonOsMainLoopTask(); } if (0 == g_PingPacketsRecv) { /* Problem with internet connection*/ ASSERT_ON_ERROR(INTERNET_CONNECTION_FAILED); } /* Internet connection is successful */ return SUCCESS; }
//***************************************************************************** // // Ping an IP Address // Arguments: // [1] IP address to ping // [2] (optional) max number of tries // [3] (optional) timeout in milliseconds // //***************************************************************************** int CMD_ping(int argc, char **argv) { SlPingStartCommand_t PingParams; SlPingReport_t Report; _NetCfgIpV4Args_t ipV4; unsigned char len = sizeof(_NetCfgIpV4Args_t);; unsigned char IsDHCP = 0; /* Read the IP parameter */ sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP,&len, (unsigned char *)&ipV4); /* Set the ping parameters */ PingParams.PingIntervalTime = PING_INTERVAL; PingParams.PingSize = PING_SIZE; PingParams.PingRequestTimeout = PING_TIMEOUT; PingParams.TotalNumberOfAttempts = NO_OF_ATTEMPTS; PingParams.Flags = 0; PingParams.Ip = ipV4.ipV4Gateway; /* Fill the GW IP address, which is our AP address */ /* Check for LAN connection */ UARTprintf("Pinging the Gateway...\n"); sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&Report, SimpleLinkPingReport); while(0 == (g_Status & PING_DONE)) { _SlNonOsMainLoopTask(); } g_Status &= ~(1 << PING_DONE_STATUS_BIT); UARTprintf("Ping Done\n"); if (!g_PingPacketsRecv) { /* Problem with LAN connection */ UARTprintf("Problem with the LAN connection\n"); return -1; } return 0; }
//***************************************************************************** //! \brief This function checks the LAN connection by pinging the AP's gateway //! //! \param None //! //! \return 0 on success, negative error-code on error //! //***************************************************************************** static long CheckLanConnection() { SlPingStartCommand_t pingParams = {0}; SlPingReport_t pingReport = {0}; long lRetVal = -1; CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_PING_DONE); g_ulPingPacketsRecv = 0; // Set the ping parameters pingParams.PingIntervalTime = PING_INTERVAL; pingParams.PingSize = PING_PKT_SIZE; pingParams.PingRequestTimeout = PING_TIMEOUT; pingParams.TotalNumberOfAttempts = NO_OF_ATTEMPTS; pingParams.Flags = 0; pingParams.Ip = g_ulGatewayIP; // Check for LAN connection lRetVal = sl_NetAppPingStart((SlPingStartCommand_t*)&pingParams, SL_AF_INET, (SlPingReport_t*)&pingReport, SimpleLinkPingReport); ASSERT_ON_ERROR(lRetVal); // Wait for NetApp Event while(!IS_PING_DONE(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } if(0 == g_ulPingPacketsRecv) { //Problem with LAN connection ASSERT_ON_ERROR(LAN_CONNECTION_FAILED); } // LAN connection is successful return SUCCESS; }
//**************************************************************************** //**************************************************************************** //**************************************************************************** //**************************************************************************** //**************************************************************************** // //! \brief pings to the default gateway and ip address of domain "www.ti.com" //! //! This function pings to the default gateway to ensure the wlan cannection, //! then check for the internet connection, if present then get the ip address //! of Domain name "www.ti.com" and pings to it //! //! \param ulDefaultGateway is the default gateway for the ap to which the //! device is connected //! //! \return -1 for unsuccessful LAN connection, -2 for problem with internet //! conection and 0 for succesful ping to the Domain name // //**************************************************************************** int PingTest(unsigned long ulDefaultGateway) { int iStatus = 0; SlPingStartCommand_t PingParams; SlPingReport_t PingReport; unsigned long ulIpAddr; char *pinghostname = "www.sina.com.cn"; // unsigned short pinghostname_len = strlen(pinghostname); // Set the ping parameters PingParams.PingIntervalTime = 100; PingParams.PingSize = 5; PingParams.PingRequestTimeout = 1000; PingParams.TotalNumberOfAttempts = 10; PingParams.Flags = 0;//?if flags parameter is set to 0, ping will report back once all requested pings are done PingParams.Ip = SL_AF_INET; // Fill the GW IP address, which is our AP address //PingParams.Ip = ulDefaultGateway; PingParams.Ip =SL_IPV4_VAL(192,168,1,1); // Check for LAN connection sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&PingReport, SimpleLinkPingReport); while (!g_uiPingDone) { //looping till ping is running Z_DelayS(1); UART_PRINT("i am waiting ping end now \n\r"); } g_uiPingDone = 0; if(g_uiPingPacketsRecv) { UART_PRINT("We were able to ping now \n\r"); g_uiPingPacketsRecv = 0; // We were able to ping the AP /* Check for Internet connection */ /* Querying for ti.com IP address */ iStatus = sl_NetAppDnsGetHostByName(pinghostname, strlen(pinghostname), &ulIpAddr, SL_AF_INET); if (iStatus < 0) { UART_PRINT("Problem with Internet connection \n\r"); // LAN connection is successful // Problem with Internet connection return -2; } // Replace the ping address to match ti.com IP address PingParams.Ip = ulIpAddr; UART_PRINT(pinghostname); UART_PRINT(" ip is : "); Z_IPDispaly(&ulIpAddr); return 0; ///not to ping www.ti.com // Try to ping www.ti.com sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&PingReport, SimpleLinkPingReport); while (!g_uiPingDone) { //looping till ping is running } if (g_uiPingPacketsRecv) { // LAN connection is successful // Internet connection is successful g_uiPingPacketsRecv = 0; return 0; } else { // LAN connection is successful // Problem with Internet connection return -2; } } else { // Problem with LAN connection UART_PRINT(" Problem with LAN connection \n\r"); return -1; } }
/*! * \brief Start the device in STA mode * * Following function configures the device to default state by cleaning * the persistent settings stored in NVMEM (viz. connection profiles & * policies, power policy etc) * * Applications may choose to skip this step if the developer is sure * that the device is in its default state at start of application * * Note that all profiles and persistent settings that were done on the * device will be lost */ _i32 startAP(void){ SlPingStartCommand_t PingParams = {0}; SlPingReport_t Report = {0}; _u8 SecType = 0; _i32 mode = ROLE_STA; _i32 retVal = configureSimpleLinkToDefaultState(); if(retVal < 0) { if (DEVICE_NOT_IN_STATION_MODE == retVal) CLI_Write((_u8 *)" Failed to configure the device in its default state \n\r"); LOOP_FOREVER(); } CLI_Write((_u8 *)" Device is configured in default state \n\r"); /* * Assumption is that the device is configured in station mode already * and it is in its default state */ mode = sl_Start(0, 0, 0); if (ROLE_AP == mode) { /* If the device is in AP mode, we need to wait for this event before doing anything */ while(!IS_IP_ACQUIRED(g_Status)) { _SlNonOsMainLoopTask(); } } else { /* Configure CC3100 to start in AP mode */ retVal = sl_WlanSetMode(ROLE_AP); if(retVal < 0) LOOP_FOREVER(); /* Configure the SSID of the CC3100 */ retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, pal_Strlen(SSID_AP_MODE), (_u8 *)SSID_AP_MODE); if(retVal < 0) LOOP_FOREVER(); SecType = SEC_TYPE_AP_MODE; /* Configure the Security parameter the AP mode */ retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, (_u8 *)&SecType); if(retVal < 0) LOOP_FOREVER(); retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, pal_Strlen(PASSWORD_AP_MODE), (_u8 *)PASSWORD_AP_MODE); if(retVal < 0) LOOP_FOREVER(); retVal = sl_Stop(SL_STOP_TIMEOUT); if(retVal < 0) LOOP_FOREVER(); CLR_STATUS_BIT(g_Status, STATUS_BIT_IP_ACQUIRED); mode = sl_Start(0, 0, 0); if (ROLE_AP == mode) { /* If the device is in AP mode, we need to wait for this event before doing anything */ while(!IS_IP_ACQUIRED(g_Status)) { _SlNonOsMainLoopTask(); } } else { CLI_Write((_u8 *)" Device couldn't be configured in AP mode \n\r"); LOOP_FOREVER(); } } CLI_Write((_u8 *)" Device started as Access Point\n\r"); /* Wait */ CLI_Write((_u8 *)" Waiting for clients to connect...!\n\r"); while((!IS_IP_LEASED(g_Status)) || (!IS_STA_CONNECTED(g_Status))) { _SlNonOsMainLoopTask(); } CLI_Write((_u8 *)" Client connected to the device \n\r"); CLI_Write((_u8 *)" Pinging...! \n\r"); /* Set the ping parameters */ PingParams.PingIntervalTime = PING_INTERVAL; PingParams.PingSize = PING_PKT_SIZE; PingParams.PingRequestTimeout = PING_TIMEOUT; PingParams.TotalNumberOfAttempts = NO_OF_ATTEMPTS; PingParams.Flags = 0; PingParams.Ip = g_StationIP; /* Fill the station IP address connected to CC3100 */ /* Ping client connected to CC3100 */ retVal = sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&Report, SimpleLinkPingReport); if(retVal < 0) LOOP_FOREVER(); /* Wait */ // while(!IS_PING_DONE(g_Status)) { _SlNonOsMainLoopTask(); } // // if (0 == g_PingPacketsRecv) // { // CLI_Write((_u8 *)" A STATION couldn't connect to the device \n\r"); // ASSERT_ON_ERROR(LAN_CONNECTION_FAILED); // } CLI_Write((_u8 *)" Device and the station are successfully connected \n\r"); return SUCCESS; }
/* * Application's entry point */ int main(int argc, char** argv) { SlPingStartCommand_t PingParams = {0}; SlPingReport_t Report = {0}; _u8 SecType = 0; _i32 mode = ROLE_STA; _i32 retVal = -1; retVal = initializeAppVariables(); ASSERT_ON_ERROR(retVal); /* Stop WDT and initialize the system-clock of the MCU */ stopWDT(); initClk(); /* Configure command line interface */ CLI_Configure(); displayBanner(); CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE); g_PingPacketsRecv = 0; /* * Following function configures the device to default state by cleaning * the persistent settings stored in NVMEM (viz. connection profiles & * policies, power policy etc) * * Applications may choose to skip this step if the developer is sure * that the device is in its default state at start of application * * Note that all profiles and persistent settings that were done on the * device will be lost */ retVal = configureSimpleLinkToDefaultState(); if(retVal < 0) { if (DEVICE_NOT_IN_STATION_MODE == retVal) CLI_Write((_u8 *)" Failed to configure the device in its default state \n\r"); LOOP_FOREVER(); } CLI_Write((_u8 *)" Device is configured in default state \n\r"); /* * Assumption is that the device is configured in station mode already * and it is in its default state */ mode = sl_Start(0, 0, 0); if (ROLE_AP == mode) { /* If the device is in AP mode, we need to wait for this event before doing anything */ while(!IS_IP_ACQUIRED(g_Status)) { _SlNonOsMainLoopTask(); } } else { /* Configure CC3100 to start in AP mode */ retVal = sl_WlanSetMode(ROLE_AP); if(retVal < 0) LOOP_FOREVER(); /* Configure the SSID of the CC3100 */ retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, pal_Strlen(SSID_AP_MODE), (_u8 *)SSID_AP_MODE); if(retVal < 0) LOOP_FOREVER(); SecType = SEC_TYPE_AP_MODE; /* Configure the Security parameter the AP mode */ retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, (_u8 *)&SecType); if(retVal < 0) LOOP_FOREVER(); retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, pal_Strlen(PASSWORD_AP_MODE), (_u8 *)PASSWORD_AP_MODE); if(retVal < 0) LOOP_FOREVER(); retVal = sl_Stop(SL_STOP_TIMEOUT); if(retVal < 0) LOOP_FOREVER(); CLR_STATUS_BIT(g_Status, STATUS_BIT_IP_ACQUIRED); mode = sl_Start(0, 0, 0); if (ROLE_AP == mode) { /* If the device is in AP mode, we need to wait for this event before doing anything */ while(!IS_IP_ACQUIRED(g_Status)) { _SlNonOsMainLoopTask(); } } else { CLI_Write((_u8 *)" Device couldn't be configured in AP mode \n\r"); LOOP_FOREVER(); } } CLI_Write((_u8 *)" Device started as Access Point\n\r"); while(1){ /* Wait */ CLI_Write((_u8 *)" Waiting for clients to connect...!\n\r"); while((!IS_IP_LEASED(g_Status)) || (!IS_STA_CONNECTED(g_Status))) { _SlNonOsMainLoopTask(); } CLI_Write((_u8 *)" Client connected to the device \n\r"); CLI_Write((_u8 *)" Pinging...! \n\r"); /* Set the ping parameters */ PingParams.PingIntervalTime = PING_INTERVAL; PingParams.PingSize = PING_SIZE; PingParams.PingRequestTimeout = PING_REQUEST_TIMEOUT; PingParams.TotalNumberOfAttempts = PING_ATTEMPT; PingParams.Flags = 0; PingParams.Ip = g_StationIP; /* Fill the station IP address connected to CC3100 */ /* Ping client connected to CC3100 */ retVal = sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&Report, SimpleLinkPingReport); if(retVal < 0) LOOP_FOREVER(); /* Wait */ while(!IS_PING_DONE(g_Status)) { _SlNonOsMainLoopTask(); } if (0 == g_PingPacketsRecv) { CLI_Write((_u8 *)" A STATION couldn't connect to the device \n\r"); ASSERT_ON_ERROR(LAN_CONNECTION_FAILED); } CLI_Write((_u8 *)" Device and the station are successfully connected \n\r"); } return SUCCESS; }