Ejemplo n.º 1
0
Archivo: main.c Proyecto: dlugaz/All
//****************************************************************************
//
//! Task function implementing the UDP client and showcasing the hibernate 
//! functionality
//!
//! \param none
//! 
//! This function  
//!    1. Creates a UDP socket
//!    2. Broadcasts a packet on the socket
//!    3. Closes the socket
//!    4. Enters the HIBernate mode
//!
//! \return None.
//
//****************************************************************************
void HIBUDPBroadcastTask(void *pvParameters)
{
    int iSocketDesc;
    long lRetVal;
    struct sockaddr_in sBroadcastAddr;
    char pcBroadcastMessage[]="32xx HIB example application";

    //
    // Check the wakeup source. If first itme entry or wakeup from HIB
    //
    if(MAP_PRCMSysResetCauseGet() == 0)
    {
        DisplayBanner(APPNAME);
        DBG_PRINT("HIB: Wake up on Power ON\n\r");
    }
    else if(MAP_PRCMSysResetCauseGet() == PRCM_HIB_EXIT)
    {
        DBG_PRINT("HIB: Woken up from Hibernate\n\r");
    }
    else
    {

    }
    GPIO_IF_LedConfigure(LED1);

    GPIO_IF_LedOff(MCU_RED_LED_GPIO);


    //
    // Configure Timer for blinking the LED for IP acquisition
    //
    LedTimerConfigNStart();

    //
    //
    // Reset The state of the machine
    //
    Network_IF_ResetMCUStateMachine();

    //
    // Start the driver
    //
    lRetVal = Network_IF_InitDriver(ROLE_STA);
    if(lRetVal < 0)
    {
       UART_PRINT("Failed to start SimpleLink Device\n\r");
       LOOP_FOREVER();
    }

    // Initialize AP security params
    SecurityParams.Key = (signed char*)SECURITY_KEY;
    SecurityParams.KeyLen = strlen(SECURITY_KEY);
    SecurityParams.Type = SECURITY_TYPE;

    //
    // Connect to the Access Point
    //
    lRetVal = Network_IF_ConnectAP(SSID_NAME, SecurityParams);
    if(lRetVal < 0)
    {
       UART_PRINT("Connection to AP failed\n\r",lRetVal);
       LOOP_FOREVER();
    }

    //
    // Disable the LED blinking Timer as Device is connected to AP
    //
    LedTimerDeinitStop();

    //
    // Switch ON RED LED to indicate that Device acquired an IP
    //
    GPIO_IF_LedOn(MCU_IP_ALLOC_IND);

    //
    // Create UDP socket
    //
    iSocketDesc = sl_Socket(AF_INET, SOCK_DGRAM, 0);
    if(iSocketDesc < 0)
    {
        DBG_PRINT("HIB: Socket create failed\n\r");
        goto end;
    }
    DBG_PRINT("HIB: Socket created\n\r");

    //
    // Assign socket structure values for a braodcast message
    //
    sBroadcastAddr.sin_family = AF_INET;
    sBroadcastAddr.sin_addr.s_addr= sl_Htonl(0xFFFFFFFF);
    sBroadcastAddr.sin_port= sl_Htons(APP_UDP_PORT);

    //
    // Broadcast message
    //
    lRetVal = sendto(iSocketDesc, (char *)&pcBroadcastMessage[0],
                        sizeof(pcBroadcastMessage), 0,
                     (struct sockaddr *)&sBroadcastAddr,sizeof(sBroadcastAddr));
    if(lRetVal < 0)
    {
        ERR_PRINT(lRetVal);
        LOOP_FOREVER();
    }

    UNUSED(lRetVal);
    DBG_PRINT("HIB: sent message\n\r");

    //
    // Close the socket
    //
    close(iSocketDesc);
    DBG_PRINT("HIB: Socket closed\n\r");

    //
    // Stop the driver
    //
    lRetVal = Network_IF_DeInitDriver();
    if(lRetVal < 0)
    {
       UART_PRINT("Failed to stop SimpleLink Device\n\r");
       LOOP_FOREVER();
    }

    //
    // Switch Off RED & Green LEDs to indicate that Device is
    // disconnected from AP and Simplelink is shutdown
    //
    GPIO_IF_LedOff(MCU_IP_ALLOC_IND);
    GPIO_IF_LedOff(MCU_GREEN_LED_GPIO);

    //
    // Enter HIB here configuring the wakeup-timer
    //
    EnterHIBernate();
end:
    DBG_PRINT("HIB: Test Complete\n\r");

    //
    // Loop here
    //
    LOOP_FOREVER();
}
Ejemplo n.º 2
0
//****************************************************************************
//
//! Task function implementing the TCP server and showcasing the  
//! deepsleep functionality
//!
//! \param none
//! 
//! This function  
//!    1. Creates a TCP socket and binds to it
//!    2. Listens on the socket
//!    3. Accepts a client connection
//!    4. Receives a packet on the socket
//!    5. Closes the socket
//!
//! \return None.
//
//****************************************************************************
void TCPServerTask(void *pvParameters)
{
    int iSocketDesc, iClientFD;
    long lRetVal = -1;
    struct sockaddr_in sClientAddr;
    int iRecvLen;
    unsigned short usClientAddrLen = sizeof(sClientAddr);
    unsigned char aucRecvBuffer[MAX_BUF];

    DisplayBanner(APPLICATION_NAME);

    DBG_PRINT("DEEPSLEEP: Test Begin\n\r");

    //
    // GPIO Configuration
    //
    GPIO_IF_LedConfigure(LED1|LED3);

    GPIO_IF_LedOff(MCU_RED_LED_GPIO);
    GPIO_IF_LedOff(MCU_GREEN_LED_GPIO);    

    //
    // Start the driver
    //
    lRetVal = Network_IF_InitDriver(ROLE_STA);
    if(lRetVal < 0)
    {
       UART_PRINT("Failed to start SimpleLink Device\n\r");
       LOOP_FOREVER();
    }    

    GPIO_IF_LedOn(MCU_GREEN_LED_GPIO);

    LedTimerConfigNStart();

    // Initialize AP security params
    SecurityParams.Key = (signed char*)SECURITY_KEY;
    SecurityParams.KeyLen = strlen(SECURITY_KEY);
    SecurityParams.Type = SECURITY_TYPE;

    //
    // Connect to the Access Point
    //
    lRetVal = Network_IF_ConnectAP(SSID_NAME,SecurityParams);
    if(lRetVal < 0)
    {
       Report("Connection to AP failed\n\r");
       GPIO_IF_LedOff(MCU_IP_ALLOC_IND);
       LOOP_FOREVER();
    }

    LedTimerDeinitStop();

    //
    // Switch ON RED LED to indicate that Device acquired an IP
    //
    GPIO_IF_LedOn(MCU_IP_ALLOC_IND);

    //
    // Create and Bind TCP server socket
    //
    iSocketDesc = CreateTCPServerSocket(APP_TCP_PORT);
    if(iSocketDesc < 0)
    {
        DBG_PRINT("DEEPSLEEP: Socket create failed\n");
        goto end;
    }
    DBG_PRINT("DEEPSLEEP: Socket created\n\r");
    DBG_PRINT("DEEPSLEEP: Listening on Socket...\n\r");

    //
    // Start Listening on the socket
    //
    if( listen(iSocketDesc, 5) != 0)
    {
        DBG_PRINT("DEEPSLEEP: Listen failed\n\r");
        goto end;
    }

    while(FOREVER)
    {
        //
        // Accept new client connections (Here just one)
        //
        DBG_PRINT("DEEPSLEEP: Waiting for client.......\n\r");
        iClientFD = accept(iSocketDesc, 
                       (struct sockaddr*)&sClientAddr, 
                       &usClientAddrLen);
        if(iClientFD < 0)
        {
            ERR_PRINT(iClientFD);
            break;
        }

        DBG_PRINT("DEEPSLEEP: Connected to client [0x%x] \n\r", 
                           htonl(sClientAddr.sin_addr.s_addr));

        do
        {
            //
            // Receive on the socket
            //
            iRecvLen = recv(iClientFD,aucRecvBuffer,MAX_BUF,0);

            if(iRecvLen > 0)
            {
                DBG_PRINT("DEEPSLEEP: received %d bytes\n\r", iRecvLen);
                DBG_PRINT("DEEPSLEEP: received message : %s\n\r", aucRecvBuffer);
            }
            else
            {
                ERR_PRINT(iRecvLen);
                break;
            }
        }while(iRecvLen > 0);

        //
        // Close the client descriptor
        //
        close(iClientFD);
    }



end:

    //
    // Close the socket. Commented as not expected to reach here
    //
    close(iSocketDesc);

    //
    // Stop the driver
    //
    lRetVal = Network_IF_DeInitDriver();
    if(lRetVal < 0)
    {
       UART_PRINT("Failed to stop SimpleLink Device\n\r");
       LOOP_FOREVER();
    }

    //
    // Switch Off RED & Green LEDs to indicate that Device is
    // disconnected from AP and Simplelink is shutdown
    //
    GPIO_IF_LedOff(MCU_IP_ALLOC_IND);
    GPIO_IF_LedOff(MCU_GREEN_LED_GPIO);

    DBG_PRINT("DEEPSLEEP: Test Complete\n\r");

    //
    // Loop here
    //
    LOOP_FOREVER();
}
Ejemplo n.º 3
0
Archivo: main.c Proyecto: nqd/cc3200
//****************************************************************************
//
//! Task function implementing the gettime functionality using an NTP server
//!
//! \param none
//!
//! This function
//!    1. Initializes the required peripherals
//!    2. Initializes network driver and connects to the default AP
//!    3. Creates a UDP socket, gets the NTP server IP address using DNS
//!    4. Periodically gets the NTP time and displays the time
//!
//! \return None.
//
//****************************************************************************
void GetNTPTimeTask(void *pvParameters)
{
    int iSocketDesc;
    long lRetVal = -1;

    UART_PRINT("GET_TIME: Test Begin\n\r");

    //
    // Configure LED
    //
    GPIO_IF_LedConfigure(LED1|LED3);

    GPIO_IF_LedOff(MCU_RED_LED_GPIO);
    GPIO_IF_LedOff(MCU_GREEN_LED_GPIO);    


    //
    // Reset The state of the machine
    //
    Network_IF_ResetMCUStateMachine();

    //
    // Start the driver
    //
    lRetVal = Network_IF_InitDriver(ROLE_STA);
    if(lRetVal < 0)
    {
       UART_PRINT("Failed to start SimpleLink Device\n\r",lRetVal);
       LOOP_FOREVER();
    }

    // switch on Green LED to indicate Simplelink is properly up
    GPIO_IF_LedOn(MCU_ON_IND);

    // Start Timer to blink Red LED till AP connection
    LedTimerConfigNStart();

    // Initialize AP security params
    SecurityParams.Key = (signed char *)SECURITY_KEY;
    SecurityParams.KeyLen = strlen(SECURITY_KEY);
    SecurityParams.Type = SECURITY_TYPE;

    //
    // Connect to the Access Point
    //
    lRetVal = Network_IF_ConnectAP(SSID_NAME, SecurityParams);
    if(lRetVal < 0)
    {
       UART_PRINT("Connection to an AP failed\n\r");
       LOOP_FOREVER();
    }

    //
    // Disable the LED blinking Timer as Device is connected to AP
    //
    LedTimerDeinitStop();

    //
    // Switch ON RED LED to indicate that Device acquired an IP
    //
    GPIO_IF_LedOn(MCU_IP_ALLOC_IND);

    //
    // Create UDP socket
    //
    iSocketDesc = sl_Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(iSocketDesc < 0)
    {
        ERR_PRINT(iSocketDesc);
        goto end;
    }
    g_sAppData.iSockID = iSocketDesc;

    UART_PRINT("Socket created\n\r");

    //
    // Get the NTP server host IP address using the DNS lookup
    //
    lRetVal = Network_IF_GetHostIP((char*)g_acSNTPserver, \
                                    &g_sAppData.ulDestinationIP);
    
    if( lRetVal >= 0)
    {
    
        struct SlTimeval_t timeVal;
        timeVal.tv_sec =  SERVER_RESPONSE_TIMEOUT;    // Seconds
        timeVal.tv_usec = 0;     // Microseconds. 10000 microseconds resolution
        lRetVal = sl_SetSockOpt(g_sAppData.iSockID,SL_SOL_SOCKET,SL_SO_RCVTIMEO,\
                        (unsigned char*)&timeVal, sizeof(timeVal)); 
        if(lRetVal < 0)
        {
           ERR_PRINT(lRetVal);
           LOOP_FOREVER();
        }
        
        while(1)
        {
            //
            // Get the NTP time and display the time
            //
            lRetVal = GetSNTPTime(GMT_DIFF_TIME_HRS, GMT_DIFF_TIME_MINS);
            if(lRetVal < 0)
            {
                UART_PRINT("Server Get Time failed\n\r");
                break;                
            }

            //
            // Wait a while before resuming
            //
            MAP_UtilsDelay(SLEEP_TIME);
        }
    }
    else
    {
        UART_PRINT("DNS lookup failed. \n\r");
    }

    //
    // Close the socket
    //
    close(iSocketDesc);
    UART_PRINT("Socket closed\n\r");

end:
    //
    // Stop the driver
    //
    lRetVal = Network_IF_DeInitDriver();
    if(lRetVal < 0)
    {
       UART_PRINT("Failed to stop SimpleLink Device\n\r");
       LOOP_FOREVER();
    }

    //
    // Switch Off RED & Green LEDs to indicate that Device is
    // disconnected from AP and Simplelink is shutdown
    //
    GPIO_IF_LedOff(MCU_IP_ALLOC_IND);
    GPIO_IF_LedOff(MCU_GREEN_LED_GPIO);

    UART_PRINT("GET_TIME: Test Complete\n\r");

    //
    // Loop here
    //
    LOOP_FOREVER();
}