in source/1nce_zero_touch_provisioning.c [338:403]
TlsTransportStatus_t nce_connect( NetworkContext_t * pxNetworkContext )
{
TlsTransportStatus_t xNetworkStatus = TLS_TRANSPORT_CONNECT_FAILURE;
BackoffAlgorithmStatus_t xBackoffAlgStatus = BackoffAlgorithmSuccess;
BackoffAlgorithmContext_t xReconnectParams;
uint16_t usNextRetryBackOff = 0U;
NetworkCredentials_t tNetworkCredentials = { 0 };
LogInfo( ( "Connecting to 1NCE server." ) );
tNetworkCredentials.disableSni = democonfigDISABLE_SNI;
/* Set the credentials for establishing a TLS connection. */
tNetworkCredentials.pRootCa = ( const unsigned char * ) democonfigROOT_CA_PEM;
tNetworkCredentials.rootCaSize = sizeof( democonfigROOT_CA_PEM );
tNetworkCredentials.pClientCert = ( const unsigned char * ) democonfigCLIENT_CERTIFICATE_PEM;
tNetworkCredentials.clientCertSize = sizeof( democonfigCLIENT_CERTIFICATE_PEM );
tNetworkCredentials.pPrivateKey = ( const unsigned char * ) democonfigCLIENT_PRIVATE_KEY_PEM;
tNetworkCredentials.privateKeySize = sizeof( democonfigCLIENT_PRIVATE_KEY_PEM );
/* Initialize reconnect attempts and interval. */
BackoffAlgorithm_InitializeParams( &xReconnectParams,
mqttexampleRETRY_BACKOFF_BASE_MS,
mqttexampleRETRY_MAX_BACKOFF_DELAY_MS,
mqttexampleRETRY_MAX_ATTEMPTS );
/* Attempt to connect to 1NCE server. If connection fails, retry after
* a timeout. Timeout value will exponentially increase till maximum
* attempts are reached.
*/
do
{
LogInfo( ( "Creating a TLS connection to %s:%u.",
ONBOARDING_ENDPOINT,
ONBOARDING_PORT ) );
/* Attempt to create a mutually authenticated TLS connection. */
xNetworkStatus = TLS_FreeRTOS_Connect( pxNetworkContext,
ONBOARDING_ENDPOINT,
ONBOARDING_PORT,
&tNetworkCredentials,
nceTRANSPORT_SEND_RECV_TIMEOUT_MS,
nceTRANSPORT_SEND_RECV_TIMEOUT_MS );
if( xNetworkStatus != TLS_TRANSPORT_SUCCESS )
{
/* Generate a random number and calculate backoff value (in milliseconds) for
* the next connection retry.
* Note: It is recommended to seed the random number generator with a device-specific
* entropy source so that possibility of multiple devices retrying failed network operations
* at similar intervals can be avoided. */
xBackoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &xReconnectParams, uxRand(), &usNextRetryBackOff );
if( xBackoffAlgStatus == BackoffAlgorithmRetriesExhausted )
{
LogError( ( "Connection to the broker failed, all attempts exhausted." ) );
}
else if( xBackoffAlgStatus == BackoffAlgorithmSuccess )
{
LogWarn( ( "Connection to the broker failed. "
"Retrying connection with backoff and jitter." ) );
vTaskDelay( pdMS_TO_TICKS( usNextRetryBackOff ) );
}
}
} while ( ( xNetworkStatus != TLS_TRANSPORT_SUCCESS ) && ( xBackoffAlgStatus == BackoffAlgorithmSuccess ) );
return xNetworkStatus;
}