Ejemplo n.º 1
0
/*
 * !!! IMPORTANT !!!
 * This function does not conform to RFC 4122, even though it returns
 * a string formatted as an UUID. Do not use this to generate proper UUID!
 */
void createNewInstallationId(ParseClientInternal *parseClient) {
    static int randInitialized = FALSE;

    if (!randInitialized) {
        SlDateTime_t dateTime;
        getDeviceTimeDate(&dateTime);
        // Terrible choice for initialization, prone to collision
        srand(dateTime.sl_tm_sec);
        randInitialized = TRUE;
    }

    snprintf(parseClient->installationId,
    		 sizeof(parseClient->installationId)-1,
    		 "%x%x%x%x%x%x%x%x-%x%x%x%x-%x%x%x%x-%x%x%x%x-%x%x%x%x%x%x%x%x%x%x%x%x",
             rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16,
             rand()%16, rand()%16, rand()%16, rand()%16,
             rand()%16, rand()%16, rand()%16, rand()%16,
             rand()%16, rand()%16, rand()%16, rand()%16,
             rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16, rand()%16
            );
}
Ejemplo n.º 2
0
int isPingTime() {
    SlDateTime_t dateTime = {0};
    getDeviceTimeDate(&dateTime);

    return (timeDiffInSec(dateTime, lastDateTime) > PUSH_KEEP_ALIVE) ? TRUE : FALSE;
}
Ejemplo n.º 3
0
void updateLastPingTime() {
    getDeviceTimeDate(&lastDateTime);
}
Ejemplo n.º 4
0
Archivo: main.c Proyecto: Mecabot/IoT
//*****************************************************************************
//
//! This function obtains the current time from a SNTP server if required due
//! to not having current time (when booting up) or periodically to update
//! the time
//!
//! \param None
//!
//! \return  0 on success else error code
//! \return  Error Number of failure
//
//*****************************************************************************
long GetCurrentTime()
{
    int iSocketDesc;
    long lRetVal = -1;

	//
	// Get the time and date currently stored in the RTC
	//
	getDeviceTimeDate();

	//
	// Calculate time difference between the last time we obtained time from a NTP server
	//
	timeDifference = dateTime.sl_tm_hour - hourSet; // This roughly works, it does however reset after midnight.

	//
	// Get the NTP time to use with the SSL process. Only call this every 6 hours to update the RTC
	// As we do not want to be calling the NTP server too often
	//
	if (timeDifference > 6 || timeDifference < 0)
	{
		//
		// Create UDP socket
		//
		iSocketDesc = sl_Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
		if(iSocketDesc < 0)
		{
			CLI_Write("Could not create UDP socket.\n\r");
			close(iSocketDesc);
			return iSocketDesc;
		}
		else
		{
			CLI_Write("Socket successfully created\n\r");
		}
		g_sAppData.iSockID = iSocketDesc;

		//
		// Get the NTP server host IP address using the DNS lookup
		//
		lRetVal = getHostIP((char*)g_acSNTPserver, &g_sAppData.ulDestinationIP);
		if( lRetVal >= 0)
		{
			//
			// Configure the recieve timeout
			//
			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)
			{
			   CLI_Write("Could not configure socket option (receive timeout).\n\r");
			   close(iSocketDesc);
			   return lRetVal;
			}
		}
		else
		{
			CLI_Write("DNS lookup failed.");
		}

		//
		// Get current time from the SNTP server
		//
		CLI_Write("Fetching Time From SNTP Server\n\r");
		lRetVal = GetSNTPTime(GMT_DIFF_TIME_HRS, GMT_DIFF_TIME_MINS);
		if(lRetVal < 0)
		{
			CLI_Write("Server Get Time failed.\n\r");
			close(iSocketDesc);
			return lRetVal;
		}
		else
		{
			hourSet = dateTime.sl_tm_hour; // Set to current hour as we did get the time successfully
			CLI_Write("Server Get Time Successful\n\n\r");
		}

		//
		// Close the socket
		//
		close(iSocketDesc);
	}

	return 0;
}