static OtaJobParseErr_t handleCustomJob()

in source/ota.c [1952:2028]


static OtaJobParseErr_t handleCustomJob( const char * pJson,
                                         uint32_t messageLength )
{
    OtaErr_t otaErr = OtaErrNone;
    OtaJobParseErr_t err = OtaJobParseErrUnknown;
    OtaJobDocument_t jobDoc = { 0 };
    const char * jobDocValue = NULL;
    const char * jobIdValue = NULL;

    jobDoc.parseErr = OtaJobParseErrUnknown;

    /* If this is a valid custom job, extract job document and job ID data from the JSON payload.*/
    if( ( JSONSuccess == JSON_SearchConst( pJson,
                                           messageLength,
                                           OTA_JSON_JOB_DOC_KEY,
                                           strlen( OTA_JSON_JOB_DOC_KEY ),
                                           &jobDocValue,
                                           &jobDoc.jobDocLength,
                                           NULL ) ) &&

        ( JSONSuccess == JSON_SearchConst( pJson,
                                           messageLength,
                                           OTA_JSON_JOB_ID_KEY,
                                           strlen( OTA_JSON_JOB_ID_KEY ),
                                           &jobIdValue,
                                           &jobDoc.jobIdLength,
                                           NULL ) ) )
    {
        if( ( jobDoc.jobIdLength > 0U ) && ( jobDoc.jobIdLength <= OTA_JOB_ID_MAX_SIZE ) ) /* LCOV_EXCL_BR_LINE */
        {
            jobDoc.pJobDocJson = ( const uint8_t * ) jobDocValue;
            jobDoc.pJobId = ( const uint8_t * ) jobIdValue;
            ( void ) memcpy( otaAgent.pActiveJobName, jobDoc.pJobId, jobDoc.jobIdLength );
        }
        else
        {
            jobDoc.parseErr = OtaJobParseErrNonConformingJobDoc;
        }

        /* We have an unknown job parser error. Check to see if we can pass control
         * to a callback for parsing */
        otaAgent.OtaAppCallback( OtaJobEventParseCustomJob, &jobDoc );

        if( jobDoc.parseErr == OtaJobParseErrNone )
        {
            otaErr = otaControlInterface.updateJobStatus( &otaAgent, jobDoc.status, jobDoc.reason, jobDoc.subReason );

            LogInfo( ( "Job document parsed from external callback" ) );
            err = jobDoc.parseErr;

            /* We don't need the job name memory anymore since we're done with this job. */
            ( void ) memset( otaAgent.pActiveJobName, 0, OTA_JOB_ID_MAX_SIZE );
        }
        else
        {
            /* Job is malformed - return an error */
            err = OtaJobParseErrNonConformingJobDoc;

            LogDebug( ( "Failed to parse custom job document: OtaJobParseErr_t=%s, jobIdLength=%lu",
                        OTA_JobParse_strerror( jobDoc.parseErr ), ( unsigned long ) jobDoc.jobIdLength ) );
        }
    }
    else
    {
        /* No active jobs present.*/
        err = OtaJobParseErrNoActiveJobs;
    }

    /* Log the error.*/
    if( otaErr != OtaErrNone )
    {
        LogError( ( "Failed to update job status: updateJobStatus returned error: OtaErr_t=%s",
                    OTA_Err_strerror( otaErr ) ) );
    }

    return err;
}