//***************************************************************************** //! \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; // Check for Internet connection lRetVal = sl_NetAppDnsGetHostByName(HOST_NAME, sizeof(HOST_NAME), &ulIpAddr, SL_AF_INET); ASSERT_ON_ERROR(__LINE__, 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(__LINE__, 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 return 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; }
//***************************************************************************** //! \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; }
/* * 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; }