Ejemplo n.º 1
0
/*!
    \brief Connecting to a WLAN Access point

    This function connects to the required AP (SSID_NAME).
    The function will return once we are connected and have acquired IP address

    \param[in]  None

    \return     0 on success, negative error-code on error

    \note

    \warning    If the WLAN connection fails or we don't acquire an IP address,
                We will be stuck in this function forever.
*/
static _i32 establishConnectionWithAP()
{
    SlSecParams_t secParams = {0};
    _i32 retVal = 0;

    secParams.Key = (_i8 *)PASSKEY;
    secParams.KeyLen = pal_Strlen(PASSKEY);
    secParams.Type = SEC_TYPE;

    retVal = sl_WlanConnect((_i8 *)SSID_NAME, pal_Strlen(SSID_NAME), 0, &secParams, 0);
    ASSERT_ON_ERROR(retVal);

    /* Wait */
    while((!IS_CONNECTED(g_Status)) || (!IS_IP_ACQUIRED(g_Status))) { _SlNonOsMainLoopTask(); }

    return SUCCESS;
}
Ejemplo n.º 2
0
/*!
    \brief Connecting to a WLAN Access point

    This function connects to the required AP (ENT_SSID_NAME).
    The function will return once we are connected and have acquired IP address

    \param[in]  None

    \return     0 on success, negative error-code on error

    \note

    \warning    If the WLAN connection fails or we don't acquire an IP address,
                We will be stuck in this function forever.
*/
static _i32 establishConnectionWithAP()
{
    SlSecParams_t secParams = {0};
    SlSecParamsExt_t eapParams = {0};

    _i32 retVal = -1;

    secParams.Key = ENT_PASS_KEY;
    secParams.KeyLen = pal_Strlen(ENT_PASS_KEY);
    secParams.Type = ENT_SEC_TYPE;

    eapParams.EapMethod = ENT_SEC_METHOD;
    eapParams.User = ENT_USER_NAME;
    eapParams.UserLen = pal_Strlen(ENT_USER_NAME);
    eapParams.AnonUserLen = 0;

    retVal = sl_WlanConnect(ENT_SSID_NAME, pal_Strlen(ENT_SSID_NAME), NULL, &secParams, &eapParams);
    ASSERT_ON_ERROR(retVal);

    /* Wait */
    while((!IS_CONNECTED(g_Status)) || (!IS_IP_ACQUIRED(g_Status))) { _SlNonOsMainLoopTask(); }

    return SUCCESS;
}
Ejemplo n.º 3
0
/*!
    \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;
}
Ejemplo n.º 4
0
/*
 * Application's entry point
 */
