1.修复ota上报状态的问题
authord15521469561 <1731678236@qq.com>
Mon, 8 Jun 2026 11:47:53 +0000 (19:47 +0800)
committerd15521469561 <1731678236@qq.com>
Mon, 8 Jun 2026 11:47:53 +0000 (19:47 +0800)
2.配网失败绑定失败的逻辑处理

13 files changed:
main/app_main.c
main/https_ota.c
main/https_ota.h
main/iap.c
main/iap.h
main/n455_ota.c
main/n455_ota.h
main/project_common.c
main/project_common.h
main/uart.c
main/user_http.c
main/user_mqtt.c
main/user_mqtt.h

index 05a8cd5..5f7e988 100644 (file)
@@ -85,6 +85,7 @@ static void onesec_timer_callback(void* arg)
     Https_Ota_Check_TimeOut();
     #endif
     
+    iap_cnt_timeout_cnt();
     #if ENABLE_HTTP_N455_OTA_FUNC
     //===========455  ota timeout=================//
     N455_Ota_Check_TimeOut();
index e74c3f5..ca3d58b 100644 (file)
@@ -28,18 +28,35 @@ static const char *HTTPS_OTA = "HTTPS_OTA";
 #endif
 #include "esp_crt_bundle.h"
 
-https_ota_status_t g_ota_status = HTTPS_OTA_STATUS_IDLE; // 全局OTA状态
+https_ota_status_t g_ota_status = HTTPS_OTA_STATUS_IDLE; // https全局OTA状态
 
-#define HTTPS_OTA_TIMEOUT_TIME  150//单位s
+#define HTTPS_OTA_TIMEOUT_TIME      150//单位s
 
-static volatile uint8_t ota_timeout_req = 0; //超时升级失败
-static volatile uint16_t ota_timeout_count = 0;
+#define HTTPS_OTA_MAX_RETRY_CNT     3 // 失败重试次数const uint8_t MAX_RETRY = 3;
+#define HTTPS_OTA_RETRY_TIMES       500//单位ms const int max_retry_times = 500;超时重试时间
 
-static volatile uint16_t ota_faile_count  = 0;
+// static volatile uint8_t ota_timeout_req = 0; //超时升级失败
+// static volatile uint16_t ota_timeout_count = 0;
 
-static uint8_t https_update_ota_flag = 0;
+// static volatile uint16_t ota_faile_count  = 0;
 
-static uint8_t https_ota_init = 0;
+// static uint8_t https_update_ota_flag = 0; //根据版本号比对 是否需要升级
+
+// static uint8_t https_ota_init = 0;
+
+
+
+
+typedef struct{
+    volatile uint8_t ota_timeout_req;//超时退出标识
+    volatile uint16_t ota_timeout_count; //超时计数器
+    volatile uint16_t ota_faile_count;//失败计数器
+    uint8_t https_update_ota_flag;//根据版本号是否需要升级
+    uint8_t https_ota_init;//ota升级task初始化
+    esp_https_ota_handle_t https_ota_handle; //ota句柄
+}__attribute__((packed)) https_ota_ctx_t;
+
+static https_ota_ctx_t s_ota_ctx = { 0 };
 #define HTTPS_OTA_RETRY_NUM  3
 
 #define OTA_URL_SIZE 128
@@ -93,7 +110,7 @@ static esp_err_t _http_client_init_cb(esp_http_client_handle_t http_client)
     // err = esp_http_client_set_header(http_client, "Custom-Header", "Value");
     return err;
 }
-esp_https_ota_handle_t https_ota_handle = NULL;
+
 #if 0
 /*
 func:OTA升级回调函数
@@ -279,26 +296,28 @@ ota_end:
 #else
 static void Https_Ota_Task_Cb(void *pvParameter)
 {
-    const int max_retry_times = 500;
+    
     
     Mqtt_Disconnect();  // 关闭MQTTs
     vTaskDelay(200 / portTICK_PERIOD_MS);
 
-    https_ota_handle=NULL;
+    s_ota_ctx.https_ota_handle=NULL;
     ESP_LOGI(HTTPS_OTA, "Starting Advanced OTA example:%s\n",https_ota_url);
     g_ota_status = HTTPS_OTA_STATUS_UPGRADING;
-    Https_Ota_Report_Status(g_ota_status);
+    Https_Ota_Report_Status(HTTPS_OTA_STATUS_UPGRADING);
 
     vTaskDelay(100 / portTICK_PERIOD_MS);
     esp_err_t ota_finish_err = ESP_OK;
     bool ota_success = false;
-    const uint8_t MAX_RETRY = 3;
-    ota_timeout_count = HTTPS_OTA_TIMEOUT_TIME;
+    
+    s_ota_ctx.ota_timeout_count = HTTPS_OTA_TIMEOUT_TIME;
     // for统一3次重试
-    for(uint8_t try_idx = 0; try_idx < MAX_RETRY; try_idx++)
+    uint8_t file_header_check = 0;
+    for(uint8_t try_idx = 0; try_idx < HTTPS_OTA_MAX_RETRY_CNT; try_idx++)
     {
+        file_header_check = 0;
         ota_success = false;
-        ESP_LOGI(HTTPS_OTA,"==== OTA try %d/%d ====",try_idx+1,MAX_RETRY);
+        ESP_LOGI(HTTPS_OTA,"==== OTA try %d/%d ====",try_idx+1,HTTPS_OTA_MAX_RETRY_CNT);
         esp_http_client_config_t config = {
             .url = https_ota_url,
             .timeout_ms = 5000,
@@ -313,34 +332,38 @@ static void Https_Ota_Task_Cb(void *pvParameter)
 
         esp_err_t err = ESP_FAIL;
         // 先释放上一轮句柄
-        if (https_ota_handle != NULL)
+        if (s_ota_ctx.https_ota_handle != NULL)
         {
-            esp_https_ota_abort(https_ota_handle);
-            https_ota_handle = NULL;
+            esp_https_ota_abort(s_ota_ctx.https_ota_handle);
+            s_ota_ctx.https_ota_handle = NULL;
         }
-        https_ota_handle = NULL;
-        err = esp_https_ota_begin(&ota_config, &https_ota_handle);
+        s_ota_ctx.https_ota_handle = NULL;
+        err = esp_https_ota_begin(&ota_config, &s_ota_ctx.https_ota_handle);
         if(err != ESP_OK)
         {
             ESP_LOGI(HTTPS_OTA, "esp_https_ota_begin failed, try:%d", try_idx+1);
-            vTaskDelay(pdMS_TO_TICKS(max_retry_times));
+            vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
             continue;
         }
 
         err = ESP_FAIL;
         esp_app_desc_t app_desc;
-        err = esp_https_ota_get_img_desc(https_ota_handle, &app_desc);
+        err = esp_https_ota_get_img_desc(s_ota_ctx.https_ota_handle, &app_desc);
         if (err != ESP_OK) {
             ESP_LOGI(HTTPS_OTA, "esp_https_ota_get_img_desc failed");
-            vTaskDelay(pdMS_TO_TICKS(max_retry_times));
-            continue;
+            vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
+            file_header_check=1;
+            ota_success = 0;
+            break;
         }
         err = ESP_FAIL;
         err = validate_image_header(&app_desc);
         if (err != ESP_OK) {
             ESP_LOGI(HTTPS_OTA, "image header verification failed");
-            vTaskDelay(pdMS_TO_TICKS(max_retry_times));
-            continue;
+            vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
+            file_header_check=1;
+            ota_success = 0;
+            break;
         }else{
             ESP_LOGI(HTTPS_OTA, "image header verification success");
         }
@@ -348,16 +371,21 @@ static void Https_Ota_Task_Cb(void *pvParameter)
         // 下载循环
         bool download_abort = false;
         while (1) {
-            if(ota_timeout_req){
+            if(s_ota_ctx.ota_timeout_req){
 
                 ESP_LOGI(HTTPS_OTA, "user timeout");
                 download_abort = true;
                 break;
             }
-            err = esp_https_ota_perform(https_ota_handle);
+            if(s_ota_ctx.https_ota_handle == NULL)
+            {
+                download_abort = true;
+                break;
+            }
+            err = esp_https_ota_perform(s_ota_ctx.https_ota_handle);
             if (err == ESP_ERR_HTTPS_OTA_IN_PROGRESS) {
-                ESP_LOGI(HTTPS_OTA, "Image bytes read: %d ota_timeout_req:%d", esp_https_ota_get_image_len_read(https_ota_handle),ota_timeout_req);
-                if(ota_timeout_req){
+                ESP_LOGI(HTTPS_OTA, "Image bytes read: %d ota_timeout_req:%d", esp_https_ota_get_image_len_read(s_ota_ctx.https_ota_handle),s_ota_ctx.ota_timeout_req);
+                if(s_ota_ctx.ota_timeout_req){
                     ESP_LOGI(HTTPS_OTA, "user stop!!!!timeout!!!!");
 
                     download_abort = true;
@@ -372,19 +400,19 @@ static void Https_Ota_Task_Cb(void *pvParameter)
         }
 
         // 数据没收全
-        if (esp_https_ota_is_complete_data_received(https_ota_handle) != true) {
+        if (esp_https_ota_is_complete_data_received(s_ota_ctx.https_ota_handle) != true) {
             ESP_LOGI(HTTPS_OTA, "Complete data was not received. continue ota %d",common_setting.wifi_status.wifi_state);
-            vTaskDelay(pdMS_TO_TICKS(max_retry_times));
+            vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
             continue;
         }
 
         // 执行finish
-        ota_finish_err = esp_https_ota_finish(https_ota_handle);
+        ota_finish_err = esp_https_ota_finish(s_ota_ctx.https_ota_handle);
         if ((err == ESP_OK) && (ota_finish_err == ESP_OK)) {
             ESP_LOGI(HTTPS_OTA, "ESP_HTTPS_OTA upgrade successful. Rebooting ...");
             g_ota_status = HTTPS_OTA_STATUS_SUCCESS;
-            ota_timeout_count=0;
-            https_ota_handle = NULL;
+            s_ota_ctx.ota_timeout_count=0;
+            s_ota_ctx.https_ota_handle = NULL;
             ota_success = true;
             break; //升级成功跳出for,不再重试
         } else {
@@ -392,23 +420,23 @@ static void Https_Ota_Task_Cb(void *pvParameter)
                 ESP_LOGI(HTTPS_OTA, "Image validation failed, image is corrupted");
             }
             ESP_LOGI(HTTPS_OTA, "ESP_HTTPS_OTA upgrade failed 0x%x", ota_finish_err);
-            vTaskDelay(pdMS_TO_TICKS(max_retry_times));
+            vTaskDelay(pdMS_TO_TICKS(HTTPS_OTA_RETRY_TIMES));
             continue;
         }
     }
 
+    //文件不对 不是本模组需要升级的bin文件
     // 成功分支
     if(ota_success)
     {
         #if (!ENABLE_HTTP_N455_OTA_FUNC)
-        uint8_t ret = Https_Ota_Report_Status(g_ota_status);
+        g_ota_status = HTTPS_OTA_STATUS_SUCCESS;
+        uint8_t ret = Https_Ota_Report_Status(HTTPS_OTA_STATUS_SUCCESS);
         Https_Ota_Clear_Upgrade_Flag();
-        if(!ret)
+        if(1)
         {
-            esp_err_t ret = esp_ota_mark_app_invalid_rollback();
-            if(common_setting.wifi_status.wifi_state == 1){
-                xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
-            }
+            ret = esp_ota_mark_app_invalid_rollback();
+            xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
         }
         #else
         if(N455_Ota_Get_Update_Flag())
@@ -436,19 +464,24 @@ static void Https_Ota_Task_Cb(void *pvParameter)
         vTaskDelete(NULL);
         return;
     }else{
-        if(https_ota_handle != NULL && g_ota_status != HTTPS_OTA_STATUS_SUCCESS){
-            esp_https_ota_abort(https_ota_handle);
-            https_ota_handle = NULL;
+        if(s_ota_ctx.https_ota_handle != NULL && g_ota_status != HTTPS_OTA_STATUS_SUCCESS){
+            esp_https_ota_abort(s_ota_ctx.https_ota_handle);
+            s_ota_ctx.https_ota_handle = NULL;
         }
-        ota_timeout_req=0;
+        s_ota_ctx.ota_timeout_req=0;
         ESP_LOGI(HTTPS_OTA, "ESP_HTTPS_OTA upgrade failed after 3 retry");
         g_ota_status = HTTPS_OTA_STATUS_FAILED;
-        ota_timeout_count=0;
-        Https_Ota_Report_Status(HTTPS_OTA_STATUS_FAILED);
-        Https_Ota_Clear_Upgrade_Flag();
-        if(common_setting.wifi_status.wifi_state == 1){
-            xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
+        s_ota_ctx.ota_timeout_count=0;
+        if(file_header_check)
+        {
+            file_header_check=0;
+            Https_Ota_Report_Status(HTTPS_OTA_STATUS_FILE_ERR);
+        }else{
+            Https_Ota_Report_Status(HTTPS_OTA_STATUS_FAILED);
         }
+       
+        Https_Ota_Clear_Upgrade_Flag();
+        xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
         vTaskDelete(NULL);
     }
 }
@@ -457,10 +490,10 @@ static void Https_Ota_Task_Cb(void *pvParameter)
 void Https_Ota_Clear_Upgrade_Flag(void)
 {
     g_ota_status = HTTPS_OTA_STATUS_IDLE;
-    ota_timeout_count = 0;
-    https_ota_init=0;
-    ota_timeout_req=0;
-    https_update_ota_flag = 0; //升级标识清除
+    s_ota_ctx.ota_timeout_count = 0;
+    s_ota_ctx.https_ota_init=0;
+    s_ota_ctx.ota_timeout_req=0;
+    s_ota_ctx.https_update_ota_flag = 0; //升级标识清除
     memset(https_ota_url,0,sizeof(https_ota_url));
 }
 
@@ -471,7 +504,7 @@ void Https_Ota_Clear_Upgrade_Flag(void)
 // url 升级地址
 uint8_t Https_Ota_Version_Check(char *version,char *url)
 {
-    https_update_ota_flag=0;
+    s_ota_ctx.https_update_ota_flag=0;
     if(version == NULL || url == NULL)
     {
         return 0;
@@ -481,7 +514,7 @@ uint8_t Https_Ota_Version_Check(char *version,char *url)
     ESP_LOGI(HTTPS_OTA, "ver: %s %04x FW_VERSION:%04x", version,new_version,FW_VERSION);
     if(new_version != FW_VERSION && new_version != 0xffff)
     {
-        https_update_ota_flag = 1;
+        s_ota_ctx.https_update_ota_flag = 1;
         strncpy(https_ota_url, url, OTA_URL_SIZE - 1);
         https_ota_url[OTA_URL_SIZE - 1] = '\0'; // 确保字符串以null结尾
         ESP_LOGI(HTTPS_OTA, "need upgrade\n");
@@ -496,13 +529,13 @@ uint8_t Https_Ota_Version_Check(char *version,char *url)
 uint8_t Https_Ota_Get_Update_Flag(void)
 {
 
-    return https_update_ota_flag;
+    return s_ota_ctx.https_update_ota_flag;
 
 }
 //
 void Https_Ota_Clear_Update_Flag(void)
 {
-    https_update_ota_flag=0;
+    s_ota_ctx.https_update_ota_flag=0;
 }
 
 
@@ -559,8 +592,8 @@ void Https_Ota_Stop(void)
 {
     if(g_ota_status != HTTPS_OTA_STATUS_SUCCESS && g_ota_status > HTTPS_OTA_STATUS_IDLE)
     {
-        if(!ota_timeout_req){
-            ota_timeout_req = 1;
+        if(!s_ota_ctx.ota_timeout_req){
+            s_ota_ctx.ota_timeout_req = 1;
         }
     }
 
@@ -574,24 +607,24 @@ void Https_Ota_Stop(void)
 
 void Https_Ota_Task(void)
 {
-    if(https_ota_init)
+    if(s_ota_ctx.https_ota_init)
     {
         return;
     }
-    ota_timeout_req = 0;
-    ota_timeout_count = 0;
-    https_ota_init=1;
+    s_ota_ctx.ota_timeout_req = 0;
+    s_ota_ctx.ota_timeout_count = 0;
+    s_ota_ctx.https_ota_init=1;
     g_ota_status = HTTPS_OTA_STATUS_UPGRADING;
     BaseType_t ret = xTaskCreate(Https_Ota_Task_Cb, "https_ota_task", HTTPS_OTA_STACK_SIZE, NULL, 4, NULL);
     if(ret !=pdPASS )
     {
-        https_ota_init=0;
+        s_ota_ctx.https_ota_init=0;
         ESP_LOGI(HTTPS_OTA, "create https ota task failed\n");
         Https_Ota_Clear_Upgrade_Flag();
         g_ota_status = HTTPS_OTA_STATUS_IDLE;
     }else{
 
-        https_ota_init=1;
+        s_ota_ctx.https_ota_init=1;
         g_ota_status = HTTPS_OTA_STATUS_UPGRADING;
         ESP_LOGI(HTTPS_OTA, "task success\n");
 
@@ -604,33 +637,33 @@ void Https_Ota_Check_TimeOut(void)
 {
     if(g_ota_status == HTTPS_OTA_STATUS_UPGRADING) //一直没有成功
     {
-        if(ota_timeout_count > 0)
+        if(s_ota_ctx.ota_timeout_count > 0)
         {
-            ota_timeout_count--;
-            ESP_LOGI(HTTPS_OTA, "OTA ota_timeout_count: %d", ota_timeout_count);
-            if(ota_timeout_count== 0)
+            s_ota_ctx.ota_timeout_count--;
+            ESP_LOGI(HTTPS_OTA, "OTA ota_timeout_count: %d", s_ota_ctx.ota_timeout_count);
+            if(s_ota_ctx.ota_timeout_count== 0)
             {
-                ota_timeout_req=1;
+                s_ota_ctx.ota_timeout_req=1;
             }
         }
     }else{
-        ota_timeout_count=0;
+        s_ota_ctx.ota_timeout_count=0;
     }
-    if(ota_faile_count > 0)
+    if(s_ota_ctx.ota_faile_count > 0)
     {
-        ota_faile_count--;
-        if(ota_faile_count == 0)
+        s_ota_ctx.ota_faile_count--;
+        if(s_ota_ctx.ota_faile_count == 0)
         {
             Https_Ota_Start();
 
         }
     }
-
 }
 /*
  *函数功能:OTA事件回调
  *无返回值
  */
