Support multiple transports in the same compilation unit (#434)

By removing the definition of the NetworkContext struct in the header file, we allow the application to define it. This allows an application writer to use multiple transports in the same compilation unit. That way, multiple .c files do not have to be created for each transport.
This commit is contained in:
Oscar Michael Abrina
2020-12-01 17:24:18 -08:00
committed by GitHub
parent a9fd30af94
commit 73b0d1b259
27 changed files with 406 additions and 106 deletions

View File

@ -56,6 +56,15 @@
/*-----------------------------------------------------------*/
/* Each compilation unit must define the NetworkContext struct.
* void * is used as this utility can be used by both plaintext and TLS demos. */
struct NetworkContext
{
void * pParams;
};
/*-----------------------------------------------------------*/
extern UBaseType_t uxRand();
/*-----------------------------------------------------------*/

View File

@ -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
@ -19,8 +19,8 @@
* 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.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*
*/

View File

@ -122,6 +122,12 @@
*/
#define IOT_CORE_ALPN_PROTOCOL_NAME "\x0ex-amzn-http-ca"
/* Each compilation unit must define the NetworkContext struct. */
struct NetworkContext
{
TlsTransportParams_t * pParams;
};
/**
* @brief A buffer used in the demo for storing HTTP request headers and
* HTTP response headers and body.
@ -207,6 +213,7 @@ static void prvHTTPDemoTask( void * pvParameters )
TransportInterface_t xTransportInterface;
/* The network context for the transport layer interface. */
NetworkContext_t xNetworkContext = { 0 };
TlsTransportParams_t xTlsTransportParams = { 0 };
BaseType_t xIsConnectionEstablished = pdFALSE;
/* The user of this demo must check the logs for any failure codes. */
@ -215,6 +222,9 @@ static void prvHTTPDemoTask( void * pvParameters )
/* Remove compiler warnings about unused parameters. */
( void ) pvParameters;
/* Set the pParams member of the network context with desired transport. */
xNetworkContext.pParams = &xTlsTransportParams;
/**************************** Connect. ******************************/
/* Attempt to connect to the HTTP server. If connection fails, retry after a

View File

@ -164,6 +164,12 @@
*/
#define httpexampleNUMBER_HTTP_PATHS ( 4 )
/* Each compilation unit must define the NetworkContext struct. */
struct NetworkContext
{
PlaintextTransportParams_t * pParams;
};
/**
* @brief A pair containing a path string of the URI and its length.
*/
@ -269,6 +275,7 @@ static void prvHTTPDemoTask( void * pvParameters )
TransportInterface_t xTransportInterface;
/* The network context for the transport layer interface. */
NetworkContext_t xNetworkContext = { 0 };
PlaintextTransportParams_t xPlaintextTransportParams = { 0 };
/* An array of HTTP paths to request. */
const httpPathStrings_t xHttpMethodPaths[] =
{
@ -294,6 +301,9 @@ static void prvHTTPDemoTask( void * pvParameters )
/* Remove compiler warnings about unused parameters. */
( void ) pvParameters;
/* Set the pParams member of the network context with desired transport. */
xNetworkContext.pParams = &xPlaintextTransportParams;
/**************************** Connect. ******************************/
/* Attempt to connect to the HTTP server. If connection fails, retry after a