Browse Source

esp_https_ota: Add definition for esp_https_ota_abort

Update esp_https_ota API to use esp_https_ota_abort in case of error
Shubham Kulkarni 5 năm trước cách đây
mục cha
commit
bc78b8f2da

+ 20 - 0
components/esp_https_ota/include/esp_https_ota.h

@@ -135,6 +135,7 @@ bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_ha
  *
  * @note     If this API returns successfully, esp_restart() must be called to
  *           boot from the new firmware image
+ *           esp_https_ota_finish should not be called after calling esp_https_ota_abort
  *
  * @param[in]  https_ota_handle   pointer to esp_https_ota_handle_t structure
  *
@@ -147,6 +148,25 @@ bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_ha
 esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle);
 
 
+/**
+ * @brief Clean-up HTTPS OTA Firmware upgrade and close HTTPS connection
+ *
+ * This function closes the HTTP connection and frees the ESP HTTPS OTA context.
+ *
+ * @note     esp_https_ota_abort should not be called after calling esp_https_ota_finish
+ *
+ * @param[in]  https_ota_handle   pointer to esp_https_ota_handle_t structure
+ *
+ * @return
+ *    - ESP_OK: Clean-up successful
+ *    - ESP_ERR_INVALID_STATE: Invalid ESP HTTPS OTA state
+ *    - ESP_FAIL: OTA not started
+ *    - ESP_ERR_NOT_FOUND: OTA handle not found
+ *    - ESP_ERR_INVALID_ARG: Invalid argument
+ */
+esp_err_t esp_https_ota_abort(esp_https_ota_handle_t https_ota_handle);
+
+
 /**
  * @brief   Reads app description from image header. The app description provides information
  *          like the "Firmware version" of the image.

+ 39 - 5
components/esp_https_ota/src/esp_https_ota.c

@@ -381,6 +381,40 @@ esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle)
     return err;
 }
 
+esp_err_t esp_https_ota_abort(esp_https_ota_handle_t https_ota_handle)
+{
+    esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
+    if (handle == NULL) {
+        return ESP_ERR_INVALID_ARG;
+    }
+
+    if (handle->state < ESP_HTTPS_OTA_BEGIN) {
+        return ESP_FAIL;
+    }
+
+    esp_err_t err = ESP_OK;
+    switch (handle->state) {
+        case ESP_HTTPS_OTA_SUCCESS:
+        case ESP_HTTPS_OTA_IN_PROGRESS:
+            err = esp_ota_abort(handle->update_handle);
+            /* falls through */
+        case ESP_HTTPS_OTA_BEGIN:
+            if (handle->ota_upgrade_buf) {
+                free(handle->ota_upgrade_buf);
+            }
+            if (handle->http_client) {
+                _http_cleanup(handle->http_client);
+            }
+            break;
+        default:
+            err = ESP_ERR_INVALID_STATE;
+            ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
+            break;
+    }
+    free(handle);
+    return err;
+}
+
 int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle)
 {
     esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
@@ -417,13 +451,13 @@ esp_err_t esp_https_ota(const esp_http_client_config_t *config)
         }
     }
 
-    esp_err_t ota_finish_err = esp_https_ota_finish(https_ota_handle);
     if (err != ESP_OK) {
-        /* If there was an error in esp_https_ota_perform(),
-           then it is given more precedence than error in esp_https_ota_finish()
-         */
+        esp_https_ota_abort(https_ota_handle);
         return err;
-    } else if (ota_finish_err != ESP_OK) {
+    }
+
+    esp_err_t ota_finish_err = esp_https_ota_finish(https_ota_handle);
+    if (ota_finish_err != ESP_OK) {
         return ota_finish_err;
     }
     return ESP_OK;