#ifndef OTA_UPGRADE_H #define OTA_UPGRADE_H #include "esp_err.h" #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "esp_system.h" #include "esp_event.h" #include "esp_log.h" #include "esp_ota_ops.h" #include "esp_app_format.h" #include "esp_http_client.h" #include "esp_partition.h" #include "nvs_flash.h" #include "protocol_examples_common.h" #include "errno.h" #include "esp_wifi.h" #define BUFFSIZE (1024*4) #define OTA_TASK_STACK_SIZE 1024*24//1024*24 #define OTA_TASK_PRIORITY 5 /** * @brief OTA升级状态回调函数类型 * @param status 升级状态 * @param progress 升级进度(0-100) * @param message 状态信息 */ typedef void (*ota_status_callback_t)(const char* status, int progress, const char* message); // 提取信息管理 typedef struct { char filename[64]; uint32_t original_size; uint32_t extracted_size; uint32_t timestamp; uint32_t crc; bool compressed; bool verified; } extraction_info_t; // OTA升级状态结构体 typedef struct { TaskHandle_t task_handle; ota_status_callback_t status_callback; SemaphoreHandle_t mutex; //互斥信号量 bool is_running; int progress; bool cancel_requested; } ota_upgrade_context_t; esp_err_t http_json_download_flash(char* filename, const char* firmware_url); void trigger_ota_upgrade(char* url); void check_ota_progress(void); /** * @brief 初始化OTA升级模块 * @return esp_err_t ESP_OK表示成功 */ esp_err_t ota_upgrade_init(void); /** * @brief 设置OTA升级状态回调函数 * @param callback 回调函数指针 */ void ota_upgrade_set_callback(ota_status_callback_t callback); /** * @brief 开始OTA升级(非阻塞方式,在新任务中运行) * @param firmware_url 固件下载URL * @return esp_err_t ESP_OK表示任务创建成功 */ esp_err_t ota_upgrade_start(const char* firmware_url); /** * @brief 开始OTA升级(阻塞方式,在当前任务中运行) * @param firmware_url 固件下载URL * @return esp_err_t 升级结果 */ esp_err_t ota_upgrade_start_blocking(const char* firmware_url); /** * @brief 获取当前升级进度 * @return int 进度百分比(0-100),-1表示未在升级 */ int ota_upgrade_get_progress(void); /** * @brief 检查是否正在升级 * @return true 正在升级 * @return false 未在升级 */ bool ota_upgrade_is_running(void); /** * @brief 取消OTA升级 * @return esp_err_t ESP_OK表示取消成功 */ esp_err_t ota_upgrade_cancel(void); #endif // OTA_UPGRADE_H