int main(int argc, char** argv)
{
    SlSecParams_t secParams = {0};
    _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();

    /*
     * 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(" Failed to configure the device in its default state \n\r");

        LOOP_FOREVER();
    }

    CLI_Write(" Device is configured in default state \n\r");

    /*
     * Initializing the CC3100 device
     * Assumption is that the device is configured in station mode already
     * and it is in its default state
     */
    retVal = sl_Start(0, 0, 0);
    if ((retVal < 0) ||
        (ROLE_STA != retVal) )
    {
        CLI_Write(" Failed to start the device \n\r");
        LOOP_FOREVER();
    }

    CLI_Write(" Device started as STATION \n\r");

    /* Delete all profiles (0xFF) stored and reset policy settings */
    retVal = sl_WlanProfileDel(0xFF);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }
    retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION ,
                     SL_CONNECTION_POLICY(0, 0, 0, 0, 0), 0, 0);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    /* Add unsecured AP profile with priority 6 (7 is highest) */
    retVal = sl_WlanProfileAdd((_i8 *)UNSEC_SSID_NAME,
                      pal_Strlen(UNSEC_SSID_NAME), g_BSSID, 0, 0, 6, 0);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    /* Add WPA2 secured AP profile with priority 7 (highest) */
    secParams.Type = SL_SEC_TYPE_WPA;
    secParams.Key = SEC_SSID_KEY;
    secParams.KeyLen = pal_Strlen(SEC_SSID_KEY);
    retVal = sl_WlanProfileAdd((_i8 *)SEC_SSID_NAME,
                      pal_Strlen(SEC_SSID_NAME), g_BSSID, &secParams, 0, 7, 0);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    /*
     * Enable auto connect (connection to stored profiles according to priority)
     * Connection should first be established to higher (secured) profile as in
     * this example
     */
    retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION ,
                     SL_CONNECTION_POLICY(1,0,0,0,0), 0, 0);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    /* Wait for the connection to be established */
    while((!IS_CONNECTED(g_Status)) || (!IS_IP_ACQUIRED(g_Status))) { _SlNonOsMainLoopTask(); }

    CLI_Write(" Device connected to the AP using 'auto' connection policy\n\r");

    /* Delete all profiles (0xFF) stored */
    retVal = sl_WlanProfileDel(0xFF);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    g_Status = 0;

    /* Restart the device */
    retVal = sl_Stop(SL_STOP_TIMEOUT);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    retVal = sl_Start(0, 0, 0);
    if ((retVal < 0) ||
        (ROLE_STA != retVal) )
    {
        CLI_Write(" Failed to start the device \n\r");
        LOOP_FOREVER();
    }

    /*
     * Set connection policy to Fast - Device will connect to the last connected AP.
     * This feature can be used to reconnect to AP
     */
    retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,
                     SL_CONNECTION_POLICY(1, 1, 0, 0, 0), 0, 0);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    /* Connect to the open AP */
    retVal = establishConnectionWithAP();
    if(retVal < 0)
    {
        CLI_Write(" Failed to establish connection w/ an AP \n\r");
        LOOP_FOREVER();
    }
    CLI_Write(" Device was connected to the AP using host-driver API \n\r");

    g_Status = 0; /* Status variable */

    /* Restart the Device */
    retVal = sl_Stop(SL_STOP_TIMEOUT);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    retVal = sl_Start(0, 0, 0);
    if ((retVal < 0) ||
        (ROLE_STA != retVal) )
    {
        CLI_Write(" Failed to start the device \n\r");
        LOOP_FOREVER();
    }

    /* Wait for the connection to be established */
    while((!IS_CONNECTED(g_Status)) || (!IS_IP_ACQUIRED(g_Status))) { _SlNonOsMainLoopTask(); }
    CLI_Write(" Device connected to the AP using 'fast' connection policy\n\r");

    /* Delete all profiles (0xFF) stored */
    retVal = sl_WlanProfileDel(0xFF);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    /* Set Auto SmartConfig policy */
    retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(1,0,0,0,1), 0, 0);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    /* Restart the Device */
    g_Status = 0;
    retVal = sl_Stop(SL_STOP_TIMEOUT);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    retVal = sl_Start(0, 0, 0);
    if ((retVal < 0) ||
        (ROLE_STA != retVal) )
    {
        CLI_Write(" Failed to start the device \n\r");
        LOOP_FOREVER();
    }

    /*
     * After restarting , device will wait for a command for 2 second and start
     * the SmartConfig process if no command is received
     */
    CLI_Write(" Device waiting to be connected using SmartConfig technology \n\r");
    while((!IS_CONNECTED(g_Status)) || (!IS_IP_ACQUIRED(g_Status))) { _SlNonOsMainLoopTask(); }

    CLI_Write(" Device connected to the AP \n\r");

    /*
     * Cleaning all profiles and setting the default connection policy before exiting the application
     * Set connection policy to Auto + SmartConfig (Device's default connection policy)
     */
    retVal = sl_WlanProfileDel(0xFF);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0);
    if(retVal < 0)
    {
        LOOP_FOREVER();
    }

    return 0;
}
Ejemplo n.º 5
0
Archivo: main.c Proyecto: bmxrt/CC3100
int main(int argc, char** argv)
{
    SlSockAddrIn_t      Addr = {0};

    _u32  cipher = SL_SEC_MASK_SSL_RSA_WITH_RC4_128_SHA;
    _u32  googleIP = 0;
    _u8   method = SL_SO_SEC_METHOD_SSLV3;

    _i32   AddrSize = -1;
    _i32   g_SockID = -1;
    _i32   retVal = -1;

    retVal = initializeAppVariables();
    ASSERT_ON_ERROR(retVal);

    /* Stop WDT and initialize the system-clock of the MCU
       These functions needs to be implemented in PAL */
    stopWDT();
    initClk();

    /* Configure command line interface */
    CLI_Configure();

    displayBanner();

    /*
     * 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(" Failed to configure the device in its default state \n\r");
        }

        LOOP_FOREVER();
    }

    CLI_Write(" 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
     */
    /* Initializing the CC3100 device */
    retVal = sl_Start(0, 0, 0);
    if ((retVal < 0) ||
            (ROLE_STA != retVal) )
    {
        CLI_Write(" Failed to start the device \n\r");
        LOOP_FOREVER();
    }

    CLI_Write(" Device started as STATION \n\r");

    /* Connecting to WLAN AP - Set with static parameters defined at the top
       After this call we will be connected and have IP address */
    retVal = establishConnectionWithAP();
    if(retVal < 0)
    {
        CLI_Write(" Failed to establish connection w/ an AP \n\r");
        LOOP_FOREVER();
    }

    CLI_Write(" Connection established w/ AP and IP is acquired \n\r");

    /* Update the CC3100 time */
    retVal = SetTime();
    if (retVal < 0)
    {
        CLI_Write(" Failed to set the device time \n\r");
        LOOP_FOREVER();
    }

    CLI_Write(" Establishing secure connection w/ google server \n\r");

    /* get the server name via a DNS request */
    retVal = sl_NetAppDnsGetHostByName(g_Google, pal_Strlen(g_Google),
                                       &googleIP, SL_AF_INET);
    if( retVal < 0 )
    {
        CLI_Write(" Failed to get the IP address \n\r");
        LOOP_FOREVER();
    }

    Addr.sin_family = SL_AF_INET;
    Addr.sin_port = sl_Htons(GOOGLE_DST_PORT);
    Addr.sin_addr.s_addr = sl_Htonl(googleIP);

    AddrSize = sizeof(SlSockAddrIn_t);

    /* opens a secure socket */
    g_SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, SL_SEC_SOCKET);
    if( g_SockID < 0 )
    {
        CLI_Write(" Failed to open socket \n\r");
        LOOP_FOREVER();
    }

    /* configure the socket as SSLV3.0 */
    retVal = sl_SetSockOpt(g_SockID, SL_SOL_SOCKET, SL_SO_SECMETHOD,
                           &method, sizeof(method));
    if( retVal < 0 )
    {
        CLI_Write(" Failed to configure the socket \n\r");
        LOOP_FOREVER();
    }

    /* configure the socket as RSA with RC4 128 SHA */
    retVal = sl_SetSockOpt(g_SockID, SL_SOL_SOCKET, SL_SO_SECURE_MASK,
                           &cipher, sizeof(cipher));
    if( retVal < 0 )
    {
        CLI_Write(" Failed to configure the socket \n\r");
        LOOP_FOREVER();
    }

    /* configure the socket with GOOGLE CA certificate-for server verification*/
    retVal = sl_SetSockOpt(g_SockID, SL_SOL_SOCKET, SL_SO_SECURE_FILES_CA_FILE_NAME,
                           SL_SSL_CA_CERT, pal_Strlen(SL_SSL_CA_CERT));
    if( retVal < 0 )
    {
        CLI_Write(" Failed to configure the socket \n\r");
        LOOP_FOREVER();
    }

    /* connect to the peer device - GMail server */
    retVal = sl_Connect(g_SockID, ( SlSockAddr_t *)&Addr, AddrSize);
    if (retVal < 0 )
    {
        CLI_Write(" Failed to connect w/ google server \n\r");
        LOOP_FOREVER();
    }

    CLI_Write(" Connection w/ google server established successfully \n\r");


    /* Stop the CC3100 device */
    retVal = sl_Stop(SL_STOP_TIMEOUT);
    if(retVal < 0)
        LOOP_FOREVER();

    return 0;
}
Ejemplo n.º 6
0
    /*!
     * \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;

}
Ejemplo n.º 7
0
/*
 * 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;
}
void HttpServer(){
	 _u8   SecType = 0;
	    _i32   retVal = -1;
	    _i32   mode = ROLE_STA;
	   /*
	     * 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(" Failed to configure the device in its default state \n\r");

	        LOOP_FOREVER();
	    }

	    CLI_Write(" 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(mode < 0)
	    {
	        LOOP_FOREVER();
	    }
	    else
	    {
	        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 AP mode without security */
	    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 in 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();

	    /* Restart the CC3100 */
	    retVal = sl_Stop(SL_STOP_TIMEOUT);
	    if(retVal < 0)
	        LOOP_FOREVER();

	    g_Status = 0;

	    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(" Device couldn't come in AP mode \n\r");
	        LOOP_FOREVER();
	    }

	    CLI_Write(" \r\n Device is configured in AP mode \n\r");

	    CLI_Write(" Waiting for client to connect\n\r");
	    /* wait for client to connect */
	    while((!IS_IP_LEASED(g_Status)) || (!IS_STA_CONNECTED(g_Status))) { _SlNonOsMainLoopTask(); }

	    CLI_Write(" Client connected\n\r");

	    /* Enable the HTTP Authentication */
	    retVal = set_authentication_check(TRUE);
	    if(retVal < 0)
	        LOOP_FOREVER();

	    /* Get authentication parameters */
	    retVal = get_auth_name(g_auth_name);
	    if(retVal < 0)
	        LOOP_FOREVER();

	    retVal = get_auth_password(g_auth_password);
	    if(retVal < 0)
	        LOOP_FOREVER();

	    retVal = get_auth_realm(g_auth_realm);
	    if(retVal < 0)
	        LOOP_FOREVER();

	    CLI_Write((_u8 *)"\r\n Authentication parameters: ");
	    CLI_Write((_u8 *)"\r\n Name = ");
	    CLI_Write(g_auth_name);
	    CLI_Write((_u8 *)"\r\n Password = "******"\r\n Realm = ");
	    CLI_Write(g_auth_realm);

	    /* Get the domain name */
	    retVal = get_domain_name(g_domain_name);
	    if(retVal < 0)
	        LOOP_FOREVER();

	    CLI_Write((_u8 *)"\r\n\r\n Domain name = ");
	    CLI_Write(g_domain_name);

	    /* Get URN */
	    retVal = get_device_urn(g_device_urn);
	    if(retVal < 0)
	        LOOP_FOREVER();

	    CLI_Write((_u8 *)"\r\n Device URN = ");
	    CLI_Write(g_device_urn);
	    CLI_Write((_u8 *)"\r\n");

	    /* Process the async events from the NWP */
	    while(1)
	    {
	        Semaphore_pend(Semaphore_CC3100_Req, BIOS_WAIT_FOREVER);
	        _SlNonOsMainLoopTask();
	    }

}
/*!
    \brief This function handles callback for the HTTP server events

    \param[in]      pEvent - Contains the relevant event information
    \param[in]      pResponse - Should be filled by the user with the
                    relevant response information

    \return         None

    \note

    \warning
*/
void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *pEvent,
                                  SlHttpServerResponse_t *pResponse)
{
    unsigned int ulSpeed, n;

    if(pEvent == NULL || pResponse == NULL)
        CLI_Write(" [HTTP EVENT] NULL Pointer Error \n\r");

    switch (pEvent->Event)
    {
        case SL_NETAPP_HTTPGETTOKENVALUE_EVENT:
        {
            _u8 status = 0;
            _u8 *ptr = 0;

            ptr = pResponse->ResponseData.token_value.data;
            pResponse->ResponseData.token_value.len = 0;

  /* Check if the token is for the original CC3100 demo    */
            if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token,
                                         pal_Strlen(GET_token)) == 0)
            {
                //status = GetLEDStatus();
                pal_Memcpy(ptr, "LED1_", pal_Strlen("LED1_"));
                ptr += 5;
                pResponse->ResponseData.token_value.len += 5;
                if(status & 0x01)
                {
                    pal_Memcpy(ptr, "ON", pal_Strlen("ON"));
                    ptr += 2;
                    pResponse->ResponseData.token_value.len += 2;
                }
                else
                {
                    pal_Memcpy(ptr, "OFF", pal_Strlen("OFF"));
                    ptr += 3;
                    pResponse->ResponseData.token_value.len += 3;
                }
                pal_Memcpy(ptr,",LED2_", pal_Strlen(",LED2_"));
                ptr += 6;
                pResponse->ResponseData.token_value.len += 6;
                if(status & 0x02)
                {
                    pal_Memcpy(ptr, "ON", pal_Strlen("ON"));
                    ptr += 2;
                    pResponse->ResponseData.token_value.len += 2;
                }
                else
                {
                    pal_Memcpy(ptr, "OFF", pal_Strlen("OFF"));
                    ptr += 3;
                    pResponse->ResponseData.token_value.len += 3;
                }

                *ptr = '\0';
            }


/* Check if the token is for the added MCU LED toggle demo    */

/* check if the get LED on event, return the LED on status  */
           if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token0,
                                                    pal_Strlen(GET_token0)) == 0)
           {
        	   if(g_ui32LED_Status)
        	   {
        		   pal_Memcpy(ptr, "checked", pal_Strlen("checked")); /* return the on status to update the "LED ON" button */
        		   ptr += 7;
        		   pResponse->ResponseData.token_value.len += 7;
        	   }
        	   else
        	   {
        		   pal_Memcpy(ptr, " ", pal_Strlen(" "));            /* do not update the "LED ON" button */
        	 	   ptr += 1;
        	       pResponse->ResponseData.token_value.len += 1;
        	   }
 			   *ptr = '\0';
           }

 /* check if the get LED off event, return the LED off status */
           else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token1,
							   pal_Strlen(GET_token1)) == 0)
		  {
			   if(!g_ui32LED_Status)
			   {
				   pal_Memcpy(ptr, "checked", pal_Strlen("checked")); /* return the off status to update the "LED OFF" button */
				   ptr += 7;
				   pResponse->ResponseData.token_value.len += 7;
			   }
			   else
			   {
				   pal_Memcpy(ptr, " ", pal_Strlen(" "));            /* do not update the "LED OFF" button */
				   ptr += 1;
				   pResponse->ResponseData.token_value.len += 1;
			   }
		  	   *ptr = '\0';
		  }

