static void prvProvClientConnect()

in source/azure_iot_provisioning_client.c [145:217]


static void prvProvClientConnect( AzureIoTProvisioningClient_t * pxAzureProvClient )
{
    AzureIoTMQTTConnectInfo_t xConnectInfo = { 0 };
    AzureIoTResult_t xResult;
    AzureIoTMQTTResult_t xMQTTResult;
    bool xSessionPresent;
    uint32_t ulPasswordLength = 0;
    size_t xMQTTUsernameLength;
    az_result xCoreResult;

    if( pxAzureProvClient->_internal.ulWorkflowState != azureiotprovisioningWF_STATE_CONNECT )
    {
        AZLogError( ( "AzureIoTProvisioning connect action called in wrong state: [%u]",
                      ( uint16_t ) pxAzureProvClient->_internal.ulWorkflowState ) );
        return;
    }

    xConnectInfo.pcUserName = pxAzureProvClient->_internal.pucScratchBuffer;
    xConnectInfo.pcPassword = xConnectInfo.pcUserName + azureiotconfigUSERNAME_MAX;

    if( az_result_failed(
            xCoreResult = az_iot_provisioning_client_get_user_name( &pxAzureProvClient->_internal.xProvisioningClientCore,
                                                                    ( char * ) xConnectInfo.pcUserName,
                                                                    azureiotconfigUSERNAME_MAX, &xMQTTUsernameLength ) ) )
    {
        AZLogError( ( "AzureIoTProvisioning failed to get username: core error=0x%08x", ( uint16_t ) xCoreResult ) );
        xResult = AzureIoT_TranslateCoreError( xCoreResult );
    }
    /* Check if token refresh is set, then generate password */
    else if( ( pxAzureProvClient->_internal.pxTokenRefresh ) &&
             ( pxAzureProvClient->_internal.pxTokenRefresh( pxAzureProvClient,
                                                            pxAzureProvClient->_internal.xGetTimeFunction() +
                                                            azureiotprovisioningDEFAULT_TOKEN_TIMEOUT_IN_SEC,
                                                            pxAzureProvClient->_internal.pucSymmetricKey,
                                                            pxAzureProvClient->_internal.ulSymmetricKeyLength,
                                                            ( uint8_t * ) xConnectInfo.pcPassword,
                                                            azureiotconfigPASSWORD_MAX,
                                                            &ulPasswordLength ) ) )
    {
        AZLogError( ( "AzureIoTProvisioning failed to generate auth token" ) );
        xResult = eAzureIoTErrorTokenGenerationFailed;
    }
    else
    {
        xConnectInfo.xCleanSession = true;
        xConnectInfo.pcClientIdentifier = pxAzureProvClient->_internal.pucRegistrationID;
        xConnectInfo.usClientIdentifierLength = ( uint16_t ) pxAzureProvClient->_internal.ulRegistrationIDLength;
        xConnectInfo.usUserNameLength = ( uint16_t ) xMQTTUsernameLength;
        xConnectInfo.usKeepAliveSeconds = azureiotprovisioningKEEP_ALIVE_TIMEOUT_SECONDS;
        xConnectInfo.usPasswordLength = ( uint16_t ) ulPasswordLength;

        if( ( xMQTTResult = AzureIoTMQTT_Connect( &( pxAzureProvClient->_internal.xMQTTContext ),
                                                  &xConnectInfo, NULL, azureiotprovisioningCONNACK_RECV_TIMEOUT_MS,
                                                  &xSessionPresent ) ) != eAzureIoTMQTTSuccess )
        {
            AZLogError( ( "AzureIoTProvisioning failed to establish MQTT connection: Server=%.*s, MQTT error=0x%08x",
                          ( int16_t ) pxAzureProvClient->_internal.ulEndpointLength,
                          pxAzureProvClient->_internal.pucEndpoint,
                          ( uint16_t ) xMQTTResult ) );
            xResult = eAzureIoTErrorServerError;
        }
        else
        {
            /* Successfully established and MQTT connection with the broker. */
            AZLogInfo( ( "AzureIoTProvisioning established an MQTT connection with %.*s",
                         ( int16_t ) pxAzureProvClient->_internal.ulEndpointLength,
                         pxAzureProvClient->_internal.pucEndpoint ) );
            xResult = eAzureIoTSuccess;
        }
    }

    prvProvClientUpdateState( pxAzureProvClient, xResult );
}