Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ota app callback for job and update complete #186

Merged
merged 11 commits into from
Feb 26, 2021
8 changes: 4 additions & 4 deletions docs/doxygen/include/size_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</tr>
<tr>
<td>ota.c</td>
<td><center>7.9K</center></td>
<td><center>7.0K</center></td>
<td><center>8.0K</center></td>
<td><center>7.1K</center></td>
</tr>
<tr>
<td>ota_interface.c</td>
Expand Down Expand Up @@ -39,7 +39,7 @@
</tr>
<tr>
<td><b>Total estimates</b></td>
<td><b><center>11.9K</center></b></td>
<td><b><center>10.7K</center></b></td>
<td><b><center>12.0K</center></b></td>
<td><b><center>10.8K</center></b></td>
</tr>
</table>
3 changes: 3 additions & 0 deletions source/include/ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ typedef enum OtaJobEvent
OtaJobEventProcessed = 3, /*!< @brief OTA event queued by OTA_SignalEvent is processed. */
OtaJobEventSelfTestFailed = 4, /*!< @brief OTA self-test failed for current job. */
OtaJobEventParseCustomJob = 5, /*!< @brief OTA event for parsing custom job document. */
OtaJobEventReceivedJob = 6, /*!< @brief OTA event when a new valid AFT-OTA job is received. */
OtaJobEventUpdateComplete = 7, /*!< @brief OTA event when the update is completed. */
OtaLastJobEvent = OtaJobEventStartTest
} OtaJobEvent_t;

Expand Down Expand Up @@ -195,6 +197,7 @@ typedef struct OtaJobDocument
size_t jobDocLength; /*!< @brief Job document length in bytes. */
const uint8_t * pJobId; /*!< @brief Job ID associated with the job document. */
size_t jobIdLength; /*!< @brief Length of job ID in bytes. */
uint32_t fileTypeId; /*!< @brief File Type ID from the job document. */
OtaJobParseErr_t parseErr; /*!< @brief Job parsing status. */
OtaJobStatus_t status; /*!< @brief Job status. */
int32_t reason; /*!< @brief Job status reason. */
Expand Down
76 changes: 53 additions & 23 deletions source/ota.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ static void agentShutdownCleanup( void );
/**
* @brief A helper function to cleanup resources when data ingestion is complete.
*/
static void dataHandlerCleanup( IngestResult_t result );
static void dataHandlerCleanup( void );

/**
* @brief Prepare the document model for use by sanity checking the initialization parameters and detecting all required parameters.
Expand Down Expand Up @@ -1140,17 +1140,13 @@ static OtaErr_t requestDataHandler( const OtaEventData_t * pEventData )
return err;
}

static void dataHandlerCleanup( IngestResult_t result )
static void dataHandlerCleanup( void )
{
OtaEventMsg_t eventMsg = { 0 };

/* Stop the request timer. */
( void ) otaAgent.pOtaInterface->os.timer.stop( OtaRequestTimer );

/* Negative result codes mean we should stop the OTA process
* because we are either done or in an unrecoverable error state.
* We don't want to hang on to the resources. */

/* Send event to close file. */
eventMsg.eventId = OtaAgentEventCloseFile;

Expand All @@ -1162,9 +1158,6 @@ static void dataHandlerCleanup( IngestResult_t result )
eventMsg.eventId ) );
}

/* Let main application know of our result. */
otaAgent.OtaAppCallback( ( result == IngestResultFileComplete ) ? OtaJobEventActivate : OtaJobEventFail, NULL );

/* Clear any remaining string memory holding the job name since this job is done. */
( void ) memset( otaAgent.pActiveJobName, 0, OTA_JOB_ID_MAX_SIZE );
}
Expand All @@ -1175,10 +1168,16 @@ static OtaErr_t processDataHandler( const OtaEventData_t * pEventData )
OtaPalStatus_t closeResult = OTA_PAL_COMBINE_ERR( OtaPalUninitialized, 0 );
OtaEventMsg_t eventMsg = { 0 };
IngestResult_t result = IngestResultUninitialized;
OtaJobDocument_t jobDoc = { 0 };

