Skip to content

Commit 6cda0cf

Browse files
kar-rahul-awsactions-userchinglee-iot
authored
Update coreSNTP demo file to fix warnings (FreeRTOS#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 <[email protected]> Co-authored-by: Ching-Hsin,Lee <[email protected]>
1 parent 8f3277f commit 6cda0cf

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

FreeRTOS-Plus/Demo/coreSNTP_Windows_Simulator/SNTPClientTask.c

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ static void populateAuthContextForServer( const char * pServer,
550550
static SntpStatus_t addClientAuthCode( SntpAuthContext_t * pAuthContext,
551551
const SntpServerInfo_t * pTimeServer,
552552
void * pRequestBuffer,
553-
uint16_t bufferSize,
553+
size_t bufferSize,
554554
uint16_t * pAuthCodeSize );
555555

556556

@@ -586,7 +586,7 @@ static SntpStatus_t addClientAuthCode( SntpAuthContext_t * pAuthContext,
586586
static SntpStatus_t validateServerAuth( SntpAuthContext_t * pAuthContext,
587587
const SntpServerInfo_t * pTimeServer,
588588
const void * pResponseData,
589-
size_t responseSize );
589+
uint16_t responseSize );
590590

591591
/**
592592
* @brief Generates a random number using PKCS#11.
@@ -668,6 +668,7 @@ void calculateCurrentTime( UTCTime_t * pBaseTime,
668668
UTCTime_t * pCurrentTime )
669669
{
670670
uint64_t msElapsedSinceLastSync = 0;
671+
uint64_t currentTimeSecs;
671672
TickType_t ticksElapsedSinceLastSync = xTaskGetTickCount() - lastSyncTickCount;
672673

673674
/* Calculate time elapsed since last synchronization according to the number
@@ -686,13 +687,30 @@ void calculateCurrentTime( UTCTime_t * pBaseTime,
686687
/* Set the current UTC time in the output parameter. */
687688
if( msElapsedSinceLastSync >= 1000 )
688689
{
689-
pCurrentTime->secs = pBaseTime->secs + msElapsedSinceLastSync / 1000;
690+
currentTimeSecs = ( uint64_t ) ( pBaseTime->secs ) + ( msElapsedSinceLastSync / 1000 );
691+
692+
/* Support case of UTC timestamp rollover on 7 February 2038. */
693+
if( currentTimeSecs > UINT32_MAX )
694+
{
695+
/* Assert when the UTC timestamp rollover. */
696+
configASSERT( !( currentTimeSecs > UINT32_MAX ) );
697+
698+
/* Subtract an extra second as timestamp 0 represents the epoch for
699+
* UTC era 1. */
700+
LogWarn( ( "UTC timestamp rollover." ) );
701+
pCurrentTime->secs = ( uint32_t ) ( currentTimeSecs - UINT32_MAX - 1 );
702+
}
703+
else
704+
{
705+
pCurrentTime->secs = ( uint32_t ) ( currentTimeSecs );
706+
}
707+
690708
pCurrentTime->msecs = msElapsedSinceLastSync % 1000;
691709
}
692710
else
693711
{
694712
pCurrentTime->secs = pBaseTime->secs;
695-
pCurrentTime->msecs = msElapsedSinceLastSync;
713+
pCurrentTime->msecs = ( uint32_t ) ( msElapsedSinceLastSync );
696714
}
697715
}
698716

@@ -842,7 +860,7 @@ static int32_t UdpTransport_Recv( NetworkContext_t * pNetworkContext,
842860
static void sntpClient_GetTime( SntpTimestamp_t * pCurrentTime )
843861
{
844862
UTCTime_t currentTime;
845-
uint64_t ntpSecs;
863+
uint32_t ntpSecs;
846864

847865
/* Obtain mutex for accessing system clock variables */
848866
xSemaphoreTake( xMutex, portMAX_DELAY );
@@ -862,8 +880,12 @@ static void sntpClient_GetTime( SntpTimestamp_t * pCurrentTime )
862880
* converting from UNIX time to SNTP timestamp. */
863881
if( ntpSecs > UINT32_MAX )
864882
{
883+
/* Assert when SNTP time rollover. */
884+
configASSERT( !( ntpSecs > UINT32_MAX ) );
885+
865886
/* Subtract an extra second as timestamp 0 represents the epoch for
866887
* NTP era 1. */
888+
LogWarn( ( "SNTP timestamp rollover." ) );
867889
pCurrentTime->seconds = ntpSecs - UINT32_MAX - 1;
868890
}
869891
else
@@ -977,7 +999,7 @@ static void populateAuthContextForServer( const char * pServer,
977999
for( index = 0; index < strlen( pKeyHexString ); index += 2 )
9781000
{
9791001
char byteString[ 3 ] = { pKeyHexString[ index ], pKeyHexString[ index + 1 ], '\0' };
980-
uint8_t byteVal = strtoul( byteString, NULL, 16 );
1002+
uint8_t byteVal = ( uint8_t ) ( strtoul( byteString, NULL, 16 ) );
9811003
pAuthContext->pAuthKey[ index / 2 ] = byteVal;
9821004
}
9831005
}
@@ -1014,7 +1036,7 @@ static CK_RV setupPkcs11ObjectForAesCmac( const SntpAuthContext_t * pAuthContext
10141036
};
10151037

10161038
/* Update the attributes array with the key of AES-CMAC operation. */
1017-
aes_cmac_template[ 6 ].pValue = pAuthContext->pAuthKey;
1039+
aes_cmac_template[ 6 ].pValue = ( uint8_t * ) ( pAuthContext->pAuthKey );
10181040
aes_cmac_template[ 6 ].ulValueLen = sizeof( pAuthContext->pAuthKey );
10191041

10201042
result = xInitializePkcs11Session( pPkcs11Session );
@@ -1056,7 +1078,7 @@ static CK_RV setupPkcs11ObjectForAesCmac( const SntpAuthContext_t * pAuthContext
10561078
SntpStatus_t addClientAuthCode( SntpAuthContext_t * pAuthContext,
10571079
const SntpServerInfo_t * pTimeServer,
10581080
void * pRequestBuffer,
1059-
uint16_t bufferSize,
1081+
size_t bufferSize,
10601082
uint16_t * pAuthCodeSize )
10611083
{
10621084
CK_RV result = CKR_OK;
@@ -1279,7 +1301,7 @@ static uint32_t generateRandomNumber()
12791301
if( pkcs11Status == CKR_OK )
12801302
{
12811303
if( pFunctionList->C_GenerateRandom( session,
1282-
&randomNum,
1304+
( uint8_t * ) ( &randomNum ),
12831305
sizeof( randomNum ) ) != CKR_OK )
12841306
{
12851307
LogError( ( "Failed to generate random number. "
@@ -1304,7 +1326,7 @@ static uint32_t generateRandomNumber()
13041326
void initializeSystemClock( void )
13051327
{
13061328
/* On boot-up initialize the system time as the first second in the configured year. */
1307-
int64_t startupTimeInUnixSecs = translateYearToUnixSeconds( democonfigSYSTEM_START_YEAR );
1329+
uint32_t startupTimeInUnixSecs = translateYearToUnixSeconds( democonfigSYSTEM_START_YEAR );
13081330

13091331
systemClock.baseTime.secs = startupTimeInUnixSecs;
13101332
systemClock.baseTime.msecs = 0;
@@ -1429,7 +1451,6 @@ static bool createUdpSocket( Socket_t * pSocket )
14291451
static void closeUdpSocket( Socket_t * pSocket )
14301452
{
14311453
bool status = false;
1432-
struct freertos_sockaddr bindAddress;
14331454

14341455
configASSERT( pSocket != NULL );
14351456

@@ -1455,15 +1476,15 @@ static bool calculateBackoffForNextPoll( BackoffAlgorithmContext_t * pBackoffCon
14551476
if( shouldInitializeContext == true )
14561477
{
14571478
/* Initialize reconnect attempts and interval.*/
1458-
BackoffAlgorithm_InitializeParams( &pBackoffContext,
1479+
BackoffAlgorithm_InitializeParams( pBackoffContext,
14591480
minPollPeriod,
14601481
SNTP_DEMO_POLL_MAX_BACKOFF_DELAY_SEC,
14611482
SNTP_DEMO_MAX_SERVER_BACKOFF_RETRIES );
14621483
}
14631484

14641485
/* Generate a random number and calculate the new backoff poll period to wait before the next
14651486
* time poll attempt. */
1466-
status = BackoffAlgorithm_GetNextBackoff( &pBackoffContext, generateRandomNumber(), &newPollPeriod );
1487+
status = BackoffAlgorithm_GetNextBackoff( pBackoffContext, generateRandomNumber(), &newPollPeriod );
14671488

14681489
if( status == BackoffAlgorithmRetriesExhausted )
14691490
{

0 commit comments

Comments
 (0)