+
 static void ota_event_handler(void* arg, esp_event_base_t event_base,
                                 int32_t event_id, void* event_data)
 {
@@ -767,18 +800,18 @@ uint8_t Https_Ota_Report_Status(uint8_t ota_status)
             continue;
         }      
         cJSON_AddStringToObject(pRoot, "serialNumber", (const char*)common_storage.net_info.serial_number);
-        #if (!ENABLE_HTTP_N455_OTA_FUNC)
+        #if (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 0)
         if(ota_status == HTTPS_OTA_STATUS_SUCCESS)
-        #else
+        #elif (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 1)
         if(ota_status == HTTPS_OTA_STATUS_SUCCESS || ota_status == N455_OTA_STATUS_SUCCESS)
         #endif
         {
             cJSON_AddNumberToObject(pRoot,"upgradeStatus", 3);
             cJSON_AddStringToObject(pRoot, "detailMsg", "升级成功");
         }
-        #if (!ENABLE_HTTP_N455_OTA_FUNC)
+        #if  (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 0)
         else if(ota_status == HTTPS_OTA_STATUS_UPGRADING)
-        #else
+        #elif (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 1)
         else if(ota_status == HTTPS_OTA_STATUS_UPGRADING || ota_status == N455_OTA_STATUS_UPGRADING)
         #endif
         {
@@ -786,19 +819,38 @@ uint8_t Https_Ota_Report_Status(uint8_t ota_status)
             cJSON_AddNumberToObject(pRoot,"upgradeStatus", 5);
         }
         else{
-            cJSON_AddStringToObject(pRoot, "detailMsg", "升级失败");
-            cJSON_AddNumberToObject(pRoot,"upgradeStatus", 4);
+            #if  (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 0)
+            if(ota_status == HTTPS_OTA_STATUS_FILE_ERR)
+            #elif (ENABLE_HTTPS_OTA_FUNC == 1 && ENABLE_HTTP_N455_OTA_FUNC == 1)
+            if (ota_status == N455_OTA_STATUS_FILE_ERR || ota_status == HTTPS_OTA_STATUS_FILE_ERR)
+            #endif
+            {   
+                cJSON_AddStringToObject(pRoot, "detailMsg", "升级文件不对");
+                cJSON_AddNumberToObject(pRoot,"upgradeStatus", 4);
+            }
+            else{
+                cJSON_AddStringToObject(pRoot, "detailMsg", "升级失败");
+                cJSON_AddNumberToObject(pRoot,"upgradeStatus", 4);
+            }
+            
         }
 
 
         char *post_data = cJSON_Print(pRoot);
-        if(strlen(post_data) == 0 && post_data == NULL)//数据为空说明cJSON构建失败
+        if(post_data == NULL)
         {
             cJSON_Delete(pRoot);
             vTaskDelay(50);
+            continue;
+        }
+        if(strlen(post_data) <= 0)//数据为空说明cJSON构建失败
+        {
+            cJSON_Delete(pRoot);
+            pRoot = NULL;
+            vTaskDelay(50);
             continue; 
         }
-        ESP_LOGI(HTTPS_OTA, "http_config.url = %s", http_config.url);
+        ESP_LOGI(HTTPS_OTA, "http_config.url = %s post_data=%s", http_config.url,post_data);
         
         http_client = esp_http_client_init(&http_config);
         if(http_client == NULL)
@@ -807,6 +859,7 @@ uint8_t Https_Ota_Report_Status(uint8_t ota_status)
             ESP_LOGI(HTTPS_OTA, "http_config.url = %s", http_config.url);
             cJSON_free(post_data);
             cJSON_Delete(pRoot);
+            pRoot = NULL;
             vTaskDelay(50);
             continue;
         }
@@ -816,26 +869,10 @@ uint8_t Https_Ota_Report_Status(uint8_t ota_status)
         esp_http_client_set_header(http_client, "Content-Type", "application/json");
 
         esp_http_client_set_post_field(http_client, post_data, strlen(post_data));
-        esp_err_t err = esp_http_client_open(http_client, 0);
+        // esp_err_t err = esp_http_client_open(http_client, 0);
+        esp_err_t err = esp_http_client_perform(http_client);
         if (err == ESP_OK)
         {
-            
-            int header_ret = esp_http_client_fetch_headers(http_client);//获取HTTP响应头,返回值为响应头的总长度,如果返回值小于0表示获取响应头失败
-            if(header_ret < 0)
-            {
-                ESP_LOGE(HTTPS_OTA, "Failed to fetch headers");
-                result =  0;
-                cJSON_free(post_data);
-                cJSON_Delete(pRoot);
-                if(http_client != NULL)
-                {   
-                    esp_http_client_close(http_client);
-                    esp_http_client_cleanup(http_client);
-                    http_client=NULL;
-                }
-                vTaskDelay(50);
-                continue;
-            }
             int status_code = esp_http_client_get_status_code(http_client);
             int read_len = esp_http_client_get_content_length(http_client);
             if(status_code == 200)
@@ -855,6 +892,7 @@ uint8_t Https_Ota_Report_Status(uint8_t ota_status)
         }
         cJSON_free(post_data);
         cJSON_Delete(pRoot);
+        pRoot = NULL;
         if(http_client != NULL)
         {   
             esp_http_client_close(http_client);
index 346e9c4..f07ddb0 100644 (file)
@@ -11,6 +11,7 @@ typedef enum {
     HTTPS_OTA_STATUS_UPGRADING = 1,   // 升级中
     HTTPS_OTA_STATUS_SUCCESS = 2,     // 升级成功
     HTTPS_OTA_STATUS_FAILED = 3,      // 升级失败
+    HTTPS_OTA_STATUS_FILE_ERR = 4,        //升级文件不对
 } https_ota_status_t;
 
 extern https_ota_status_t g_ota_status;
index a26cf3a..4652463 100644 (file)
@@ -1,6 +1,7 @@
 #include "project_common.h"
 #include "n455_ota.h"
 #include "https_ota.h"
+#include "user_mqtt.h"
 static const char *IAP_TAG = "IAP";
 
 static const esp_partition_t *update_partition = NULL;
@@ -167,6 +168,39 @@ uint8_t Iap_Write_Cache(uint32_t addr, uint16_t cache_size)
        return REPLY_STATUS_OK;
 }
 
