From 6cda0cf7fc0a2efb418914dacd34d115fe8514b1 Mon Sep 17 00:00:00 2001 From: Rahul Kar <118818625+kar-rahul-aws@users.noreply.github.com> Date: Fri, 3 May 2024 12:47:33 +0530 Subject: [PATCH] Update coreSNTP demo file to fix warnings (#1218) * Update demo file to fix warnings * Remove extra overflow check in sntpClient_GetTime function * Add assert checking for UTC rollover * Fix configASSERT condition --------- Co-authored-by: GitHub Action Co-authored-by: Ching-Hsin,Lee --- .../SNTPClientTask.c | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/FreeRTOS-Plus/Demo/coreSNTP_Windows_Simulator/SNTPClientTask.c b/FreeRTOS-Plus/Demo/coreSNTP_Windows_Simulator/SNTPClientTask.c index 77a8ea09d88..17f8b528049 100644 --- a/FreeRTOS-Plus/Demo/coreSNTP_Windows_Simulator/SNTPClientTask.c +++ b/FreeRTOS-Plus/Demo/coreSNTP_Windows_Simulator/SNTPClientTask.c @@ -550,7 +550,7 @@ static void populateAuthContextForServer( const char * pServer, static SntpStatus_t addClientAuthCode( SntpAuthContext_t * pAuthContext, const SntpServerInfo_t * pTimeServer, void * pRequestBuffer, - uint16_t bufferSize, + size_t bufferSize, uint16_t * pAuthCodeSize ); @@ -586,7 +586,7 @@ static SntpStatus_t addClientAuthCode( SntpAuthContext_t * pAuthContext, static SntpStatus_t validateServerAuth( SntpAuthContext_t * pAuthContext, const SntpServerInfo_t * pTimeServer, const void * pResponseData, - size_t responseSize ); + uint16_t responseSize ); /** * @brief Generates a random number using PKCS#11. @@ -668,6 +668,7 @@ void calculateCurrentTime( UTCTime_t * pBaseTime, UTCTime_t * pCurrentTime ) { uint64_t msElapsedSinceLastSync = 0; + uint64_t currentTimeSecs; TickType_t ticksElapsedSinceLastSync = xTaskGetTickCount() - lastSyncTickCount; /* Calculate time elapsed since last synchronization according to the number @@ -686,13 +687,30 @@ void calculateCurrentTime( UTCTime_t * pBaseTime, /* Set the current UTC time in the output parameter. */ if( msElapsedSinceLastSync >= 1000 ) { - pCurrentTime->secs = pBaseTime->secs + msElapsedSinceLastSync / 1000; + currentTimeSecs = ( uint64_t ) ( pBaseTime->secs ) + ( msElapsedSinceLastSync / 1000 ); + + /* Support case of UTC timestamp rollover on 7 February 2038. */ + if( currentTimeSecs > UINT32_MAX ) + { + /* Assert when the UTC timestamp rollover. */ + configASSERT( !( currentTimeSecs > UINT32_MAX ) ); + + /* Subtract an extra second as timestamp 0 represents the epoch for + * UTC era 1. */ + LogWarn( ( "UTC timestamp rollover." ) ); + pCurrentTime->secs = ( uint32_t ) ( currentTimeSecs - UINT32_MAX - 1 ); + } + else + { + pCurrentTime->secs = ( uint32_t ) ( currentTimeSecs ); + } + pCurrentTime->msecs = msElapsedSinceLastSync % 1000; } else { pCurrentTime->secs = pBaseTime->secs; - pCurrentTime->msecs = msElapsedSinceLastSync; + pCurrentTime->msecs = ( uint32_t ) ( msElapsedSinceLastSync ); } } @@ -842,7 +860,7 @@ static int32_t UdpTransport_Recv( NetworkContext_t * pNetworkContext, static void sntpClient_GetTime( SntpTimestamp_t * pCurrentTime ) { UTCTime_t currentTime; - uint64_t ntpSecs; + uint32_t ntpSecs; /* Obtain mutex for accessing system clock variables */ xSemaphoreTake( xMutex, portMAX_DELAY ); @@ -862,8 +880,12 @@ static void sntpClient_GetTime( SntpTimestamp_t * pCurrentTime ) * converting from UNIX time to SNTP timestamp. */ if( ntpSecs > UINT32_MAX ) { + /* Assert when SNTP time rollover. */ + configASSERT( !( ntpSecs > UINT32_MAX ) ); + /* Subtract an extra second as timestamp 0 represents the epoch for * NTP era 1. */ + LogWarn( ( "SNTP timestamp rollover." ) ); pCurrentTime->seconds = ntpSecs - UINT32_MAX - 1; } else @@ -977,7 +999,7 @@ static void populateAuthContextForServer( const char * pServer, for( index = 0; index < strlen( pKeyHexString ); index += 2 ) { char byteString[ 3 ] = { pKeyHexString[ index ], pKeyHexString[ index + 1 ], '\0' }; - uint8_t byteVal = strtoul( byteString, NULL, 16 ); + uint8_t byteVal = ( uint8_t ) ( strtoul( byteString, NULL, 16 ) ); pAuthContext->pAuthKey[ index / 2 ] = byteVal; } } @@ -1014,7 +1036,7 @@ static CK_RV setupPkcs11ObjectForAesCmac( const SntpAuthContext_t * pAuthContext }; /* Update the attributes array with the key of AES-CMAC operation. */ - aes_cmac_template[ 6 ].pValue = pAuthContext->pAuthKey; + aes_cmac_template[ 6 ].pValue = ( uint8_t * ) ( pAuthContext->pAuthKey ); aes_cmac_template[ 6 ].ulValueLen = sizeof( pAuthContext->pAuthKey ); result = xInitializePkcs11Session( pPkcs11Session ); @@ -1056,7 +1078,7 @@ static CK_RV setupPkcs11ObjectForAesCmac( const SntpAuthContext_t * pAuthContext SntpStatus_t addClientAuthCode( SntpAuthContext_t * pAuthContext, const SntpServerInfo_t * pTimeServer, void * pRequestBuffer, - uint16_t bufferSize, + size_t bufferSize, uint16_t * pAuthCodeSize ) { CK_RV result = CKR_OK; @@ -1279,7 +1301,7 @@ static uint32_t generateRandomNumber() if( pkcs11Status == CKR_OK ) { if( pFunctionList->C_GenerateRandom( session, - &randomNum, + ( uint8_t * ) ( &randomNum ), sizeof( randomNum ) ) != CKR_OK ) { LogError( ( "Failed to generate random number. " @@ -1304,7 +1326,7 @@ static uint32_t generateRandomNumber() void initializeSystemClock( void ) { /* On boot-up initialize the system time as the first second in the configured year. */ - int64_t startupTimeInUnixSecs = translateYearToUnixSeconds( democonfigSYSTEM_START_YEAR ); + uint32_t startupTimeInUnixSecs = translateYearToUnixSeconds( democonfigSYSTEM_START_YEAR ); systemClock.baseTime.secs = startupTimeInUnixSecs; systemClock.baseTime.msecs = 0; @@ -1429,7 +1451,6 @@ static bool createUdpSocket( Socket_t * pSocket ) static void closeUdpSocket( Socket_t * pSocket ) { bool status = false; - struct freertos_sockaddr bindAddress; configASSERT( pSocket != NULL ); @@ -1455,7 +1476,7 @@ static bool calculateBackoffForNextPoll( BackoffAlgorithmContext_t * pBackoffCon if( shouldInitializeContext == true ) { /* Initialize reconnect attempts and interval.*/ - BackoffAlgorithm_InitializeParams( &pBackoffContext, + BackoffAlgorithm_InitializeParams( pBackoffContext, minPollPeriod, SNTP_DEMO_POLL_MAX_BACKOFF_DELAY_SEC, SNTP_DEMO_MAX_SERVER_BACKOFF_RETRIES ); @@ -1463,7 +1484,7 @@ static bool calculateBackoffForNextPoll( BackoffAlgorithmContext_t * pBackoffCon /* Generate a random number and calculate the new backoff poll period to wait before the next * time poll attempt. */ - status = BackoffAlgorithm_GetNextBackoff( &pBackoffContext, generateRandomNumber(), &newPollPeriod ); + status = BackoffAlgorithm_GetNextBackoff( pBackoffContext, generateRandomNumber(), &newPollPeriod ); if( status == BackoffAlgorithmRetriesExhausted ) {