Skip to content

Commit d9c48b5

Browse files
committed
Increase ms interval values to 32-bit from 16-bit
1 parent 39bf8aa commit d9c48b5

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

docs/doxygen/code_examples/backoff_algorithm_posix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int main()
2121
BackoffAlgorithmStatus_t retryStatus = BackoffAlgorithmSuccess;
2222
BackoffAlgorithmContext_t retryParams;
2323
char serverAddress[] = "amazon.com";
24-
uint16_t nextRetryBackoff = 0;
24+
uint32_t nextRetryBackoff = 0;
2525

2626
/* Initialize reconnect attempts and interval. */
2727
BackoffAlgorithm_InitializeParams( &retryParams,

source/backoff_algorithm.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
BackoffAlgorithmStatus_t BackoffAlgorithm_GetNextBackoff( BackoffAlgorithmContext_t * pRetryContext,
4141
uint32_t randomValue,
42-
uint16_t * pNextBackOff )
42+
uint32_t * pNextBackOff )
4343
{
4444
BackoffAlgorithmStatus_t status = BackoffAlgorithmSuccess;
4545

@@ -50,11 +50,11 @@ BackoffAlgorithmStatus_t BackoffAlgorithm_GetNextBackoff( BackoffAlgorithmContex
5050
if( ( pRetryContext->maxRetryAttempts == BACKOFF_ALGORITHM_RETRY_FOREVER ) ||
5151
( pRetryContext->attemptsDone < pRetryContext->maxRetryAttempts ) )
5252
{
53-
/* The next backoff value is a random value between 0 and the maximum jitter value
53+
/* The next backoff value is a random value between 1 and the maximum jitter value
5454
* for the retry attempt. */
5555

56-
/* Choose a random value for back-off time between 0 and the max jitter value. */
57-
*pNextBackOff = ( uint16_t ) ( randomValue % ( pRetryContext->nextJitterMax + ( uint32_t ) 1U ) );
56+
/* Choose a random value for back-off time between 1 and the max jitter value. */
57+
*pNextBackOff = ( randomValue % pRetryContext->nextJitterMax ) + 1U;
5858

5959
/* Increment the retry attempt. */
6060
pRetryContext->attemptsDone++;
@@ -84,8 +84,8 @@ BackoffAlgorithmStatus_t BackoffAlgorithm_GetNextBackoff( BackoffAlgorithmContex
8484
/*-----------------------------------------------------------*/
8585

8686
void BackoffAlgorithm_InitializeParams( BackoffAlgorithmContext_t * pContext,
87-
uint16_t backOffBase,
88-
uint16_t maxBackOff,
87+
uint32_t backOffBase,
88+
uint32_t maxBackOff,
8989
uint32_t maxAttempts )
9090
{
9191
assert( pContext != NULL );

source/include/backoff_algorithm.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ typedef struct BackoffAlgorithmContext
7070
/**
7171
* @brief The maximum backoff delay (in milliseconds) between consecutive retry attempts.
7272
*/
73-
uint16_t maxBackoffDelay;
73+
uint32_t maxBackoffDelay;
7474

7575
/**
7676
* @brief The total number of retry attempts completed.
@@ -81,7 +81,7 @@ typedef struct BackoffAlgorithmContext
8181
/**
8282
* @brief The maximum backoff value (in milliseconds) for the next retry attempt.
8383
*/
84-
uint16_t nextJitterMax;
84+
uint32_t nextJitterMax;
8585

8686
/**
8787
* @brief The maximum number of retry attempts.
@@ -99,14 +99,14 @@ typedef struct BackoffAlgorithmContext
9999
* @param[in] maxBackOff The maximum backoff delay (in milliseconds) between
100100
* consecutive retry attempts.
101101
* @param[in] backOffBase The base value (in milliseconds) of backoff delay to
102-
* use in the exponential backoff and jitter model.
102+
* use in the exponential backoff and jitter model. Must be non-zero.
103103
* @param[in] maxAttempts The maximum number of retry attempts. Set the value to
104104
* #BACKOFF_ALGORITHM_RETRY_FOREVER to retry for ever.
105105
*/
106106
/* @[define_backoffalgorithm_initializeparams] */
107107
void BackoffAlgorithm_InitializeParams( BackoffAlgorithmContext_t * pContext,
108-
uint16_t backOffBase,
109-
uint16_t maxBackOff,
108+
uint32_t backOffBase,
109+
uint32_t maxBackOff,
110110
uint32_t maxAttempts );
111111
/* @[define_backoffalgorithm_initializeparams] */
112112

@@ -125,7 +125,7 @@ void BackoffAlgorithm_InitializeParams( BackoffAlgorithmContext_t * pContext,
125125
* for the next retry attempt. The value does not exceed the maximum backoff delay
126126
* configured in the context.
127127
*
128-
* @note For generating a random number, it is recommended to use a Random Number Generator
128+
* @note For generating a random number, it is recommended to use a random number generator
129129
* that is seeded with a device-specific entropy source so that possibility of collisions
130130
* between multiple devices retrying the network operations can be mitigated.
131131
*
@@ -135,7 +135,7 @@ void BackoffAlgorithm_InitializeParams( BackoffAlgorithmContext_t * pContext,
135135
/* @[define_backoffalgorithm_getnextbackoff] */
136136
BackoffAlgorithmStatus_t BackoffAlgorithm_GetNextBackoff( BackoffAlgorithmContext_t * pRetryContext,
137137
uint32_t randomValue,
138-
uint16_t * pNextBackOff );
138+
uint32_t * pNextBackOff );
139139
/* @[define_backoffalgorithm_getnextbackoff] */
140140

141141
/* *INDENT-OFF* */

test/unit-test/backoff_algorithm_utest.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
static BackoffAlgorithmContext_t retryParams;
4343
/* Return value of #BackoffAlgorithm_GetNextBackoff. */
4444
static BackoffAlgorithmStatus_t BackoffAlgorithmStatus;
45-
static uint16_t nextBackoff;
45+
static uint32_t nextBackoff;
4646
static uint32_t testRandomVal;
4747

4848
/* ============================ UNITY FIXTURES ============================ */
@@ -83,8 +83,8 @@ int suiteTearDown( int numFailures )
8383
*/
8484
static void verifyContextData( BackoffAlgorithmContext_t * pContext,
8585
uint32_t expectedAttemptsDone,
86-
uint16_t expectedNextJitterMax,
87-
uint16_t expectedMaxBackoff,
86+
uint32_t expectedNextJitterMax,
87+
uint32_t expectedMaxBackoff,
8888
uint32_t expectedMaxAttempts )
8989
{
9090
TEST_ASSERT_EQUAL( expectedNextJitterMax, pContext->nextJitterMax );
@@ -133,9 +133,9 @@ void test_BackoffAlgorithm_InitializeParams_Sets_Jitter_Correctly( void )
133133
void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_Less_Than_Jitter_Max( void )
134134
{
135135
int iter = 1;
136-
uint16_t expectedAttemptsDone = 0;
137-
uint16_t expectedNextJitterMax = TEST_BACKOFF_BASE_VALUE;
138-
uint16_t expectedNextBackoff = 0;
136+
uint32_t expectedAttemptsDone = 0;
137+
uint32_t expectedNextJitterMax = TEST_BACKOFF_BASE_VALUE;
138+
uint32_t expectedNextBackoff = 0;
139139

140140
for( ; iter < 2; iter++ )
141141
{
@@ -145,7 +145,7 @@ void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_Less_Than_Jitter_Max(
145145

146146
/* As the random value is less than the max jitter value, the expected
147147
* next backoff value should remain the same as the random value. */
148-
expectedNextBackoff = testRandomVal;
148+
expectedNextBackoff = ( testRandomVal % retryParams.nextJitterMax ) + 1U;
149149

150150
/* The jitter max value should double with the above call for use in next call. */
151151
expectedNextJitterMax *= 2;
@@ -180,8 +180,8 @@ void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_Less_Than_Jitter_Max(
180180
void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_More_Than_Jitter_Max( void )
181181
{
182182
int iter = 1;
183-
uint16_t expectedAttemptsDone = 0;
184-
uint16_t expectedNextJitterMax = TEST_BACKOFF_BASE_VALUE;
183+
uint32_t expectedAttemptsDone = 0;
184+
uint32_t expectedNextJitterMax = TEST_BACKOFF_BASE_VALUE;
185185

186186
for( ; iter < 2; iter++ )
187187
{
@@ -199,7 +199,7 @@ void test_BackoffAlgorithm_GetNextBackoff_Success_RandVal_More_Than_Jitter_Max(
199199
/* As the random value is greater than the jitter max value, the expected
200200
* next backoff value should be truncated to a value within the jitter window
201201
* for the retry attempt. */
202-
uint16_t expectedNextBackoff = ( testRandomVal % ( retryParams.nextJitterMax + 1U ) );
202+
uint32_t expectedNextBackoff = ( testRandomVal % retryParams.nextJitterMax ) + 1U;
203203

204204
/* Call the BackoffAlgorithm_GetNextBackoff API a couple times. */
205205
TEST_ASSERT_EQUAL( BackoffAlgorithmSuccess,
@@ -258,7 +258,7 @@ void test_BackoffAlgorithm_GetNextBackoff_Returns_Cap_Backoff( void )
258258
* Thus, the BackoffAlgorithm_GetNextBackoff API should return the random value as
259259
* the next back-off value. */
260260
testRandomVal = retryParams.nextJitterMax;
261-
uint16_t expectedBackoffVal = testRandomVal;
261+
uint16_t expectedBackoffVal = ( testRandomVal % retryParams.nextJitterMax ) + 1U;
262262

263263
/* Call the BackoffAlgorithm_GetNextBackoff API. */
264264
TEST_ASSERT_EQUAL( BackoffAlgorithmSuccess,
@@ -278,7 +278,7 @@ void test_BackoffAlgorithm_GetNextBackoff_Returns_Cap_Backoff( void )
278278
/* Now, set the random value as the maximum back-off value to
279279
* expect that the next back-off value returned by the API is the maximum
280280
* back-off value.*/
281-
testRandomVal = TEST_BACKOFF_MAX_VALUE;
281+
testRandomVal = TEST_BACKOFF_MAX_VALUE - 1;
282282

283283
/* Call BackoffAlgorithm_GetNextBackoff API again to verify that it now returns the
284284
* cap value as the next back-off value. */
@@ -326,7 +326,7 @@ void test_BackoffAlgorithm_GetNextBackoff_NextJitterMax_Below_Cap_Backoff( void
326326
TEST_ASSERT_EQUAL( BackoffAlgorithmSuccess,
327327
BackoffAlgorithm_GetNextBackoff( &retryParams, testRandomVal, &nextBackoff ) );
328328
/* Make sure that zero is returned as the next backoff value . */
329-
TEST_ASSERT_EQUAL( 0, nextBackoff );
329+
TEST_ASSERT_EQUAL( 1, nextBackoff );
330330

331331
/* Verify that the context data for expected data after the API call. */
332332
verifyContextData( &retryParams,

0 commit comments

Comments
 (0)