//***************************************************************************** // //! \brief Get the NTP time //! //! \param none //! //! \return void //! \note //! \warning // //***************************************************************************** uint8_t GetTime(struct tm * decodedTime) { const char * g_acSNTPserver = "wwv.nist.gov"; //Add any one of the above servers unsigned long ulDestinationIP; int iSocketDesc; long lRetVal; char cDataBuf[48]; int iAddrSize; SlSockAddr_t sAddr; SlSockAddrIn_t sLocalAddr; // // Create UDP socket // iSocketDesc = sl_Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(iSocketDesc < 0) { ERR_PRINT(iSocketDesc); return 0; } //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, &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(iSocketDesc,SL_SOL_SOCKET,SL_SO_RCVTIMEO,\ (unsigned char*)&timeVal, sizeof(timeVal)); if(lRetVal < 0) { ERR_PRINT(lRetVal); // // Close the socket // close(iSocketDesc); UART_PRINT("Socket closed\n\r"); return 0; } // // Send a query ? to the NTP server to get the NTP time // memset(cDataBuf, 0, sizeof(cDataBuf)); cDataBuf[0] = '\x1b'; sAddr.sa_family = AF_INET; // the source port sAddr.sa_data[0] = 0x00; sAddr.sa_data[1] = 0x7B; // UDP port number for NTP is 123 sAddr.sa_data[2] = (char)((ulDestinationIP>>24)&0xff); sAddr.sa_data[3] = (char)((ulDestinationIP>>16)&0xff); sAddr.sa_data[4] = (char)((ulDestinationIP>>8)&0xff); sAddr.sa_data[5] = (char)(ulDestinationIP&0xff); lRetVal = sl_SendTo(iSocketDesc, cDataBuf, sizeof(cDataBuf), 0, &sAddr, sizeof(sAddr)); if (lRetVal != sizeof(cDataBuf)) { // could not send SNTP request //ASSERT_ON_ERROR(SERVER_GET_TIME_FAILED); return 0; } // // Wait to receive the NTP time from the server // sLocalAddr.sin_family = SL_AF_INET; sLocalAddr.sin_port = 0; sLocalAddr.sin_addr.s_addr = 0; lRetVal = sl_Bind(iSocketDesc, (SlSockAddr_t *)&sLocalAddr, sizeof(SlSockAddrIn_t)); iAddrSize = sizeof(SlSockAddrIn_t); lRetVal = sl_RecvFrom(iSocketDesc, cDataBuf, sizeof(cDataBuf), 0, (SlSockAddr_t *)&sLocalAddr, (SlSocklen_t*)&iAddrSize); //ASSERT_ON_ERROR(lRetVal); if(lRetVal < 0) { UART_PRINT("Server Get Time failed\n\r"); // // Close the socket // close(iSocketDesc); UART_PRINT("Socket closed\n\r"); return 0; } // We received the time DecodeSNTPTime(cDataBuf, decodedTime); } else {
//***************************************************************************** // //! UARTCommandHandler //! //! @brief The function handles commands arrived from CLI //! //! @param usBuffer is the receive buffer from the UART interface to PC //! //! @return 0 on success or error code on failure //! // //***************************************************************************** long UARTCommandHandler(char *usBuffer) { int iIndex = 0; int iParamcount = 0; long lRetVal = -1; signed char cStatus1 = 0; signed char cStatus2 = 0; if(usBuffer == NULL) { UART_PRINT("Null pointer\r\n"); return -1; } switch(usBuffer[1]) { //********************************************** // Case 01: Connect to default AP //********************************************** case UART_COMMAND_SIMPLE_CONFIG_START: if(!IS_CONNECTED(Network_IF_CurrentMCUState())) { LedTimerConfigNStart(); // Setting Acess Point's security parameters SecurityParams.Key = (signed char *)SECURITY_KEY; SecurityParams.KeyLen = strlen(SECURITY_KEY); SecurityParams.Type = SECURITY_TYPE; lRetVal = Network_IF_ConnectAP(SSID_NAME,SecurityParams); if(lRetVal < 0) { UART_PRINT("Error: %d Connecting to an AP.", lRetVal); return lRetVal; } } // // 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); DispatcherUartSendPacket((char*)pucUARTOKString, sizeof(pucUARTOKString)); break; //********************************************** // Case 02: Configure sender (source) email //********************************************** // TODO Phase 2: Include /* case UART_COMMAND_EMAIL_SOURCE: memset(serveruser,0,sizeof(serveruser)); memset(pcServerpass,0,sizeof(pcServerpass)); pcOfserveruser = &serveruser[0]; ofpcServerpass = &pcServerpass[0]; // '<' To maintain RFC 2821 format *pcOfserveruser++= '<'; iIndex = 2; while ((int)usBuffer[iIndex] != 0x0D) { //look for comma ',' for separation of params if((int)usBuffer[iIndex] == 44) { iParamcount++; } else { if(iParamcount==1) { //Enter smtp server username (email address) *pcOfserveruser++ = usBuffer[iIndex]; } if(iParamcount==2) { //Enter username's password *ofpcServerpass++ = usBuffer[iIndex]; } } iIndex++; } // '>' To maintain RFC 2821 format *pcOfserveruser++= '>'; *pcOfserveruser++= '\0'; *ofpcServerpass++= '\0'; //Set variables in smtp.c cStatus1 = smtpSetVariable(serveruser, USERNAME_VAR); cStatus2 = smtpSetVariable(pcServerpass, PASSWORD_VAR); //If error in return if(cStatus1 == 0 && cStatus2 == 0) { DispatcherUartSendPacket((char*)pucUARTOKString, sizeof(pucUARTOKString)); } else { DispatcherUartSendPacket((char*)putUARTErrorInputString, \ sizeof(putUARTErrorInputString)); } break; */ //********************************************** // Case 03: Configure sender (source) email //********************************************** case UART_COMMAND_EMAIL_HEADER: memset(pcEmailto,0,sizeof(pcEmailto)); pcOfemailto = &pcEmailto[0]; pcOfemailsubject = &pcEmailsubject[0]; // '<' To maintain RFC 2821 format *pcOfemailto++= '<'; iIndex = 2; while ((int)usBuffer[iIndex] != 0x0D && usBuffer[iIndex] != '\0') { //look for comma ',' for separation of params if((int)usBuffer[iIndex] == 44) { iParamcount++; } else { if(iParamcount==1) { //Enter destination email address *pcOfemailto++ = usBuffer[iIndex]; } if(iParamcount==2) { //Enter email subject *pcOfemailsubject++ = usBuffer[iIndex]; } } iIndex++; } // '>' To maintain RFC 2821 format *pcOfemailto++= '>'; *pcOfemailto++= '\0'; *pcOfemailsubject= '\0'; SlNetAppDestination_t destEmailAdd; memcpy(destEmailAdd.Email,pcEmailto,strlen(pcEmailto)+1); cStatus1 = sl_NetAppEmailSet(SL_NET_APP_EMAIL_ID,NETAPP_DEST_EMAIL, \ strlen(pcEmailto)+1, \ (unsigned char *)&destEmailAdd); SlNetAppEmailSubject_t emailSubject; memcpy(emailSubject.Value,pcEmailsubject,strlen(pcEmailsubject)+1); cStatus2 = sl_NetAppEmailSet(SL_NET_APP_EMAIL_ID,NETAPP_SUBJECT, \ strlen(pcEmailsubject)+1, \ (unsigned char *)&emailSubject); // Check for Error in setting the variables if(cStatus1 == 0 && cStatus2 == 0) { DispatcherUartSendPacket((char*)pucUARTOKString, \ sizeof(pucUARTOKString)); } else { DispatcherUartSendPacket((char*)putUARTErrorInputString, \ sizeof(putUARTErrorInputString)); } break; //********************************************** // Case 04: Record email message //********************************************** case UART_COMMAND_EMAIL_MESSAGE: pcOfemailmessage = &pcEmailmessage[0]; //Enter "Message" iIndex =3; while ((int)usBuffer[iIndex] != 0x0D && usBuffer[iIndex] != '\0') { if((int)usBuffer[iIndex] == 62) { iParamcount++; } else { if(iParamcount==0) { *pcOfemailmessage++ = usBuffer[iIndex]; } } iIndex++; } *pcOfemailmessage= '\0'; /* TODO here unsigned char is converting to char */ DispatcherUartSendPacket((char*)pucUARTOKString, \ sizeof(pucUARTOKString)); break; //********************************************** // Case 05: Send email message using configurations //********************************************** case UART_COMMAND_EMAIL_SEND: { // reset Orange LED state GPIO_IF_LedOff(MCU_SENDING_DATA_IND); // TODO: If no destination email given, default to hardcoded value SlNetAppEmailOpt_t eMailServerSetting; lRetVal = Network_IF_GetHostIP(GMAIL_HOST_NAME, &eMailServerSetting.Ip); if(lRetVal >= 0) { eMailServerSetting.Family = AF_INET; eMailServerSetting.Port = GMAIL_HOST_PORT; eMailServerSetting.SecurityMethod = SL_SO_SEC_METHOD_SSLV3; eMailServerSetting.SecurityCypher = SL_SEC_MASK_SSL_RSA_WITH_RC4_128_MD5; lRetVal = sl_NetAppEmailSet(SL_NET_APP_EMAIL_ID, \ NETAPP_ADVANCED_OPT, \ sizeof(SlNetAppEmailOpt_t), \ (unsigned char*)&eMailServerSetting); ASSERT_ON_ERROR(lRetVal); } else { UART_PRINT("Error:%d GetHostIP.", lRetVal); return -1; } g_cConnectStatus = sl_NetAppEmailConnect(); // If return -1, throw connect error if(g_cConnectStatus == -1) { DispatcherUartSendPacket((char*)pucUARTErrorSocketCreateString, \ sizeof(pucUARTErrorSocketCreateString)); } // If return -2, throw socket option error if(g_cConnectStatus == -2) { DispatcherUartSendPacket((char*)pucUARTErrorSocketOptionString, \ sizeof(pucUARTErrorSocketOptionString)); } if(g_cConnectStatus == 0) { SlNetAppServerError_t sEmailErrorInfo; long lRetCode = SL_EMAIL_ERROR_FAILED; if((lRetCode = sl_NetAppEmailSend(pcEmailto,pcEmailsubject, \ pcEmailmessage, \ &sEmailErrorInfo)) == SL_EMAIL_ERROR_NONE) { // Blink LED7 to indicate email has been sent for(iIndex=0 ;iIndex<5 ;iIndex++) { MAP_UtilsDelay(6000000); GPIO_IF_LedOff(MCU_SENDING_DATA_IND); MAP_UtilsDelay(6000000); GPIO_IF_LedOn(MCU_SENDING_DATA_IND); } DispatcherUartSendPacket((char*)putUARTFinishString, \ sizeof(putUARTFinishString)); } else { lRetVal = EmailHandleERROR(lRetCode,(char*)sEmailErrorInfo.Value); ASSERT_ON_ERROR(lRetVal); } } } break; case UART_COMMAND_SMART_CONFIG: GPIO_IF_LedOff(MCU_IP_ALLOC_IND); DispatcherUartSendPacket((char*)pucUARTSmartConfigString, \ sizeof(pucUARTSmartConfigString)); // Start LED blinking Timer LedTimerConfigNStart(); //Reset the Network Status before Entering Smart Config Network_IF_UnsetMCUMachineState(STATUS_BIT_CONNECTION); Network_IF_UnsetMCUMachineState(STATUS_BIT_IP_AQUIRED); // start smart config process lRetVal = SmartConfigConnect(); ASSERT_ON_ERROR(lRetVal); while (!(IS_CONNECTED(Network_IF_CurrentMCUState())) || \ !(IS_IP_ACQUIRED(Network_IF_CurrentMCUState()))) { MAP_UtilsDelay(100); } LedTimerDeinitStop(); GPIO_IF_LedOn(MCU_IP_ALLOC_IND); break; default: DispatcherUartSendPacket((char*)pucUARTErrorString, \ sizeof(pucUARTErrorString)); break; } return SUCCESS; }
//**************************************************************************** // //! 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(); }