+static uint8_t iap_update_cnt = 0;
+
+static uint8_t flag = 0;
+//预留
+void iap_cnt_timeout_cnt(void)
+{
+       
+       if(iap_update_cnt > 0)
+       {
+               //在升级中mqtt需要断开
+               if(!flag && get_mqtt_init_flag())
+               {
+                       flag = 1;
+                       xEventGroupSetBits(common_event_group, COMMON_MQTT_INIT_BIT);
+               }
+               
+               iap_update_cnt--;
+               if(iap_update_cnt == 0)
+               {
+                       flag=0;
+                       ESP_LOGI("IAP","START MQTT");
+                       xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
+               }
+       }
+       else{
+               if(flag)
+               {
+                       flag=0;
+                       
+               }
+       }
+}
+
 void Iap_Process(uint8_t* data, uint8_t len)
 {
        #if (ENABLE_HTTP_N455_OTA_FUNC && ENABLE_HTTPS_OTA_FUNC)
@@ -188,9 +222,11 @@ void Iap_Process(uint8_t* data, uint8_t len)
        {
                return;
        }
+       iap_update_cnt = 10;
        // ESP_LOGI(IAP_TAG, "Iap_Process:%d %d\n", iap_cmd.cmd.cmd2,iap_auth);
        if(iap_cmd.cmd.cmd2 == CMD2_IAP_FETCH || iap_cmd.cmd.cmd2 == CMD2_IAP_CERTIFICATE || iap_cmd.cmd.cmd2 == CMD2_IAP_END || iap_auth == 1)
        {
+
                // 分析命令码
                switch(iap_cmd.cmd.cmd2)
                {
index 3180e16..e75666e 100644 (file)
@@ -201,5 +201,5 @@ extern int32_t iap_end_cnt;
 
 
 void Iap_Process(uint8_t* data, uint8_t len);
-
+void iap_cnt_timeout_cnt(void);
 #endif
index 79858a2..7097e69 100644 (file)
@@ -28,7 +28,7 @@ static const char *N455_OTA = "455_OTA";
 #endif
 #include "esp_crt_bundle.h"
 
-N455_ota_status_t n455_g_ota_status = N455_OTA_STATUS_IDLE; // 
+N455_ota_status_t n455_g_ota_status = N455_OTA_STATUS_IDLE; // 455升级状态
 #define OTA_URL_MAX_LEN 128
 #define OAT_CONNECT_RETRY_NUM  3 //重连服务器次数 3
 #define OTA_TOTAL_TIMEOUT_S 60 //总超时时间 单位s
@@ -39,15 +39,27 @@ static char n455_ota_url[OTA_URL_MAX_LEN] = "https://pxn-iot.s3-accelerate.amazo
 // BIN 文件头
 #define FW_HEADER_SIZE          256     // 前256字节是头,需要跳过
 
-// 全局升级应答
-static volatile bool  uart_ack_received = false; // UART应答的 1 标识uart收到回复 
-static volatile uint8_t uart_ack = 0xFF; //UART应答的 1 成功 2 失败 0xFF未收到应答 0表示收到但失败
+// // 全局升级应答
+// static volatile bool  uart_ack_received = false; // UART应答的 1 标识uart收到回复 
+// static volatile uint8_t uart_ack = 0xFF; //UART应答的 1 成功 2 失败 0xFF未收到应答 0表示收到但失败
 
-//task 标识
-static  uint8_t n455_ota_init = 0;
-static volatile uint8_t n455_ota_stop = 0;
-static volatile uint16_t n455_ota_timeout = 0;
-static uint8_t n455_ota_update_flag = 0; //ota是否需要升级
+// //task 标识
+// static  uint8_t n455_ota_init = 0;
+// static volatile uint8_t n455_ota_stop_flag = 0;
+// static volatile uint16_t n455_ota_timeout_cnt = 0;
+// static uint8_t n455_ota_update_flag = 0; //ota是否需要升级 根据版本号比对
+
+typedef struct{
+    volatile bool  uart_ack_received; //收到回复串口的应答 1收到应答
+    volatile uint8_t uart_ack;//应答标识 0xff未收到回复 1成功 2 失败
+    uint8_t n455_ota_init; //ota初始化
+    volatile uint8_t n455_ota_stop_flag;//ota超时停止标识
+    volatile uint16_t n455_ota_timeout_cnt; //ota超时倒计时
+    uint8_t n455_ota_update_flag; //根据版本是否需要Ota
+
+
+}__attribute__((packed)) n455_ota_ctx_t;
+static n455_ota_ctx_t n455_ota_ctx = {0};
 /*
  *函数功能:接收升级应答
  *cmd:命令类型
@@ -57,8 +69,8 @@ void N455_Ota_Capture_Ack(uint8_t cmd, uint8_t ack)
 {
     if(cmd == CMD1_N455)
     {
-        uart_ack = ack;
-        uart_ack_received = true;
+        n455_ota_ctx.uart_ack = ack;
+        n455_ota_ctx.uart_ack_received = true;
     }
 }
 
@@ -69,8 +81,8 @@ void N455_Ota_Capture_Ack(uint8_t cmd, uint8_t ack)
 */ 
 static bool ota_wait_ack(uint8_t expect_cmd2)
 {
-    uart_ack_received = false;
-    uart_ack = 0xFF;
+    n455_ota_ctx.uart_ack_received = false;
+    n455_ota_ctx.uart_ack = 0xFF;
 
     uint32_t start = xTaskGetTickCount();
     uint32_t ack_outime = OTA_TIMEOUT_MS;
@@ -83,23 +95,23 @@ static bool ota_wait_ack(uint8_t expect_cmd2)
     }
     while((xTaskGetTickCount() - start) < ack_outime)
     {
-        if(n455_ota_stop)
+        if(n455_ota_ctx.n455_ota_stop_flag)
         {
-            uart_ack_received=false;
+            n455_ota_ctx.uart_ack_received=false;
             break;
         }
-        if(uart_ack_received)
+        if(n455_ota_ctx.uart_ack_received)
         {
            break;
         }
         vTaskDelay(1 / portTICK_PERIOD_MS);
     }
-    if(uart_ack_received == false)
+    if(n455_ota_ctx.uart_ack_received == false)
     {
         ESP_LOGW(N455_OTA, "TIMEOUT %02X", expect_cmd2);
         return false;
     }
-    if(uart_ack == 1) // 1表示成功
+    if(n455_ota_ctx.uart_ack == 1) // 1表示成功
     {
         return true;
     }
@@ -186,7 +198,7 @@ static bool ota_send_with_retry(uint8_t cmd2, uint8_t *data, uint8_t len)
         ok = ota_wait_ack(cmd2);//等待结果回复
         if(!ok)
         {
-            if(n455_ota_stop)
+            if(n455_ota_ctx.n455_ota_stop_flag)
             {
                 retry=MAX_RETRY_TIMES; // 停止升级,退出重试
                 return false;
@@ -249,7 +261,7 @@ void N455_Ota_Task_Cb(void *param)
         .keep_alive_enable = true,
     };
     esp_http_client_handle_t client = NULL;
-    n455_ota_timeout = OTA_TOTAL_TIMEOUT_S;
+    n455_ota_ctx.n455_ota_timeout_cnt = OTA_TOTAL_TIMEOUT_S;
     n455_g_ota_status = N455_OTA_STATUS_UPGRADING;
 
     uint8_t buf[MAX_PACKET_CONTENT];
@@ -264,10 +276,10 @@ void N455_Ota_Task_Cb(void *param)
    
     for (size_t i = 0; i < OAT_CONNECT_RETRY_NUM; i++)
     {
-        if(n455_ota_stop)
+        if(n455_ota_ctx.n455_ota_stop_flag)
         {
             ESP_LOGI(N455_OTA, "ota stopped");
-            n455_ota_stop=0;
+            n455_ota_ctx.n455_ota_stop_flag=0;
             n455_g_ota_status = N455_OTA_STATUS_FAILED;
             goto ota_end;
             return;
@@ -381,10 +393,10 @@ void N455_Ota_Task_Cb(void *param)
             offset_size=0;
             while (1)
             {
-                if(n455_ota_stop)
+                if(n455_ota_ctx.n455_ota_stop_flag)
                 {
                     ESP_LOGI(N455_OTA, "ota stopped");
-                    n455_ota_stop = 0;
+                    n455_ota_ctx.n455_ota_stop_flag = 0;
                     n455_g_ota_status = N455_OTA_STATUS_FAILED;
                     goto ota_end;
                     return;
@@ -430,10 +442,10 @@ void N455_Ota_Task_Cb(void *param)
                 goto ota_end;
                 return;
             }
-            if(n455_ota_stop)
+            if(n455_ota_ctx.n455_ota_stop_flag)
             {
                 ESP_LOGI(N455_OTA, "ota stopped");
-                n455_ota_stop = 0;
+                n455_ota_ctx.n455_ota_stop_flag = 0;
                 n455_g_ota_status = N455_OTA_STATUS_FAILED;
                 goto ota_end;
                 return;
@@ -451,10 +463,10 @@ void N455_Ota_Task_Cb(void *param)
             uint8_t crc_data[8] = {0};
             memcpy(crc_data, &real_write_len, 4); // 前4字节是固件大小
             memcpy(crc_data+4, &crc_value, 4); // 接着4字节是CRC32
-            if(n455_ota_stop)
+            if(n455_ota_ctx.n455_ota_stop_flag)
             {
                 ESP_LOGI(N455_OTA, "ota stopped");
-                n455_ota_stop = 0;
+                n455_ota_ctx.n455_ota_stop_flag = 0;
                 n455_g_ota_status = N455_OTA_STATUS_FAILED;
                 goto ota_end;
                 return;
@@ -466,10 +478,10 @@ void N455_Ota_Task_Cb(void *param)
                 goto ota_end;
                 return;
             }
-            if(n455_ota_stop)
+            if(n455_ota_ctx.n455_ota_stop_flag)
             {
                 ESP_LOGI(N455_OTA, "ota stopped");
-                n455_ota_stop = 0;
+                n455_ota_ctx.n455_ota_stop_flag = 0;
                 n455_g_ota_status = N455_OTA_STATUS_FAILED;
                 goto ota_end;
                 return;
@@ -481,10 +493,10 @@ void N455_Ota_Task_Cb(void *param)
                 goto ota_end;
                 return;
             }
-            if(n455_ota_stop)
+            if(n455_ota_ctx.n455_ota_stop_flag)
             {
                 ESP_LOGI(N455_OTA, "ota stop");
-                n455_ota_stop = 0;
+                n455_ota_ctx.n455_ota_stop_flag = 0;
                 n455_g_ota_status = N455_OTA_STATUS_FAILED;
                 goto ota_end;
                 return;
@@ -497,10 +509,10 @@ void N455_Ota_Task_Cb(void *param)
                 goto ota_end;
                 return;
             }
-            if(n455_ota_stop)
+            if(n455_ota_ctx.n455_ota_stop_flag)
             {
                 ESP_LOGI(N455_OTA, "ota stop");
-                n455_ota_stop = 0;
+                n455_ota_ctx.n455_ota_stop_flag = 0;
                 n455_g_ota_status = N455_OTA_STATUS_FAILED;
                 
                 goto ota_end;
@@ -515,7 +527,7 @@ void N455_Ota_Task_Cb(void *param)
             Wifi_Stop();
             Https_Ota_Clear_Upgrade_Flag(); // 清除32升级标识
             
-            n455_ota_init=0;
+            n455_ota_ctx.n455_ota_init=0;
             vTaskDelay(10 / portTICK_PERIOD_MS);
             esp_restart();
             vTaskDelete(NULL);
@@ -533,9 +545,9 @@ ota_end:
         esp_http_client_cleanup(client);
         client = NULL;
     }
-    n455_ota_init=0;
+    n455_ota_ctx.n455_ota_init=0;
     n455_g_ota_status = N455_OTA_STATUS_FAILED;
-    n455_ota_stop = 0;
+    n455_ota_ctx.n455_ota_stop_flag = 0;
     //如果32ota成功了 455升级失败了 清除32的升级标识 回滚固件
     if(Https_Get_Ota_Success() == HTTPS_OTA_STATUS_SUCCESS)
     {
@@ -577,11 +589,11 @@ void N455_Ota_Task_Cb(void *param)
         Mqtt_Disconnect();  // 关闭MQTTs
         vTaskDelay(200 / portTICK_PERIOD_MS);
         n455_g_ota_status = N455_OTA_STATUS_UPGRADING;
-        Https_Ota_Report_Status(g_ota_status);
+        Https_Ota_Report_Status(N455_OTA_STATUS_UPGRADING);
     }
     ///////////////////////
     vTaskDelay(50 / portTICK_PERIOD_MS);
-    const int MAX_OTA_RETRY = 3;  // 整个OTA只试3次
+    
 
     uint8_t buf[MAX_PACKET_CONTENT];
     uint32_t real_write_len = 0;
@@ -592,17 +604,19 @@ void N455_Ota_Task_Cb(void *param)
     int wlen = 0;
     esp_http_client_handle_t client = NULL;
     bool failed = false;
-    n455_ota_timeout = OTA_TOTAL_TIMEOUT_S;
+    n455_ota_ctx.n455_ota_timeout_cnt = OTA_TOTAL_TIMEOUT_S;
     uint8_t write_data[sizeof(FW_FILE_HEADER_T)] = {0};
+    uint8_t file_header_check = 0;
     // ======================
     // 只循环3次 → 每次都完整重新下载(正确)
     // ======================
-    for (int retry = 0; retry < MAX_OTA_RETRY; retry++)
+    for (int retry = 0; retry < OAT_CONNECT_RETRY_NUM; retry++)
     {
-        ESP_LOGI(N455_OTA, "===== OTA 第 %d/%d 次 =====", retry+1, MAX_OTA_RETRY);
-        if(n455_ota_stop)
+        file_header_check = 0;
+        ESP_LOGI(N455_OTA, "===== OTA 第 %d/%d 次 =====", retry+1, OAT_CONNECT_RETRY_NUM);
+        if(n455_ota_ctx.n455_ota_stop_flag)
         {
-            n455_ota_stop = 0;
+            n455_ota_ctx.n455_ota_stop_flag = 0;
             if (client) {
                 esp_http_client_close(client);
                 esp_http_client_cleanup(client);
@@ -628,9 +642,9 @@ void N455_Ota_Task_Cb(void *param)
         failed = false;
 
 
-        n455_ota_stop = 0;
-        uart_ack_received = false;
-        uart_ack = 0xFF;
+        n455_ota_ctx.n455_ota_stop_flag = 0;
+        n455_ota_ctx.uart_ack_received = false;
+        n455_ota_ctx.uart_ack = 0xFF;
         n455_g_ota_status = N455_OTA_STATUS_UPGRADING;
 
         // --------------------
@@ -672,6 +686,13 @@ void N455_Ota_Task_Cb(void *param)
             {
                 ESP_LOGE(N455_OTA, "Header check fail");
                 failed = true;
+                file_header_check = 1;
+                if (client) {
+                    esp_http_client_close(client);
+                    esp_http_client_cleanup(client);
+                    client = NULL;
+                }
+                break;
             }
         }
         memset(write_data,0,sizeof(write_data));
@@ -683,7 +704,7 @@ void N455_Ota_Task_Cb(void *param)
 
         if (!failed) {
             while (1) {
-                if (n455_ota_stop) { failed = true; break; }
+                if (n455_ota_ctx.n455_ota_stop_flag) { failed = true; break; }
                 int r = esp_http_client_read(client, (char *)buf, MAX_PACKET_CONTENT);
                 if (r <= 0) break;
 
@@ -708,9 +729,9 @@ void N455_Ota_Task_Cb(void *param)
             }
             ESP_LOGE(N455_OTA, "WRITE END:%d",real_write_len);
             //超时退出不再继续ota升级
-            if(n455_ota_stop)
+            if(n455_ota_ctx.n455_ota_stop_flag)
             {
-                n455_ota_stop = 0;
+                n455_ota_ctx.n455_ota_stop_flag = 0;
                 if (client) {
                     esp_http_client_close(client);
                     esp_http_client_cleanup(client);
@@ -765,7 +786,7 @@ void N455_Ota_Task_Cb(void *param)
         if (!failed) {
             
             n455_g_ota_status = N455_OTA_STATUS_SUCCESS;
-            Https_Ota_Report_Status(n455_g_ota_status);
+            Https_Ota_Report_Status(N455_OTA_STATUS_SUCCESS);
             Wifi_Stop();
             vTaskDelay(10 / portTICK_PERIOD_MS);
             Https_Ota_Clear_Upgrade_Flag();
@@ -778,10 +799,11 @@ void N455_Ota_Task_Cb(void *param)
 
         // 失败,等3秒进入下一次重试
         ESP_LOGI(N455_OTA, "重试");
-        vTaskDelay(pdMS_TO_TICKS(1000));
+        vTaskDelay(pdMS_TO_TICKS(500));
     }
-    n455_ota_timeout=0;
-    n455_ota_stop=0;
+
+    n455_ota_ctx.n455_ota_timeout_cnt=0;
+    n455_ota_ctx.n455_ota_stop_flag=0;
     // 3次全部失败
     ESP_LOGI(N455_OTA, "全部失败");
     n455_g_ota_status = N455_OTA_STATUS_FAILED;
@@ -791,7 +813,14 @@ void N455_Ota_Task_Cb(void *param)
     }
 
     Https_Ota_Clear_Upgrade_Flag();
-    Https_Ota_Report_Status(N455_OTA_STATUS_FAILED);
+    if(file_header_check)
+    {
+        file_header_check=0;
+        Https_Ota_Report_Status(N455_OTA_STATUS_FILE_ERR);
+    }else{
+        Https_Ota_Report_Status(N455_OTA_STATUS_FAILED);
+    }
+    
     N455_Ota_Clear_Upgrade_Flag();
     xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
     vTaskDelete(NULL);
@@ -809,17 +838,17 @@ void N455_Ota_Check_TimeOut(void)
 {
     if(n455_g_ota_status == N455_OTA_STATUS_UPGRADING && n455_g_ota_status > 0) //一直没有成功
     {
-        if(n455_ota_timeout > 0)
+        if(n455_ota_ctx.n455_ota_timeout_cnt > 0)
         {
-            n455_ota_timeout--;
-            if(n455_ota_timeout == 0)
+            n455_ota_ctx.n455_ota_timeout_cnt--;
+            if(n455_ota_ctx.n455_ota_timeout_cnt == 0)
             {
                 ESP_LOGI(N455_OTA, "ota timeout");
-                n455_ota_stop = 1;
+                n455_ota_ctx.n455_ota_stop_flag = 1;
             }
         }
     }else{
-        n455_ota_timeout=0;
+        n455_ota_ctx.n455_ota_timeout_cnt=0;
     }   
 }
 
@@ -830,7 +859,7 @@ void N455_Ota_Check_TimeOut(void)
 */ 
 uint8_t N455_Ota_Version_Check(char *version,char *url)
 {
-    n455_ota_update_flag=0;
+    n455_ota_ctx.n455_ota_update_flag=0;
     if(version == NULL || url == NULL)
     {
         return 0;
@@ -840,7 +869,7 @@ uint8_t N455_Ota_Version_Check(char *version,char *url)
     ESP_LOGI(N455_OTA, "ver: %s %04x FW_VERSION:%04x", version,new_version,FW_VERSION);
     if(new_version != FW_VERSION && new_version != 0xffff)
     {
-        n455_ota_update_flag = 1;
+        n455_ota_ctx.n455_ota_update_flag = 1;
         strncpy(n455_ota_url, url, OTA_URL_MAX_LEN - 1);
         n455_ota_url[OTA_URL_MAX_LEN - 1] = '\0'; // 确保字符串以null结尾
         ESP_LOGI(N455_OTA, "need upgrade\n");
@@ -856,12 +885,12 @@ uint8_t N455_Ota_Version_Check(char *version,char *url)
 */
 uint8_t N455_Ota_Get_Update_Flag(void)
 {
-    return n455_ota_update_flag;
+    return n455_ota_ctx.n455_ota_update_flag;
 }
 
 void N455_Ota_Clear_Update_Flag(void)
 {
-    n455_ota_update_flag=0;
+    n455_ota_ctx.n455_ota_update_flag=0;
 }
 /*
  *函数功能: 455升级失败清空32升级标识
@@ -869,10 +898,10 @@ void N455_Ota_Clear_Update_Flag(void)
 void N455_Ota_Clear_Upgrade_Flag(void)
 {
     n455_g_ota_status = N455_OTA_STATUS_IDLE; // 状态重置为空闲
-    n455_ota_init=0; //升级初始化标志清零
-    n455_ota_timeout =0; //升级超时标志清零
-    n455_ota_stop = 0;//升级停止标志清零
-    n455_ota_update_flag=0; //升级标识清除
+    n455_ota_ctx.n455_ota_init=0; //升级初始化标志清零
+    n455_ota_ctx.n455_ota_timeout_cnt =0; //升级超时标志清零
+    n455_ota_ctx.n455_ota_stop_flag = 0;//升级停止标志清零
+    n455_ota_ctx.n455_ota_update_flag=0; //升级标识清除
     memset(n455_ota_url,0,sizeof(n455_ota_url));
 }
 /**
@@ -885,20 +914,20 @@ uint8_t N455_Get_Ota_Status(void)
 }
 uint8_t N455_Ota_Task(void)
 {
-    if(n455_ota_init)
+    if(n455_ota_ctx.n455_ota_init)
     {
         ESP_LOGI(N455_OTA, "N455 OTA running\n");
         return 2;
     }
-    n455_ota_init=1; //升级初始化标志清零
-    n455_ota_timeout =0; //升级超时标志清零
-    n455_ota_stop = 0;//升级停止标志清零
+    n455_ota_ctx.n455_ota_init=1; //升级初始化标志清零
+    n455_ota_ctx.n455_ota_timeout_cnt =0; //升级超时标志清零
+    n455_ota_ctx.n455_ota_stop_flag = 0;//升级停止标志清零
     n455_g_ota_status = N455_OTA_STATUS_UPGRADING;
     BaseType_t ret = xTaskCreate(N455_Ota_Task_Cb, "n455_ota_task", N455_OTA_STACK_SIZE, NULL, 6, NULL);
     if(ret !=pdPASS )
     {
         ESP_LOGI(N455_OTA, "task err\n");
-        n455_ota_init=0;
+        n455_ota_ctx.n455_ota_init=0;
         n455_g_ota_status = N455_OTA_STATUS_IDLE;
         N455_Ota_Clear_Upgrade_Flag();
         return 1;
index 743f3ef..3f09dd8 100644 (file)
@@ -1,6 +1,6 @@
 #ifndef _HTTP_N455_OTA_H_
 #define _HTTP_N455_OTA_H_
-#define ENABLE_HTTP_N455_OTA_FUNC          0 //是否开启HTTPS_OTA升级
+#define ENABLE_HTTP_N455_OTA_FUNC          1 //是否开启HTTPS_OTA升级
 #if ENABLE_HTTP_N455_OTA_FUNC
 #include <stdint.h>
 #include <stdbool.h>
@@ -99,6 +99,7 @@ typedef enum {
     N455_OTA_STATUS_UPGRADING = 1,   // 升级中
     N455_OTA_STATUS_SUCCESS = 2,     // 升级成功
     N455_OTA_STATUS_FAILED = 3,      // 升级失败
+       N455_OTA_STATUS_FILE_ERR = 4,           //升级文件不对
 } N455_ota_status_t;
 
 extern N455_ota_status_t n455_g_ota_status;
index c182eab..6ca406d 100644 (file)
@@ -1435,3 +1435,26 @@ void Esp32_Factory_Reset(void)
         ESP_LOGI(COMMON_TAG,"NO NETWOR INIFOR");
     }
 }
+
+
+void Esp32_Bind_Fail_Reset(void)
+{
+
+    //断开wifi
+    Wifi_Stop();
+    vTaskDelay(50 / portTICK_PERIOD_MS);
+    //断开mqtt释放资源
+    Mqtt_Disconnect();
+    vTaskDelay(50 / portTICK_PERIOD_MS);
+    //擦除flash
+    User_Erase_NotReset();
+    common_setting.net_pairing_info.http_url_flag=0;
+    common_setting.net_pairing_info.http_token_flag = 0;
+    common_setting.net_pairing_info.user_id_flag = 0;
+
+    common_setting.ble_status.nimble_flag=0; //配网成功 开机置1
+    common_setting.mqtt_user_cfg.mqtt_start_flag=0; //开始连接mqtt
+    common_setting.ble_status.is_bind = 0; //绑定失败
+    xEventGroupSetBits(ble_event_group, BLE_ON_BIT);
+    ESP_LOGI(COMMON_TAG,"open ble");
+}
\ No newline at end of file
index 7835ea7..1d9a018 100644 (file)
@@ -342,11 +342,7 @@ typedef struct
 //617086929577717760
 typedef struct
 {
-    
-    uint8_t mqtt_connect_flag;//0 无连接 1已连接
-    uint8_t mqtt_sub_flag; //1订阅成功 0订阅失败
     uint8_t mqtt_start_flag;//已经配网成功了开始连接mqtt表示
-    
 }__attribute__((packed)) MQTT_CONFIG_INFO_T;
 
 
@@ -515,6 +511,7 @@ int parse_utc_offset_min(const char *zone_str);
 int8_t parse_utc_offset_2(const char *zone_str);
 int8_t Get_Time_Zone(void);
 int64_t Get_Time_Stamp(void);
+void Esp32_Bind_Fail_Reset(void);
 #if ENABLE_SYNC_SNTP_TIME
 void wait_for_sntp_sync(void);
 #endif
index 2ab7923..44b45e1 100644 (file)
@@ -355,7 +355,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                 {
                     common_setting.mcu_info.vid = *(uint16_t*)&data[3];
                     #if 1
-                    common_setting.mcu_info.pid = 0xA00D;//S
+                    common_setting.mcu_info.pid = 0xA00C;//S
                     #else
                     common_setting.mcu_info.pid = *(uint16_t*)&data[5];
                     #endif
@@ -392,7 +392,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                             uart_pairing_reply.content[0]=0x00;
                         }
                     }
-                    
+                      
                 }else{
                     ESP_LOGI(UART_RECV, "mcu len %d",data[2]);
                     uart_pairing_reply.content[0]=0x00;
@@ -580,11 +580,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
             // 固件升级
             case COMMON_UART_ESP_FIRMWARE_UPDATE_CMD:
             {
-                if(host_type == HOST_TYPE_UART)
-                {
-
-                    Iap_Process(&data[3], data[2]);
-                }
+                Iap_Process(&data[3], data[2]);
             }
             break;
             
index b472a1c..7d5d136 100644 (file)
@@ -318,6 +318,7 @@ void Http_Get_Bind_Result(void *pvParameters)
                 ESP_LOGE(HTTP_TAG, "post bind fetch header err: %s", esp_err_to_name(err));
             }
         }
+
         else
         {
             result = 2;
@@ -340,7 +341,7 @@ void Http_Get_Bind_Result(void *pvParameters)
     if(result != 1) //绑定结果失败了 重新配网
     {
         //已经又通过恢复出厂开始广播配网了 
-        Esp32_Factory_Reset();
+       Esp32_Bind_Fail_Reset();
     }
     get_bind_flag=0;
     vTaskDelete(NULL);
index 26aabef..50b2480 100644 (file)
@@ -8,6 +8,8 @@
 #include "n455_ota.h"
 #if ENABLE_MQTT_FUNC
 
+
+
 #define     ERR_CODE_OK                     200
 #define     ERR_PID_SERIALNUMBER_ERR_CODE   401
 #define     ERR_JSON_CODE                   402 //不是json字符串
@@ -18,7 +20,7 @@
 #define REPLY_STATUS_FAIL 0
 
 static const char *MQTT_TAG = "MQTT";
-static esp_mqtt_client_config_t mqtt_cfg = {0};
+// static esp_mqtt_client_config_t mqtt_cfg = {0};
 static esp_mqtt_client_handle_t mqtt_client_handle = NULL;
 
 // 发送队列静态内存
@@ -29,7 +31,7 @@ StaticQueue_t mqtt_dev_report_queue_buffer;
 uint8_t mqtt_recv_queue_storage[MQTT_RECV_QUEUE_LENGTH * sizeof(mqtt_recv_msg_t)];
 QueueHandle_t mqtt_recv_queue_handle=NULL;
 StaticQueue_t mqtt_recv_queue_buffer;
-static uint8_t retry_connect_cnt = 0;
+
 
 // MQTT 任务
 TaskHandle_t mqtt_connect_task_handler;
@@ -39,13 +41,29 @@ StaticTask_t mqtt_connect_task_tcb;
 mqtt_pulish_data_t publish_data;
 
 //多任务安全标识
-static uint8_t mqtt_init_task_flag = 0;
-static uint8_t mqtt_send_data_count = 0;
-static uint8_t mqtt_init_flag = 0;
-static char cloud_msgID[64]="cloud_0196415f3b1f7e2ab7f72c4e8f91c0a2";  //应答需要用云端下发的msgID
-static uint8_t mqtt_check_connect = 0;
-static uint32_t recv_time = 0;
-static uint32_t get_time = 0;
+// static uint8_t mqtt_send_mcu_info_flag = 0;//发送主控信息标志,1表示已发送过,0表示未发送过
+// static uint8_t mqtt_init_task_flag = 0;
+// static uint8_t mqtt_send_data_count = 0;
+// static uint8_t mqtt_init_flag = 0;
+// static char cloud_msgID[64]="cloud_0196415f3b1f7e2ab7f72c4e8f91c0a2";  //应答需要用云端下发的msgID
+
+
+typedef struct {
+
+    uint8_t mqtt_init_flag; //mqtt 初始化
+    uint8_t mqtt_init_task_flag; //mqtt 任务初始化
+    uint8_t mqtt_send_data_count; //mqtt发送mcu计时器
+    uint8_t mqtt_send_mcu_info_flag; //mqtt发送mcu标识
+    uint8_t connect_flag;//mqtt连接成功的标识
+    uint8_t sub_flag;//Mqtt订阅成功的标识
+    //
+    char cloud_msgID[64]; //保存云端下发的msgid用作应答
+
+    esp_mqtt_client_config_t mqtt_cfg; //mqtt的客户端的配置集合
+
+}__attribute__((packed)) mqtt_ctx_t;
+static mqtt_ctx_t mqtt_ctx = {0};
+
 static void log_error_if_nonzero(const char *message, int error_code)
 {
     if (error_code != 0) {
@@ -94,6 +112,7 @@ char* get_publish_topic(TOPIC_TYPE type)
 
 
         case TOPIC_FIRMWARE_OTA_UPGRADE:    suffix = "firmware/upgrade";     break; //ota报文
+        case TOPIC_FIRMWARE_OTA_ACK:        suffix ="firmware/ack";break; //ota回复
         default: return NULL;
     }
     #if PID_A00D
@@ -157,6 +176,80 @@ const char *get_methon(uint8_t methon_mode)
     }
 
 }
+
+// ===================== MQTT 事件拆分(原大函数拆分为子函数) =====================
+static void mqtt_event_on_connected(esp_mqtt_event_handle_t event)
+{
+    esp_mqtt_client_handle_t mqtt_client = event->client;
+    int msg_id = 0;
+    mqtt_ctx.mqtt_send_data_count=0;
+
+    mqtt_ctx.connect_flag = 1;
+    mqtt_ctx.sub_flag = 0;
+    ESP_LOGI(MQTT_TAG, "set topic=%s",get_publish_topic(TOPIC_PROPERTY_SET));
+    ESP_LOGI(MQTT_TAG, "MQTT_EVENT_CONNECTED");
+    // /{pid}/{serialNumber}/property/post  
+    #if MQTT_USER_DEFAULT_TOPIC
+    snprintf(topic_str,sizeof(topic_str),"%s",MQTT_TOPIC);
+    msg_id = esp_mqtt_client_subscribe(mqtt_client, topic_str, 0);
+    #else
+    ESP_LOGI(MQTT_TAG, "subscribe topic=set");
+    msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_PROPERTY_SET), 0);
+    
+    ESP_LOGI(MQTT_TAG, "upgarde topic=%s",get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE));
+    msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE), 0);
+
+    if(common_setting.ble_status.is_bind == 1) // 连接mqtt请求一次http绑定接口做绑定 绑定失败配网失败 绑定成功配网成功
+    {
+        common_setting.ble_status.is_bind = 0;
+        xEventGroupSetBits(common_event_group, (EventBits_t)COMMON_HTTP_BIND_BIT);
+    }
+    #endif
+}
+
+static void mqtt_event_on_disconnected(void)
+{
+    
+    mqtt_ctx.connect_flag = 0;
+    mqtt_ctx.sub_flag = 0;
+    ESP_LOGI(MQTT_TAG, "MQTT_EVENT_DISCONNECTED");
+}
+
+static void mqtt_event_on_subscribed(void)
+{
+    mqtt_ctx.sub_flag = 1;
+    Mqtt_Send_Connect_Status();
+    ESP_LOGI(MQTT_TAG, "MQTT_EVENT_SUBSCRIBED");
+}
+
+static void mqtt_event_on_data(esp_mqtt_event_handle_t event)
+{
+    Mqtt_Recv_Data(event->topic, event->topic_len, event->data, event->data_len);
+}
+
+static void mqtt_event_on_error(esp_mqtt_event_handle_t event)
+{
+    ESP_LOGI(MQTT_TAG, "MQTT_EVENT_ERROR");
+    if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT)
+    {
+        int esp_tls_err    = event->error_handle->esp_tls_last_esp_err;
+        int tls_stack_err  = event->error_handle->esp_tls_stack_err;
+        int sock_errno     = event->error_handle->esp_transport_sock_errno;
+        int verify_flags   = event->error_handle->esp_tls_cert_verify_flags;
+
+        ESP_LOGI(MQTT_TAG, "esp-tls err:0x%04X", esp_tls_err);
+        ESP_LOGI(MQTT_TAG, "tls stack err:0x%04X", tls_stack_err);
+        ESP_LOGI(MQTT_TAG, "socket errno:%d", sock_errno);
+        ESP_LOGI(MQTT_TAG, "cert verify flags:%d", verify_flags);
+
+        if (tls_stack_err == 0x2180)
+            ESP_LOGE(MQTT_TAG, "证书格式非法");
+        if (tls_stack_err == 0x2700)
+            ESP_LOGE(MQTT_TAG, "证书不匹配服务器");
+        if (verify_flags != 0)
+            ESP_LOGE(MQTT_TAG, "SSL 证书校验失败");
+    }
+}
 /*
  * 函数功能:mqtt回调函数
  */
@@ -165,56 +258,25 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
 {
     ESP_LOGI(MQTT_TAG, "base=%s, event_id=%" PRIi32, base, event_id);
     esp_mqtt_event_handle_t event = event_data;
-    esp_mqtt_client_handle_t mqtt_client = event->client;
-    int msg_id;
+
     switch ((esp_mqtt_event_id_t)event_id) {
     case MQTT_EVENT_CONNECTED:
-       
-        mqtt_check_connect=0;
-        retry_connect_cnt = 0;
-        mqtt_send_data_count=0;
-        common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
-        common_setting.mqtt_user_cfg.mqtt_connect_flag = 1;
-        ESP_LOGI(MQTT_TAG, "set topic=%s",get_publish_topic(TOPIC_PROPERTY_SET));
-        ESP_LOGI(MQTT_TAG, "MQTT_EVENT_CONNECTED");
-        // /{pid}/{serialNumber}/property/post  
-        #if MQTT_USER_DEFAULT_TOPIC
-        snprintf(topic_str,sizeof(topic_str),"%s",MQTT_TOPIC);
-        msg_id = esp_mqtt_client_subscribe(mqtt_client, topic_str, 0);
-        #else
-        ESP_LOGI(MQTT_TAG, "subscribe topic=set");
-        msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_PROPERTY_SET), 0);
-        
-        ESP_LOGI(MQTT_TAG, "upgarde topic=%s",get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE));
-        msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE), 0);
-
-        if(common_setting.ble_status.is_bind == 1) // 连接mqtt请求一次http绑定接口做绑定 绑定失败配网失败 绑定成功配网成功
-        {
-            common_setting.ble_status.is_bind = 0;
-            xEventGroupSetBits(common_event_group, (EventBits_t)COMMON_HTTP_BIND_BIT);
-        }
-        #endif
-        
+        mqtt_event_on_connected(event);
         break;
 
     case MQTT_EVENT_DISCONNECTED:
-        common_setting.mqtt_user_cfg.mqtt_connect_flag = 0;
-        common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
        
-        
-        
-        ESP_LOGI(MQTT_TAG, "MQTT_EVENT_DISCONNECTED");
+        mqtt_event_on_disconnected();
         break;
 
     case MQTT_EVENT_SUBSCRIBED:
-        common_setting.mqtt_user_cfg.mqtt_sub_flag =1;
-        Mqtt_Send_Connect_Status();
+        mqtt_event_on_subscribed();
         // Mqtt_Send_Mcu_Info();
         ESP_LOGI(MQTT_TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d ", event->msg_id);
         break;
 
     case MQTT_EVENT_UNSUBSCRIBED:
-        common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
+        mqtt_ctx.sub_flag = 0;
         ESP_LOGI(MQTT_TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
         break;
 
@@ -224,44 +286,15 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
 
     case MQTT_EVENT_DATA:
         // ESP_LOGI(MQTT_TAG, "TOPIC=%.*s\r\n", event->topic_len, event->topic);
-        // ESP_LOGI(MQTT_TAG, "DATA=%.*s\r\n", event->data_len, event->data);
-        ESP_LOGI(MQTT_TAG, "DATA=%d %d\r\n", event->data_len, event->topic_len);
-        Mqtt_Recv_Data(event->topic,event->topic_len,event->data,event->data_len);
+        ESP_LOGI(MQTT_TAG, "DATA=%.*s\r\n", event->data_len, event->data);
+        
+        mqtt_event_on_data(event);
         break;
 
     case MQTT_EVENT_ERROR:
         ESP_LOGI(MQTT_TAG, "MQTT_EVENT_ERROR");
-        if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
-            // log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
-            // log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
-            // log_error_if_nonzero("captured as transport's socket errno",  event->error_handle->esp_transport_sock_errno);
-            // ESP_LOGI(MQTT_TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
-            int esp_tls_err = event->error_handle->esp_tls_last_esp_err;
-            int tls_stack_err = event->error_handle->esp_tls_stack_err;
-            int sock_errno = event->error_handle->esp_transport_sock_errno;
-            int verify_flags = event->error_handle->esp_tls_cert_verify_flags;
-
-            ESP_LOGI(MQTT_TAG, "  esp-tls err: 0x%04X\n", esp_tls_err);
-            ESP_LOGI(MQTT_TAG, "  tls stack err: 0x%04X\n", tls_stack_err);
-            ESP_LOGI(MQTT_TAG, "  socket errno: %d\n", sock_errno);
-            ESP_LOGI(MQTT_TAG, "  verify_flags errno: %d\n", verify_flags);
-            if(tls_stack_err == 0x2180)
-            {
-                ESP_LOGE(MQTT_TAG, "证书格式非法\n");
-            }
-            if(tls_stack_err == 0x2700)
-            {
-                ESP_LOGE(MQTT_TAG, "证书合法 但不匹配服务器\n");
-            }
-            // ==============================================
-            // 是不是证书错误!!!
-            // ==============================================
-            if (event->error_handle->esp_tls_cert_verify_flags != 0)     // SSL - 证书不匹配
-            {
-                ESP_LOGE(MQTT_TAG, "verify_flags errno\n");
-            }
-        }
-        //没意义 有链接才有断开的操作 这个不成立的
+        mqtt_ctx.sub_flag = 0;
+        mqtt_event_on_error(event);
         
         break;
     
@@ -283,27 +316,9 @@ void mqtt_check_isconnect(void)
     #if ENABLE_HTTPS_OTA_FUNC
     if(Https_Get_Ota_Status())
     {
-        mqtt_check_connect=0;
         return;
     }
     #endif
-    // if(common_setting.mqtt_user_cfg.mqtt_start_flag == 1 && common_setting.wifi_status.wifi_state == 1) //首次连接mqtt
-    // {
-    //     if(mqtt_check_connect <= 60) //连续五次连不上就不连 重新配网
-    //     { 
-    //         if(mqtt_check_connect == 60)
-    //         {
-    //             common_setting.mqtt_user_cfg.mqtt_start_flag = 0;
-    //             //断开mqtt连接
-    //             //清除配网信息重新开蓝牙
-    //             xEventGroupSetBits(common_event_group, COMMON_MQTT_INIT_BIT);//初始化失败了 或者连不成功
-
-    //         }
-    //         mqtt_check_connect+=1;
-    //     }
-    // }else{
-    //     mqtt_check_connect=0;
-    // }
 }
 /*
      *函数功能:mqtt连接状态发给mcu
@@ -323,8 +338,7 @@ void Mqtt_Send_Connect_Status(void)//包含心跳
 */
 static esp_err_t Mqtt_App_Init(void)
 {
-    
-    memset(&publish_data,0x00,sizeof(publish_data));
+   
     esp_err_t err = ESP_FAIL;
     if(common_setting.wifi_status.wifi_state != 1 || common_setting.ble_status.nimble_flag == 0)
     {
@@ -341,33 +355,30 @@ static esp_err_t Mqtt_App_Init(void)
         ESP_LOGI(MQTT_TAG, "mqtt url error");
         return ESP_FAIL;
     }
-    if(mqtt_init_flag)
+    if(mqtt_ctx.mqtt_init_flag)
     {
         //
         ESP_LOGI(MQTT_TAG, "Mqtt initialized.\n");
         return ESP_OK;
     }
+    strncpy(mqtt_ctx.cloud_msgID, "cloud_0196415f3b1f7e2ab7f72c4e8f91c2331", sizeof(mqtt_ctx.cloud_msgID)-1);
+    memset(&publish_data,0x00,sizeof(mqtt_pulish_data_t));
     mqtt_client_handle=NULL;
-    memset(&mqtt_cfg, 0, sizeof(mqtt_cfg));
-    retry_connect_cnt = 0;
-    mqtt_cfg.session.keepalive = 100; //50S
-    mqtt_cfg.session.disable_keepalive = 0;
+    memset(&mqtt_ctx.mqtt_cfg, 0, sizeof(mqtt_ctx.mqtt_cfg));
+    mqtt_ctx.mqtt_cfg.session.keepalive = 100; //50S
+    mqtt_ctx.mqtt_cfg.session.disable_keepalive = 0;
     // common_setting.mqtt_cfg.session.keepalive = 60;
-    mqtt_cfg.network.disable_auto_reconnect = false;
+    mqtt_ctx.mqtt_cfg.network.disable_auto_reconnect = false;
     // common_setting.mqtt_cfg.broker.address.transport = MQTT_TRANSPORT_OVER_TCP;
     // common_setting.mqtt_cfg.buffer.out_size = 2048;
     // common_setting.mqtt_cfg.buffer.size = 2048;
     
-    ESP_LOGI(MQTT_TAG, "URI: %s %s", common_storage.net_info.mqtt_sub_url,common_setting.net_pairing_info.mqtt_sub_url);
-    mqtt_cfg.broker.address.uri = (const char *)common_storage.net_info.mqtt_sub_url;
-    
-    
-
-    
+    ESP_LOGI(MQTT_TAG, "URI: %s", common_storage.net_info.mqtt_sub_url);
+    mqtt_ctx.mqtt_cfg.broker.address.uri = (const char *)common_storage.net_info.mqtt_sub_url;
 
-    mqtt_cfg.broker.verification.certificate =(const char *)common_storage.ssl_cert[SSL_SERVER_CRT].content;
-    mqtt_cfg.credentials.authentication.certificate = (const char *)common_storage.ssl_cert[SSL_CLIENT_CRT].content;
-    mqtt_cfg.credentials.authentication.key = (const char *)common_storage.ssl_cert[SSL_CLIENT_KEY].content;
+    mqtt_ctx.mqtt_cfg.broker.verification.certificate =(const char *)common_storage.ssl_cert[SSL_SERVER_CRT].content;
+    mqtt_ctx.mqtt_cfg.credentials.authentication.certificate = (const char *)common_storage.ssl_cert[SSL_CLIENT_CRT].content;
+    mqtt_ctx.mqtt_cfg.credentials.authentication.key = (const char *)common_storage.ssl_cert[SSL_CLIENT_KEY].content;
     char pid_str[10]={0};
     #if PID_A00D
     sprintf(pid_str,"0x%04X",common_setting.mcu_info.pid);
@@ -377,18 +388,17 @@ static esp_err_t Mqtt_App_Init(void)
     #if MQTT_USER_DEFAULT_TOPIC
     common_setting.mqtt_cfg.credentials.client_id = "basicPubSub";
     #else
-    // mqtt_cfg.credentials.client_id = (const char *)pid_str;
-    mqtt_cfg.credentials.client_id = (const char *)common_storage.net_info.serial_number;
+    // mqtt_ctx.mqtt_cfg.credentials.client_id = (const char *)pid_str;
+    mqtt_ctx.mqtt_cfg.credentials.client_id = (const char *)common_storage.net_info.serial_number;
     #endif
-    ESP_LOGI(MQTT_TAG, "Mqtt init %s",pid_str);
-    mqtt_cfg.task.priority = 4;
-    mqtt_client_handle = esp_mqtt_client_init(&mqtt_cfg);
+    ESP_LOGI(MQTT_TAG, "Mqtt init %s %s",pid_str,common_storage.net_info.serial_number);
+    mqtt_ctx.mqtt_cfg.task.priority = 4;
+    mqtt_client_handle = esp_mqtt_client_init(&mqtt_ctx.mqtt_cfg);
     if(mqtt_client_handle == NULL){
         ESP_LOGI(MQTT_TAG, "client_handle null\n");
         err = ESP_FAIL;
         return err;
     }
-    ESP_LOGI(MQTT_TAG, "Mqtt init");
     /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
     err = esp_mqtt_client_register_event(mqtt_client_handle, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
     if(err == ESP_OK)
@@ -396,7 +406,7 @@ static esp_err_t Mqtt_App_Init(void)
         err = esp_mqtt_client_start(mqtt_client_handle);
         if(err == ESP_OK)
         {
-            mqtt_init_flag=1;
+            mqtt_ctx.mqtt_init_flag=1;
             ESP_LOGI(MQTT_TAG, "success \n");
         }
     }
@@ -410,143 +420,14 @@ static esp_err_t Mqtt_App_Init(void)
  */
 uint8_t Mqtt_Get_Connect_Flag(void)
 {
-    if(mqtt_client_handle != NULL &&common_setting.mqtt_user_cfg.mqtt_connect_flag == 1 && \
-        common_setting.mqtt_user_cfg.mqtt_sub_flag == 1 && \
+    if(mqtt_client_handle != NULL && mqtt_ctx.connect_flag == 1 && \
+        mqtt_ctx.sub_flag == 1 && \
         common_setting.ble_status.ble_state == 0 && common_setting.wifi_status.wifi_state == 1)
     {
         return 1;
     }
     return 0;
 }
-
-/*
- *函数功能:开机连上mqtt订阅成功之后发送主控信息
-*/
-static uint8_t mqtt_send_mcu_info_flag = 0;//发送主控信息标志,1表示已发送过,0表示未发送过
-void Mqtt_Send_Mcu_Info(void)
-{
-
-    if(mqtt_send_mcu_info_flag)
-    {
-        return;
-    }
-    if(!Mqtt_Get_Connect_Flag()) 
-    {
-        return;
-    }
-    char pid_str[10] = {0};
-    char vid_str[10] = {0};
-    char wifi_soc[16] = {"Unknown"};
-
-    
-    // 获取芯片型号
-    uint8_t chip_len = 0;
-    get_chip_info(wifi_soc, &chip_len);
-    if (chip_len == 0) {
-        strcpy(wifi_soc, "Unknown");
-    }
-    // PID、VID 大写十六进制格式
-    snprintf(pid_str, sizeof(pid_str), "%04X", common_setting.mcu_info.pid);
-    snprintf(vid_str, sizeof(vid_str), "%04X", common_setting.mcu_info.vid);
-
-    
-    
-
-    // 版本号格式化 0x0015 → V0021
-    char fw_ver[12] = {0}, wifi_ver[12] = {0};
-    snprintf(fw_ver, sizeof(fw_ver), "0x%04X", common_setting.mcu_info.fw_version);
-    snprintf(wifi_ver, sizeof(wifi_ver), "0x%04X", (unsigned int)FW_VERSION);
-    
-    // ===================== 构建正确的 JSON =====================
-    cJSON *root = cJSON_CreateObject();
-    if (root == NULL) return;
-    
-
-    // 把设备信息加到里面
-    cJSON_AddStringToObject(root, "pid", pid_str);
-    cJSON_AddStringToObject(root, "vid", vid_str);
-    cJSON_AddStringToObject(root, "wifi_soc", wifi_soc);
-    cJSON_AddStringToObject(root, "fw_version", fw_ver);
-    cJSON_AddStringToObject(root, "wifi_version", wifi_ver);
-    cJSON_AddStringToObject(root, "serial_id", (char *)common_storage.net_info.serial_number);
-    cJSON_AddNumberToObject(root, "connected_platform", common_setting.mcu_platform_mode);
-    cJSON_AddNumberToObject(root, "power_on", 1);
-    cJSON_AddNumberToObject(root, "9", common_setting.is_connect_usb);
-
-
-    // 生成压缩格式JSON(不带换行,HTTP专用)
-    char *post_data = cJSON_Print(root);
-    // char *post_data = cJSON_PrintUnformatted(root);
-    cJSON_Delete(root);
-    root=NULL;
-    if (post_data == NULL) return;
-
-    
-    if(strlen(post_data) >= MQTT_SEND_RECV_DATA_BUF_SIZE)
-    {
-        ESP_LOGI(MQTT_TAG, "len large %d", strlen(post_data));
-        free(post_data);
-        post_data=NULL;
-        return;
-    }
-    if(strlen(post_data) == 0 || strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)) == 0)
-    {
-        ESP_LOGI(MQTT_TAG, "len no %d", strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)),strlen(post_data));
-        free(post_data);
-        post_data=NULL;
-        return;
-    }
-    // static char data_buf[64] = {0};
-    // static mqtt_send_msg_t send_msg={0};
-    // memcpy(data_buf,post_data,strlen(post_data));
-    ESP_LOGI(MQTT_TAG, "CJSON:%s", post_data);
-    
-
-    // memcpy(send_msg.topic,get_publish_topic(TOPIC_PROPERTY_ACK_POST),strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)));
-    
-    if(mqtt_dev_report_queue_handle==NULL)
-    {
-        ESP_LOGI(MQTT_TAG, "Mqtt_Send_Mcu_Info is null\n");
-        return;
-    }
-    user_esp_mqtt_client_publish(post_data,strlen(post_data),get_publish_topic(TOPIC_PROPERTY_ACK_POST));
-    free(post_data);
-    post_data = NULL;
-    mqtt_send_mcu_info_flag=0;
-    return;
-}
-void mqtt_get_dev_test(uint16_t *dpid,uint8_t num);
-/*
- * 函数功能:mcu信息
- *
- */
-void Mqtt_Send_Data(void)
-{
-    // return;
-    // ESP_LOGI(MQTT_TAG,"HR!!!!!",mqtt_send_data_count);
-    // ESP_LOGI(MQTT_TAG,"HR!!!!!:%d",mqtt_send_data_count);
-    if(mqtt_send_data_count % 5 == 0)
-    {
-        // ESP_LOGI(MQTT_TAG,"HR!!!11",mqtt_send_data_count);
-        // uint16_t test_dpid[81]= {745,777,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,271,272,273,274,275,276,
-        //     277,278,279,280,283,
-        //     284,285,286,287,288,289,59,60,62,63,64,65,66,67,68,69,70,71,72,73,
-        //     745,777,779,780,781,782,783,784,785,786,745,777,779,780,781,782,783,784,785,786,745,777,779,780,781,782,783,784,785,786};
-    
-        // // mqtt_get_dev_test(test_dpid,80);
-
-        // mqtt_get_dev_test(test_dpid,80);
-
-
-        // mqtt_get_dev_test(test_dpid,49);
-    }
-    if(mqtt_send_data_count >= 30)
-    {
-        mqtt_send_data_count = 0;
-        Mqtt_Send_Mcu_Info();  
-    }
-    mqtt_send_data_count++;
-}
 /*
  * 函数功能:原始数据传给队列
 */
@@ -565,10 +446,12 @@ void Mqtt_Recv_Data(char *topic, int topic_len, char *data, int data_len)
 
     // 拷贝主题
     memcpy(msg.topic, topic, topic_len);
+    msg.topic[topic_len] = '\0';
     msg.topic_len = topic_len;
 
     // 拷贝数据
     memcpy(msg.data, data, data_len);
+    msg.data[data_len] = '\0';
     msg.data_len = data_len;
     // ESP_LOGI(MQTT_TAG, "topic=%s data=%s\n",topic,data);
     if(mqtt_recv_queue_handle){
@@ -594,7 +477,7 @@ void Mqtt_Recv_Data(char *topic, int topic_len, char *data, int data_len)
 void Mqtt_Connect_Status_Check(void)
 {
     static uint8_t mqtt_init = 0;
-    #if (!ENABLE_HTTP_N455_OTA_FUNC)
+    #if (ENABLE_HTTP_N455_OTA_FUNC == 0 &&  ENABLE_HTTPS_OTA_FUNC == 1)
     if((xEventGroupWaitBits(common_event_group, COMMON_MQTT_CONNECT_BIT, pdFALSE, pdFALSE, 0) & COMMON_MQTT_CONNECT_BIT) != 0 && common_setting.ble_status.ble_state == 0\
     &&(Https_Get_Ota_Status() == HTTPS_OTA_STATUS_IDLE))
     #else
@@ -631,7 +514,7 @@ void Mqtt_Connect_Status_Check(void)
         xEventGroupClearBits(common_event_group, COMMON_MQTT_DISCONNECT_BIT);
         vTaskDelay(50 / portTICK_PERIOD_MS);
         
-        if(mqtt_init_flag == 1 && mqtt_client_handle != NULL){
+        if(mqtt_ctx.mqtt_init_flag == 1 && mqtt_client_handle != NULL){
             if(esp_mqtt_client_stop(mqtt_client_handle) == ESP_OK)
             {
                 
@@ -643,11 +526,10 @@ void Mqtt_Connect_Status_Check(void)
             }
             vTaskDelay(50 / portTICK_PERIOD_MS);
             esp_mqtt_client_destroy(mqtt_client_handle);
-            mqtt_client_handle=NULL;
-            retry_connect_cnt = 0;
-            mqtt_init_flag=0;
-            common_setting.mqtt_user_cfg.mqtt_connect_flag = 0;
-            common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
+            mqtt_client_handle=NULL;;
+            mqtt_ctx.mqtt_init_flag=0;
+            mqtt_ctx.connect_flag = 0;
+            mqtt_ctx.sub_flag = 0;
         }
     }
 
@@ -655,36 +537,33 @@ void Mqtt_Connect_Status_Check(void)
     if((xEventGroupWaitBits(common_event_group, COMMON_MQTT_INIT_BIT, pdFALSE, pdFALSE, 0) & COMMON_MQTT_INIT_BIT) != 0 )
     {
         xEventGroupClearBits(common_event_group, COMMON_MQTT_INIT_BIT);
-        vTaskDelay(50 / portTICK_PERIOD_MS);
-        
-        //断开wifi
-        Wifi_Stop();
-        vTaskDelay(50 / portTICK_PERIOD_MS);
         //断开mqtt释放资源
         Mqtt_Disconnect();
         vTaskDelay(50 / portTICK_PERIOD_MS);
-        User_Erase_NotReset();
-        xEventGroupSetBits(ble_event_group, BLE_ON_BIT);
         //http请求绑定结果
         // 
     }
     
 }
+uint8_t get_mqtt_init_flag(void)
+{
+    return mqtt_ctx.mqtt_init_flag;
+}
 /*
  * 函数功能:断开mqtt连接 释放mqtt资源
  */
 void Mqtt_Disconnect(void)
 {
-    if(mqtt_init_flag != 1)
+    if(!mqtt_ctx.mqtt_init_flag)
     {
         return;
     }
     if(mqtt_client_handle == NULL)
     {
-        mqtt_init_flag = 0;
+        mqtt_ctx.mqtt_init_flag = 0;
         return;
     }
-    mqtt_init_flag = 0;
+    mqtt_ctx.mqtt_init_flag = 0;
     esp_mqtt_client_stop(mqtt_client_handle);
     //
     vTaskDelay(pdMS_TO_TICKS(100));
@@ -708,16 +587,144 @@ void Mqtt_Disconnect(void)
         vQueueDelete(mqtt_recv_queue_handle);
         mqtt_recv_queue_handle = NULL;
     }
-    mqtt_init_task_flag=0;
+    mqtt_ctx.mqtt_init_task_flag=0;
     mqtt_client_handle = NULL;
-    retry_connect_cnt = 0;
-    common_setting.mqtt_user_cfg.mqtt_connect_flag = 0;
-    common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
-    mqtt_init_flag = 0;
+    mqtt_ctx.connect_flag = 0;
+    mqtt_ctx.sub_flag = 0;
+    mqtt_ctx.mqtt_init_flag = 0;
     memset(&publish_data, 0, sizeof(publish_data));
     ESP_LOGW(MQTT_TAG, "MQTT STOP");
 }
 
+/*
+ *函数功能:开机连上mqtt订阅成功之后发送主控信息
+*/
+
+void Mqtt_Send_Mcu_Info(void)
+{
+
+    if(mqtt_ctx.mqtt_send_mcu_info_flag)
+    {
+        return;
+    }
+    if(!Mqtt_Get_Connect_Flag()) 
+    {
+        return;
+    }
+    char pid_str[10] = {0};
+    char vid_str[10] = {0};
+    char wifi_soc[16] = {"Unknown"};
+
+    
+    // 获取芯片型号
+    uint8_t chip_len = 0;
+    get_chip_info(wifi_soc, &chip_len);
+    if (chip_len == 0) {
+        strcpy(wifi_soc, "Unknown");
+    }
+    // PID、VID 大写十六进制格式
+    snprintf(pid_str, sizeof(pid_str), "%04X", common_setting.mcu_info.pid);
+    snprintf(vid_str, sizeof(vid_str), "%04X", common_setting.mcu_info.vid);
+
+    
+    
+
+    // 版本号格式化 0x0015 → V0021
+    char fw_ver[12] = {0}, wifi_ver[12] = {0};
+    snprintf(fw_ver, sizeof(fw_ver), "0x%04X", common_setting.mcu_info.fw_version);
+    snprintf(wifi_ver, sizeof(wifi_ver), "0x%04X", (unsigned int)FW_VERSION);
+    
+    // ===================== 构建正确的 JSON =====================
+    cJSON *root = cJSON_CreateObject();
+    if (root == NULL) return;
+    
+
+    // 把设备信息加到里面
+    cJSON_AddStringToObject(root, "pid", pid_str);
+    cJSON_AddStringToObject(root, "vid", vid_str);
+    cJSON_AddStringToObject(root, "wifi_soc", wifi_soc);
+    cJSON_AddStringToObject(root, "fw_version", fw_ver);
+    cJSON_AddStringToObject(root, "wifi_version", wifi_ver);
+    cJSON_AddStringToObject(root, "serial_id", (char *)common_storage.net_info.serial_number);
+    cJSON_AddNumberToObject(root, "connected_platform", common_setting.mcu_platform_mode);
+    cJSON_AddNumberToObject(root, "power_on", 1);
+    cJSON_AddNumberToObject(root, "9", common_setting.is_connect_usb);
+
+
+    // 生成压缩格式JSON(不带换行,HTTP专用)
+    // char *post_data = cJSON_Print(root);
+    char *post_data = cJSON_PrintUnformatted(root);
+    cJSON_Delete(root);
+    root=NULL;
+    if (post_data == NULL) return;
+
+    
+    if(strlen(post_data) >= MQTT_SEND_RECV_DATA_BUF_SIZE)
+    {
+        ESP_LOGI(MQTT_TAG, "len large %d", strlen(post_data));
+        free(post_data);
+        post_data=NULL;
+        return;
+    }
+    if(strlen(post_data) == 0 || strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)) == 0)
+    {
+        ESP_LOGI(MQTT_TAG, "len no %d", strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)),strlen(post_data));
+        free(post_data);
+        post_data=NULL;
+        return;
+    }
+    // static char data_buf[64] = {0};
+    // static mqtt_send_msg_t send_msg={0};
+    // memcpy(data_buf,post_data,strlen(post_data));
+    ESP_LOGI(MQTT_TAG, "CJSON:%s", post_data);
+    
+
+    // memcpy(send_msg.topic,get_publish_topic(TOPIC_PROPERTY_ACK_POST),strlen(get_publish_topic(TOPIC_PROPERTY_ACK_POST)));
+    
+    if(mqtt_dev_report_queue_handle==NULL)
+    {
+        ESP_LOGI(MQTT_TAG, "Mqtt_Send_Mcu_Info is null\n");
+        return;
+    }
+    user_esp_mqtt_client_publish(post_data,strlen(post_data),get_publish_topic(TOPIC_PROPERTY_ACK_POST));
+    free(post_data);
+    post_data = NULL;
+    mqtt_ctx.mqtt_send_mcu_info_flag=0;
+    return;
+}
+void mqtt_get_dev_test(uint16_t *dpid,uint8_t num);
+/*
+ * 函数功能:mcu信息
+ *
+ */
+void Mqtt_Send_Data(void)
+{
+    // return;
+    // ESP_LOGI(MQTT_TAG,"HR!!!!!",mqtt_send_data_count);
+    // ESP_LOGI(MQTT_TAG,"HR!!!!!:%d",mqtt_send_data_count);
+    if(mqtt_ctx.mqtt_send_data_count % 5 == 0)
+    {
+        // ESP_LOGI(MQTT_TAG,"HR!!!11",mqtt_send_data_count);
+        // uint16_t test_dpid[81]= {745,777,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,271,272,273,274,275,276,
+        //     277,278,279,280,283,
+        //     284,285,286,287,288,289,59,60,62,63,64,65,66,67,68,69,70,71,72,73,
+        //     745,777,779,780,781,782,783,784,785,786,745,777,779,780,781,782,783,784,785,786,745,777,779,780,781,782,783,784,785,786};
+    
+        // // mqtt_get_dev_test(test_dpid,80);
+
+        // mqtt_get_dev_test(test_dpid,80);
+
+
+        // mqtt_get_dev_test(test_dpid,49);
+    }
+    if(mqtt_ctx.mqtt_send_data_count >= 30)
+    {
+        mqtt_ctx.mqtt_send_data_count = 0;
+        Mqtt_Send_Mcu_Info();  
+    }
+    mqtt_ctx.mqtt_send_data_count++;
+}
+
 // 是否是纯数字 如key value 特殊情况
 bool is_pure_number(const char *str) 
 {
@@ -1059,16 +1066,16 @@ void mqtt_ota_data_parse(const cJSON *array)
 
         char esp_pid[8] = {0};
         snprintf(esp_pid,sizeof(esp_pid)-1,"0x%04X",DEVICE_PID);
-        //主控的pid
-        if(strcmp(mcu_pid,mcuCode->valuestring) == 0)
+        //esp32 pid 
+        if(strcmp(esp_pid,mcuCode->valuestring) == 0)
         {
             pid_check_flag=1;
             #if ENABLE_HTTPS_OTA_FUNC
             Https_Ota_Version_Check(version->valuestring,filePath->valuestring);
             #endif
         }
-        //esp32 pid
-        else if(strcmp(esp_pid,mcuCode->valuestring) == 0)
+        //主控的pid
+        else if(strcmp(mcu_pid,mcuCode->valuestring) == 0)
         {
             pid_check_flag=1;
             #if ENABLE_HTTP_N455_OTA_FUNC
@@ -1076,11 +1083,12 @@ void mqtt_ota_data_parse(const cJSON *array)
             #endif
         }else{
             //和本地一个都不匹配就视为无效的报文
+            pid_check_flag=2;
             break;
         }
         
     }
-    if(pid_check_flag){
+    if(pid_check_flag == 1){
         //先检查模组是否需要升级 不需要就检查主控是否需要升级
         #if ENABLE_HTTPS_OTA_FUNC
         if(Https_Ota_Get_Update_Flag())
@@ -1100,7 +1108,13 @@ void mqtt_ota_data_parse(const cJSON *array)
         #endif
     }else
     {
-        ESP_LOGI(MQTT_TAG,"pid is null\n");
+        if(pid_check_flag == 2) //pid和本地不一致
+        {
+            ESP_LOGI(MQTT_TAG,"pid is null\n");
+        }else{
+            ESP_LOGI(MQTT_TAG,"is valide\n");
+        }
+        
     }
     
 }
@@ -1120,7 +1134,7 @@ void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len)
     // ========== cJSON 解析(安全版)==========
     cJSON *root = cJSON_Parse(data);
     if (root == NULL) {
-        ESP_LOGW(MQTT_TAG, "JSON error");
+        ESP_LOGI(MQTT_TAG, "JSON error");
 
         return;
     }
@@ -1152,6 +1166,7 @@ void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len)
             cJSON *version = cJSON_GetObjectItem(root , "version");
             if ((version != NULL && cJSON_IsString(version) && version->valuestring != NULL) ) 
             {
+                ESP_LOGI(MQTT_TAG,"ota file:%s",filePath->valuestring);
                 Https_Ota_Version_Check(version->valuestring, filePath->valuestring);
                 if(root != NULL)
                 {
@@ -1363,7 +1378,7 @@ void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len)
             if(strlen(msgId->valuestring) != 0)
             {
                 
-                strncpy(publish_data.msgid, msgId->valuestring, sizeof(publish_data.msgid)-1);
+                strncpy(mqtt_ctx.cloud_msgID, msgId->valuestring, sizeof(mqtt_ctx.cloud_msgID)-1);
                 cJSON *params = cJSON_GetObjectItem(payload, "params");//
                 if (params == NULL || !cJSON_IsArray(params)) {
                     
@@ -1384,14 +1399,13 @@ void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len)
                 // }
                 int code = ERR_CODE_OK;
                 
-               
-                recv_time =  xTaskGetTickCount();
+            
                 int ret = mqtt_set_dev_property(params);
                 if(ret == 0)
                 {
                     code=ERR_UART_SEND_ERR_CODE;
                 }
-                cJSON *replay_root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
+                cJSON *replay_root = create_publish_ack_json(msgId->valuestring,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
                 if(replay_root)
                 {
                     mqtt_send_publish_json(TOPIC_PROPERTY_ACK_POST,replay_root);
@@ -1408,7 +1422,7 @@ void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len)
             if(strlen(msgId->valuestring) != 0)
             {
                 
-                strncpy(publish_data.msgid, msgId->valuestring, sizeof(publish_data.msgid)-1);
+                strncpy(mqtt_ctx.cloud_msgID, msgId->valuestring, sizeof(mqtt_ctx.cloud_msgID)-1);
                 cJSON *params = cJSON_GetObjectItem(payload, "params");//
                 if (params == NULL || !cJSON_IsArray(params)) {
                     
@@ -1443,7 +1457,8 @@ void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len)
         {
             if(strlen(msgId->valuestring) != 0)
             {
-                strncpy(publish_data.msgid, msgId->valuestring, sizeof(publish_data.msgid)-1);
+                strncpy(mqtt_ctx.cloud_msgID, msgId->valuestring, sizeof(mqtt_ctx.cloud_msgID)-1);
+                mqtt_ctx.cloud_msgID[sizeof(mqtt_ctx.cloud_msgID)-1] = 0;
                 cJSON *input = cJSON_GetObjectItem(payload, "input");//
                 if (input == NULL || !cJSON_IsArray(input)) {
                     
@@ -1460,11 +1475,11 @@ void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len)
                 cJSON *replay_root = NULL;
                 cJSON *serviceId = cJSON_GetObjectItem(payload, "serviceId");//
                 if (serviceId == NULL || !cJSON_IsString(serviceId) || serviceId->valuestring == NULL) {
-                    replay_root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
+                    replay_root = create_publish_ack_json(msgId->valuestring,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
                     
                 }else{
                     strncpy(publish_data.serviceId, msgId->valuestring, sizeof(publish_data.serviceId)-1);
-                    replay_root = create_publish_ack_json(publish_data.msgid,serviceId->valuestring,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
+                    replay_root = create_publish_ack_json(msgId->valuestring,serviceId->valuestring,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
                 }
                 
                 if(replay_root)
@@ -1995,7 +2010,8 @@ char *get_devMsgId(void)
 */
 uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len)
 {
-    strncpy(publish_data.msgid,cloud_msgID,strlen(cloud_msgID));
+    strncpy(publish_data.msgid,mqtt_ctx.cloud_msgID,strlen(mqtt_ctx.cloud_msgID));
+    publish_data.msgid[sizeof(publish_data.msgid)-1] = 0;
     uint8_t mode = 0; //0是使用云端生成下发的 属于应答 1 是使用设备端生成的 属于设备主动上报的
     //应答
     
@@ -2184,8 +2200,6 @@ uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len)
         cJSON_Delete(root);
         root=NULL;
     }
-    
-    get_time = 0;
     root=NULL;
     return 1;
 }
@@ -2274,11 +2288,11 @@ static void Mqtt_Init_Task_Cb(void *p)
  */
 void Mqtt_Init_Task(void)
 {
-    if(mqtt_init_task_flag){
+    if(mqtt_ctx.mqtt_init_task_flag){
         ESP_LOGI(MQTT_TAG, "Mqtt_Init_Task inited");
         return;
     }
-    mqtt_init_task_flag=1;
+    mqtt_ctx.mqtt_init_task_flag=1;
     mqtt_recv_queue_handle = xQueueCreateStatic(MQTT_RECV_QUEUE_LENGTH, MQTT_RECV_QUEUE_ITEM_SIZE, mqtt_recv_queue_storage, &mqtt_recv_queue_buffer);
     mqtt_dev_report_queue_handle = xQueueCreateStatic(MQTT_SEND_QUEUE_LENGTH, MQTT_SEND_QUEUE_ITEM_SIZE, mqtt_dev_report_queue_storage, &mqtt_dev_report_queue_buffer);
     
index 5cf81e6..fc32b9d 100644 (file)
@@ -29,7 +29,8 @@ typedef enum {
     
     TOPIC_SERVICE_POST,        //服务下发
     TOPIC_SERVICE_REPLY,      // 服务应答
-    TOPIC_FIRMWARE_OTA_UPGRADE    //ota报文
+    TOPIC_FIRMWARE_OTA_UPGRADE,    //ota报文
+    TOPIC_FIRMWARE_OTA_ACK
 } TOPIC_TYPE;
 
 
@@ -56,7 +57,7 @@ typedef struct {
     char serviceId[SERVICEID_STR_MAX+1];       // serviceId
     
 } __attribute__((packed)) mqtt_pulish_data_t;
-extern mqtt_pulish_data_t publish_data;
+extern mqtt_pulish_data_t publish_data; //发布的集合
 
 // MQTT 接收消息结构体
 typedef struct {
@@ -116,5 +117,6 @@ uint8_t mqtt_send_publish_json(TOPIC_TYPE topic_type,cJSON *root);
 void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len);
 void mqtt_check_isconnect(void);
 void mqtt_ota_data_parse(const cJSON *array);
+uint8_t get_mqtt_init_flag(void);
 #endif
 #endif
\ No newline at end of file