From 8d9e706aa7778cea843750a61ace9c926a9b73a6 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Tue, 11 Nov 2025 16:53:06 -0800 Subject: [PATCH] Increase ms interval values to 32-bit from 16-bit --- .../code_examples/backoff_algorithm_posix.c | 2 +- source/backoff_algorithm.c | 12 ++++----- source/include/backoff_algorithm.h | 14 +++++----- test/unit-test/backoff_algorithm_utest.c | 26 +++++++++---------- tools/unity/coverage.cmake | 3 +-- 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/docs/doxygen/code_examples/backoff_algorithm_posix.c b/docs/doxygen/code_examples/backoff_algorithm_posix.c index 978a63e..5683113 100644 --- a/docs/doxygen/code_examples/backoff_algorithm_posix.c +++ b/docs/doxygen/code_examples/backoff_algorithm_posix.c @@ -21,7 +21,7 @@ int main() BackoffAlgorithmStatus_t retryStatus = BackoffAlgorithmSuccess; BackoffAlgorithmContext_t retryParams; char serverAddress[] = "amazon.com"; - uint16_t nextRetryBackoff = 0; + uint32_t nextRetryBackoff = 0; /* Initialize reconnect attempts and interval. */ BackoffAlgorithm_InitializeParams( &retryParams, diff --git a/source/backoff_algorithm.c b/source/backoff_algorithm.c index bd8916f..10e9574 100644 --- a/source/backoff_algorithm.c +++ b/source/backoff_algorithm.c @@ -39,7 +39,7 @@ BackoffAlgorithmStatus_t BackoffAlgorithm_GetNextBackoff( BackoffAlgorithmContext_t * pRetryContext, uint32_t randomValue, - uint16_t * pNextBackOff ) + uint32_t * pNextBackOff ) { BackoffAlgorithmStatus_t status = BackoffAlgorithmSuccess; @@ -50,11 +50,11 @@ BackoffAlgorithmStatus_t BackoffAlgorithm_GetNextBackoff( BackoffAlgorithmContex if( ( pRetryContext->maxRetryAttempts == BACKOFF_ALGORITHM_RETRY_FOREVER ) || ( pRetryContext->attemptsDone < pRetryContext->maxRetryAttempts ) ) { - /* The next backoff value is a random value between 0 and the maximum jitter value + /* The next backoff value is a random value between 1 and the maximum jitter value * for the retry attempt. */ - /* Choose a random value for back-off time between 0 and the max jitter value. */ - *pNextBackOff = ( uint16_t ) ( randomValue % ( pRetryContext->nextJitterMax + ( uint32_t ) 1U ) ); + /* Choose a random value for back-off time between 1 and the max jitter value. */ + *pNextBackOff = ( randomValue % pRetryContext->nextJitterMax ) + 1U; /* Increment the retry attempt. */ pRetryContext->attemptsDone++; @@ -84,8 +84,8 @@ BackoffAlgorithmStatus_t BackoffAlgorithm_GetNextBackoff( BackoffAlgorithmContex /*-----------------------------------------------------------*/ void BackoffAlgorithm_InitializeParams( BackoffAlgorithmContext_t * pContext, - uint16_t backOffBase, - uint16_t maxBackOff, + uint32_t backOffBase, + uint32_t maxBackOff, uint32_t maxAttempts ) { assert( pContext != NULL ); diff --git a/source/include/backoff_algorithm.h b/source/include/backoff_algorithm.h index 9b72e84..9e14f6f 100644 --- a/source/include/backoff_algorithm.h +++ b/source/include/backoff_algorithm.h @@ -70,7 +70,7 @@ typedef struct BackoffAlgorithmContext /** * @brief The maximum backoff delay (in milliseconds) between consecutive retry attempts. */ - uint16_t maxBackoffDelay; + uint32_t maxBackoffDelay; /** * @brief The total number of retry attempts completed. @@ -81,7 +81,7 @@ typedef struct BackoffAlgorithmContext /** * @brief The maximum backoff value (in milliseconds) for the next retry attempt. */ - uint16_t nextJitterMax; + uint32_t nextJitterMax; /** * @brief The maximum number of retry attempts. @@ -99,14 +99,14 @@ typedef struct BackoffAlgorithmContext * @param[in] maxBackOff The maximum backoff delay (in milliseconds) between * consecutive retry attempts. * @param[in] backOffBase The base value (in milliseconds) of backoff delay to - * use in the exponential backoff and jitter model. + * use in the exponential backoff and jitter model. Must be non-zero. * @param[in] maxAttempts The maximum number of retry attempts. Set the value to * #BACKOFF_ALGORITHM_RETRY_FOREVER to retry for ever. */ /* @[define_backoffalgorithm_initializeparams] */ void BackoffAlgorithm_InitializeParams( BackoffAlgorithmContext_t * pContext, - uint16_t backOffBase, - uint16_t maxBackOff, + uint32_t backOffBase, + uint32_t maxBackOff, uint32_t maxAttempts ); /* @[define_backoffalgorithm_initializeparams] */ @@ -125,7 +125,7 @@ void BackoffAlgorithm_InitializeParams( BackoffAlgorithmContext_t * pContext, * for the next retry attempt. The value does not exceed the maximum backoff delay * configured in the context. * - * @note For generating a random number, it is recommended to use a Random Number Generator + * @note For generating a random number, it is recommended to use a random number generator * that is seeded with a device-specific entropy source so that possibility of collisions * between multiple devices retrying the network operations can be mitigated. * @@ -135,7 +135,7 @@ void BackoffAlgorithm_InitializeParams( BackoffAlgorithmContext_t * pContext, /* @[define_backoffalgorithm_getnextbackoff] */ BackoffAlgorithmStatus_t BackoffAlgorithm_GetNextBackoff( BackoffAlgorithmContext_t * pRetryContext, uint32_t randomValue, - uint16_t * pNextBackOff ); + uint32_t * pNextBackOff ); /* @[define_backoffalgorithm_getnextbackoff] */ /* *INDENT-OFF* */ diff --git a/test/unit-test/backoff_algorithm_utest.c b/test/unit-test/backoff_algorithm_utest.c index 2dfecd4..4ca7e81 100644 --- a/test/unit-test/backoff_algorithm_utest.c +++ b/test/unit-test/backoff_algorithm_utest.c @@ -42,7 +42,7 @@ static BackoffAlgorithmContext_t retryParams; /* Return value of #BackoffAlgorithm_GetNextBackoff. */ static BackoffAlgorithmStatus_t BackoffAlgorithmStatus; -static uint16_t nextBackoff; +static uint32_t nextBackoff; static uint32_t testRandomVal; /* ============================ UNITY FIXTURES ============================ */ @@ -83,8 +83,8 @@ int suiteTearDown( int numFailures ) */ static void verifyContextData( BackoffAlgorithmContext_t * pContext, uint32_t expectedAttemptsDone, - uint16_t expectedNextJitterMax, - uint16_t expectedMaxBackoff, + uint32_t expectedNextJitterMax, + uint32_t expectedMaxBackoff, uint32_t expectedMaxAttempts ) { TEST_ASSERT_EQUAL( expectedNextJitterMax, pContext->nextJitterMax ); @@ -133,9 +133,9 @@ void test_BackoffAlgorithm_InitializeParams_Sets_Jitter_Correctly( void ) void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_Less_Than_Jitter_Max( void ) { int iter = 1; - uint16_t expectedAttemptsDone = 0; - uint16_t expectedNextJitterMax = TEST_BACKOFF_BASE_VALUE; - uint16_t expectedNextBackoff = 0; + uint32_t expectedAttemptsDone = 0; + uint32_t expectedNextJitterMax = TEST_BACKOFF_BASE_VALUE; + uint32_t expectedNextBackoff = 0; for( ; iter < 2; iter++ ) { @@ -145,7 +145,7 @@ void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_Less_Than_Jitter_Max( /* As the random value is less than the max jitter value, the expected * next backoff value should remain the same as the random value. */ - expectedNextBackoff = testRandomVal; + expectedNextBackoff = ( testRandomVal % retryParams.nextJitterMax ) + 1U; /* The jitter max value should double with the above call for use in next call. */ expectedNextJitterMax *= 2; @@ -180,8 +180,8 @@ void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_Less_Than_Jitter_Max( void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_More_Than_Jitter_Max( void ) { int iter = 1; - uint16_t expectedAttemptsDone = 0; - uint16_t expectedNextJitterMax = TEST_BACKOFF_BASE_VALUE; + uint32_t expectedAttemptsDone = 0; + uint32_t expectedNextJitterMax = TEST_BACKOFF_BASE_VALUE; for( ; iter < 2; iter++ ) { @@ -199,7 +199,7 @@ void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_More_Than_Jitter_Max( /* As the random value is greater than the jitter max value, the expected * next backoff value should be truncated to a value within the jitter window * for the retry attempt. */ - uint16_t expectedNextBackoff = ( testRandomVal % ( retryParams.nextJitterMax + 1U ) ); + uint32_t expectedNextBackoff = ( testRandomVal % retryParams.nextJitterMax ) + 1U; /* Call the BackoffAlgorithm_GetNextBackoff API a couple times. */ TEST_ASSERT_EQUAL( BackoffAlgorithmSuccess, @@ -258,7 +258,7 @@ void test_BackoffAlgorithm_GetNextBackoff_Returns_Cap_Backoff( void ) * Thus, the BackoffAlgorithm_GetNextBackoff API should return the random value as * the next back-off value. */ testRandomVal = retryParams.nextJitterMax; - uint16_t expectedBackoffVal = testRandomVal; + uint16_t expectedBackoffVal = ( testRandomVal % retryParams.nextJitterMax ) + 1U; /* Call the BackoffAlgorithm_GetNextBackoff API. */ TEST_ASSERT_EQUAL( BackoffAlgorithmSuccess, @@ -278,7 +278,7 @@ void test_BackoffAlgorithm_GetNextBackoff_Returns_Cap_Backoff( void ) /* Now, set the random value as the maximum back-off value to * expect that the next back-off value returned by the API is the maximum * back-off value.*/ - testRandomVal = TEST_BACKOFF_MAX_VALUE; + testRandomVal = TEST_BACKOFF_MAX_VALUE - 1; /* Call BackoffAlgorithm_GetNextBackoff API again to verify that it now returns the * cap value as the next back-off value. */ @@ -326,7 +326,7 @@ void test_BackoffAlgorithm_GetNextBackoff_NextJitterMax_Below_Cap_Backoff( void TEST_ASSERT_EQUAL( BackoffAlgorithmSuccess, BackoffAlgorithm_GetNextBackoff( &retryParams, testRandomVal, &nextBackoff ) ); /* Make sure that zero is returned as the next backoff value . */ - TEST_ASSERT_EQUAL( 0, nextBackoff ); + TEST_ASSERT_EQUAL( 1, nextBackoff ); /* Verify that the context data for expected data after the API call. */ verifyContextData( &retryParams, diff --git a/tools/unity/coverage.cmake b/tools/unity/coverage.cmake index 3cdbd31..c5f3eeb 100644 --- a/tools/unity/coverage.cmake +++ b/tools/unity/coverage.cmake @@ -52,11 +52,10 @@ execute_process( --output-file ${CMAKE_BINARY_DIR}/second_coverage.info ) -# combile baseline results (zeros) with the one after running the tests +# Skip baseline capture and use only test coverage execute_process( COMMAND lcov --base-directory ${CMAKE_BINARY_DIR} --directory ${CMAKE_BINARY_DIR} - --add-tracefile ${CMAKE_BINARY_DIR}/base_coverage.info --add-tracefile ${CMAKE_BINARY_DIR}/second_coverage.info --output-file ${CMAKE_BINARY_DIR}/coverage.info --no-external