static OtaErr_t processDataHandler()

in source/ota.c [1165:1291]


static OtaErr_t processDataHandler( const OtaEventData_t * pEventData )
{
    OtaErr_t err = OtaErrNone;
    OtaPalStatus_t closeResult = OTA_PAL_COMBINE_ERR( OtaPalUninitialized, 0 );
    OtaEventMsg_t eventMsg = { 0 };
    IngestResult_t result = IngestResultUninitialized;
    OtaJobDocument_t jobDoc = { 0 };
    OtaJobEvent_t otaJobEvent = OtaLastJobEvent;

    /* Get the file context. */
    OtaFileContext_t * pFileContext = &( otaAgent.fileContext );

    /* Set the job id and length from OTA context. */
    jobDoc.pJobId = otaAgent.pActiveJobName;
    jobDoc.jobIdLength = strlen( ( const char * ) otaAgent.pActiveJobName ) + 1U;
    jobDoc.fileTypeId = otaAgent.fileContext.fileType;

    /* Ingest data blocks received. */
    if( pEventData != NULL )
    {
        result = ingestDataBlock( pFileContext,
                                  pEventData->data,
                                  pEventData->dataLength,
                                  &closeResult );
    }
    else
    {
        result = IngestResultNullInput;
    }

    if( result == IngestResultFileComplete )
    {
        /* Check if this is firmware update. */
        if( otaAgent.fileContext.fileType == configOTA_FIRMWARE_UPDATE_FILE_TYPE_ID )
        {
            jobDoc.status = JobStatusInProgress;
            jobDoc.reason = JobReasonSigCheckPassed;

            otaJobEvent = OtaJobEventActivate;
        }
        else
        {
            jobDoc.status = JobStatusSucceeded;
            jobDoc.reason = JobReasonAccepted;
            jobDoc.subReason = ( int32_t ) otaAgent.fileContext.fileType;

            otaJobEvent = OtaJobEventUpdateComplete;
        }

        /* File receive is complete and authenticated. Update the job status. */
        err = otaControlInterface.updateJobStatus( &otaAgent, jobDoc.status, jobDoc.reason, jobDoc.subReason );

        dataHandlerCleanup();

        if( otaAgent.statistics.otaPacketsProcessed < UINT32_MAX )
        {
            /* Last file block processed, increment the statistics. */
            otaAgent.statistics.otaPacketsProcessed++;
        }

        /* Let main application know that update is complete */
        otaAgent.OtaAppCallback( otaJobEvent, &jobDoc );
    }
    else if( result < IngestResultFileComplete )
    {
        LogError( ( "Failed to ingest data block, rejecting image: ingestDataBlock returned error: OtaErr_t=%d", result ) );

        /* Call the platform specific code to reject the image. */
        ( void ) otaAgent.pOtaInterface->pal.setPlatformImageState( &( otaAgent.fileContext ), OtaImageStateRejected );

        jobDoc.status = JobStatusFailedWithVal;
        jobDoc.reason = ( int32_t ) OTA_PAL_MAIN_ERR( closeResult );
        jobDoc.subReason = ( int32_t ) OTA_PAL_SUB_ERR( closeResult );

        /* Update the job status with the with failure code. */
        err = otaControlInterface.updateJobStatus( &otaAgent, JobStatusFailedWithVal, ( int32_t ) OTA_PAL_MAIN_ERR( closeResult ), ( int32_t ) OTA_PAL_SUB_ERR( closeResult ) );

        dataHandlerCleanup();

        /* Let main application know activate event. */
        otaAgent.OtaAppCallback( OtaJobEventFail, &jobDoc );
    }
    else
    {
        if( result == IngestResultAccepted_Continue )
        {
            if( otaAgent.statistics.otaPacketsProcessed < UINT32_MAX )
            {
                /* Last file block processed, increment the statistics. */
                otaAgent.statistics.otaPacketsProcessed++;
            }

            /* Reset the momentum counter since we received a good block. */
            otaAgent.requestMomentum = 0;
            /* We're actively receiving a file so update the job status as needed. */
            err = otaControlInterface.updateJobStatus( &otaAgent, JobStatusInProgress, JobReasonReceiving, 0 );
        }

        if( otaAgent.numOfBlocksToReceive > 1U )
        {
            otaAgent.numOfBlocksToReceive--;
        }
        else
        {
            /* Start the request timer. */
            ( void ) otaAgent.pOtaInterface->os.timer.start( OtaRequestTimer, "OtaRequestTimer", otaconfigFILE_REQUEST_WAIT_MS, otaTimerCallback );

            eventMsg.eventId = OtaAgentEventRequestFileBlock;

            if( OTA_SignalEvent( &eventMsg ) == false )
            {
                LogWarn( ( "Failed to trigger requesting the next block: Unable to signal event=%d", eventMsg.eventId ) );
            }
        }
    }

    /* Application callback for event processed. */
    otaAgent.OtaAppCallback( OtaJobEventProcessed, ( const void * ) pEventData );

    if( err != OtaErrNone )
    {
        LogError( ( "Failed to update job status: updateJobStatus returned error: OtaErr_t=%s",
                    OTA_Err_strerror( err ) ) );
    }

    return err;
}