mirror of
https://github.com/FreeRTOS/FreeRTOS-Kernel.git
synced 2025-10-18 02:37:47 -04:00
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 <action@github.com> Co-authored-by: Ching-Hsin,Lee <chinglee@amazon.com>
This commit is contained in:
parent
8f3277f7eb
commit
6cda0cf7fc
1 changed files with 34 additions and 13 deletions
|
@ -550,7 +550,7 @@ static void populateAuthContextForServer( const char * pServer,
|
||||||
static SntpStatus_t addClientAuthCode( SntpAuthContext_t * pAuthContext,
|
static SntpStatus_t addClientAuthCode( SntpAuthContext_t * pAuthContext,
|
||||||
const SntpServerInfo_t * pTimeServer,
|
const SntpServerInfo_t * pTimeServer,
|
||||||
void * pRequestBuffer,
|
void * pRequestBuffer,
|
||||||
uint16_t bufferSize,
|
size_t bufferSize,
|
||||||
uint16_t * pAuthCodeSize );
|
uint16_t * pAuthCodeSize );
|
||||||
|
|
||||||
|
|
||||||
|
@ -586,7 +586,7 @@ static SntpStatus_t addClientAuthCode( SntpAuthContext_t * pAuthContext,
|
||||||
static SntpStatus_t validateServerAuth( SntpAuthContext_t * pAuthContext,
|
static SntpStatus_t validateServerAuth( SntpAuthContext_t * pAuthContext,
|
||||||
const SntpServerInfo_t * pTimeServer,
|
const SntpServerInfo_t * pTimeServer,
|
||||||
const void * pResponseData,
|
const void * pResponseData,
|
||||||
size_t responseSize );
|
uint16_t responseSize );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Generates a random number using PKCS#11.
|
* @brief Generates a random number using PKCS#11.
|
||||||
|
@ -668,6 +668,7 @@ void calculateCurrentTime( UTCTime_t * pBaseTime,
|
||||||
UTCTime_t * pCurrentTime )
|
UTCTime_t * pCurrentTime )
|
||||||
{
|
{
|
||||||
uint64_t msElapsedSinceLastSync = 0;
|
uint64_t msElapsedSinceLastSync = 0;
|
||||||
|
uint64_t currentTimeSecs;
|
||||||
TickType_t ticksElapsedSinceLastSync = xTaskGetTickCount() - lastSyncTickCount;
|
TickType_t ticksElapsedSinceLastSync = xTaskGetTickCount() - lastSyncTickCount;
|
||||||
|
|
||||||
/* Calculate time elapsed since last synchronization according to the number
|
/* 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. */
|
/* Set the current UTC time in the output parameter. */
|
||||||
if( msElapsedSinceLastSync >= 1000 )
|
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;
|
pCurrentTime->msecs = msElapsedSinceLastSync % 1000;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pCurrentTime->secs = pBaseTime->secs;
|
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 )
|
static void sntpClient_GetTime( SntpTimestamp_t * pCurrentTime )
|
||||||
{
|
{
|
||||||
UTCTime_t currentTime;
|
UTCTime_t currentTime;
|
||||||
uint64_t ntpSecs;
|
uint32_t ntpSecs;
|
||||||
|
|
||||||
/* Obtain mutex for accessing system clock variables */
|
/* Obtain mutex for accessing system clock variables */
|
||||||
xSemaphoreTake( xMutex, portMAX_DELAY );
|
xSemaphoreTake( xMutex, portMAX_DELAY );
|
||||||
|
@ -862,8 +880,12 @@ static void sntpClient_GetTime( SntpTimestamp_t * pCurrentTime )
|
||||||
* converting from UNIX time to SNTP timestamp. */
|
* converting from UNIX time to SNTP timestamp. */
|
||||||
if( ntpSecs > UINT32_MAX )
|
if( ntpSecs > UINT32_MAX )
|
||||||
{
|
{
|
||||||
|
/* Assert when SNTP time rollover. */
|
||||||
|
configASSERT( !( ntpSecs > UINT32_MAX ) );
|
||||||
|
|
||||||
/* Subtract an extra second as timestamp 0 represents the epoch for
|
/* Subtract an extra second as timestamp 0 represents the epoch for
|
||||||
* NTP era 1. */
|
* NTP era 1. */
|
||||||
|
LogWarn( ( "SNTP timestamp rollover." ) );
|
||||||
pCurrentTime->seconds = ntpSecs - UINT32_MAX - 1;
|
pCurrentTime->seconds = ntpSecs - UINT32_MAX - 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -977,7 +999,7 @@ static void populateAuthContextForServer( const char * pServer,
|
||||||
for( index = 0; index < strlen( pKeyHexString ); index += 2 )
|
for( index = 0; index < strlen( pKeyHexString ); index += 2 )
|
||||||
{
|
{
|
||||||
char byteString[ 3 ] = { pKeyHexString[ index ], pKeyHexString[ index + 1 ], '\0' };
|
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;
|
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. */
|
/* 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 );
|
aes_cmac_template[ 6 ].ulValueLen = sizeof( pAuthContext->pAuthKey );
|
||||||
|
|
||||||
result = xInitializePkcs11Session( pPkcs11Session );
|
result = xInitializePkcs11Session( pPkcs11Session );
|
||||||
|
@ -1056,7 +1078,7 @@ static CK_RV setupPkcs11ObjectForAesCmac( const SntpAuthContext_t * pAuthContext
|
||||||
SntpStatus_t addClientAuthCode( SntpAuthContext_t * pAuthContext,
|
SntpStatus_t addClientAuthCode( SntpAuthContext_t * pAuthContext,
|
||||||
const SntpServerInfo_t * pTimeServer,
|
const SntpServerInfo_t * pTimeServer,
|
||||||
void * pRequestBuffer,
|
void * pRequestBuffer,
|
||||||
uint16_t bufferSize,
|
size_t bufferSize,
|
||||||
uint16_t * pAuthCodeSize )
|
uint16_t * pAuthCodeSize )
|
||||||
{
|
{
|
||||||
CK_RV result = CKR_OK;
|
CK_RV result = CKR_OK;
|
||||||
|
@ -1279,7 +1301,7 @@ static uint32_t generateRandomNumber()
|
||||||
if( pkcs11Status == CKR_OK )
|
if( pkcs11Status == CKR_OK )
|
||||||
{
|
{
|
||||||
if( pFunctionList->C_GenerateRandom( session,
|
if( pFunctionList->C_GenerateRandom( session,
|
||||||
&randomNum,
|
( uint8_t * ) ( &randomNum ),
|
||||||
sizeof( randomNum ) ) != CKR_OK )
|
sizeof( randomNum ) ) != CKR_OK )
|
||||||
{
|
{
|
||||||
LogError( ( "Failed to generate random number. "
|
LogError( ( "Failed to generate random number. "
|
||||||
|
@ -1304,7 +1326,7 @@ static uint32_t generateRandomNumber()
|
||||||
void initializeSystemClock( void )
|
void initializeSystemClock( void )
|
||||||
{
|
{
|
||||||
/* On boot-up initialize the system time as the first second in the configured year. */
|
/* 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.secs = startupTimeInUnixSecs;
|
||||||
systemClock.baseTime.msecs = 0;
|
systemClock.baseTime.msecs = 0;
|
||||||
|
@ -1429,7 +1451,6 @@ static bool createUdpSocket( Socket_t * pSocket )
|
||||||
static void closeUdpSocket( Socket_t * pSocket )
|
static void closeUdpSocket( Socket_t * pSocket )
|
||||||
{
|
{
|
||||||
bool status = false;
|
bool status = false;
|
||||||
struct freertos_sockaddr bindAddress;
|
|
||||||
|
|
||||||
configASSERT( pSocket != NULL );
|
configASSERT( pSocket != NULL );
|
||||||
|
|
||||||
|
@ -1455,7 +1476,7 @@ static bool calculateBackoffForNextPoll( BackoffAlgorithmContext_t * pBackoffCon
|
||||||
if( shouldInitializeContext == true )
|
if( shouldInitializeContext == true )
|
||||||
{
|
{
|
||||||
/* Initialize reconnect attempts and interval.*/
|
/* Initialize reconnect attempts and interval.*/
|
||||||
BackoffAlgorithm_InitializeParams( &pBackoffContext,
|
BackoffAlgorithm_InitializeParams( pBackoffContext,
|
||||||
minPollPeriod,
|
minPollPeriod,
|
||||||
SNTP_DEMO_POLL_MAX_BACKOFF_DELAY_SEC,
|
SNTP_DEMO_POLL_MAX_BACKOFF_DELAY_SEC,
|
||||||
SNTP_DEMO_MAX_SERVER_BACKOFF_RETRIES );
|
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
|
/* Generate a random number and calculate the new backoff poll period to wait before the next
|
||||||
* time poll attempt. */
|
* time poll attempt. */
|
||||||
status = BackoffAlgorithm_GetNextBackoff( &pBackoffContext, generateRandomNumber(), &newPollPeriod );
|
status = BackoffAlgorithm_GetNextBackoff( pBackoffContext, generateRandomNumber(), &newPollPeriod );
|
||||||
|
|
||||||
if( status == BackoffAlgorithmRetriesExhausted )
|
if( status == BackoffAlgorithmRetriesExhausted )
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue