Skip to content

Commit

Permalink
Porting HTTP Mutual Auth Demo from C-SDK (FreeRTOS#414)
Browse files Browse the repository at this point in the history
Add demo to establish a mutually-authenticated network connection with the server before sending and verifying a simple POST request.
* Add demo files
* Move all "Http_Demo_Helpers" files into Common
* Update project files and add vendor config files
* Update http_demo_utils.c after backoff updates
  • Loading branch information
sukhmanm committed Nov 25, 2020
1 parent 036ec83 commit ac1a104
Show file tree
Hide file tree
Showing 13 changed files with 2,811 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* FreeRTOS Kernel V10.3.0
* FreeRTOS 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
Expand All @@ -18,6 +18,10 @@
* 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.
*
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*
*/

/* Standard includes. */
Expand All @@ -33,6 +37,49 @@

/*-----------------------------------------------------------*/

/**
* @brief The maximum number of retries for network operation with server.
*/
#define MAX_RETRY_ATTEMPTS ( 5U )

/**
* @brief The maximum back-off delay (in milliseconds) for retrying failed
* operation with server.
*/
#define RETRY_MAX_BACKOFF_DELAY_MS ( 5000U )

/**
* @brief The base back-off delay (in milliseconds) to use for network operation
* retry attempts.
*/
#define RETRY_BACKOFF_BASE_MS ( 500U )

/*-----------------------------------------------------------*/

/**
* @brief A wrapper to the "uxRand()" random number generator so that it
* can be passed to the backoffAlgorithm library for retry logic.
*
* This function implements the #BackoffAlgorithm_RNG_T type interface
* in the backoffAlgorithm library API.
*
* @note The "uxRand" function represents a pseudo random number generator.
* However, it is recommended to use a True Randon Number Generator (TRNG)
* for generating unique device-specific random values to avoid possibility
* of network collisions from multiple devices retrying network operations.
*
* @return The generated randon number. This function ALWAYS succeeds.
*/
static int32_t prvGenerateRandomNumber();

/*-----------------------------------------------------------*/

static int32_t prvGenerateRandomNumber()
{
return( uxRand() & INT32_MAX );
}

/*-----------------------------------------------------------*/
BaseType_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction,
NetworkContext_t * pxNetworkContext )
{
Expand All @@ -41,14 +88,15 @@ BaseType_t connectToServerWithBackoffRetries( TransportConnect_t connectFunction
BackoffAlgorithmStatus_t xBackoffAlgStatus = BackoffAlgorithmSuccess;
/* Struct containing the next backoff time. */
BackoffAlgorithmContext_t xReconnectParams;
uint16_t usNextBackoff = 0U;

assert( connectFunction != NULL );

/* Initialize reconnect attempts and interval */
BackoffAlgorithm_InitializeParams( &xReconnectParams,
RETRY_BACKOFF_BASE_MS,
RETRY_MAX_BACKOFF_DELAY_MS,
RETRY_MAX_ATTEMPTS,
MAX_RETRY_ATTEMPTS,
prvGenerateRandomNumber );

/* Attempt to connect to the HTTP server. If connection fails, retry after a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* FreeRTOS Kernel V10.3.0
* FreeRTOS 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
Expand All @@ -18,6 +18,10 @@
* 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.
*
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*
*/

#ifndef HTTP_DEMO_UTILS_H
Expand All @@ -32,9 +36,6 @@
#include "FreeRTOS.h"
#include "task.h"

/* Transport interface implementation include header for TLS. */
#include "using_mbedtls.h"

/* HTTP API header. */
#include "core_http_client.h"

Expand Down

0 comments on commit ac1a104

Please sign in to comment.