How to properly shutdown on OtaJobEventActivate event?


Happy New Year of 2022!

We have some doubts regarding OTA firmware update (ota_demo_core_http) and would like to clarify about it.

  1. How to properly shutdown after receiving OtaJobEventActivate?

a) From the sample demo code, it seems that OTA_Shutdown might not get called since OTA_ActivateNewImage() may issue a reboot. May I know why OTA_Shutdown is called after and not before?

b) In the sample code, MQTTAgent thread might be still be still running at this point of time, how can we terminate MQTT Agent gracefully?

Sample Code (ota_demo_core_http.c)

static void prvOtaAppCallback( OtaJobEvent_t xEvent,
                               const void * pData )
    OtaErr_t xOtaError = OtaErrUninitialized;

    switch( xEvent )
        case OtaJobEventActivate:
            LogInfo( ( "Received OtaJobEventActivate callback from OTA Agent." ) );

            /* Activate the new firmware image. */

            /* Initiate Shutdown of OTA Agent.
             * If it is required that the unsubscribe operations are not
             * performed while shutting down please set the second parameter to
             * 0 instead of 1.
            OTA_Shutdown( otaexampleOTA_SHUTDOWN_WAIT_TICKS, otaexampleUNSUBSCRIBE_AFTER_OTA_SHUTDOWN );


Hi, OTA_ActivateNewImage() will not work if OTA_Shutdown() has been called before it as OTA_Shutdown() clears the library state which is used by OTA_ActivateNewImage(). OTA_ActivateNewImage() should reset the device if successful so code after it is only run when OTA_ActivateNewImage() fails. The above code runs OTA_ActivateNewImage() and if it fails, cleans up the OTA Agent.

If you would like to also clean up the MQTT Agent, you can use MQTTAgent_Terminate.