/* 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 )
{
Expand All @@ -1194,26 +1193,51 @@ static OtaErr_t processDataHandler( const OtaEventData_t * pEventData )

if( result == IngestResultFileComplete )
{
/* File receive is complete and authenticated. Update the job status with the self_test ready identifier. */
err = otaControlInterface.updateJobStatus( &otaAgent, JobStatusInProgress, JobReasonSigCheckPassed, 0 );
dataHandlerCleanup( result );
/* Check if this is firmware update. */
if( otaAgent.fileContext.fileType == configOTA_FIRMWARE_UPDATE_FILE_TYPE_ID )
{
jobDoc.status = JobStatusInProgress;
jobDoc.reason = JobReasonSigCheckPassed;

/* Let main application know activate event. */
otaAgent.OtaAppCallback( OtaJobEventActivate, &jobDoc );
}
else
{
jobDoc.status = JobStatusSucceeded;
jobDoc.reason = JobReasonAccepted;
jobDoc.subReason = ( int32_t ) otaAgent.fileContext.fileType;

/* Let main application know that update is complete */
otaAgent.OtaAppCallback( OtaJobEventUpdateComplete, &jobDoc );
}

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

dataHandlerCleanup();

/* Last file block processed, increment the statistics. */
otaAgent.statistics.otaPacketsProcessed++;
}
else if( result < IngestResultFileComplete )
{
LogError( ( "Failed to ingest data block, rejecting image: ingestDataBlock returned error: "
"OtaErr_t=%d",
result ) );
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 ) closeResult;
jobDoc.subReason = result;

/* Let main application know activate event. */
otaAgent.OtaAppCallback( OtaJobEventFail, &jobDoc );

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

dataHandlerCleanup( result );
dataHandlerCleanup();
}
else
{
Expand All @@ -1235,18 +1259,13 @@ static OtaErr_t processDataHandler( const OtaEventData_t * pEventData )
else
{
/* Start the request timer. */
( void ) otaAgent.pOtaInterface->os.timer.start( OtaRequestTimer,
"OtaRequestTimer",
otaconfigFILE_REQUEST_WAIT_MS,
otaTimerCallback );
( 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: "
"event=%d",
eventMsg.eventId ) );
LogWarn( ( "Failed to trigger requesting the next block: Unable to signal event=%d", eventMsg.eventId ) );
}
}
}
Expand Down Expand Up @@ -2269,6 +2288,7 @@ static OtaFileContext_t * parseJobDoc( const JsonDocParam_t * pJsonExpectedParam
OtaFileContext_t * pFinalFile = NULL;
OtaFileContext_t * pFileContext = &( otaAgent.fileContext );
JsonDocModel_t otaJobDocModel;
OtaJobDocument_t jobDoc = { 0 };

parseError = initDocModel( &otaJobDocModel,
pJsonExpectedParams,
Expand Down Expand Up @@ -2299,6 +2319,16 @@ static OtaFileContext_t * parseJobDoc( const JsonDocParam_t * pJsonExpectedParam
LogInfo( ( "Job parsing success: "
"OtaJobParseErr_t=%s, Job name=%s",
OTA_JobParse_strerror( err ), ( const char * ) pFileContext->pJobName ) );

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

/* Let the application know to release buffer.*/
otaAgent.OtaAppCallback( OtaJobEventReceivedJob, ( const void * ) &jobDoc );
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ add_subdirectory( unit-test )
add_custom_target( coverage
COMMAND ${CMAKE_COMMAND} -DCMOCK_DIR=${CMOCK_DIR}
-P ${MODULE_ROOT_DIR}/tools/cmock/coverage.cmake
DEPENDS cmock unity ota_utest ota_base64_utest ota_job_parsing_utest ota_cbor_utest
DEPENDS cmock unity ota_utest ota_base64_utest ota_cbor_utest
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
7 changes: 0 additions & 7 deletions test/unit-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ create_test(ota_base64_utest
"${test_include_directories}"
)

create_test(ota_job_parsing_utest
"ota_job_parsing_utest.c"
"${utest_link_list}"
"${utest_dep_list}"
"${test_include_directories}"
)

create_test(ota_cbor_utest
"ota_cbor_utest.c"
"${utest_link_list}"
Expand Down
Loading