Skip to content

Commit

Permalink
Merge branch 'main' into update_readme_tcp_qemu
Browse files Browse the repository at this point in the history
  • Loading branch information
kar-rahul-aws committed May 6, 2024
2 parents 9e2115d + 6cda0cf commit 42ad45e
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions FreeRTOS-Plus/Demo/coreSNTP_Windows_Simulator/SNTPClientTask.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );


Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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 );
}
}

Expand Down Expand Up @@ -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 );
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. "
Expand All @@ -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;
Expand Down Expand Up @@ -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 );

Expand All @@ -1455,15 +1476,15 @@ 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 );
}

/* 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 )
{
Expand Down

0 comments on commit 42ad45e

Please sign in to comment.