/* check if the get LED toggle on event */
           else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token2,
		  		 							   pal_Strlen(GET_token2)) == 0)
		  {
			   if(g_ui32LED_Toggle_Status)
			   {
				   pal_Memcpy(ptr, "checked", pal_Strlen("checked"));    /* return the on status to update the "LED toggle on" button */
				   ptr += 7;
				   pResponse->ResponseData.token_value.len += 7;
			   }
			   else
			   {
				   pal_Memcpy(ptr, " ", pal_Strlen(" "));               /* do not update the "LED toggle on" button */
				   ptr += 1;
				   pResponse->ResponseData.token_value.len += 1;
			   }
			   *ptr = '\0';
		  }

/* check if the get LED toggle off event */
           else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token3,
		 							   pal_Strlen(GET_token3)) == 0)
		  {
			   if(!g_ui32LED_Toggle_Status)
			   {
				   pal_Memcpy(ptr, "checked", pal_Strlen("checked"));   /* return the off status to update the "LED toggle off" button */
				   ptr += 7;
				   pResponse->ResponseData.token_value.len += 7;
			   }
			   else
			   {
				   pal_Memcpy(ptr, " ", pal_Strlen(" "));                /* do not update the "LED toggle off" button */
				   ptr += 1;
				   pResponse->ResponseData.token_value.len += 1;
			   }
			   *ptr = '\0';
		  }

/* check if the get toggle speed event  */
           else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token4,
										   pal_Strlen(GET_token4)) == 0)
		  {
			   n = Decimal2String(ptr, g_ui32AnimSpeed); /* write speed to the buffer and update the string length */
			   ptr += n;
			   pResponse->ResponseData.token_value.len += n;
			   *ptr = '\0';
		  }

/* check if the get button 1 count event */
           else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token5,
											   pal_Strlen(GET_token5)) == 0)
		  {
			   n = Decimal2String(ptr, g_ui32SW1Presses);  /* write number of button press to the buffer and update the string length */
			   ptr += n;
			   pResponse->ResponseData.token_value.len += n;
			   *ptr = '\0';
		  }

/* check if the get button 2 count event */
           else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token6,
		  		  							   pal_Strlen(GET_token6)) == 0)
		  {
			   n = Decimal2String(ptr, g_ui32SW2Presses);  /* write number of button press to the buffer and update the string length */
  			   ptr += n;
  			   pResponse->ResponseData.token_value.len += n;
  			   *ptr = '\0';
  		  }


/* check if the get temperature event  */
           else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token7,
		  							   pal_Strlen(GET_token7)) == 0)
		  {
			   if(!g_ui32Temperature_U)
			   {
				   n = Decimal2String(ptr, g_ui32InternalTempC); /* write temperature in C to the buffer and update the string length */
			   }
			   else
			   {
				   n = Decimal2String(ptr, g_ui32InternalTempF); /* write temperature in F to the buffer and update the string length */
			   }
			   ptr += n;
			   pResponse->ResponseData.token_value.len += n;
			   *ptr = '\0';
		  }

