AzureIoTResult_t AzureIoTADUClient_SendResponse()

in source/azure_iot_adu_client.c [267:324]


AzureIoTResult_t AzureIoTADUClient_SendResponse( AzureIoTADUClient_t * pxAzureIoTADUClient,
                                                 AzureIoTHubClient_t * pxAzureIoTHubClient,
                                                 AzureIoTADURequestDecision_t xRequestDecision,
                                                 uint32_t ulPropertyVersion,
                                                 uint8_t * pucWritablePropertyResponseBuffer,
                                                 uint32_t ulWritablePropertyResponseBufferSize,
                                                 uint32_t * pulRequestId )
{
    az_result xCoreResult;
    AzureIoTResult_t xResult;
    az_json_writer jw;
    az_span xPayload;

    if( ( pxAzureIoTADUClient == NULL ) || ( pxAzureIoTHubClient == NULL ) ||
        ( pucWritablePropertyResponseBuffer == NULL ) || ( ulWritablePropertyResponseBufferSize == 0 ) )
    {
        AZLogError( ( "AzureIoTADUClient_SendResponse failed: invalid argument" ) );
        return eAzureIoTErrorInvalidArgument;
    }

    xCoreResult = az_json_writer_init( &jw, az_span_create(
                                           pucWritablePropertyResponseBuffer,
                                           ( int32_t ) ulWritablePropertyResponseBufferSize ), NULL );

    if( az_result_failed( xCoreResult ) )
    {
        AZLogError( ( "az_json_writer_init failed: 0x%08x", ( uint16_t ) xCoreResult ) );
        return AzureIoT_TranslateCoreError( xCoreResult );
    }

    xCoreResult = az_iot_adu_client_get_service_properties_response(
        &pxAzureIoTADUClient->_internal.xADUClient,
        ( int32_t ) ulPropertyVersion,
        xRequestDecision == eAzureIoTADURequestDecisionAccept ? AZ_IOT_ADU_CLIENT_REQUEST_DECISION_ACCEPT : AZ_IOT_ADU_CLIENT_REQUEST_DECISION_REJECT,
        &jw );

    if( az_result_failed( xCoreResult ) )
    {
        AZLogError( ( "az_iot_adu_client_get_service_properties_response failed: 0x%08x (%d)", ( uint16_t ) xCoreResult, ( int16_t ) ulWritablePropertyResponseBufferSize ) );
        return AzureIoT_TranslateCoreError( xCoreResult );
    }

    xPayload = az_json_writer_get_bytes_used_in_destination( &jw );

    xResult = AzureIoTHubClient_SendPropertiesReported(
        pxAzureIoTHubClient,
        az_span_ptr( xPayload ),
        ( uint32_t ) az_span_size( xPayload ),
        pulRequestId );

    if( xResult != eAzureIoTSuccess )
    {
        AZLogError( ( "[ADU] Failed sending ADU writable properties response: 0x%08x", ( uint16_t ) xResult ) );
        return eAzureIoTErrorPublishFailed;
    }

    return eAzureIoTSuccess;
}