From 7b4f726031104e5d8e4683cc74b8f50dd1b53450 Mon Sep 17 00:00:00 2001 From: Archit Aggarwal Date: Tue, 24 Nov 2020 11:55:14 -0800 Subject: [PATCH] Replace retry-utils with submodule to FreeRTOS/backoffAlgorithm (#1401) Use the new location of "retry utils" library which is the new `FreeRTOS/backoffAlgorithm` repository. Changes include: * Adding the submodule * Changing calling code of demos and tests to use the renamed API * Updating build files of demos/tests to use the submodule files --- .gitmodules | 4 + CMakeLists.txt | 3 +- .../defender_demo_json/CMakeLists.txt | 8 +- .../defender_demo_json/mqtt_operations.c | 30 +- demos/http/common/src/http_demo_utils.c | 28 +- demos/http/http_demo_basic_tls/CMakeLists.txt | 10 +- .../http_demo_basic_tls/http_demo_basic_tls.c | 4 +- .../http/http_demo_mutual_auth/CMakeLists.txt | 10 +- demos/http/http_demo_plaintext/CMakeLists.txt | 10 +- .../http/http_demo_s3_download/CMakeLists.txt | 10 +- .../http_demo_s3_download.c | 4 +- .../CMakeLists.txt | 10 +- .../http_demo_s3_download_multithreaded.c | 4 +- demos/http/http_demo_s3_upload/CMakeLists.txt | 10 +- .../http_demo_s3_upload/http_demo_s3_upload.c | 4 +- demos/mqtt/mqtt_demo_basic_tls/CMakeLists.txt | 6 +- .../mqtt_demo_basic_tls/mqtt_demo_basic_tls.c | 54 +-- .../mqtt/mqtt_demo_mutual_auth/CMakeLists.txt | 6 +- .../mqtt_demo_mutual_auth.c | 54 +-- demos/mqtt/mqtt_demo_plaintext/CMakeLists.txt | 6 +- .../mqtt_demo_plaintext/mqtt_demo_plaintext.c | 54 +-- .../mqtt/mqtt_demo_serializer/CMakeLists.txt | 6 +- .../mqtt_demo_serializer.c | 54 +-- .../CMakeLists.txt | 6 +- .../mqtt_demo_subscription_manager.c | 30 +- .../subscription-manager/CMakeLists.txt | 3 + .../pkcs11/pkcs11_demo_objects/CMakeLists.txt | 2 +- demos/shadow/shadow_demo_main/CMakeLists.txt | 6 +- .../shadow_demo_main/shadow_demo_helpers.c | 30 +- integration-test/http/CMakeLists.txt | 7 +- integration-test/http/http_system_test.c | 28 +- libraries/CMakeLists.txt | 1 - libraries/standard/backoffAlgorithm | 1 + libraries/standard/retry-utils/CMakeLists.txt | 15 - .../retry-utils/retryUtilsFilePaths.cmake | 14 - .../retry-utils/source/include/retry_utils.h | 174 -------- .../standard/retry-utils/source/retry_utils.c | 110 ------ .../standard/retry-utils/utest/CMakeLists.txt | 70 ---- .../retry-utils/utest/retry_utils_utest.c | 372 ------------------ platform/lexicon.txt | 2 - 40 files changed, 283 insertions(+), 977 deletions(-) create mode 160000 libraries/standard/backoffAlgorithm delete mode 100644 libraries/standard/retry-utils/CMakeLists.txt delete mode 100644 libraries/standard/retry-utils/retryUtilsFilePaths.cmake delete mode 100644 libraries/standard/retry-utils/source/include/retry_utils.h delete mode 100644 libraries/standard/retry-utils/source/retry_utils.c delete mode 100644 libraries/standard/retry-utils/utest/CMakeLists.txt delete mode 100644 libraries/standard/retry-utils/utest/retry_utils_utest.c diff --git a/.gitmodules b/.gitmodules index cafea65264..0fd5ec3505 100644 --- a/.gitmodules +++ b/.gitmodules @@ -29,3 +29,7 @@ path = libraries/standard/corePKCS11 branch = main url = https://github.com/FreeRTOS/corePKCS11.git +[submodule "libraries/standard/backoffAlgorithm"] + path = libraries/standard/backoffAlgorithm + branch = main + url = https://github.com/FreeRTOS/backoffAlgorithm.git diff --git a/CMakeLists.txt b/CMakeLists.txt index edfb5903db..169f8635ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,8 +73,7 @@ if(${BUILD_TESTS}) # Create a list for each unit test target. set(utest_targets openssl_utest sockets_utest - plaintext_utest clock_utest - retry_utils_utest) + plaintext_utest clock_utest) # Add a target for running coverage on tests. add_custom_target(coverage diff --git a/demos/defender/defender_demo_json/CMakeLists.txt b/demos/defender/defender_demo_json/CMakeLists.txt index 5134e07b12..067143f069 100644 --- a/demos/defender/defender_demo_json/CMakeLists.txt +++ b/demos/defender/defender_demo_json/CMakeLists.txt @@ -5,6 +5,9 @@ set( DEMO_NAME "defender_demo" ) # Include MQTT library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreMQTT/mqttFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Include JSON library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreJSON/jsonFilePaths.cmake ) @@ -19,6 +22,7 @@ add_executable( ${DEMO_NAME} "report_builder.c" ${MQTT_SOURCES} ${MQTT_SERIALIZER_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ${JSON_SOURCES} ${DEFENDER_SOURCES} ) @@ -27,12 +31,12 @@ check_aws_credentials( ${DEMO_NAME} ) target_link_libraries( ${DEMO_NAME} PRIVATE clock_posix - openssl_posix - retry_utils ) + openssl_posix ) target_include_directories( ${DEMO_NAME} PUBLIC ${LOGGING_INCLUDE_DIRS} ${MQTT_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${JSON_INCLUDE_PUBLIC_DIRS} ${DEFENDER_INCLUDE_PUBLIC_DIRS} ${CMAKE_CURRENT_LIST_DIR} ) diff --git a/demos/defender/defender_demo_json/mqtt_operations.c b/demos/defender/defender_demo_json/mqtt_operations.c index 9aa4fb09e8..b40a7e674a 100644 --- a/demos/defender/defender_demo_json/mqtt_operations.c +++ b/demos/defender/defender_demo_json/mqtt_operations.c @@ -50,8 +50,8 @@ /* OpenSSL sockets transport implementation. */ #include "openssl_posix.h" -/* Reconnect parameters. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Clock for timer. */ #include "clock.h" @@ -253,7 +253,7 @@ static MQTTPublishCallback_t appPublishCallback = NULL; /** * @brief The random number generator to use for exponential backoff with * jitter retry logic. - * This function is an implementation the #RetryUtils_RNG_t interface type + * This function is an implementation the #BackoffAlgorithm_RNG_t interface type * of the retry utils library API. * * @return The generated random number. This function ALWAYS succeeds @@ -341,9 +341,9 @@ static int32_t generateRandomNumber() static bool connectToBrokerWithBackoffRetries( NetworkContext_t * pNetworkContext ) { bool returnStatus = false; - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; OpensslStatus_t opensslStatus = OPENSSL_SUCCESS; - RetryUtilsContext_t reconnectParams; + BackoffAlgorithmContext_t reconnectParams; ServerInfo_t serverInfo; OpensslCredentials_t opensslCredentials; uint16_t nextRetryBackOff = 0U; @@ -381,11 +381,11 @@ static bool connectToBrokerWithBackoffRetries( NetworkContext_t * pNetworkContex srand( tp.tv_nsec ); /* Initialize reconnect attempts and interval */ - RetryUtils_InitializeParams( &reconnectParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &reconnectParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); /* Attempt to connect to MQTT broker. If connection fails, retry after * a timeout. Timeout value will exponentially increase until maximum @@ -413,20 +413,20 @@ static bool connectToBrokerWithBackoffRetries( NetworkContext_t * pNetworkContex else { /* Get back-off value (in milliseconds) for the next connection retry. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &reconnectParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsRetriesExhausted ) + if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted ) { LogError( ( "Connection to the broker failed, all attempts exhausted." ) ); } - else if( retryUtilsStatus == RetryUtilsSuccess ) + else if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Connection to the broker failed. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); } } - } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); return returnStatus; } diff --git a/demos/http/common/src/http_demo_utils.c b/demos/http/common/src/http_demo_utils.c index 339b3671cd..e0a58f50e1 100644 --- a/demos/http/common/src/http_demo_utils.c +++ b/demos/http/common/src/http_demo_utils.c @@ -34,8 +34,8 @@ /* Demo utils header. */ #include "http_demo_utils.h" -/* Retry utilities. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Third party parser utilities. */ #include "http_parser.h" @@ -67,7 +67,7 @@ /** * @brief The random number generator to use for exponential backoff with * jitter retry logic. - * This function is an implementation the #RetryUtils_RNG_t interface type + * This function is an implementation the #BackoffAlgorithm_RNG_t interface type * of the retry utils library API. * * @return The generated random number. This function ALWAYS succeeds @@ -89,9 +89,9 @@ int32_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction, { int32_t returnStatus = EXIT_FAILURE; /* Status returned by the retry utilities. */ - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; /* Struct containing the next backoff time. */ - RetryUtilsContext_t reconnectParams; + BackoffAlgorithmContext_t reconnectParams; uint16_t nextRetryBackOff = 0U; struct timespec tp; @@ -106,11 +106,11 @@ int32_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction, srand( tp.tv_nsec ); /* Initialize reconnect attempts and interval */ - RetryUtils_InitializeParams( &reconnectParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &reconnectParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); /* Attempt to connect to HTTP server. If connection fails, retry after * a timeout. Timeout value will exponentially increase until maximum @@ -122,10 +122,10 @@ int32_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction, if( returnStatus != EXIT_SUCCESS ) { /* Get back-off value (in milliseconds) for the next connection retry. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &reconnectParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsSuccess ) + if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Connection to the HTTP server failed. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); @@ -135,7 +135,7 @@ int32_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction, LogError( ( "Connection to the HTTP server failed, all attempts exhausted." ) ); } } - } while( ( returnStatus == EXIT_FAILURE ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( returnStatus == EXIT_FAILURE ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); if( returnStatus == EXIT_FAILURE ) { diff --git a/demos/http/http_demo_basic_tls/CMakeLists.txt b/demos/http/http_demo_basic_tls/CMakeLists.txt index 774ec94c17..64a8de81a6 100644 --- a/demos/http/http_demo_basic_tls/CMakeLists.txt +++ b/demos/http/http_demo_basic_tls/CMakeLists.txt @@ -3,27 +3,31 @@ set( DEMO_NAME "http_demo_basic_tls" ) # Include HTTP library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Demo target. add_executable( ${DEMO_NAME} "${DEMO_NAME}.c" - "../common/src/http_demo_utils.c" + "${DEMOS_DIR}/http/common/src/http_demo_utils.c" ${HTTP_SOURCES} ${HTTP_THIRD_PARTY_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) target_link_libraries( ${DEMO_NAME} PRIVATE openssl_posix - retry_utils ) target_include_directories( ${DEMO_NAME} PUBLIC - "../common/include" + "${DEMOS_DIR}/http/common/include" ${HTTP_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${LOGGING_INCLUDE_DIRS} ) diff --git a/demos/http/http_demo_basic_tls/http_demo_basic_tls.c b/demos/http/http_demo_basic_tls/http_demo_basic_tls.c index 2f55f325fb..e96d459733 100644 --- a/demos/http/http_demo_basic_tls/http_demo_basic_tls.c +++ b/demos/http/http_demo_basic_tls/http_demo_basic_tls.c @@ -40,8 +40,8 @@ /* OpenSSL transport header. */ #include "openssl_posix.h" -/* Retry utilities. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Check that hostname of the server is defined. */ #ifndef SERVER_HOST diff --git a/demos/http/http_demo_mutual_auth/CMakeLists.txt b/demos/http/http_demo_mutual_auth/CMakeLists.txt index 02f941d2a1..5ab4a761f1 100644 --- a/demos/http/http_demo_mutual_auth/CMakeLists.txt +++ b/demos/http/http_demo_mutual_auth/CMakeLists.txt @@ -5,13 +5,17 @@ set( DEMO_NAME "http_demo_mutual_auth" ) # Include HTTP library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Demo target. add_executable( ${DEMO_NAME} "${DEMO_NAME}.c" - "../common/src/http_demo_utils.c" + "${DEMOS_DIR}/http/common/src/http_demo_utils.c" ${HTTP_SOURCES} ${HTTP_THIRD_PARTY_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) # Add to default target if all required macros needed to run this demo are defined @@ -21,14 +25,14 @@ target_link_libraries( ${DEMO_NAME} PRIVATE openssl_posix - retry_utils ) target_include_directories( ${DEMO_NAME} PUBLIC - "../common/include" + "${DEMOS_DIR}/http/common/include" ${HTTP_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${LOGGING_INCLUDE_DIRS} ) diff --git a/demos/http/http_demo_plaintext/CMakeLists.txt b/demos/http/http_demo_plaintext/CMakeLists.txt index 9c64a83911..6b1bc32d17 100644 --- a/demos/http/http_demo_plaintext/CMakeLists.txt +++ b/demos/http/http_demo_plaintext/CMakeLists.txt @@ -3,27 +3,31 @@ set( DEMO_NAME "http_demo_plaintext" ) # Include HTTP library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Demo target. add_executable( ${DEMO_NAME} "${DEMO_NAME}.c" - "../common/src/http_demo_utils.c" + "${DEMOS_DIR}/http/common/src/http_demo_utils.c" ${HTTP_SOURCES} ${HTTP_THIRD_PARTY_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) target_link_libraries( ${DEMO_NAME} PRIVATE plaintext_posix - retry_utils ) target_include_directories( ${DEMO_NAME} PUBLIC - "../common/include" + "${DEMOS_DIR}/http/common/include" ${HTTP_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${LOGGING_INCLUDE_DIRS} ) diff --git a/demos/http/http_demo_s3_download/CMakeLists.txt b/demos/http/http_demo_s3_download/CMakeLists.txt index 40246425ae..b8693d7d3b 100644 --- a/demos/http/http_demo_s3_download/CMakeLists.txt +++ b/demos/http/http_demo_s3_download/CMakeLists.txt @@ -3,13 +3,17 @@ set( DEMO_NAME "http_demo_s3_download" ) # Include HTTP library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Demo target. add_executable( ${DEMO_NAME} "${DEMO_NAME}.c" - "../common/src/http_demo_utils.c" + "${DEMOS_DIR}/http/common/src/http_demo_utils.c" ${HTTP_SOURCES} ${HTTP_THIRD_PARTY_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) # Add to default target if all required macros needed to run this demo are defined @@ -19,14 +23,14 @@ target_link_libraries( ${DEMO_NAME} PRIVATE openssl_posix - retry_utils ) target_include_directories( ${DEMO_NAME} PUBLIC - "../common/include" + "${DEMOS_DIR}/http/common/include" ${HTTP_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${HTTP_INCLUDE_THIRD_PARTY_DIRS} ${HTTP_INCLUDE_PRIVATE_DIRS} ${CMAKE_CURRENT_LIST_DIR} diff --git a/demos/http/http_demo_s3_download/http_demo_s3_download.c b/demos/http/http_demo_s3_download/http_demo_s3_download.c index a2db50d389..c3c1244ce1 100644 --- a/demos/http/http_demo_s3_download/http_demo_s3_download.c +++ b/demos/http/http_demo_s3_download/http_demo_s3_download.c @@ -41,8 +41,8 @@ /* OpenSSL transport header. */ #include "openssl_posix.h" -/* Retry utilities. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Check that TLS port of the server is defined. */ #ifndef HTTPS_PORT diff --git a/demos/http/http_demo_s3_download_multithreaded/CMakeLists.txt b/demos/http/http_demo_s3_download_multithreaded/CMakeLists.txt index 827e7b6a59..2b5dfc0e5a 100644 --- a/demos/http/http_demo_s3_download_multithreaded/CMakeLists.txt +++ b/demos/http/http_demo_s3_download_multithreaded/CMakeLists.txt @@ -3,13 +3,17 @@ set( DEMO_NAME "http_demo_s3_download_multithreaded" ) # Include HTTP library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Demo target. add_executable( ${DEMO_NAME} "${DEMO_NAME}.c" - "../common/src/http_demo_utils.c" + "${DEMOS_DIR}/http/common/src/http_demo_utils.c" ${HTTP_SOURCES} ${HTTP_THIRD_PARTY_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) # Add to default target if all required macros needed to run this demo are defined @@ -19,15 +23,15 @@ target_link_libraries( ${DEMO_NAME} PRIVATE openssl_posix - retry_utils rt ) target_include_directories( ${DEMO_NAME} PUBLIC - "../common/include" + "${DEMOS_DIR}/http/common/include" ${HTTP_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${HTTP_INCLUDE_THIRD_PARTY_DIRS} ${HTTP_INCLUDE_PRIVATE_DIRS} ${CMAKE_CURRENT_LIST_DIR} diff --git a/demos/http/http_demo_s3_download_multithreaded/http_demo_s3_download_multithreaded.c b/demos/http/http_demo_s3_download_multithreaded/http_demo_s3_download_multithreaded.c index 00df261cb9..cad575cbf1 100644 --- a/demos/http/http_demo_s3_download_multithreaded/http_demo_s3_download_multithreaded.c +++ b/demos/http/http_demo_s3_download_multithreaded/http_demo_s3_download_multithreaded.c @@ -46,8 +46,8 @@ /* OpenSSL transport header. */ #include "openssl_posix.h" -/* Retry utilities. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Check that TLS port of the server is defined. */ #ifndef HTTPS_PORT diff --git a/demos/http/http_demo_s3_upload/CMakeLists.txt b/demos/http/http_demo_s3_upload/CMakeLists.txt index f8c29c27a0..c2f56a145f 100644 --- a/demos/http/http_demo_s3_upload/CMakeLists.txt +++ b/demos/http/http_demo_s3_upload/CMakeLists.txt @@ -3,13 +3,17 @@ set( DEMO_NAME "http_demo_s3_upload" ) # Include HTTP library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Demo target. add_executable( ${DEMO_NAME} "${DEMO_NAME}.c" - "../common/src/http_demo_utils.c" + "${DEMOS_DIR}/http/common/src/http_demo_utils.c" ${HTTP_SOURCES} ${HTTP_THIRD_PARTY_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) # Add to default target if all required macros needed to run this demo are defined @@ -19,14 +23,14 @@ target_link_libraries( ${DEMO_NAME} PRIVATE openssl_posix - retry_utils ) target_include_directories( ${DEMO_NAME} PUBLIC - "../common/include" + "${DEMOS_DIR}/http/common/include" ${HTTP_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${HTTP_INCLUDE_THIRD_PARTY_DIRS} ${HTTP_INCLUDE_PRIVATE_DIRS} ${CMAKE_CURRENT_LIST_DIR} diff --git a/demos/http/http_demo_s3_upload/http_demo_s3_upload.c b/demos/http/http_demo_s3_upload/http_demo_s3_upload.c index 0bfeeeed6e..f5047d9f4e 100644 --- a/demos/http/http_demo_s3_upload/http_demo_s3_upload.c +++ b/demos/http/http_demo_s3_upload/http_demo_s3_upload.c @@ -41,8 +41,8 @@ /* OpenSSL transport header. */ #include "openssl_posix.h" -/* Retry utilities. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Check that TLS port of the server is defined. */ #ifndef HTTPS_PORT diff --git a/demos/mqtt/mqtt_demo_basic_tls/CMakeLists.txt b/demos/mqtt/mqtt_demo_basic_tls/CMakeLists.txt index e761a9a368..2652735edf 100644 --- a/demos/mqtt/mqtt_demo_basic_tls/CMakeLists.txt +++ b/demos/mqtt/mqtt_demo_basic_tls/CMakeLists.txt @@ -3,12 +3,16 @@ set( DEMO_NAME "mqtt_demo_basic_tls" ) # Include MQTT library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreMQTT/mqttFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Demo target. add_executable( ${DEMO_NAME} "${DEMO_NAME}.c" ${MQTT_SOURCES} ${MQTT_SERIALIZER_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) target_link_libraries( @@ -16,13 +20,13 @@ target_link_libraries( PRIVATE clock_posix openssl_posix - retry_utils ) target_include_directories( ${DEMO_NAME} PUBLIC ${MQTT_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${LOGGING_INCLUDE_DIRS} ) diff --git a/demos/mqtt/mqtt_demo_basic_tls/mqtt_demo_basic_tls.c b/demos/mqtt/mqtt_demo_basic_tls/mqtt_demo_basic_tls.c index 17b952ec70..52f3483368 100644 --- a/demos/mqtt/mqtt_demo_basic_tls/mqtt_demo_basic_tls.c +++ b/demos/mqtt/mqtt_demo_basic_tls/mqtt_demo_basic_tls.c @@ -53,8 +53,8 @@ /* OpenSSL sockets transport implementation. */ #include "openssl_posix.h" -/* Retry parameters. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Clock for timer. */ #include "clock.h" @@ -254,7 +254,7 @@ static MQTTSubAckStatus_t globalSubAckStatus = MQTTSubAckFailure; /** * @brief The random number generator to use for exponential backoff with * jitter retry logic. - * This function is an implementation the #RetryUtils_RNG_t interface type + * This function is an implementation the #BackoffAlgorithm_RNG_t interface type * of the retry utils library API. * * @return The generated random number. This function ALWAYS succeeds @@ -454,9 +454,9 @@ static int32_t generateRandomNumber() static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext ) { int returnStatus = EXIT_SUCCESS; - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; OpensslStatus_t opensslStatus = OPENSSL_SUCCESS; - RetryUtilsContext_t reconnectParams; + BackoffAlgorithmContext_t reconnectParams; ServerInfo_t serverInfo; OpensslCredentials_t opensslCredentials; uint16_t nextRetryBackOff; @@ -472,11 +472,11 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext opensslCredentials.sniHostName = BROKER_ENDPOINT; /* Initialize reconnect attempts and interval */ - RetryUtils_InitializeParams( &reconnectParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &reconnectParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); /* Attempt to connect to MQTT broker. If connection fails, retry after * a timeout. Timeout value will exponentially increase till maximum @@ -500,21 +500,21 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext if( opensslStatus != OPENSSL_SUCCESS ) { /* Get back-off value (in milliseconds) for the next connection retry. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &reconnectParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsRetriesExhausted ) + if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted ) { LogError( ( "Connection to the broker failed, all attempts exhausted." ) ); returnStatus = EXIT_FAILURE; } - else if( retryUtilsStatus == RetryUtilsSuccess ) + else if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Connection to the broker failed. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); } } - } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); return returnStatus; } @@ -730,18 +730,18 @@ static int handleResubscribe( MQTTContext_t * pMqttContext ) { int returnStatus = EXIT_SUCCESS; MQTTStatus_t mqttStatus = MQTTSuccess; - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; - RetryUtilsContext_t retryParams; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; + BackoffAlgorithmContext_t retryParams; uint16_t nextRetryBackOff = 0U; assert( pMqttContext != NULL ); /* Initialize retry attempts and interval. */ - RetryUtils_InitializeParams( &retryParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &retryParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); do { @@ -784,21 +784,21 @@ static int handleResubscribe( MQTTContext_t * pMqttContext ) if( globalSubAckStatus == MQTTSubAckFailure ) { /* Get back-off value (in milliseconds) for the next re-subscribe attempt. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &retryParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &retryParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsRetriesExhausted ) + if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted ) { LogError( ( "Server rejected subscription request, all attempts exhausted." ) ); returnStatus = EXIT_FAILURE; } - else if( retryUtilsStatus == RetryUtilsSuccess ) + else if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Server rejected subscription request. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); } } - } while( ( globalSubAckStatus == MQTTSubAckFailure ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( globalSubAckStatus == MQTTSubAckFailure ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); return returnStatus; } diff --git a/demos/mqtt/mqtt_demo_mutual_auth/CMakeLists.txt b/demos/mqtt/mqtt_demo_mutual_auth/CMakeLists.txt index 9e0d4b3db4..8634ebc495 100644 --- a/demos/mqtt/mqtt_demo_mutual_auth/CMakeLists.txt +++ b/demos/mqtt/mqtt_demo_mutual_auth/CMakeLists.txt @@ -4,12 +4,16 @@ set( DEMO_NAME "mqtt_demo_mutual_auth" ) # Include MQTT library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreMQTT/mqttFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Demo target. add_executable( ${DEMO_NAME} "${DEMO_NAME}.c" ${MQTT_SOURCES} ${MQTT_SERIALIZER_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) # Add to default target if all required macros needed to run this demo are defined @@ -20,13 +24,13 @@ target_link_libraries( PRIVATE clock_posix openssl_posix - retry_utils ) target_include_directories( ${DEMO_NAME} PUBLIC ${MQTT_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${LOGGING_INCLUDE_DIRS} ) diff --git a/demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c b/demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c index d2017f79f8..d4630919ff 100644 --- a/demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c +++ b/demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c @@ -69,8 +69,8 @@ /* OpenSSL sockets transport implementation. */ #include "openssl_posix.h" -/* Retry parameters. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Clock for timer. */ #include "clock.h" @@ -356,7 +356,7 @@ static MQTTSubAckStatus_t globalSubAckStatus = MQTTSubAckFailure; /** * @brief The random number generator to use for exponential backoff with * jitter retry logic. - * This function is an implementation the #RetryUtils_RNG_t interface type + * This function is an implementation the #BackoffAlgorithm_RNG_t interface type * of the retry utils library API. * * @return The generated random number. This function ALWAYS succeeds @@ -556,9 +556,9 @@ static int32_t generateRandomNumber() static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext ) { int returnStatus = EXIT_SUCCESS; - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; OpensslStatus_t opensslStatus = OPENSSL_SUCCESS; - RetryUtilsContext_t reconnectParams; + BackoffAlgorithmContext_t reconnectParams; ServerInfo_t serverInfo; OpensslCredentials_t opensslCredentials; uint16_t nextRetryBackOff; @@ -608,11 +608,11 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext } /* Initialize reconnect attempts and interval */ - RetryUtils_InitializeParams( &reconnectParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &reconnectParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); /* Attempt to connect to MQTT broker. If connection fails, retry after * a timeout. Timeout value will exponentially increase until maximum @@ -636,21 +636,21 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext if( opensslStatus != OPENSSL_SUCCESS ) { /* Get back-off value (in milliseconds) for the next connection retry. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &reconnectParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsRetriesExhausted ) + if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted ) { LogError( ( "Connection to the broker failed, all attempts exhausted." ) ); returnStatus = EXIT_FAILURE; } - else if( retryUtilsStatus == RetryUtilsSuccess ) + else if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Connection to the broker failed. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); } } - } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); return returnStatus; } @@ -860,18 +860,18 @@ static int handleResubscribe( MQTTContext_t * pMqttContext ) { int returnStatus = EXIT_SUCCESS; MQTTStatus_t mqttStatus = MQTTSuccess; - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; - RetryUtilsContext_t retryParams; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; + BackoffAlgorithmContext_t retryParams; uint16_t nextRetryBackOff = 0U; assert( pMqttContext != NULL ); /* Initialize retry attempts and interval. */ - RetryUtils_InitializeParams( &retryParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &retryParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); do { @@ -914,21 +914,21 @@ static int handleResubscribe( MQTTContext_t * pMqttContext ) if( globalSubAckStatus == MQTTSubAckFailure ) { /* Get back-off value (in milliseconds) for the next re-subscribe attempt. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &retryParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &retryParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsRetriesExhausted ) + if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted ) { LogError( ( "Subscription to topic failed, all attempts exhausted." ) ); returnStatus = EXIT_FAILURE; } - else if( retryUtilsStatus == RetryUtilsSuccess ) + else if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Server rejected subscription request. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); } } - } while( ( globalSubAckStatus == MQTTSubAckFailure ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( globalSubAckStatus == MQTTSubAckFailure ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); return returnStatus; } diff --git a/demos/mqtt/mqtt_demo_plaintext/CMakeLists.txt b/demos/mqtt/mqtt_demo_plaintext/CMakeLists.txt index e623362116..5c0bed74b5 100644 --- a/demos/mqtt/mqtt_demo_plaintext/CMakeLists.txt +++ b/demos/mqtt/mqtt_demo_plaintext/CMakeLists.txt @@ -3,12 +3,16 @@ set( DEMO_NAME "mqtt_demo_plaintext" ) # Include MQTT library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreMQTT/mqttFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Demo target. add_executable( ${DEMO_NAME} "${DEMO_NAME}.c" ${MQTT_SOURCES} ${MQTT_SERIALIZER_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) target_link_libraries( @@ -16,13 +20,13 @@ target_link_libraries( PRIVATE clock_posix plaintext_posix - retry_utils ) target_include_directories( ${DEMO_NAME} PUBLIC ${MQTT_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${LOGGING_INCLUDE_DIRS} ) diff --git a/demos/mqtt/mqtt_demo_plaintext/mqtt_demo_plaintext.c b/demos/mqtt/mqtt_demo_plaintext/mqtt_demo_plaintext.c index 7ca22214b0..cb09a7f111 100644 --- a/demos/mqtt/mqtt_demo_plaintext/mqtt_demo_plaintext.c +++ b/demos/mqtt/mqtt_demo_plaintext/mqtt_demo_plaintext.c @@ -50,8 +50,8 @@ /* Plaintext sockets transport implementation. */ #include "plaintext_posix.h" -/* Retry parameters. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Clock for timer. */ #include "clock.h" @@ -211,7 +211,7 @@ static MQTTSubAckStatus_t globalSubAckStatus = MQTTSubAckFailure; /** * @brief The random number generator to use for exponential backoff with * jitter retry logic. - * This function is an implementation the #RetryUtils_RNG_t interface type + * This function is an implementation the #BackoffAlgorithm_RNG_t interface type * of the retry utils library API. * * @return The generated random number. This function ALWAYS succeeds @@ -348,9 +348,9 @@ static int32_t generateRandomNumber() static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext ) { int returnStatus = EXIT_SUCCESS; - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; SocketStatus_t socketStatus = SOCKETS_SUCCESS; - RetryUtilsContext_t reconnectParams; + BackoffAlgorithmContext_t reconnectParams; ServerInfo_t serverInfo; uint16_t nextRetryBackOff = 0U; @@ -360,11 +360,11 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext serverInfo.port = BROKER_PORT; /* Initialize reconnect attempts and interval */ - RetryUtils_InitializeParams( &reconnectParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &reconnectParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); /* Attempt to connect to MQTT broker. If connection fails, retry after * a timeout. Timeout value will exponentially increase till maximum @@ -387,21 +387,21 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext if( socketStatus != SOCKETS_SUCCESS ) { /* Get back-off value (in milliseconds) for the next connection retry. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &reconnectParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsRetriesExhausted ) + if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted ) { LogError( ( "Connection to the broker failed, all attempts exhausted." ) ); returnStatus = EXIT_FAILURE; } - else if( retryUtilsStatus == RetryUtilsSuccess ) + else if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Connection to the broker failed. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); } } - } while( ( socketStatus != SOCKETS_SUCCESS ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( socketStatus != SOCKETS_SUCCESS ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); return returnStatus; } @@ -467,18 +467,18 @@ static int handleResubscribe( MQTTContext_t * pMqttContext ) { int returnStatus = EXIT_SUCCESS; MQTTStatus_t mqttStatus = MQTTSuccess; - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; - RetryUtilsContext_t retryParams; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; + BackoffAlgorithmContext_t retryParams; uint16_t nextRetryBackOff = 0U; assert( pMqttContext != NULL ); /* Initialize retry attempts and interval. */ - RetryUtils_InitializeParams( &retryParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &retryParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); do { @@ -521,21 +521,21 @@ static int handleResubscribe( MQTTContext_t * pMqttContext ) if( globalSubAckStatus == MQTTSubAckFailure ) { /* Get back-off value (in milliseconds) for the next re-subscribe attempt. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &retryParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &retryParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsRetriesExhausted ) + if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted ) { LogError( ( "Subscription to topic failed, all attempts exhausted." ) ); returnStatus = EXIT_FAILURE; } - else if( retryUtilsStatus == RetryUtilsSuccess ) + else if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Server rejected subscription request. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); } } - } while( ( globalSubAckStatus == MQTTSubAckFailure ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( globalSubAckStatus == MQTTSubAckFailure ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); return returnStatus; } diff --git a/demos/mqtt/mqtt_demo_serializer/CMakeLists.txt b/demos/mqtt/mqtt_demo_serializer/CMakeLists.txt index 55de724b8f..c9a9999275 100644 --- a/demos/mqtt/mqtt_demo_serializer/CMakeLists.txt +++ b/demos/mqtt/mqtt_demo_serializer/CMakeLists.txt @@ -3,11 +3,15 @@ set( DEMO_NAME "mqtt_demo_serializer" ) # Include MQTT library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreMQTT/mqttFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Demo target. add_executable( ${DEMO_NAME} "${DEMO_NAME}.c" ${MQTT_SERIALIZER_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) target_link_libraries( @@ -15,13 +19,13 @@ target_link_libraries( PRIVATE clock_posix plaintext_posix - retry_utils ) target_include_directories( ${DEMO_NAME} PUBLIC ${MQTT_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${LOGGING_INCLUDE_DIRS} ) diff --git a/demos/mqtt/mqtt_demo_serializer/mqtt_demo_serializer.c b/demos/mqtt/mqtt_demo_serializer/mqtt_demo_serializer.c index 39ae49a62c..6a264c0ed4 100644 --- a/demos/mqtt/mqtt_demo_serializer/mqtt_demo_serializer.c +++ b/demos/mqtt/mqtt_demo_serializer/mqtt_demo_serializer.c @@ -55,8 +55,8 @@ /* Plaintext transport implementation. */ #include "plaintext_posix.h" -/* Retry parameters. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Check that the broker endpoint is defined. */ #ifndef BROKER_ENDPOINT @@ -144,7 +144,7 @@ /** * @brief The random number generator to use for exponential backoff with * jitter retry logic. - * This function is an implementation the #RetryUtils_RNG_t interface type + * This function is an implementation the #BackoffAlgorithm_RNG_t interface type * of the retry utils library API. * * @return The generated random number. This function ALWAYS succeeds @@ -351,9 +351,9 @@ static int32_t generateRandomNumber() static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext ) { int returnStatus = EXIT_SUCCESS; - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; SocketStatus_t socketStatus = SOCKETS_SUCCESS; - RetryUtilsContext_t reconnectParams; + BackoffAlgorithmContext_t reconnectParams; ServerInfo_t serverInfo; uint16_t nextRetryBackOff = 0U; struct timespec tp; @@ -372,11 +372,11 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext srand( tp.tv_nsec ); /* Initialize reconnect attempts and interval */ - RetryUtils_InitializeParams( &reconnectParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &reconnectParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); /* Attempt to connect to MQTT broker. If connection fails, retry after * a timeout. Timeout value will exponentially increase till maximum @@ -399,21 +399,21 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext if( socketStatus != SOCKETS_SUCCESS ) { /* Get back-off value (in milliseconds) for the next connection retry. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &reconnectParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsRetriesExhausted ) + if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted ) { LogError( ( "Connection to the broker failed, all attempts exhausted." ) ); returnStatus = EXIT_FAILURE; } - else if( retryUtilsStatus == RetryUtilsSuccess ) + else if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Connection to the broker failed. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); } } - } while( ( socketStatus != SOCKETS_SUCCESS ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( socketStatus != SOCKETS_SUCCESS ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); return returnStatus; } @@ -908,8 +908,8 @@ int main( int argc, bool controlPacketSent = false; bool publishPacketSent = false; NetworkContext_t networkContext = { 0 }; - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; - RetryUtilsContext_t retryParams; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; + BackoffAlgorithmContext_t retryParams; uint16_t nextRetryBackOff = 0U; ( void ) argc; @@ -942,11 +942,11 @@ int main( int argc, /**************************** Subscribe, Re-subscribe, and Keep-Alive ******************************/ /* Initialize retry attempts and interval. */ - RetryUtils_InitializeParams( &retryParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &retryParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); do { @@ -998,20 +998,20 @@ int main( int argc, mqttProcessIncomingPacket( &networkContext, &fixedBuffer ); /* Get back-off value (in milliseconds) for the next re-subscribe attempt. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &retryParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &retryParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsRetriesExhausted ) + if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted ) { LogError( ( "Subscription to topic failed, all attempts exhausted." ) ); } - else if( retryUtilsStatus == RetryUtilsSuccess ) + else if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Server rejected subscription request. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); } } - } while( ( globalSubAckStatus == false ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( globalSubAckStatus == false ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); assert( globalSubAckStatus == true ); diff --git a/demos/mqtt/mqtt_demo_subscription_manager/CMakeLists.txt b/demos/mqtt/mqtt_demo_subscription_manager/CMakeLists.txt index 9a142868f3..06eac7b695 100644 --- a/demos/mqtt/mqtt_demo_subscription_manager/CMakeLists.txt +++ b/demos/mqtt/mqtt_demo_subscription_manager/CMakeLists.txt @@ -6,6 +6,9 @@ add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/subscription-manager ) # Include MQTT library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreMQTT/mqttFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + set( DEMO_NAME "mqtt_demo_subscription_manager" ) # Demo target. @@ -14,6 +17,7 @@ add_executable( "${DEMO_NAME}.c" ${MQTT_SOURCES} ${MQTT_SERIALIZER_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) # Add to default target if all required macros needed to run this demo are defined @@ -25,7 +29,6 @@ target_link_libraries( mqtt_subscription_manager clock_posix openssl_posix - retry_utils ) target_include_directories( @@ -39,6 +42,7 @@ target_include_directories( PUBLIC ${CMAKE_CURRENT_LIST_DIR}/subscription-manager ${MQTT_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${LOGGING_INCLUDE_DIRS} ) diff --git a/demos/mqtt/mqtt_demo_subscription_manager/mqtt_demo_subscription_manager.c b/demos/mqtt/mqtt_demo_subscription_manager/mqtt_demo_subscription_manager.c index 23fe384bb3..b972afb65c 100644 --- a/demos/mqtt/mqtt_demo_subscription_manager/mqtt_demo_subscription_manager.c +++ b/demos/mqtt/mqtt_demo_subscription_manager/mqtt_demo_subscription_manager.c @@ -59,8 +59,8 @@ /* OpenSSL sockets transport implementation. */ #include "openssl_posix.h" -/* Reconnect parameters. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Clock for timer. */ #include "clock.h" @@ -306,7 +306,7 @@ static bool globalReceivedPrecipitationData = false; /** * @brief The random number generator to use for exponential backoff with * jitter retry logic. - * This function is an implementation the #RetryUtils_RNG_t interface type + * This function is an implementation the #BackoffAlgorithm_RNG_t interface type * of the retry utils library API. * * @return The generated random number. This function ALWAYS succeeds @@ -522,9 +522,9 @@ static int32_t generateRandomNumber() static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext ) { int returnStatus = EXIT_SUCCESS; - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; OpensslStatus_t opensslStatus = OPENSSL_SUCCESS; - RetryUtilsContext_t reconnectParams; + BackoffAlgorithmContext_t reconnectParams; ServerInfo_t serverInfo; OpensslCredentials_t opensslCredentials; uint16_t nextRetryBackOff; @@ -550,11 +550,11 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext /* Initialize reconnect attempts and interval. */ - RetryUtils_InitializeParams( &reconnectParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &reconnectParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); /* Attempt to connect to MQTT broker. If connection fails, retry after * a timeout. Timeout value will exponentially increase until maximum @@ -578,21 +578,21 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext if( opensslStatus != OPENSSL_SUCCESS ) { /* Get back-off value (in milliseconds) for the next connection retry. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &reconnectParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsRetriesExhausted ) + if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted ) { LogError( ( "Connection to the broker failed, all attempts exhausted." ) ); returnStatus = EXIT_FAILURE; } - else if( retryUtilsStatus == RetryUtilsSuccess ) + else if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Connection to the broker failed. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); } } - } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); return returnStatus; } diff --git a/demos/mqtt/mqtt_demo_subscription_manager/subscription-manager/CMakeLists.txt b/demos/mqtt/mqtt_demo_subscription_manager/subscription-manager/CMakeLists.txt index e1e0a57011..7d0f3b1796 100644 --- a/demos/mqtt/mqtt_demo_subscription_manager/subscription-manager/CMakeLists.txt +++ b/demos/mqtt/mqtt_demo_subscription_manager/subscription-manager/CMakeLists.txt @@ -1,6 +1,9 @@ # Include MQTT library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreMQTT/mqttFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + set( LIBRARY_NAME "mqtt_subscription_manager" ) # Library target. add_library( ${LIBRARY_NAME} diff --git a/demos/pkcs11/pkcs11_demo_objects/CMakeLists.txt b/demos/pkcs11/pkcs11_demo_objects/CMakeLists.txt index 2de6074015..17d9b8a6c2 100644 --- a/demos/pkcs11/pkcs11_demo_objects/CMakeLists.txt +++ b/demos/pkcs11/pkcs11_demo_objects/CMakeLists.txt @@ -40,7 +40,7 @@ target_compile_definitions( target_include_directories( ${DEMO_NAME} PUBLIC - "../common/include" + "${DEMOS_DIR}/pkcs11/common/include" ${PKCS_INCLUDE_PUBLIC_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${LOGGING_INCLUDE_DIRS} diff --git a/demos/shadow/shadow_demo_main/CMakeLists.txt b/demos/shadow/shadow_demo_main/CMakeLists.txt index 89b6f1b6c1..d8e498141b 100755 --- a/demos/shadow/shadow_demo_main/CMakeLists.txt +++ b/demos/shadow/shadow_demo_main/CMakeLists.txt @@ -5,6 +5,9 @@ set( DEMO_NAME "shadow_demo_main" ) # Include MQTT library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreMQTT/mqttFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # Include Shadow library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/aws/device-shadow-for-aws-iot-embedded-sdk/shadowFilePaths.cmake ) @@ -18,6 +21,7 @@ add_executable( "shadow_demo_helpers.c" ${MQTT_SOURCES} ${MQTT_SERIALIZER_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ${SHADOW_SOURCES} ${JSON_SOURCES} ) @@ -30,7 +34,6 @@ target_link_libraries( PRIVATE clock_posix openssl_posix - retry_utils ) target_include_directories( @@ -38,6 +41,7 @@ target_include_directories( PUBLIC ${LOGGING_INCLUDE_DIRS} ${MQTT_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${SHADOW_INCLUDE_PUBLIC_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${JSON_INCLUDE_PUBLIC_DIRS} diff --git a/demos/shadow/shadow_demo_main/shadow_demo_helpers.c b/demos/shadow/shadow_demo_main/shadow_demo_helpers.c index 73e9ba3bc0..be7c732ad8 100755 --- a/demos/shadow/shadow_demo_main/shadow_demo_helpers.c +++ b/demos/shadow/shadow_demo_main/shadow_demo_helpers.c @@ -47,8 +47,8 @@ /* OpenSSL sockets transport implementation. */ #include "openssl_posix.h" -/* Reconnect parameters. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Clock for timer. */ #include "clock.h" @@ -246,7 +246,7 @@ static bool mqttSessionEstablished = false; /** * @brief The random number generator to use for exponential backoff with * jitter retry logic. - * This function is an implementation the #RetryUtils_RNG_t interface type + * This function is an implementation the #BackoffAlgorithm_RNG_t interface type * of the retry utils library API. * * @return The generated random number. This function ALWAYS succeeds @@ -322,9 +322,9 @@ static int32_t generateRandomNumber() static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext ) { int returnStatus = EXIT_SUCCESS; - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; OpensslStatus_t opensslStatus = OPENSSL_SUCCESS; - RetryUtilsContext_t reconnectParams; + BackoffAlgorithmContext_t reconnectParams; ServerInfo_t serverInfo; OpensslCredentials_t opensslCredentials; uint16_t nextRetryBackOff = 0U; @@ -362,11 +362,11 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext srand( tp.tv_nsec ); /* Initialize reconnect attempts and interval */ - RetryUtils_InitializeParams( &reconnectParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &reconnectParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); /* Attempt to connect to MQTT broker. If connection fails, retry after * a timeout. Timeout value will exponentially increase until maximum @@ -390,21 +390,21 @@ static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext if( opensslStatus != OPENSSL_SUCCESS ) { /* Get back-off value (in milliseconds) for the next connection retry. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &reconnectParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsRetriesExhausted ) + if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted ) { LogError( ( "Connection to the broker failed, all attempts exhausted." ) ); returnStatus = EXIT_FAILURE; } - else if( retryUtilsStatus == RetryUtilsSuccess ) + else if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Connection to the broker failed. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); } } - } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); return returnStatus; } diff --git a/integration-test/http/CMakeLists.txt b/integration-test/http/CMakeLists.txt index 6b4be07f00..9a94760d5d 100644 --- a/integration-test/http/CMakeLists.txt +++ b/integration-test/http/CMakeLists.txt @@ -4,6 +4,9 @@ cmake_minimum_required (VERSION 3.2.0) # Include HTTP library's source and header path variables. include( ${CMAKE_SOURCE_DIR}/libraries/standard/coreHTTP/httpFilePaths.cmake ) +# Include backoffAlgorithm library file path configuration. +include( ${CMAKE_SOURCE_DIR}/libraries/standard/backoffAlgorithm/backoffAlgorithmFilePaths.cmake ) + # ==================== Define your project name (edit) ======================== set(project_name "http_system") @@ -12,11 +15,13 @@ set(project_name "http_system") # list the files you would like to test here list(APPEND real_source_files ${HTTP_SOURCES} + ${BACKOFF_ALGORITHM_SOURCES} ) # list the directories the module under test includes list(APPEND real_include_directories . ${HTTP_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${LOGGING_INCLUDE_DIRS} ) @@ -26,6 +31,7 @@ list(APPEND real_include_directories list(APPEND test_include_directories . ${HTTP_INCLUDE_PUBLIC_DIRS} + ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS} ${LOGGING_INCLUDE_DIRS} ) @@ -61,7 +67,6 @@ list(APPEND stest_dep_list ${real_name} clock_posix openssl_posix - retry_utils ) set(stest_name "${project_name}_test") diff --git a/integration-test/http/http_system_test.c b/integration-test/http/http_system_test.c index 337e310871..a5b0242d7c 100644 --- a/integration-test/http/http_system_test.c +++ b/integration-test/http/http_system_test.c @@ -48,8 +48,8 @@ /* Include OpenSSL implementation of transport interface. */ #include "openssl_posix.h" -/* Retry parameters. */ -#include "retry_utils.h" +/*Include backoff algorithm header for retry logic.*/ +#include "backoff_algorithm.h" /* Ensure that config macros, required for TLS connection, have been defined. */ #ifndef SERVER_HOST @@ -175,7 +175,7 @@ static size_t networkDataLen = 0U; /** * @brief The random number generator to use for exponential backoff with * jitter retry logic. - * This function is an implementation the #RetryUtils_RNG_t interface type + * This function is an implementation the #BackoffAlgorithm_RNG_t interface type * of the retry utils library API. * * @return The generated random number. This function ALWAYS succeeds @@ -246,9 +246,9 @@ static int32_t generateRandomNumber() static void connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext ) { /* Status returned by the retry utilities. */ - RetryUtilsStatus_t retryUtilsStatus = RetryUtilsSuccess; + BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess; /* Struct containing the next backoff time. */ - RetryUtilsContext_t reconnectParams; + BackoffAlgorithmContext_t reconnectParams; /* Status returned by OpenSSL transport implementation. */ OpensslStatus_t opensslStatus; uint16_t nextRetryBackOff = 0U; @@ -272,11 +272,11 @@ static void connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContex srand( tp.tv_nsec ); /* Initialize reconnect attempts and interval */ - RetryUtils_InitializeParams( &reconnectParams, - CONNECTION_RETRY_BACKOFF_BASE_MS, - CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, - CONNECTION_RETRY_MAX_ATTEMPTS, - generateRandomNumber ); + BackoffAlgorithm_InitializeParams( &reconnectParams, + CONNECTION_RETRY_BACKOFF_BASE_MS, + CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS, + CONNECTION_RETRY_MAX_ATTEMPTS, + generateRandomNumber ); /* Attempt to connect to HTTP server. If connection fails, retry after * a timeout. Timeout value will exponentially increase until maximum @@ -294,10 +294,10 @@ static void connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContex if( opensslStatus != OPENSSL_SUCCESS ) { /* Get back-off value (in milliseconds) for the next connection retry. */ - retryUtilsStatus = RetryUtils_GetNextBackOff( &reconnectParams, &nextRetryBackOff ); - assert( retryUtilsStatus != RetryUtilsRngFailure ); + backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, &nextRetryBackOff ); + assert( backoffAlgStatus != BackoffAlgorithmRngFailure ); - if( retryUtilsStatus == RetryUtilsSuccess ) + if( backoffAlgStatus == BackoffAlgorithmSuccess ) { LogWarn( ( "Connection to the HTTP server failed. Retrying connection after backoff." ) ); ( void ) sleep( nextRetryBackOff / NUM_MILLISECONDS_IN_SECOND ); @@ -307,7 +307,7 @@ static void connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContex LogError( ( "Connection to the HTTP server failed, all attempts exhausted." ) ); } } - } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( retryUtilsStatus == RetryUtilsSuccess ) ); + } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) ); TEST_ASSERT_EQUAL( OPENSSL_SUCCESS, opensslStatus ); TEST_ASSERT_NOT_EQUAL( -1, networkContext.socketDescriptor ); diff --git a/libraries/CMakeLists.txt b/libraries/CMakeLists.txt index 1184f9201a..4e7669657c 100644 --- a/libraries/CMakeLists.txt +++ b/libraries/CMakeLists.txt @@ -20,4 +20,3 @@ endif() # Add build configuration for all 3rd party modules. add_subdirectory(${3RDPARTY_DIR}) -add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/standard/retry-utils) diff --git a/libraries/standard/backoffAlgorithm b/libraries/standard/backoffAlgorithm new file mode 160000 index 0000000000..bc5ce2ed93 --- /dev/null +++ b/libraries/standard/backoffAlgorithm @@ -0,0 +1 @@ +Subproject commit bc5ce2ed93ac063d75a4d3661e5253477c4e4fef diff --git a/libraries/standard/retry-utils/CMakeLists.txt b/libraries/standard/retry-utils/CMakeLists.txt deleted file mode 100644 index 0c4a6bf1d0..0000000000 --- a/libraries/standard/retry-utils/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -# Include file path configuration for retry utils library. -include(retryUtilsFilePaths.cmake) - -# Library target for retry utils. -add_library( retry_utils - ${RETRY_UTILS_SOURCES} ) - -target_include_directories( retry_utils - PUBLIC - ${RETRY_UTILS_INCLUDE_PUBLIC_DIRS} ) - -# Include unit tests. -if(BUILD_TESTS) - add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/utest) -endif() diff --git a/libraries/standard/retry-utils/retryUtilsFilePaths.cmake b/libraries/standard/retry-utils/retryUtilsFilePaths.cmake deleted file mode 100644 index e4ae47f9fa..0000000000 --- a/libraries/standard/retry-utils/retryUtilsFilePaths.cmake +++ /dev/null @@ -1,14 +0,0 @@ -# This file is to add source files and include directories -# into variables so that it can be reused from different repositories -# in their Cmake based build system by including this file. -# -# Files specific to the repository such as test runner, platform tests -# are not added to the variables. - -# Retry Utils library source files. -set( RETRY_UTILS_SOURCES - "${CMAKE_CURRENT_LIST_DIR}/source/retry_utils.c" ) - -# Retry Utils library Public Include directories. -set( RETRY_UTILS_INCLUDE_PUBLIC_DIRS - "${CMAKE_CURRENT_LIST_DIR}/source/include" ) diff --git a/libraries/standard/retry-utils/source/include/retry_utils.h b/libraries/standard/retry-utils/source/include/retry_utils.h deleted file mode 100644 index 0cb55a6f48..0000000000 --- a/libraries/standard/retry-utils/source/include/retry_utils.h +++ /dev/null @@ -1,174 +0,0 @@ -/* - * AWS IoT Device SDK for Embedded C V202011.00 - * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/** - * @file retry_utils.h - * @brief Declaration of retry utility functions and constants for exponential backoff with - * jitter strategy of retry attempts. - * This library represents the "Full Jitter" backoff strategy explained in the - * following document. - * https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/ - * - */ - -#ifndef RETRY_UTILS_H_ -#define RETRY_UTILS_H_ - -/* Standard include. */ -#include - -/** - * @page retryutils_page Retry Utilities - * @brief An abstraction of utilities for retrying with exponential back off and - * jitter. - * - * @section retryutils_overview Overview - * The retry utilities are a set of APIs that aid in retrying with exponential - * backoff and jitter. Exponential backoff with jitter is strongly recommended - * for retrying failed actions over the network with servers. Please see - * https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/ for - * more information about the benefits with AWS. - * - * Exponential backoff with jitter is typically used when retrying a failed - * connection to the server. In an environment with poor connectivity, a client - * can get disconnected at any time. A backoff strategy helps the client to - * conserve battery by not repeatedly attempting reconnections when they are - * unlikely to succeed. - * - * Before retrying the failed communication to the server, there is a delay period. - * In this delay period, the task that is retrying must sleep for some random amount - * of milliseconds between 0 and the lesser of the backoff window (related to the retry attempt) - * and a predefined maximum delay value. The backoff window is doubled with each retry - * attempt until the maximum delay value is reached.
- * - * > sleep_ms = random_between( 0, min( 2attempts_count * base_ms, maximum_ms ) ) - */ - -/** - * @brief Constant to represent unlimited number of retry attempts. - */ -#define RETRY_UTILS_RETRY_FOREVER 0 - -/** - * @brief Interface for a random number generator. - * The user should supply the platform-specific random number generator to the - * library through the @ref RetryUtils_InitializeParams API function. - * - * @note It is recommended that a true random number generator is supplied - * to the library. The random number generator should be seeded with an entropy - * source in the system. - * - * @return The random number if successful; otherwise a negative value to indicate - * failure. - */ -typedef int32_t ( * RetryUtils_RNG_t )(); - -/** - * @brief Status for @ref RetryUtils_GetNextBackOff. - */ -typedef enum RetryUtilsStatus -{ - RetryUtilsSuccess = 0, /**< @brief The function successfully calculated the next back-off value. */ - RetryUtilsRngFailure = 1, /**< @brief The function encountered failure in generating random number. */ - RetryUtilsRetriesExhausted /**< @brief The function exhausted all retry attempts. */ -} RetryUtilsStatus_t; - -/** - * @brief Represents parameters required for calculating the back-off delay for the - * next retry attempt. - */ -typedef struct RetryUtilsContext -{ - /** - * @brief The maximum backoff delay (in milliseconds) between consecutive retry attempts. - */ - uint16_t maxBackOffDelay; - - /** - * @brief The total number of retry attempts completed. - * This value is incremented on every call to #RetryUtils_GetNextBackOff API. - */ - uint32_t attemptsDone; - - /** - * @brief The maximum backoff value (in milliseconds) for the next retry attempt. - */ - uint16_t nextJitterMax; - - /** - * @brief The maximum number of retry attempts. - */ - uint32_t maxRetryAttempts; - - /** - * @brief The random number generator function used for calculating the - * backoff value for the next retry attempt. - */ - RetryUtils_RNG_t pRng; -} RetryUtilsContext_t; - -/** - * @brief Initializes the context for using retry utils. The parameters - * are required for calculating the next retry backoff delay. - * This function must be called by the application before the first new retry attempt. - * - * @param[out] pRetryParams The context to initialize with parameters required - * for the next backoff delay calculation function. - * @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. - * @param[in] maxAttempts The maximum number of retry attempts. Set the value to - * #RETRY_UTILS_RETRY_FOREVER to retry for ever. - * @param[in] pRng The platform-specific function to use for random number generation. - * The random number generator should be seeded before calling this function. - */ -/* @[define_retryutils_initializeparams] */ -void RetryUtils_InitializeParams( RetryUtilsContext_t * pContext, - uint16_t backOffBase, - uint16_t maxBackOff, - uint32_t maxAttempts, - RetryUtils_RNG_t pRng ); -/* @[define_retryutils_initializeparams] */ - -/** - * @brief Simple exponential backoff and jitter function that provides the - * delay value for the next retry attempt. - * After a failure of an operation that needs to be retried, the application - * should use this function to obtain the backoff delay value for the next retry, - * and then wait for the backoff time period before retrying the operation. - * - * @param[in, out] pRetryContext Structure containing parameters for the next backoff - * value calculation. - * @param[out] pNextBackOff This will be populated with the backoff value (in milliseconds) - * for the next retry attempt. The value does not exceed the maximum backoff delay - * configured in the context. - * - * @return #RetryUtilsSuccess after a successful sleep, #RetryUtilsRngFailure for a failure - * in random number generation, #RetryUtilsRetriesExhausted when all attempts are exhausted. - */ -/* @[define_retryutils_getnextbackoff] */ -RetryUtilsStatus_t RetryUtils_GetNextBackOff( RetryUtilsContext_t * pRetryContext, - uint16_t * pNextBackOff ); -/* @[define_retryutils_getnextbackoff] */ - -#endif /* ifndef RETRY_UTILS_H_ */ diff --git a/libraries/standard/retry-utils/source/retry_utils.c b/libraries/standard/retry-utils/source/retry_utils.c deleted file mode 100644 index ad6fbabb51..0000000000 --- a/libraries/standard/retry-utils/source/retry_utils.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * AWS IoT Device SDK for Embedded C V202011.00 - * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/** - * @file retry_utils.c - * @brief Implementation of the retry utils API for a "Full Jitter" exponential backoff - * with jitter strategy. - */ - -/* Standard includes. */ -#include -#include - -/* Include API header. */ -#include "retry_utils.h" - -/*-----------------------------------------------------------*/ - -RetryUtilsStatus_t RetryUtils_GetNextBackOff( RetryUtilsContext_t * pRetryContext, - uint16_t * pNextBackOff ) -{ - RetryUtilsStatus_t status = RetryUtilsSuccess; - int32_t randomVal = 0; - - /* If RETRY_UTILS_RETRY_FOREVER is set to 0, try forever. */ - if( ( pRetryContext->attemptsDone < pRetryContext->maxRetryAttempts ) || - ( pRetryContext->maxRetryAttempts == RETRY_UTILS_RETRY_FOREVER ) ) - { - /* Generate a random number. */ - randomVal = pRetryContext->pRng(); - - if( randomVal < 0 ) - { - status = RetryUtilsRngFailure; - } - else - { - /* The next backoff value is a random value between 0 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 ) ( randomVal % ( pRetryContext->nextJitterMax + 1U ) ); - - /* Increment the retry attempt. */ - pRetryContext->attemptsDone++; - - /* Double the max jitter value for the next retry attempt, only - * if the new value will be less than the max backoff time value. */ - if( pRetryContext->nextJitterMax < ( pRetryContext->maxBackOffDelay / 2U ) ) - { - pRetryContext->nextJitterMax += pRetryContext->nextJitterMax; - } - else - { - pRetryContext->nextJitterMax = pRetryContext->maxBackOffDelay; - } - } - } - else - { - /* When max retry attempts are exhausted, let application know by - * returning RetryUtilsRetriesExhausted. Application may choose to - * restart the retry process after calling RetryUtils_InitializeParams(). */ - status = RetryUtilsRetriesExhausted; - } - - return status; -} - -/*-----------------------------------------------------------*/ - -void RetryUtils_InitializeParams( RetryUtilsContext_t * pContext, - uint16_t backOffBase, - uint16_t maxBackOff, - uint32_t maxAttempts, - RetryUtils_RNG_t pRng ) -{ - assert( pContext != NULL ); - - /* Initialize the context with parameters used in calculating the backoff - * value for the next retry attempt. */ - pContext->nextJitterMax = backOffBase; - pContext->maxBackOffDelay = maxBackOff; - pContext->maxRetryAttempts = maxAttempts; - pContext->pRng = pRng; - - /* The total number of retry attempts is zero at initialization. */ - pContext->attemptsDone = 0; -} - -/*-----------------------------------------------------------*/ diff --git a/libraries/standard/retry-utils/utest/CMakeLists.txt b/libraries/standard/retry-utils/utest/CMakeLists.txt deleted file mode 100644 index 7065f07d7d..0000000000 --- a/libraries/standard/retry-utils/utest/CMakeLists.txt +++ /dev/null @@ -1,70 +0,0 @@ -# Include file path configuration for retry utils library. -include(../retryUtilsFilePaths.cmake) - -project ("retry utils unit test") -cmake_minimum_required (VERSION 3.2.0) - -# ==================== Define your project name (edit) ======================== -set(project_name "retry_utils") - -# ===================== Create your mock here (edit) ======================== - -# list the files to mock here -list(APPEND mock_list - "" - ) - -# list the directories your mocks need -list(APPEND mock_include_list - "" - ) -#list the definitions of your mocks to control what to be included -list(APPEND mock_define_list - "" - ) - -# ================= Create the library under test here (edit) ================== - -# list the files you would like to test here -list(APPEND real_source_files - ${RETRY_UTILS_SOURCES} - ) -# list the directories the module under test includes -list(APPEND real_include_directories - ${RETRY_UTILS_INCLUDE_PUBLIC_DIRS} - ) - -# ===================== Create UnitTest Code here (edit) ===================== - -# list the directories your test needs to include -list(APPEND test_include_directories - ${RETRY_UTILS_INCLUDE_PUBLIC_DIRS} - ) - -# ============================= (end edit) =================================== - -set(mock_name "${project_name}_mock") -set(real_name "${project_name}_real") - -create_real_library(${real_name} - "${real_source_files}" - "${real_include_directories}" - "" - ) - -list(APPEND utest_link_list - lib${real_name}.a - ) - -list(APPEND utest_dep_list - ${real_name} - ) - -set(utest_name "${project_name}_utest") -set(utest_source "${project_name}_utest.c") -create_test(${utest_name} - ${utest_source} - "${utest_link_list}" - "${utest_dep_list}" - "${test_include_directories}" - ) diff --git a/libraries/standard/retry-utils/utest/retry_utils_utest.c b/libraries/standard/retry-utils/utest/retry_utils_utest.c deleted file mode 100644 index abcf4edb8e..0000000000 --- a/libraries/standard/retry-utils/utest/retry_utils_utest.c +++ /dev/null @@ -1,372 +0,0 @@ -/* - * AWS IoT Device SDK for Embedded C V202011.00 - * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include -#include - -/* Unity include. */ -#include "unity.h" - -/* Retry utils library include */ -#include "retry_utils.h" - -/* Return value of mockRng. */ -static int32_t randomValToReturn; - -#define TEST_BACKOFF_BASE_VALUE ( 1000 ) -#define TEST_BACKOFF_MAX_VALUE ( 10000 ) -#define TEST_MAX_ATTEMPTS ( 5 ) -/* Parameters to track the next max jitter or number of attempts done. */ -static RetryUtilsContext_t retryParams; -/* Return value of #RetryUtils_GetNextBackOff. */ -static RetryUtilsStatus_t retryUtilsStatus; -static uint16_t nextBackOff; - -/** - * @brief A mock random number generator. - */ -static int32_t mockRng() -{ - return randomValToReturn; -} - -/* ============================ UNITY FIXTURES ============================ */ - -/* Called before each test method. */ -void setUp() -{ - /* Initialize context with random number generator that succeeds. */ - RetryUtils_InitializeParams( &retryParams, - TEST_BACKOFF_BASE_VALUE, - TEST_BACKOFF_MAX_VALUE, - TEST_MAX_ATTEMPTS, - mockRng ); -} - -/* Called after each test method. */ -void tearDown() -{ - randomValToReturn = 0; - retryUtilsStatus = RetryUtilsSuccess; - nextBackOff = 0; -} - -/* Called at the beginning of the whole suite. */ -void suiteSetUp() -{ -} - -/* Called at the end of the whole suite. */ -int suiteTearDown( int numFailures ) -{ - return numFailures; -} - -/* ========================================================================== */ - -/** - * @brief Helper method to make assertions on data of the context object. - */ -static void verifyContextData( RetryUtilsContext_t * pContext, - uint32_t expectedAttemptsDone, - uint16_t expectedNextJitterMax, - uint16_t expectedMaxBackOff, - uint32_t expectedMaxAttempts, - RetryUtils_RNG_t pExpectedRng ) -{ - TEST_ASSERT_EQUAL( expectedNextJitterMax, pContext->nextJitterMax ); - TEST_ASSERT_EQUAL( expectedAttemptsDone, pContext->attemptsDone ); - TEST_ASSERT_EQUAL( expectedMaxAttempts, pContext->maxRetryAttempts ); - TEST_ASSERT_EQUAL( expectedMaxBackOff, pContext->maxBackOffDelay ); - TEST_ASSERT_EQUAL_PTR( pExpectedRng, pContext->pRng ); -} - -/** - * @brief Test that #RetryUtils_InitializeParams initializes the context - * data as expected. - */ -void test_RetryUtils_InitializeParams_Sets_Jitter_Correctly( void ) -{ - RetryUtils_InitializeParams( &retryParams, - TEST_BACKOFF_BASE_VALUE, - TEST_BACKOFF_MAX_VALUE, - TEST_MAX_ATTEMPTS, - mockRng ); - verifyContextData( &retryParams, - 0, - TEST_BACKOFF_BASE_VALUE, - TEST_BACKOFF_MAX_VALUE, - TEST_MAX_ATTEMPTS, - mockRng ); -} - -/** - * @brief Test that #RetryUtils_GetNextBackOff returns the expected failure - * and does not update the context data when the random number generator fails. - */ -void test_RetryUtils_GetNextBackOff_Rng_Failure( void ) -{ - int32_t testNegativeVal[] = { -1, -10 }; - uint iter = 0; - - /* Initialize context with random number generator that fails. */ - RetryUtils_InitializeParams( &retryParams, - TEST_BACKOFF_BASE_VALUE, - TEST_BACKOFF_MAX_VALUE, - RETRY_UTILS_RETRY_FOREVER, - mockRng ); - - /* Test the #RetryUtils_GetNextBackOff API with the 2 negative values - * from the mock random number generator. */ - - for( ; iter < sizeof( testNegativeVal ) / sizeof( int32_t ); iter++ ) - { - /* Set the random value generated to be negative. */ - randomValToReturn = testNegativeVal[ iter ]; - - /* Make sure that the API function returns RNG failure.*/ - TEST_ASSERT_EQUAL( RetryUtilsRngFailure, - RetryUtils_GetNextBackOff( &retryParams, &nextBackOff ) ); - - /* Make sure that the context data has not changed as the call to - * RetryUtils_GetNextBackOff failed. */ - verifyContextData( &retryParams, - 0, - TEST_BACKOFF_BASE_VALUE, - TEST_BACKOFF_MAX_VALUE, - RETRY_UTILS_RETRY_FOREVER, - mockRng ); - } -} - -/** - * @brief Test that #RetryUtils_GetNextBackOff returns the expected next back-off - * value and updates the contents of the context as expected when the random value - * generated is lower than the max jitter value for the retry attempts. - * - * This test assumes that the max jitter value has not reached the configured - * maximum back-off value. - */ -void test_RetryUtils_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; - - for( ; iter < 2; iter++ ) - { - /* Set the random value to be generated as a value lower than - * the jitter max value for the next retry attempt. */ - randomValToReturn = retryParams.nextJitterMax / 2; - - /* 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 = randomValToReturn; - - /* The jitter max value should double with the above call for use in next call. */ - expectedNextJitterMax *= 2; - - /* The number of attempts should be updated by the above API call. */ - expectedAttemptsDone++; - - TEST_ASSERT_LESS_THAN( TEST_BACKOFF_MAX_VALUE, expectedNextJitterMax ); - - /* Call the RetryUtils_GetNextBackOff API a couple times. */ - TEST_ASSERT_EQUAL( RetryUtilsSuccess, - RetryUtils_GetNextBackOff( &retryParams, &nextBackOff ) ); - TEST_ASSERT_EQUAL( expectedNextBackOff, nextBackOff ); - - /* Verify that the context data for expected data after the API call. */ - verifyContextData( &retryParams, - expectedAttemptsDone, - expectedNextJitterMax, - TEST_BACKOFF_MAX_VALUE, - TEST_MAX_ATTEMPTS, - mockRng ); - } -} - -/** - * @brief Test that #RetryUtils_GetNextBackOff returns the expected next back-off - * value and updates the contents of the context when the random value generated - * is higher than the max jitter value for the retry attempts. - * - * This test assumes that the max jitter value has not reached the configured - * maximum back-off value. - */ -void test_RetryUtils_GetNextBackOff_Success_RandVal_More_Than_Jitter_Max( void ) -{ - int iter = 1; - uint16_t expectedAttemptsDone = 0; - uint16_t expectedNextJitterMax = TEST_BACKOFF_BASE_VALUE; - - for( ; iter < 2; iter++ ) - { - /* Set the random value to be generated as a value greater than - * the jitter max value for the next retry attempt. */ - randomValToReturn = retryParams.nextJitterMax + 1; - - /* The jitter max value should double with the above call for use in next call. */ - expectedNextJitterMax *= 2; - TEST_ASSERT_LESS_THAN( TEST_BACKOFF_MAX_VALUE, expectedNextJitterMax ); - - /* The number of attempts should be updated by the above API call. */ - expectedAttemptsDone++; - - /* 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 = ( randomValToReturn % ( retryParams.nextJitterMax + 1U ) ); - - /* Call the RetryUtils_GetNextBackOff API a couple times. */ - TEST_ASSERT_EQUAL( RetryUtilsSuccess, - RetryUtils_GetNextBackOff( &retryParams, &nextBackOff ) ); - TEST_ASSERT_EQUAL( expectedNextBackOff, nextBackOff ); - - /* Verify that the context data for expected data after the API call. */ - verifyContextData( &retryParams, - expectedAttemptsDone, - expectedNextJitterMax, - TEST_BACKOFF_MAX_VALUE, - TEST_MAX_ATTEMPTS, - mockRng ); - } -} - -/** - * @brief Tests the #RetryUtils_GetNextBackOff API when the next back-off value - * is requested for exhausted retry attempts. - */ -void test_retryUtils_GetNextBackOff_Attempts_Exhausted() -{ - /* Update the context data to represent that maximum configured number of - * retry attempts have been completed. */ - retryParams.attemptsDone = TEST_MAX_ATTEMPTS; - - /* Call the RetryUtils_GetNextBackOff API. */ - TEST_ASSERT_EQUAL( RetryUtilsRetriesExhausted, - RetryUtils_GetNextBackOff( &retryParams, &nextBackOff ) ); - /* Make sure that the value of the output parameter has not changed. */ - TEST_ASSERT_EQUAL( 0, nextBackOff ); - - /* Make sure that the context data has not changed as the call to - * RetryUtils_GetNextBackOff failed. */ - verifyContextData( &retryParams, - TEST_MAX_ATTEMPTS /* Number of attempts shouldn't change */, - TEST_BACKOFF_BASE_VALUE, - TEST_BACKOFF_MAX_VALUE, - TEST_MAX_ATTEMPTS, - mockRng ); -} - -/** - * @brief Test that the value of the next max jitter has a lower bound that will - * then be capped at some max threshold. - */ -void test_RetryUtils_GetNextBackOff_Returns_Cap_BackOff( void ) -{ - /* Initialize to 0 attempts, so the max value of next jitter will increase. */ - retryParams.attemptsDone = 0U; - - /* Update the next jitter value to greater than half the maximum backoff so - * that the RetryUtils_GetNextBackOff API updates the next jitter value to - * the configured maximum backoff value. */ - retryParams.nextJitterMax = ( TEST_BACKOFF_MAX_VALUE / 2U ) + 1; - - /* Set the random value to be generated equal to the current jitter max value. - * Thus, the RetryUtils_GetNextBackOff API should return the random value as - * the next back-off value. */ - randomValToReturn = retryParams.nextJitterMax; - uint16_t expectedBackOffVal = randomValToReturn; - - /* Call the RetryUtils_GetNextBackOff API. */ - TEST_ASSERT_EQUAL( RetryUtilsSuccess, - RetryUtils_GetNextBackOff( &retryParams, &nextBackOff ) ); - /* Make sure that the expected value is returned for the next backoff. */ - TEST_ASSERT_EQUAL( expectedBackOffVal, nextBackOff ); - - /* Verify that the next jitter max value has been set to the cap back-off value - * configured in the context. */ - verifyContextData( &retryParams, - 1, - TEST_BACKOFF_MAX_VALUE /* New jitter max */, - TEST_BACKOFF_MAX_VALUE, - TEST_MAX_ATTEMPTS, - mockRng ); - - - /* Now, set the random value to be generated as the maximum back-off value to - * expect that the next back-off value returned by the API is the maximum - * back-off value.*/ - randomValToReturn = TEST_BACKOFF_MAX_VALUE; - - /* Call RetryUtils_GetNextBackOff API again to verify that it now returns the - * cap value as the next back-off value. */ - TEST_ASSERT_EQUAL( RetryUtilsSuccess, - RetryUtils_GetNextBackOff( &retryParams, &nextBackOff ) ); - /* Make sure that the capped backoff value is returned as the next backoff value . */ - TEST_ASSERT_EQUAL( TEST_BACKOFF_MAX_VALUE, nextBackOff ); - - /* Verify that the context data for expected data after the API call. */ - verifyContextData( &retryParams, - 2, - TEST_BACKOFF_MAX_VALUE /* jitter max remains unchanged */, - TEST_BACKOFF_MAX_VALUE, - TEST_MAX_ATTEMPTS, - mockRng ); -} - -/** - * @brief Test that the value of the next max jitter has a lower bound that will - * then be capped at some max threshold. - */ -void test_RetryUtils_GetNextBackOff_Returns_Rand_Val( void ) -{ - /* Initialize to 0 attempts, so the max value of next jitter will increase. */ - retryParams.attemptsDone = 0U; - - /* Set the returned random value to zero to test that the - * RetryUtils_GetNextBackOff API will return zero as the - * next back-off value.*/ - randomValToReturn = 0; - - /* Update the next jitter max value to one less than half of max backoff - * to make sure that the RetryUtils_GetNextBackOff API does not update it - * to the cap value in the call.*/ - retryParams.nextJitterMax = ( TEST_BACKOFF_MAX_VALUE / 2U ) - 1; - - /* Call the RetryUtils_GetNextBackOff API. */ - TEST_ASSERT_EQUAL( RetryUtilsSuccess, - RetryUtils_GetNextBackOff( &retryParams, &nextBackOff ) ); - /* Make sure that zero is returned as the next backoff value . */ - TEST_ASSERT_EQUAL( 0, nextBackOff ); - - /* Verify that the context data for expected data after the API call. */ - verifyContextData( &retryParams, - 1, - TEST_BACKOFF_MAX_VALUE - 2U /* next jitter max value */, - TEST_BACKOFF_MAX_VALUE, - TEST_MAX_ATTEMPTS, - mockRng ); -} diff --git a/platform/lexicon.txt b/platform/lexicon.txt index 886008d227..7d2ad211f7 100644 --- a/platform/lexicon.txt +++ b/platform/lexicon.txt @@ -115,8 +115,6 @@ reconnectparam recv recvtimeout recvtimeoutms -retryutilsretriesexhausted -retryutilssuccess retvalue sdk sendtimeout