/* check if the temperature in C event */
           else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token8,
										  pal_Strlen(GET_token8)) == 0)
		 {
			   if(!g_ui32Temperature_U)
			   {
				   pal_Memcpy(ptr, "checked", pal_Strlen("checked")); /* update the temperature in C button and update the string length */
				   ptr += 7;
				   pResponse->ResponseData.token_value.len += 7;
			   }
			   else
			   {
				   pal_Memcpy(ptr, " ", pal_Strlen(" "));
				   ptr += 1;
				   pResponse->ResponseData.token_value.len += 1;
			   }
			   *ptr = '\0';
		 }

/* check if the temperature in F event */
           else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token9,
														  pal_Strlen(GET_token9)) == 0)
		 {
			   if(g_ui32Temperature_U)
			   {
				   pal_Memcpy(ptr, "checked", pal_Strlen("checked")); /* update the temperature in F button and update the string length */
				   ptr += 7;
				   pResponse->ResponseData.token_value.len += 7;
			   }
			   else
			   {
				   pal_Memcpy(ptr, " ", pal_Strlen(" "));
				   ptr += 1;
				   pResponse->ResponseData.token_value.len += 1;
			   }
			   *ptr = '\0';
		 }

        }
        break;

        case SL_NETAPP_HTTPPOSTTOKENVALUE_EVENT:
        {
            _u8 led = 0;
            _u8 *ptr = pEvent->EventData.httpPostData.token_name.data;

/* Check if the post command is for original CC310 demo  */
            if(pal_Memcmp(ptr, POST_token, pal_Strlen(POST_token)) == 0)
            {
                ptr = pEvent->EventData.httpPostData.token_value.data;
                if(pal_Memcmp(ptr, "LED", 3) != 0)
                    break;

                ptr += 3;
                led = *ptr;
                ptr += 2;
                if(led == '1')
                {
                    if(pal_Memcmp(ptr, "ON", 2) == 0)
                    {
                        turnLedOn(LED1);
                    }
                    else
                    {
                        turnLedOff(LED1);
                    }
                }
                else if(led == '2')
                {
                    if(pal_Memcmp(ptr, "ON", 2) == 0)
                    {
                        turnLedOn(LED2);
                    }
                    else
                    {
                        turnLedOff(LED2);
                    }
                }
            }

/* Check if the post command is for add MCU demo: LED on  */
			if(pal_Memcmp(ptr, POST_token0, pal_Strlen(POST_token0)) == 0)
			{
				g_ui32LED_Status =1;                           /* turn the LED on */
				turnLedOn(LED1);
				if(g_ui32LED_Toggle_Status)            /* reenable LED toggling */
				{
					IoSetTimer(g_ui32AnimSpeed);
				}
			}

/* Check if the post command is for add MCU demo: LED off  */
			else if(pal_Memcmp(ptr, POST_token1, pal_Strlen(POST_token1)) == 0)
			{
				g_ui32LED_Status =0;
				turnLedOff(LED1);
				if(g_ui32LED_Toggle_Status)
				{
					IoSetTimer(0);         /*reenable LED toggling */
				}
			}

/* Check if the post command is for add MCU demo: LED toggle on  */
			else if(pal_Memcmp(ptr, POST_token2, pal_Strlen(POST_token2)) == 0)
			{
				g_ui32LED_Toggle_Status = 1;
				if(g_ui32LED_Status)
				{
					IoSetTimer(g_ui32AnimSpeed);  /* enable LED toggling  */
				}
			}

/* Check if the post command is for add MCU demo: LED toggle off  */
			else if(pal_Memcmp(ptr, POST_token3, pal_Strlen(POST_token3)) == 0)
			{
				g_ui32LED_Toggle_Status = 0;
				IoSetTimer(0);               /* disable toggling */
				if(g_ui32LED_Status)
				{
					turnLedOn(LED1);
				}
				else
				{
					turnLedOff(LED1);
				}
			}

/* Check if the post command is for add MCU demo: set LED toggling speed  */
			else if(pal_Memcmp(ptr, POST_token4, pal_Strlen(POST_token4)) == 0)
			{
				ptr = pEvent->EventData.httpPostData.token_value.data;
				ulSpeed = String2Decimal(ptr);  /* get speed value from string */
				g_ui32AnimSpeed = ulSpeed;
				if (g_ui32LED_Toggle_Status && g_ui32LED_Status)
				{
					IoSetTimer(ulSpeed);
				}
			}

/* Check if the post command is for add MCU demo: Clear button 1 count  */
			else if(pal_Memcmp(ptr, POST_token5, pal_Strlen(POST_token5)) == 0)
			{
				g_ui32SW1Presses = 0;
			}

/* Check if the post command is for add MCU demo: Clear button 2 count  */
			else if(pal_Memcmp(ptr, POST_token6, pal_Strlen(POST_token6)) == 0)
			{
				g_ui32SW2Presses = 0;
			}

/* Check if the post command is for add MCU demo: temperature in C */
			else if(pal_Memcmp(ptr, POST_token8, pal_Strlen(POST_token8)) == 0)
			{
				g_ui32Temperature_U = 0;
			}

/* Check if the post command is for add MCU demo: temperature in F */
			else if(pal_Memcmp(ptr, POST_token9, pal_Strlen(POST_token9)) == 0)
			{
				g_ui32Temperature_U = 1;
			}

        }
        break;

        default:
        break;
    }
}