//--tested, working--// int WiFiUDP::endPacket() { // //only do the rest of this function if a socket actually exists // if (_socketIndex == NO_SOCKET_AVAIL) { return 0; } // //fill in the address structure // SlSockAddrIn_t sendAddress; memset(&sendAddress, 0, sizeof(SlSockAddrIn_t)); sendAddress.sin_family = SL_AF_INET; sendAddress.sin_port = sl_Htons(_sendPort); sendAddress.sin_addr.s_addr = _sendIP; // //use the simplelink library to send the tx buffer // int socketHandle = WiFiClass::_handleArray[_socketIndex]; int iRet = sl_SendTo(socketHandle, tx_buf, tx_fillLevel, NULL, (SlSockAddr_t*)&sendAddress, sizeof(SlSockAddrIn_t)); if (iRet < 0) { return 0; } // //reset all tx buffer indicators // memset(tx_buf, 0, UDP_TX_PACKET_MAX_SIZE); tx_fillLevel = 0; return 1; }
int main0(void){ // "Embedded Systems: Real Time Interfacing to ARM Cortex M Microcontrollers", // ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2014, Volume 2, Program 11.2 UINT8 IsDHCP = 0; _NetCfgIpV4Args_t ipV4; SlSockAddrIn_t Addr; UINT16 AddrSize = 0; INT16 SockID = 0; UINT32 data; unsigned char len = sizeof(_NetCfgIpV4Args_t); initClk(); // PLL 50 MHz, ADC needs PPL active 15 ADC0_InitSWTriggerSeq3(7); // Ain7 is on PD0 16 sl_Start(0, 0, 0); // Initializing the CC3100 device 17 WlanConnect(); // connect to AP 18 sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP,&len, // 19 (unsigned char *)&ipV4); // 20 Addr.sin_family = SL_AF_INET; // 21 Addr.sin_port = sl_Htons((UINT16)PORT_NUM); // 22 Addr.sin_addr.s_addr = sl_Htonl((UINT32)IP_ADDR); // 23 AddrSize = sizeof(SlSockAddrIn_t); // 24 SockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); // 25 while(1){ uBuf[0] = ATYPE; // analog data type 26 uBuf[1] = '='; // 27 data = ADC0_InSeq3(); // 0 to 4095, Ain7 is on PD0 28 Int2Str(data,(char*)&uBuf[2]); // 6 digit number 29 sl_SendTo(SockID, uBuf, BUF_SIZE, 0, // 30 (SlSockAddr_t *)&Addr, AddrSize); // 31 ROM_SysCtlDelay(ROM_SysCtlClockGet() / 25); // 40ms 32 } }
//**************************************************************************** // //! \brief Opening a UDP client side socket and sending data //! //! This function opens a UDP socket and tries to connect to a Server IP_ADDR //! waiting on port PORT_NUM. //! Then the function will send 1000 UDP packets to the server. //! //! \param[in] port number on which the server will be listening on //! //! \return 0 on success, -1 on Error. // //**************************************************************************** int BsdUdpClient(unsigned short usPort) { int iCounter; short sTestBufLen; SlSockAddrIn_t sAddr; int iAddrSize; int iSockID; int iStatus; long lLoopCount = 0; // filling the buffer for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++) { g_cBsdBuf[iCounter] = (char)(iCounter % 10); } sTestBufLen = BUF_SIZE; //filling the UDP server socket address sAddr.sin_family = SL_AF_INET; sAddr.sin_port = sl_Htons((unsigned short)usPort); sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)g_ulDestinationIp); iAddrSize = sizeof(SlSockAddrIn_t); // creating a UDP socket iSockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); if( iSockID < 0 ) { // error ASSERT_ON_ERROR(UCP_CLIENT_FAILED); } // for a UDP connection connect is not required // sending 1000 packets to the UDP server while (lLoopCount < g_ulPacketCount) { // sending packet iStatus = sl_SendTo(iSockID, g_cBsdBuf, sTestBufLen, 0, (SlSockAddr_t *)&sAddr, iAddrSize); if( iStatus <= 0 ) { // error sl_Close(iSockID); ASSERT_ON_ERROR(UCP_CLIENT_FAILED); } lLoopCount++; } UART_PRINT("Sent %u packets successfully\n\r",g_ulPacketCount); //closing the socket after sending 1000 packets sl_Close(iSockID); return SUCCESS; }
/* * ::sendto() */ int sendto(int s, const void *buffer, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) { SlSockAddr_t sl_sockaddr; SlSockAddr_t *sl_sockaddr_ptr; if (dest_addr != NULL) { switch (dest_addr->sa_family) { default: errno = EAFNOSUPPORT; return -1; case AF_INET: { if (addrlen != sizeof(struct sockaddr_in)) { errno = EINVAL; return -1; } SlSockAddrIn_t *sl_addr_in = (SlSockAddrIn_t*)&sl_sockaddr; struct sockaddr_in *addr_in = (struct sockaddr_in*)dest_addr; sl_addr_in->sin_family = addr_in->sin_family; sl_addr_in->sin_port = addr_in->sin_port; sl_addr_in->sin_addr.s_addr = addr_in->sin_addr.s_addr; addrlen = sizeof(SlSockAddrIn_t); break; } } sl_sockaddr_ptr = &sl_sockaddr; } else { sl_sockaddr_ptr = NULL; addrlen = 0; } int result = sl_SendTo(s, buffer, length, flags, sl_sockaddr_ptr, addrlen); if (result < 0) { switch (result) { default: errno = EINVAL; break; } return -1; } return result; }
//**************************************************************************** // //! \brief Opening a UDP client side socket and sending data //! //! This function opens a UDP socket and tries to connect to a Server IP_ADDR //! waiting on port PORT_NUM. //! Then the function will send 1000 UDP packets to the server. //! //! \param[in] port number on which the server will be listening on //! //! \return 0 on success, -1 on Error. // //**************************************************************************** int BsdUdpClient(unsigned short usPort, unsigned long ulDesitnationIP) { int iCounter; short sTestBufLen; SlSockAddrIn_t sAddr; int iAddrSize; int iSockID; int iStatus; int iPackets; // filling the buffer for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++) { g_cBsdBuf[iCounter] = (char)(iCounter % 10); } sTestBufLen = BUF_SIZE; // // Filling the UDP server socket address // sAddr.sin_family = SL_AF_INET; sAddr.sin_port = sl_Htons((unsigned short)usPort); sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)ulDesitnationIP); iAddrSize = sizeof(SlSockAddrIn_t); // // Initialize number of packets variable // iPackets = UDP_PACKET_COUNT; // // Creating a UDP socket // iSockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); if( iSockID < 0 ) { // error return -1; } UART_PRINT("\n\rSending Packets\n\r"); // // Loop forever sending packets // while (1) { // // Sending packet // iStatus = sl_SendTo(iSockID, g_cBsdBuf, sTestBufLen, 0, ( SlSockAddr_t *)&sAddr, iAddrSize); // // Decrement the packet count // iPackets--; UART_PRINT("."); // // If send fails or packet count reaches 0 stop acking the watchdog // so that it resets the system // if( iStatus > 0 && iPackets > 0) { WatchdogAck(); } UtilsDelay(8000000); } }
int main1(void){ UINT8 IsDHCP = 0; _NetCfgIpV4Args_t ipV4; SlSockAddrIn_t Addr; UINT16 AddrSize = 0; INT16 SockID = 0; INT16 Status = 1; // ok UINT32 data; unsigned char len = sizeof(_NetCfgIpV4Args_t); stopWDT(); // Stop WDT initClk(); // PLL 50 MHz, ADC needs PPL active Board_Init(); // initialize LaunchPad I/O ConfigureUART(); // Initialize the UART. UARTprintf("Section 11.4 IoT example, Volume 2 Real-time interfacing\n"); #if ADC ADC0_InitSWTriggerSeq3(7); // Ain7 is on PD0 UARTprintf("This node is configured to measure signals from Ain7=PD0\n"); #endif #if EKG UARTprintf("This node is configured to generate simulated EKG data\n"); #endif UARTprintf(" and send UDP packets to IP: %d.%d.%d.%d Port: %d\n\n", SL_IPV4_BYTE(IP_ADDR,3), SL_IPV4_BYTE(IP_ADDR,2), SL_IPV4_BYTE(IP_ADDR,1), SL_IPV4_BYTE(IP_ADDR,0),PORT_NUM); while(1){ sl_Start(0, 0, 0);/* Initializing the CC3100 device */ /* Connecting to WLAN AP - Set with static parameters defined at the top After this call we will be connected and have IP address */ WlanConnect(); // connect to AP /* Read the IP parameter */ sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP,&len,(unsigned char *)&ipV4); UARTprintf("This node is at IP: %d.%d.%d.%d\n", SL_IPV4_BYTE(ipV4.ipV4,3), SL_IPV4_BYTE(ipV4.ipV4,2), SL_IPV4_BYTE(ipV4.ipV4,1), SL_IPV4_BYTE(ipV4.ipV4,0)); while(Status > 0){ Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons((UINT16)PORT_NUM); Addr.sin_addr.s_addr = sl_Htonl((UINT32)IP_ADDR); AddrSize = sizeof(SlSockAddrIn_t); SockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); if( SockID < 0 ){ UARTprintf("SockIDerror "); Status = -1; // error }else{ while(Status>0){ UARTprintf("\nSending a UDP packet ..."); uBuf[0] = ATYPE; // defines this as an analog data type uBuf[1] = '='; #if ADC data = ADC0_InSeq3(); // 0 to 4095, Ain7 is on PD0 #endif #if EKG data = EKGbuf[EKGindex]; EKGindex = (EKGindex+1)%EKGSIZE; // 100 Hz #endif Int2Str(data,(char*)&uBuf[2]); // [2] to [7] is 6 digit number UARTprintf(" %s ",uBuf); LED_Toggle(); Status = sl_SendTo(SockID, uBuf, BUF_SIZE, 0, (SlSockAddr_t *)&Addr, AddrSize); ROM_SysCtlDelay(ROM_SysCtlClockGet() / 25); // 80ms if( Status <= 0 ){ UARTprintf("SockIDerror %d ",Status); }else{ UARTprintf("ok"); } } sl_Close(SockID); } } } }
//***************************************************************************** // //! Gets the current time from the selected SNTP server //! //! \brief This function obtains the NTP time from the server. //! //! \param pGetTime is pointer to Get time structure //! //! \return 0 : success, -ve : failure //! // //***************************************************************************** long GetSNTPTime(tGetTime *pGetTime) { /* NTP Packet Header: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |LI | VN |Mode | Stratum | Poll | Precision | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Root Delay | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Root Dispersion | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Reference Identifier | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | Reference Timestamp (64) | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | Originate Timestamp (64) | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | Receive Timestamp (64) | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | Transmit Timestamp (64) | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Key Identifier (optional) (32) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | | | Message Digest (optional) (128) | | | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ char cDataBuf[48]; long lRetVal = 0; SlSockAddr_t sAddr; unsigned long ulElapsedSec; // // 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)((pGetTime->ulNtpServerIP >>24)&0xff); sAddr.sa_data[3] = (char)((pGetTime->ulNtpServerIP >>16)&0xff); sAddr.sa_data[4] = (char)((pGetTime->ulNtpServerIP >>8)&0xff); sAddr.sa_data[5] = (char)(pGetTime->ulNtpServerIP&0xff); lRetVal = sl_SendTo(pGetTime->iSocket, cDataBuf, sizeof(cDataBuf), 0, &sAddr, sizeof(sAddr)); if (lRetVal != sizeof(cDataBuf)) { return FAILURE; } lRetVal = sl_Recv(pGetTime->iSocket, cDataBuf, sizeof(cDataBuf), 0); // // Confirm that the MODE is 4 --> server // if ((cDataBuf[0] & 0x7) != 4) // expect only server response { return FAILURE; } else { // // Getting the data from the Transmit Timestamp (seconds) field // This is the time at which the reply departed the // server for the client // ulElapsedSec = cDataBuf[40]; ulElapsedSec <<= 8; ulElapsedSec += cDataBuf[41]; ulElapsedSec <<= 8; ulElapsedSec += cDataBuf[42]; ulElapsedSec <<= 8; ulElapsedSec += cDataBuf[43]; // // Compute the UTC time // TimeToString(ulElapsedSec, sDisplayInfo.ucUTCTime); // // Set the time zone // ulElapsedSec += (pGetTime->ucGmtDiffHr * SEC_IN_HOUR); ulElapsedSec += (pGetTime->ucGmtDiffMins * SEC_IN_MIN); // // Compute the local time // TimeToString(ulElapsedSec, sDisplayInfo.ucLocalTime); } return SUCCESS; }
//***************************************************************************** // //! \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 {
int main(void) { UINT8 IsDHCP = 0; int32_t i32CommandStatus; _NetCfgIpV4Args_t ipV4; SlSockAddrIn_t Addr; UINT16 AddrSize = 0; INT16 SockID = 0; UINT32 data; long x = 0; //counter unsigned char len = sizeof(_NetCfgIpV4Args_t); int Status = 0; /* Stop WDT */ stopWDT(); /* Initialize the system clock of MCU */ initClk(); Board_Init(); // initialize LaunchPad I/O and PD1 LED ConfigureUART(); // Initialize the UART. UARTprintf("Section 11.4 IoT example, Volume 2 Real-time interfacing\n"); UARTprintf("This application is configured to generate text\n"); UARTprintf(" and send UDP packets to IP: %d.%d.%d.%d Port: %d\n\n", SL_IPV4_BYTE(IP_ADDR,3), SL_IPV4_BYTE(IP_ADDR,2), SL_IPV4_BYTE(IP_ADDR,1), SL_IPV4_BYTE(IP_ADDR,0),PORT_NUM); //added code from the powerpoint slide /* Initializing the CC3100 device */ sl_Start(0, 0, 0); /* Connecting to WLAN AP - Set with static parameters defined at the top After this call we will be connected and have IP address */ WlanConnect(); /* Read the IP parameter */ sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO,&IsDHCP,&len,(unsigned char *)&ipV4); UARTprintf("This node is at IP: %d.%d.%d.%d\n", SL_IPV4_BYTE(ipV4.ipV4,3), SL_IPV4_BYTE(ipV4.ipV4,2), SL_IPV4_BYTE(ipV4.ipV4,1), SL_IPV4_BYTE(ipV4.ipV4,0)); Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons((UINT16)PORT_NUM); Addr.sin_addr.s_addr = sl_Htonl((UINT32)IP_ADDR); AddrSize = sizeof(SlSockAddrIn_t); SockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); // Loop forever waiting for commands from PC... // while(1) { // Print prompt for user. UARTprintf("\n>"); // Peek to see if a full command is ready for processing. while(UARTPeek('\r') == -1) LED_On(); // Approximately 1 millisecond delay. ROM_SysCtlDelay(ROM_SysCtlClockGet() / 3000); } // A '\r' was detected so get the line of text from the receive buffer. while(Status >= 0){ UARTprintf("\nSending a UDP packet ...\n"); UARTgets(g_cInput,sizeof(g_cInput)); //this function receives the input from the Putty and places it in a string //DO NOT CHANGE ANYTHING ABOVE THIS COMMENT //WHAT WE NEED TO DO: //work with the g_cInput to get the array of letters typed into the Putty //then send that Array using UARTprintf uBuf[0] = ATYPE; // defines this as an analog data type uBuf[1] = '='; data = 1000; Int2Str(data,(char*)&uBuf[2]); // [2] to [7] is 6 digit number UARTprintf(" %s ",uBuf); //this line sends a string to the receiver //the above 5 lines print out a = 1000; //everything below this is just error cases if( SockID < 0 ){ UARTprintf("SockIDerror "); Status = -1; // error }else{ LED_Toggle(); Status = sl_SendTo(SockID, uBuf, BUF_SIZE, 0, (SlSockAddr_t *)&Addr, AddrSize); if( Status <= 0 ){ sl_Close(SockID); UARTprintf("SockIDerror %d ",Status); }else{ UARTprintf("ok"); } } ROM_SysCtlDelay(ROM_SysCtlClockGet() / 100); // 10ms LED_Off(); } }
//**************************************************************************** // MAIN FUNCTION //**************************************************************************** int main(void) { long lRetVal; char cCmdBuff[20]; signed char cCmd = APP_SLEEP; SlSockAddrIn_t sAddr; SlSockAddrIn_t sLocalAddr; SlSockAddrIn_t sBrdAddr; int iCounter; int iAddrSize; int iSockID; int iStatus; long lLoopCount = 0; short sTestBufLen; struct SlTimeval_t timeVal; // // Board Initialization // BoardInit(); // // uDMA Initialization // UDMAInit(); // // Configure the pinmux settings for the peripherals exercised // Note: pinmux has been modified after the output from pin mux tools // to enable sleep clk for the peripherals exercised // PinMuxConfig(); // // Initialize the platform // platform_init(); // // Initialise the UART terminal // InitTerm(); // // Display banner // DisplayBanner(); // // starting the simplelink // lRetVal = sl_Start(NULL, NULL, NULL); if (lRetVal < 0) { UART_PRINT("Failed to start the device \n\r"); LOOP_FOREVER(); } // // Swtich to STA mode if device is not // SwitchToStaMode(lRetVal); // // set connection policy // sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(0, 0, 0, 0, 0), NULL, 0); // // Set the power management policy of NWP // lRetVal = sl_WlanPolicySet(SL_POLICY_PM, SL_NORMAL_POLICY, NULL, 0); UART_PRINT("Trying to Connect to AP: %s ...\r\n",SSID_NAME); // //Connecting to WLAN AP // lRetVal = WlanConnect(); if(lRetVal < 0) { UART_PRINT("Failed to establish connection w/ an AP \n\r"); LOOP_FOREVER(); } // filling the buffer for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++) { g_cBsdBuf[iCounter] = (char)(iCounter % 10); } sTestBufLen = BUF_SIZE; //filling the UDP server socket address sLocalAddr.sin_family = SL_AF_INET; sLocalAddr.sin_port = sl_Htons((unsigned short)PORT_NUM); sLocalAddr.sin_addr.s_addr = 0; //filling the UDP server socket address sBrdAddr.sin_family = SL_AF_INET; sBrdAddr.sin_port = sl_Htons((unsigned short)PORT_NUM); sBrdAddr.sin_addr.s_addr = sl_Htonl((unsigned int)g_ulDestinationIp); iAddrSize = sizeof(SlSockAddrIn_t); // creating a UDP socket iSockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); /* setting time out for socket recv */ timeVal.tv_sec = 5; // Seconds timeVal.tv_usec = 0; // Microseconds. 10000 microseconds resolution sl_SetSockOpt(iSockID,SL_SOL_SOCKET,SL_SO_RCVTIMEO, (_u8 *)&timeVal, sizeof(timeVal)); // binding the UDP socket to the UDP server address iStatus = sl_Bind(iSockID, (SlSockAddr_t *)&sLocalAddr, iAddrSize); if( iStatus < 0 ) { // error sl_Close(iSockID); ASSERT_ON_ERROR(iStatus); } // // setting Apps power policy // lp3p0_setup_power_policy(POWER_POLICY_STANDBY); UART_PRINT("enter one of the following command:\n\r"); UART_PRINT("sleep - for putting the system into LPDS mode\n\r"); UART_PRINT(" GPIO 13 and timer(5 sec) are the wk source configured\n\r"); UART_PRINT("recv - for receiving 1000 UDP packets\n\r"); UART_PRINT("send - for broadcasting 1000 UDP packets\n\r"); do{ UART_PRINT("cmd#"); // // get cmd over UART // GetCmd(cCmdBuff, 20); // // parse the command // ParseCmd(cCmdBuff, &cCmd); if(cCmd == APP_SLEEP) { // // set timer and gpio as wake src // set_rtc_as_wk_src(WK_LPDS, LPDS_DUR_SEC, false); set_gpio_as_wk_src(WK_LPDS, GPIO_SRC_WKUP, PRCM_LPDS_FALL_EDGE); cc_idle_task_pm(); } else if(cCmd == APP_RECV) { lLoopCount = 0; /// waits for 1000 packets from a UDP client while (lLoopCount < g_ulPacketCount) { iStatus = sl_RecvFrom(iSockID, g_cBsdBuf, sTestBufLen, 0, ( SlSockAddr_t *)&sAddr, (SlSocklen_t*)&iAddrSize ); if( iStatus < 0 ) { //error break; } lLoopCount++; } UART_PRINT("Recieved %u packets successfully \n\r",lLoopCount); if(lLoopCount != g_ulPacketCount) { if(iStatus == SL_EAGAIN) { UART_PRINT("timed out\n\r"); } else { UART_PRINT("recv error: %d\n\r", iStatus); } } } else if(cCmd == APP_SEND) { lLoopCount = 0; // sending 1000 packets to the UDP server while (lLoopCount < g_ulPacketCount) { // sending packet iStatus = sl_SendTo(iSockID, g_cBsdBuf, sTestBufLen, 0, (SlSockAddr_t *)&sBrdAddr, iAddrSize); if( iStatus <= 0 ) { // error UART_PRINT("send error\n\r"); break; } lLoopCount++; } UART_PRINT("Sent %u packets successfully\n\r",lLoopCount); } }while(FOREVER); }
//***************************************************************************** // //! Gets the current time from the selected SNTP server //! //! \brief This function obtains the NTP time from the server. //! //! \param GmtDiffHr is the GMT Time Zone difference in hours //! \param GmtDiffMins is the GMT Time Zone difference in minutes //! //! \return 0 : success, -ve : failure //! //***************************************************************************** long GetSNTPTime(unsigned char ucGmtDiffHr, unsigned char ucGmtDiffMins) { /* NTP Packet Header: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |LI | VN |Mode | Stratum | Poll | Precision | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Root Delay | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Root Dispersion | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Reference Identifier | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | Reference Timestamp (64) | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | Originate Timestamp (64) | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | Receive Timestamp (64) | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | Transmit Timestamp (64) | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Key Identifier (optional) (32) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | | | Message Digest (optional) (128) | | | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ char cDataBuf[48]; long lRetVal = 0; int iAddrSize; // // 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)((g_sAppData.ulDestinationIP>>24)&0xff); sAddr.sa_data[3] = (char)((g_sAppData.ulDestinationIP>>16)&0xff); sAddr.sa_data[4] = (char)((g_sAppData.ulDestinationIP>>8)&0xff); sAddr.sa_data[5] = (char)(g_sAppData.ulDestinationIP&0xff); lRetVal = sl_SendTo(g_sAppData.iSockID, cDataBuf, sizeof(cDataBuf), 0, &sAddr, sizeof(sAddr)); if (lRetVal != sizeof(cDataBuf)) { // could not send SNTP request hourSet = 25; // This will ensure we try to fetch time again //ASSERT_ON_ERROR(SERVER_GET_TIME_FAILED); } // // 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; if(g_sAppData.ulElapsedSec == 0) { lRetVal = sl_Bind(g_sAppData.iSockID, (SlSockAddr_t *)&sLocalAddr, sizeof(SlSockAddrIn_t)); } iAddrSize = sizeof(SlSockAddrIn_t); lRetVal = sl_RecvFrom(g_sAppData.iSockID, cDataBuf, sizeof(cDataBuf), 0, (SlSockAddr_t *)&sLocalAddr, (SlSocklen_t*)&iAddrSize); ASSERT_ON_ERROR(lRetVal); // // Confirm that the MODE is 4 --> server // if ((cDataBuf[0] & 0x7) != 4) // expect only server response { hourSet = 25; // This will ensure we try to fetch time again //ASSERT_ON_ERROR(SERVER_GET_TIME_FAILED); // MODE is not server, abort } else { unsigned char iIndex; // // Getting the data from the Transmit Timestamp (seconds) field // This is the time at which the reply departed the // server for the client // g_sAppData.ulElapsedSec = cDataBuf[40]; g_sAppData.ulElapsedSec <<= 8; g_sAppData.ulElapsedSec += cDataBuf[41]; g_sAppData.ulElapsedSec <<= 8; g_sAppData.ulElapsedSec += cDataBuf[42]; g_sAppData.ulElapsedSec <<= 8; g_sAppData.ulElapsedSec += cDataBuf[43]; // // seconds are relative to 0h on 1 January 1900 // g_sAppData.ulElapsedSec -= TIME2013; // // in order to correct the timezone // g_sAppData.ulElapsedSec += (ucGmtDiffHr * SEC_IN_HOUR); g_sAppData.ulElapsedSec += (ucGmtDiffMins * SEC_IN_MIN); g_sAppData.pcCCPtr = &g_sAppData.acTimeStore[0]; // // day, number of days since beginning of 2013 // g_sAppData.isGeneralVar = g_sAppData.ulElapsedSec/SEC_IN_DAY; memcpy(g_sAppData.pcCCPtr, g_acDaysOfWeek2013[g_sAppData.isGeneralVar%7], 3); g_sAppData.pcCCPtr += 3; *g_sAppData.pcCCPtr++ = '\x20'; // // month // g_sAppData.isGeneralVar %= 365; for (iIndex = 0; iIndex < 12; iIndex++) { g_sAppData.isGeneralVar -= g_acNumOfDaysPerMonth[iIndex]; if (g_sAppData.isGeneralVar < 0) break; } if(iIndex == 12) { iIndex = 0; } memcpy(g_sAppData.pcCCPtr, g_acMonthOfYear[iIndex], 3); g_sAppData.pcCCPtr += 3; *g_sAppData.pcCCPtr++ = '\x20'; // Set the Month Value dateTime.sl_tm_mon = iIndex + 1; // // date // restore the day in current month // g_sAppData.isGeneralVar += g_acNumOfDaysPerMonth[iIndex]; g_sAppData.uisCCLen = itoa(g_sAppData.isGeneralVar + 1, g_sAppData.pcCCPtr); g_sAppData.pcCCPtr += g_sAppData.uisCCLen; *g_sAppData.pcCCPtr++ = '\x20'; // Set the Date dateTime.sl_tm_day = g_sAppData.isGeneralVar + 1; // // time // g_sAppData.ulGeneralVar = g_sAppData.ulElapsedSec%SEC_IN_DAY; // number of seconds per hour g_sAppData.ulGeneralVar1 = g_sAppData.ulGeneralVar%SEC_IN_HOUR; // number of hours g_sAppData.ulGeneralVar /= SEC_IN_HOUR; g_sAppData.uisCCLen = itoa(g_sAppData.ulGeneralVar, g_sAppData.pcCCPtr); g_sAppData.pcCCPtr += g_sAppData.uisCCLen; *g_sAppData.pcCCPtr++ = ':'; // Set the hour dateTime.sl_tm_hour = g_sAppData.ulGeneralVar; // number of minutes per hour g_sAppData.ulGeneralVar = g_sAppData.ulGeneralVar1/SEC_IN_MIN; // Set the minutes dateTime.sl_tm_min = g_sAppData.ulGeneralVar; // number of seconds per minute g_sAppData.ulGeneralVar1 %= SEC_IN_MIN; g_sAppData.uisCCLen = itoa(g_sAppData.ulGeneralVar, g_sAppData.pcCCPtr); g_sAppData.pcCCPtr += g_sAppData.uisCCLen; *g_sAppData.pcCCPtr++ = ':'; g_sAppData.uisCCLen = itoa(g_sAppData.ulGeneralVar1, g_sAppData.pcCCPtr); g_sAppData.pcCCPtr += g_sAppData.uisCCLen; *g_sAppData.pcCCPtr++ = '\x20'; //Set the seconds dateTime.sl_tm_sec = g_sAppData.ulGeneralVar1; // // year // number of days since beginning of 2013 // g_sAppData.ulGeneralVar = g_sAppData.ulElapsedSec/SEC_IN_DAY; g_sAppData.ulGeneralVar /= 365; g_sAppData.uisCCLen = itoa(YEAR2013 + g_sAppData.ulGeneralVar, g_sAppData.pcCCPtr); g_sAppData.pcCCPtr += g_sAppData.uisCCLen; *g_sAppData.pcCCPtr++ = '\0'; //Set the year dateTime.sl_tm_year = 2013 + g_sAppData.ulGeneralVar; CLI_Write("Response from server: "); CLI_Write((unsigned char *)g_acSNTPserver); CLI_Write("\n\r"); CLI_Write((unsigned char *)g_sAppData.acTimeStore); CLI_Write("\n\r\n\r"); //Set time of the device for certificate verification. lRetVal = setDeviceTimeDate(); if(lRetVal < 0) { CLI_Write("Unable to set time in the device.\n\r"); return lRetVal; } } return SUCCESS; }