static void prvProvClientRequest()

in source/azure_iot_provisioning_client.c [268:345]


static void prvProvClientRequest( AzureIoTProvisioningClient_t * pxAzureProvClient )
{
    AzureIoTMQTTResult_t xMQTTResult;
    AzureIoTResult_t xResult;
    AzureIoTMQTTPublishInfo_t xMQTTPublishInfo = { 0 };
    size_t xMQTTTopicLength;
    size_t xMQTTPayloadLength = 0;
    uint16_t usPublishPacketIdentifier;
    az_result xCoreResult;
    az_span xCustomPayloadProperty = az_span_create( ( uint8_t * ) pxAzureProvClient->_internal.pucRegistrationPayload,
                                                     ( int32_t ) pxAzureProvClient->_internal.ulRegistrationPayloadLength );

    /* Check the state.  */
    if( pxAzureProvClient->_internal.ulWorkflowState != azureiotprovisioningWF_STATE_REQUEST )
    {
        AZLogWarn( ( "AzureIoTProvisioning request action called in wrong state: [%u]",
                     ( uint16_t ) pxAzureProvClient->_internal.ulWorkflowState ) );
    }
    else
    {
        /* Check if previous this is the 1st request or subsequent query request */
        if( pxAzureProvClient->_internal.xLastResponsePayloadLength == 0 )
        {
            xCoreResult =
                az_iot_provisioning_client_register_get_publish_topic( &pxAzureProvClient->_internal.xProvisioningClientCore,
                                                                       ( char * ) pxAzureProvClient->_internal.pucScratchBuffer,
                                                                       azureiotconfigTOPIC_MAX, &xMQTTTopicLength );
        }
        else
        {
            xCoreResult =
                az_iot_provisioning_client_query_status_get_publish_topic( &pxAzureProvClient->_internal.xProvisioningClientCore,
                                                                           pxAzureProvClient->_internal.xRegisterResponse.operation_id,
                                                                           ( char * ) pxAzureProvClient->_internal.pucScratchBuffer,
                                                                           azureiotconfigTOPIC_MAX, &xMQTTTopicLength );
        }

        if( az_result_failed( xCoreResult ) )
        {
            prvProvClientUpdateState( pxAzureProvClient, eAzureIoTErrorFailed );
            return;
        }

        xMQTTPayloadLength = pxAzureProvClient->_internal.ulScratchBufferLength - ( uint32_t ) xMQTTTopicLength;
        xCoreResult =
            az_iot_provisioning_client_get_request_payload( &pxAzureProvClient->_internal.xProvisioningClientCore,
                                                            xCustomPayloadProperty, NULL,
                                                            ( uint8_t * ) ( pxAzureProvClient->_internal.pucScratchBuffer +
                                                                            xMQTTTopicLength ),
                                                            ( size_t ) xMQTTPayloadLength, &xMQTTPayloadLength );

        if( az_result_failed( xCoreResult ) )
        {
            prvProvClientUpdateState( pxAzureProvClient, eAzureIoTErrorFailed );
            return;
        }

        xMQTTPublishInfo.xQOS = eAzureIoTMQTTQoS0;
        xMQTTPublishInfo.pcTopicName = pxAzureProvClient->_internal.pucScratchBuffer;
        xMQTTPublishInfo.usTopicNameLength = ( uint16_t ) xMQTTTopicLength;
        xMQTTPublishInfo.pvPayload = pxAzureProvClient->_internal.pucScratchBuffer + xMQTTTopicLength;
        xMQTTPublishInfo.xPayloadLength = xMQTTPayloadLength;
        usPublishPacketIdentifier = AzureIoTMQTT_GetPacketId( &( pxAzureProvClient->_internal.xMQTTContext ) );

        if( ( xMQTTResult = AzureIoTMQTT_Publish( &( pxAzureProvClient->_internal.xMQTTContext ),
                                                  &xMQTTPublishInfo, usPublishPacketIdentifier ) ) != eAzureIoTMQTTSuccess )
        {
            AZLogError( ( "AzureIoTProvisioning failed to publish prov request: MQTT error=0x%08x", ( uint16_t ) xMQTTResult ) );
            xResult = eAzureIoTErrorPublishFailed;
        }
        else
        {
            xResult = eAzureIoTSuccess;
        }

        prvProvClientUpdateState( pxAzureProvClient, xResult );
    }
}