OtaPalStatus_t otaPal_SetPlatformImageState()

in components/ota_pal/source/ota_pal.c [786:884]


OtaPalStatus_t otaPal_SetPlatformImageState(OtaFileContext_t *const pFileContext,
                                            OtaImageState_t eState)
{
    OtaPalMainStatus_t mainErr = OtaPalSuccess;
    int state;

    (void)pFileContext;

    LogInfo(("%s, %d", __func__, eState));

    switch (eState)
    {
    case OtaImageStateAccepted:
        LogInfo(("Set image as valid one!"));
        state = ESP_OTA_IMG_VALID;
        break;

    case OtaImageStateRejected:
        LogWarn(("Set image as invalid!"));
        state = ESP_OTA_IMG_INVALID;
        break;

    case OtaImageStateAborted:
        LogWarn(("Set image as aborted!"));
        state = ESP_OTA_IMG_ABORTED;
        break;

    case OtaImageStateTesting:
        LogWarn(("Set image as testing!"));
        return OTA_PAL_COMBINE_ERR(OtaPalSuccess, 0);

    default:
        LogWarn(("Set image invalid state!"));
        return OTA_PAL_COMBINE_ERR(OtaPalBadImageState, 0);
    }

    uint32_t ota_flags;
    /* Get current active (running) firmware image flags */
    esp_err_t ret = aws_esp_ota_get_boot_flags(&ota_flags, true);

    if (ret != ESP_OK)
    {
        LogError(("Failed to get ota flags %d", ret));
        return OTA_PAL_COMBINE_ERR(OtaPalCommitFailed, 0);
    }

    /* If this is first request to set platform state, post bootup and there is not OTA being
     * triggered yet, then operate on active image flags, else use passive image flags */
    if ((ota_ctx.cur_ota == NULL) && (ota_ctx.data_write_len == 0))
    {
        if (ota_flags == ESP_OTA_IMG_PENDING_VERIFY)
        {
            ret = aws_esp_ota_set_boot_flags(state, true);

            if (ret != ESP_OK)
            {
                LogError(("Failed to set ota flags %d", ret));
                return OTA_PAL_COMBINE_ERR(OtaPalCommitFailed, 0);
            }
            else
            {
                /* RTC watchdog timer can now be stopped */
                disable_rtc_wdt();
            }
        }
        else
        {
            LogWarn(("Image not in self test mode %d", ota_flags));
            mainErr = ota_flags == ESP_OTA_IMG_VALID ? OtaPalSuccess : OtaPalCommitFailed;
        }

        /* For debug purpose only, get current flags */
        aws_esp_ota_get_boot_flags(&ota_flags, true);
    }
    else
    {
        if ((eState == OtaImageStateAccepted) && (ota_ctx.valid_image == false))
        {
            /* Incorrect update image or not yet validated */
            return OTA_PAL_COMBINE_ERR(OtaPalCommitFailed, 0);
        }

        if (ota_flags != ESP_OTA_IMG_VALID)
        {
            LogError(("Currently executing firmware not marked as valid, abort"));
            return OTA_PAL_COMBINE_ERR(OtaPalCommitFailed, 0);
        }

        ret = aws_esp_ota_set_boot_flags(state, false);

        if (ret != ESP_OK)
        {
            LogError(("Failed to set ota flags %d", ret));
            return OTA_PAL_COMBINE_ERR(OtaPalCommitFailed, 0);
        }
    }

    return OTA_PAL_COMBINE_ERR(mainErr, 0);
}