优化内存空间
authord15521469561 <1731678236@qq.com>
Thu, 18 Jun 2026 08:50:55 +0000 (16:50 +0800)
committerd15521469561 <1731678236@qq.com>
Thu, 18 Jun 2026 08:50:55 +0000 (16:50 +0800)
18 files changed:
main/app_main.c
main/ble.c
main/gap.c
main/gatt_svc.c
main/https_ota.c
main/iap.c
main/n455_ota.c
main/project_common.c
main/project_common.h
main/uart.c
main/uart.h
main/user_flash.c
main/user_http.c
main/user_http.h
main/user_mqtt.c
main/user_mqtt.h
main/wifi.c
main/wifi.h

index a152ef5..853ae38 100644 (file)
@@ -15,7 +15,7 @@
 #include "ble.h"
 #include "n455_ota.h"
 #include "esp_chip_info.h"
-static const char *MAIN_TAG = "main";
+static const char MAIN_TAG[] = "main";
 // MAIN_LOOP
 #define MAIN_LOOP_TASK_STACK            (2*1024)
 TaskHandle_t main_loop_task_handler;
@@ -29,12 +29,13 @@ static esp_timer_handle_t onesec_timer_handle = NULL;
 static void onesec_timer_callback(void* arg)
 {
     #if ENABLE_MQTT_FUNC
-    Mqtt_Send_Data();
+    Mqtt_Report_Device_Base_Info();
     Mqtt_Report_Wifi_Rssi_Check();
     #endif
-    
     // struct tm local_time = {0};
     // Get_Time(&local_time);
+
+    // Get_Time_No_Zone_Stamp();
     #if ENABLE_HTTP_N455_OTA_FUNC
     static uint8_t onesec_timer_count = 0;
     if(onesec_timer_count < 65)
@@ -50,16 +51,6 @@ static void onesec_timer_callback(void* arg)
     }
     #endif
     #if ENABLE_HTTPS_OTA_FUNC
-    // static uint8_t onesec_timer_count = 0;
-    // if(onesec_timer_count < 65)
-    // {
-    //     onesec_timer_count++;
-    //     if(onesec_timer_count == 20){
-            
-    //         // xEventGroupSetBits(common_event_group, COMMON_HTTPS_OTA_BIT);
-    //     }
-        
-    // }
     //===========esp32 ota timeout=================//
     Https_Ota_Check_TimeOut();
     #endif
@@ -284,7 +275,7 @@ void app_main(void)
         ret = nvs_flash_init();
     }
     ESP_ERROR_CHECK( ret );
-
+   
     user_flash_mutex = xSemaphoreCreateBinary();
     xSemaphoreGive(user_flash_mutex);
 
@@ -305,10 +296,10 @@ void app_main(void)
     User_Flash_Check(); 
     // UART
     Uart_Initialize();
-    uart_task_handler = xTaskCreateStatic(Uart_Task, "Uart_Task", UART_TASK_STACK, NULL, 7, uart_task_stack, &uart_task_tcb);
-
-    uart_send_task_handler = xTaskCreateStatic(Uart_Tx_Task, "Uart_Tx_Task", 1*1024, NULL, 6, uart_send_task_stack, &uart_send_task_tcb);
+    uart_task_handler = xTaskCreateStatic(Uart_Task, "Uart_Task", UART_TASK_STACK, NULL, 6, uart_task_stack, &uart_task_tcb);
 
+    uart_send_task_handler = xTaskCreateStatic(Uart_Tx_Task, "Uart_Tx_Task", UART_SEND_TASK_STACK, NULL, 5, uart_send_task_stack, &uart_send_task_tcb);
+    
 #if (1)//#if (IOT_ENABLE)
     // LWIP
     ESP_ERROR_CHECK(esp_netif_init());
index 4639272..97c8fcf 100644 (file)
@@ -1,7 +1,7 @@
 #include "project_common.h"
 #include "ble.h"
 #if ENABLE_BLE_FUNC
-static const char *BLE_TAG = "ble";
+static const char BLE_TAG[] = "ble";
 
 uint8_t ble_write_queue_storage[BLE_WRTIE_QUEUE_LENGTH*BLE_WRTIE_QUEUE_ITEM_SIZE];
 QueueHandle_t ble_write_queue_handle;
index 674585b..bcd0a0d 100644 (file)
@@ -7,7 +7,7 @@
 #include "project_common.h"
 #include "ble.h"
 #if ENABLE_BLE_FUNC
-static const char *GAP_TAG = "gap";
+static const char GAP_TAG[] = "gap";
 uint16_t ble_mtu_max = 23; // 默认MTU为23字节,实际可用为20字节
 static bool ble_advertising_enabled = true; // 广播开关
 
index d399665..ababf67 100644 (file)
@@ -7,7 +7,7 @@
 #include "project_common.h"
 #include "ble.h"
 #if ENABLE_BLE_FUNC
-static const char *GATT_TAG = "gatt";
+static const char GATT_TAG[] = "gatt";
 
 /* Private function declarations */
 static int gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
index b5d95ec..487d96b 100644 (file)
@@ -18,7 +18,7 @@
 #include "wifi.h"
 #include "n455_ota.h"
 #if (ENABLE_HTTPS_OTA_FUNC)
-static const char *HTTPS_OTA = "HTTPS_OTA";
+static const char HTTPS_OTA[] = "HTTPS_OTA";
 #if CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
 #include "esp_efuse.h"
 #endif
@@ -33,19 +33,13 @@ https_ota_status_t g_ota_status = HTTPS_OTA_STATUS_IDLE; // https全局OTA状态
 #define HTTPS_OTA_TIMEOUT_TIME      150//单位s
 
 #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 uint8_t ota_timeout_req = 0; //超时升级失败
-// static volatile uint16_t ota_timeout_count = 0;
-
-// static volatile uint16_t ota_faile_count  = 0;
-
-// static uint8_t https_update_ota_flag = 0; //根据版本号比对 是否需要升级
-
-// static uint8_t https_ota_init = 0;
+#define HTTPS_OTA_RETRY_TIMES       2000//单位ms const int max_retry_times = 500;超时重试时间
 
+#define HTTPS_OTA_RETRY_NUM  2
 
+#define OTA_URL_SIZE 128
 
+#define HTTPS_OTA_STACK_SIZE   (4*1024)
 
 typedef struct{
     volatile uint8_t ota_timeout_req;//超时退出标识
@@ -59,11 +53,7 @@ typedef struct{
 
 static https_ota_ctx_t s_ota_ctx = { 0 };
 static uint8_t ota_retry_cnt = 0;
-#define HTTPS_OTA_RETRY_NUM  2
-
-#define OTA_URL_SIZE 128
 
-#define HTTPS_OTA_STACK_SIZE   (4*1024)
 static char https_ota_url[OTA_URL_SIZE] = "https://pxn-iot.s3-accelerate.amazonaws.com/iot01/2026/06/02/6a1e460abfcb5bab677760e5.bin";//"https://pxn-iot.s3-accelerate.amazonaws.com/iot01/2026/06/02/6a1e460abfcb5bab677760e5.bin";//"https://pxn-iot.s3.us-east-2.amazonaws.com/iot01/2026/05/22/6a0fb4324db40762648f18b3.bin";
 /* Event handler for catching system events */
 /*
@@ -681,7 +671,7 @@ static void Https_Ota_Task_Cb(void *pvParameter)
     https_ota_handle=NULL;
     ESP_LOGI(HTTPS_OTA, "Starting Advanced OTA example:%s\n",https_ota_url);
     g_ota_status = HTTPS_OTA_STATUS_UPGRADING;
-    if(!s_ota_ctx.ota_auto)
+    if(1)
     {
         int ret = Https_Ota_Report_Status(HTTPS_OTA_STATUS_UPGRADING);
         if(!ret)
@@ -792,8 +782,8 @@ static void Https_Ota_Task_Cb(void *pvParameter)
             }
             err = esp_https_ota_perform(https_ota_handle);
             if (err != ESP_ERR_HTTPS_OTA_IN_PROGRESS) {
-                ESP_LOGI(HTTPS_OTA, "ota timout");
-                
+                ESP_LOGI(HTTPS_OTA, "ota timout=%d",err);
+                download_abort = true;
                 break;
             }
             if(s_ota_ctx.ota_timeout_req){
@@ -855,7 +845,7 @@ static void Https_Ota_Task_Cb(void *pvParameter)
             {
                 esp_err_t ret = esp_ota_mark_app_invalid_rollback();
                 ESP_LOGI(HTTPS_OTA, "esp32 restart: %d", ret);
-                if(!s_ota_ctx.ota_auto){
+                if(1){
                     Https_Ota_Report_Status(HTTPS_OTA_STATUS_FAILED);
                 }
                 N455_Ota_Clear_Upgrade_Flag();
@@ -865,7 +855,7 @@ static void Https_Ota_Task_Cb(void *pvParameter)
         }else{
             //不需要升级455 直接重启
             g_ota_status = HTTPS_OTA_STATUS_SUCCESS;
-            if(!s_ota_ctx.ota_auto){
+            if(1){
                 Https_Ota_Report_Status(HTTPS_OTA_STATUS_SUCCESS);
             }
             if(s_ota_ctx.ota_auto) //如果是自动升级 不重启 还是一样重连mqtt
@@ -900,7 +890,7 @@ static void Https_Ota_Task_Cb(void *pvParameter)
         ESP_LOGI(HTTPS_OTA, "ESP_HTTPS_OTA upgrade failed after 3 retry");
         g_ota_status = HTTPS_OTA_STATUS_FAILED;
         s_ota_ctx.ota_timeout_count=0;
-        if(s_ota_ctx.ota_auto){
+        if(0){
             file_header_check=0;
         }else
         {
@@ -954,7 +944,7 @@ uint8_t Https_Ota_Version_Check(char *version,char *url,uint8_t ota_auto)
     }
     uint16_t new_version = version_str_to_hex(version);
     // 核心:把 0x0A15 转成浮点数 10.21
-    ESP_LOGI(HTTPS_OTA, "ver: %s %04x ESP32_VERSION:%04x", version,new_version,FW_VERSION);
+    ESP_LOGI(HTTPS_OTA, "ver: %s 0x%04x ESP32_VERSION:0x%04x", version,new_version,FW_VERSION);
     if(new_version != FW_VERSION && new_version != 0xffff)
     {
         s_ota_ctx.https_update_ota_flag = 1;
index 4652463..242e6c6 100644 (file)
@@ -2,7 +2,7 @@
 #include "n455_ota.h"
 #include "https_ota.h"
 #include "user_mqtt.h"
-static const char *IAP_TAG = "IAP";
+static const char IAP_TAG[] = "IAP";
 
 static const esp_partition_t *update_partition = NULL;
 
@@ -222,7 +222,7 @@ void Iap_Process(uint8_t* data, uint8_t len)
        {
                return;
        }
-       iap_update_cnt = 10;
+       iap_update_cnt = 20;
        // 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)
        {
index 6227e22..ea3779b 100644 (file)
@@ -18,7 +18,7 @@
 #include "uart.h"
 #include "https_ota.h"
 #if (ENABLE_HTTP_N455_OTA_FUNC)
-static const char *N455_OTA = "455_OTA";
+static const char N455_OTA[] = "455_OTA";
 #if CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
 #include "esp_efuse.h"
 #endif
@@ -28,18 +28,14 @@ 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; // 455升级状态
+
 #define OTA_URL_MAX_LEN 128
 #define OAT_CONNECT_RETRY_NUM  2 //重连服务器次数 1
 #define OTA_TOTAL_TIMEOUT_S   60 //总超时时间 单位s
 #define N455_OTA_STACK_SIZE   (4*1024)
-static char n455_ota_url[OTA_URL_MAX_LEN] = "https://pxn-iot.s3-accelerate.amazonaws.com/iot01/2026/06/03/6a1f95e5bfcb3c271127dd8b.bin";
-
-
 // BIN 文件头
 #define FW_HEADER_SIZE          256     // 前256字节是头,需要跳过
 
-
 typedef struct{
     volatile bool  uart_ack_received; //收到回复串口的应答 1收到应答
     volatile uint8_t uart_ack;//应答标识 0xff未收到回复 1成功 2 失败
@@ -51,7 +47,13 @@ typedef struct{
 
 
 }__attribute__((packed)) n455_ota_ctx_t;
+
+static char n455_ota_url[OTA_URL_MAX_LEN] = "https://pxn-iot.s3-accelerate.amazonaws.com/iot01/2026/06/03/6a1f95e5bfcb3c271127dd8b.bin";
+
 static n455_ota_ctx_t n455_ota_ctx = {0};
+
+N455_ota_status_t n455_g_ota_status = N455_OTA_STATUS_IDLE; // 455升级状态
+
 /*
  *函数功能:接收升级应答
  *cmd:命令类型
@@ -582,7 +584,7 @@ void N455_Ota_Task_Cb(void *param)
         Mqtt_Disconnect();  // 关闭MQTTs
         vTaskDelay(50 / portTICK_PERIOD_MS);
         n455_g_ota_status = N455_OTA_STATUS_UPGRADING;
-        if(!n455_ota_ctx.ota_auto) //自动升级不需要上报状态
+        if(1) //自动升级不需要上报状态
         {
             int ret = Https_Ota_Report_Status(N455_OTA_STATUS_UPGRADING);
             if(!ret)
@@ -883,7 +885,7 @@ void N455_Ota_Task_Cb(void *param)
         if (!failed) {
             
             n455_g_ota_status = N455_OTA_STATUS_SUCCESS;
-            if(!n455_ota_ctx.ota_auto){
+            if(1){
 
                 Https_Ota_Report_Status(N455_OTA_STATUS_SUCCESS);
             }
@@ -924,7 +926,7 @@ void N455_Ota_Task_Cb(void *param)
     }
 
     Https_Ota_Clear_Upgrade_Flag();
-    if(n455_ota_ctx.ota_auto){
+    if(0){
         file_header_check=0;
     }else{
         if(file_header_check)
@@ -981,7 +983,7 @@ uint8_t N455_Ota_Version_Check(char *version,char *url,uint8_t ota_auto)
     }
     uint16_t new_version = version_str_to_hex(version);
     // 核心:把 0x0A15 转成浮点数 10.21
-    ESP_LOGI(N455_OTA, "ver: %s %04x MCU_VERSION:%04x", version,new_version,common_setting.mcu_info.fw_version);
+    ESP_LOGI(N455_OTA, "ver: %s 0x%04x MCU_VERSION:0x%04x", version,new_version,common_setting.mcu_info.fw_version);
     ESP_LOGI(N455_OTA, "url:%s", url);
     if(new_version != common_setting.mcu_info.fw_version && new_version != 0xffff)
     {
@@ -1014,11 +1016,7 @@ uint8_t N455_Ota_Get_Update_Flag(void)
 void N455_Ota_Clear_Upgrade_Flag(void)
 {
     n455_g_ota_status = N455_OTA_STATUS_IDLE; // 状态重置为空闲
-    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; //升级标识清除
-    n455_ota_ctx.ota_auto = 0; //自动升级标志清零
+    memset(&n455_ota_ctx,0,sizeof(n455_ota_ctx));
     // memset(n455_ota_url,0,sizeof(n455_ota_url));
 }
 /**
index 9b1f2e1..ce31940 100644 (file)
@@ -3,7 +3,9 @@
 #include "user_mqtt.h"
 #include "user_flash.h"
 
-static const char *COMMON_TAG = "COMMON";
+#define NET_PAIRING_REPLY_MAX_PAYLOAD (BLE_BUF_SIZE - 5) // 5 = header(1) + cmd(1) + len(2) + crc(1)
+
+static const char COMMON_TAG[] = "COMMON";
 typedef struct {
     uint16_t fw_version;
     uint16_t pid;
@@ -85,9 +87,6 @@ COMMON_SETTING_T common_setting =
 
 // static BLE_SEND_REPLY_T net_pairing_reply;
 
-#define NET_PAIRING_REPLY_MAX_PAYLOAD (BLE_BUF_SIZE - 5) // 5 = header(1) + cmd(1) + len(2) + crc(1)
-
-
 
 
 void Check_Host_Type(uint8_t cmd, uint8_t* pbuf, uint16_t len, uint8_t host_type)
@@ -1239,10 +1238,10 @@ void int64_to_str(uint64_t ts, char *out,size_t out_len) {
 
 #define TIME_DEFAULT_ZONE  8
 #define TIME_DEFAULT_ZONE_STAMP  (8*3600)  
-int8_t Time_Zone = TIME_DEFAULT_ZONE*2;//默认是东八区 16这是455需要知道的时区
+int8_t Time_Zone = TIME_DEFAULT_ZONE*2;//默认是东八区 16这是455需要知道的时区 -24~+24
 int64_t Time_Stamp = 946684800; //默认是2000-01-01 00:00:00
 /*
- * 函数功能:获取时区 默认是东八区 已经是*2了8*2=16
+ * 函数功能:获取时区 默认是东八区 已经是*2了8*2=16 给主控使用的接口
  * 函数返回值:int8_t 范围是-24~+24
 */
 int8_t Get_Time_Zone(void)
@@ -1250,14 +1249,31 @@ int8_t Get_Time_Zone(void)
     return Time_Zone;
 }
 /*
- * 函数功能:获取世界时间戳不带时区的时间戳
+ * 函数功能:获取世界时间戳带时区的时间戳
  * 函数返回值:int64_t 默认是2000-01-01 00:00:00 
 */
 int64_t Get_Time_Stamp(void)
 {
-    return Time_Stamp;
+    time_t now = 0;
+    time(&now);
+    return now;
 }
+/*
+ * 函数功能:获取世界时间戳不带时区的时间戳 给主控使用的
+ * 函数返回值:int64_t 默认是2000-01-01 00:00:00 
+*/
 
+int64_t Get_Time_No_Zone_Stamp(void)
+{
+    int64_t local_ts = Get_Time_Stamp();
+    // Time_Zone 单位:0.5小时,1单位 = 1800秒
+    int64_t zone_offset_sec = (int64_t)Time_Zone * 1800;
+    // 本地时间戳 减去 时区偏移秒数 = UTC时间戳
+    int64_t utc_ts = local_ts - zone_offset_sec;
+    
+    ESP_LOGI(COMMON_TAG,"Time_Stamp=%ld local_ts=%ld Time_Zone = %d utc_ts=%ld",(uint32_t)Time_Stamp,(uint32_t)local_ts,Time_Zone,(uint32_t)utc_ts);
+    return utc_ts;
+}
 
 /*
  * 函数功能:设置本地时间
@@ -1280,7 +1296,6 @@ void set_system_time_int_from_utc(int64_t utc_timestamp_sec,const char *zone)
     settimeofday(&tv, NULL);
 
 }
-
 void Power_On_Default_Time(void)
 {
     //开机默认时间
@@ -1295,7 +1310,7 @@ void Get_Time(struct tm *time_out)
     time_t now = 0;
     time(&now);
    
-    time_t local = now + TIME_DEFAULT_ZONE_STAMP;
+    time_t local = now + TIME_DEFAULT_ZONE_STAMP; //加8个小时是因为底层已经自动减去了8个小时
     localtime_r(&local,time_out);
     #if 1
     ESP_LOGI(COMMON_TAG, "time_out:%04d-%02d-%02d %02d:%02d:%02d", 
index 984bbcb..52482c4 100644 (file)
@@ -516,6 +516,7 @@ int8_t Get_Time_Zone(void);
 int64_t Get_Time_Stamp(void);
 void Esp32_Bind_Fail_Reset(void);
 void Power_On_Default_Time(void);
+int64_t Get_Time_No_Zone_Stamp(void);
 #if ENABLE_SYNC_SNTP_TIME
 void wait_for_sntp_sync(void);
 #endif
index e4c5dcc..df669b5 100644 (file)
@@ -3,28 +3,30 @@
 #include "uart.h"
 #include "ble.h"
 #include "n455_ota.h"
-static const char *UART_TAG = "UART_TASK";
-static const char *UART_SEND = "UART_SEND";
-static const char *UART_RECV = "UART_RECV";
-// static const int uart_rx_max = UART_BUF_SIZE;
+static const char UART_TAG[] = "UART_TASK";
+static const char UART_SEND[] = "UART_SEND";
+static const char UART_RECV[] = "UART_RECV";
 
+#define UART_SEND_BLE_STATUS_RETRY_NUM       3
+#define UART_SEND_BLE_STATUS_RETRY_TIME       500 //单位ms
+#define RX_CACHE_MAX_LEN  1024
+#define READ_BUF_MAX_LEN (256)
+
+static volatile uint8_t ble_connected_notify_flag = 0;
+static uint16_t ble_connected_notify_count= 0;
+static uint32_t rx_last_recv_tick  = 0;
 
 uint8_t uart_tx_queue_storage[UART_TX_QUEUE_LENGTH*UART_TX_QUEUE_ITEM_SIZE];
 QueueHandle_t uart_tx_queue_handle;
 StaticQueue_t uart_tx_queue_buffer;
 
-// //
-// uint8_t uart_rx_queue_storage[UART_RX_QUEUE_LENGTH*UART_RX_QUEUE_ITEM_SIZE];
-// QueueHandle_t uart_rx_queue_handle;
-// StaticQueue_t uart_rx_queue_buffer;
-
 // UART
 TaskHandle_t uart_task_handler;
 StackType_t uart_task_stack[UART_TASK_STACK];
 StaticTask_t uart_task_tcb;
 
 TaskHandle_t uart_send_task_handler;
-StackType_t uart_send_task_stack[1*1024];
+StackType_t uart_send_task_stack[UART_SEND_TASK_STACK];
 StaticTask_t uart_send_task_tcb; 
 /*
  * 函数功能:uart初始化
@@ -44,29 +46,45 @@ void Uart_Initialize(void)
     uart_driver_install(UART_NUM_1, UART_BUF_SIZE, 0, 0, NULL, 0);
         
     uart_tx_queue_handle = xQueueCreateStatic(UART_TX_QUEUE_LENGTH, UART_TX_QUEUE_ITEM_SIZE, uart_tx_queue_storage, &uart_tx_queue_buffer);
-    
-    // uart_rx_queue_handle = xQueueCreateStatic(UART_RX_QUEUE_LENGTH, UART_RX_QUEUE_ITEM_SIZE, uart_rx_queue_storage, &uart_rx_queue_buffer);
+
 }
-static volatile uint8_t ble_connected_notify_flag = 0;
-static uint16_t ble_connected_notify_count= 0;
-#define UART_SEND_BLE_STATUS_RETRY_NUM       3
-#define UART_SEND_BLE_STATUS_RETRY_TIME       500 //单位ms
+
+/*
+ *函数功能:设置蓝牙连接成功标识
+ *参数说明:无
+ *返回值:无
+*/
+void Uart_Set_Ble_Status(void)
+{
+    ble_connected_notify_count = 0;
+    ble_connected_notify_flag = 1;
+
+}
+/*
+ *函数功能:清空蓝牙连接成功标识
+ *参数说明:无
+ *返回值:无
+*/
+void Uart_Clear_Ble_Status(void)
+{
+    ble_connected_notify_flag = 0;
+    ble_connected_notify_count =0 ;
+
+}
+
+
+
 void Uart_Tx_Queue_Process(void)
 {
     if(ble_connected_notify_flag)
     {
         if(ble_connected_notify_count <= UART_SEND_BLE_STATUS_RETRY_TIME*UART_SEND_BLE_STATUS_RETRY_NUM)
-        {
-            if(ble_connected_notify_count == 0) //第一次发送
-            {
-                
-                Uart_Reply_Send(COMMON_UART_BLE_CONNECT_STATUS,NULL,0,HOST_TYPE_UART);
-            }
-            ble_connected_notify_count++;
+        { 
             if(ble_connected_notify_count % UART_SEND_BLE_STATUS_RETRY_TIME == 0)
             {
                 Uart_Reply_Send(COMMON_UART_BLE_CONNECT_STATUS,NULL,0,HOST_TYPE_UART);
             }
+            ble_connected_notify_count++;
         } 
     }else{
         ble_connected_notify_count = 0;
@@ -77,10 +95,9 @@ void Uart_Tx_Queue_Process(void)
         uart_write_bytes(UART_NUM_1, uart_tx_data.buffer, uart_tx_data.size);
     }
 }
-#define  RX_CACHE_MAX_LEN  1024
 
-static uint8_t  rx_cache[RX_CACHE_MAX_LEN] = {0};
-static uint32_t rx_cache_len = 0;
+
+
 // =====================  =====================
 #if 0
 
@@ -204,31 +221,7 @@ void Uart_Recv_Handle(uint8_t *data, uint16_t len)
 }
 #endif
 
-/*
- *函数功能:设置蓝牙连接成功标识
- *参数说明:无
- *返回值:无
-*/
-void Uart_Set_Ble_Status(void)
-{
-    ESP_LOGI(UART_TAG,"Uart_Set_Ble_Status!!!!");
-    ble_connected_notify_count = 0;
-    ble_connected_notify_flag = 1;
-    // ble_connected_first_flag = 0;
 
-}
-/*
- *函数功能:清空蓝牙连接成功标识
- *参数说明:无
- *返回值:无
-*/
-void Uart_Clear_Ble_Status(void)
-{
-    // ble_connected_first_flag=0;
-    ble_connected_notify_flag = 0;
-    ble_connected_notify_count =0 ;
-
-}
 void Uart_Tx_Task(void *arg)
 {
     while (1)
@@ -240,14 +233,15 @@ void Uart_Tx_Task(void *arg)
 /*
  * 函数功能: uart接收发送task
 */
-#define READ_BUF_MAX_LEN (256)
 
-static uint32_t rx_last_recv_tick  = 0;
 void Uart_Task(void *arg)
 {
     esp_log_level_set(UART_TAG, ESP_LOG_ERROR);
     static uint32_t now = 0 ;
     static uint8_t read_buf[READ_BUF_MAX_LEN]={0};
+
+    static uint8_t  rx_cache[RX_CACHE_MAX_LEN] = {0};
+    static uint32_t rx_cache_len = 0;
     while (1)
     {
         
@@ -361,7 +355,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                             common_setting.mcu_info.product_name_len = name_len;
                             common_setting.mcu_info_got_flag = 1;
                             uart_pairing_reply.content[0]=0x01;
-                            ESP_LOGI(UART_RECV, "start nimble 0x%04x 0x%04x 0x%04x 0x%04x %s\n",common_setting.mcu_info.vid,common_setting.mcu_info.pid,\
+                            ESP_LOGI(UART_RECV, "start nimble 0x%04X 0x%04X 0x%04X 0x%04X %s\n",common_setting.mcu_info.vid,common_setting.mcu_info.pid,\
                             common_setting.mcu_info.fw_version,common_setting.mcu_info.build_version,common_setting.mcu_info.product_name);
                             #if 1
                             Ble_Start_Nimble_NetWork();////开启蓝牙配网
@@ -498,22 +492,13 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                 *(uint16_t*)&uart_pairing_reply.content[4] = BUILD_VERSION;
             }
             break;
-            //按键灯的颜色
-            case COMMON_UART_GET_KEY_COLOR_CMD:
-            {
-                //通过mqtt发送给上位机
-                #if ENABLE_MQTT_FUNC
-                
-                #endif
-            }
-            break;
             case COMMON_UART_GET_TIMESTAMP_CMD:
             {
                 uart_pairing_reply.send_flag = 1;
                 uart_pairing_reply.len = 9;
                 uart_pairing_reply.content[0] = 10;
                 uart_pairing_reply.content[1] = Get_Time_Zone();//时区
-                int64_t time = Get_Time_Stamp();
+                int64_t time = Get_Time_No_Zone_Stamp();
                 memcpy(&uart_pairing_reply.content[2],&time,8); //时间戳
             }
             break;
@@ -537,9 +522,6 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
             {
                 if(data[3] == 1) // 停止配网
                 {
-                    // 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;
                     
                 }
             }
@@ -552,15 +534,6 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                 #endif
             }
             break;
-            case 0xB1: //这是USB通过串口给模组升级的指令 模组暂时不用 使用的是0xE0升级
-            {
-                // ESP_LOG_BUFFER_HEX(UART_RECV,data,len);
-                #if ENABLE_HTTP_N455_OTA_FUNC
-                N455_Ota_Capture_Ack(data[1],data[4]);
-                #endif
-            }
-            break;
-
             // 固件升级
             case COMMON_UART_ESP_FIRMWARE_UPDATE_CMD:
             {
@@ -579,10 +552,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
         if(uart_pairing_reply.send_flag)
         {
             Uart_Reply_Send(uart_pairing_reply.cmd, uart_pairing_reply.content, uart_pairing_reply.len, HOST_TYPE_UART);
-            
-            // Check_Host_Type(uart_pairing_reply.cmd, uart_pairing_reply.content, uart_pairing_reply.len, host_type);
         }
-        // ESP_LOGI(UART_RECV, "uart recv %d bytes:cmd %0x", len, data[1]);
     }
     else if(data[0] == FRAMEHEAD_PANEL_TO_IOT)
     {
index 8117648..2f776db 100644 (file)
@@ -100,27 +100,21 @@ typedef struct
 #define UART_TX_QUEUE_LENGTH          8
 #define UART_TX_QUEUE_ITEM_SIZE       (sizeof(UART_RECV_DATA_T))
 
-#define UART_RX_QUEUE_LENGTH          4
-#define UART_RX_QUEUE_ITEM_SIZE       (sizeof(UART_RECV_DATA_T))
 
 extern uint8_t uart_tx_queue_storage[UART_TX_QUEUE_LENGTH*UART_TX_QUEUE_ITEM_SIZE];
 extern QueueHandle_t uart_tx_queue_handle;
 extern StaticQueue_t uart_tx_queue_buffer;
 
 
-// extern uint8_t uart_rx_queue_storage[UART_RX_QUEUE_LENGTH*UART_RX_QUEUE_ITEM_SIZE];
-// extern QueueHandle_t uart_rx_queue_handle;
-// extern StaticQueue_t uart_rx_queue_buffer;
-
 //不能改小 或者会影响usb 串口升级升级失败
 #define UART_TASK_STACK                 (2*1024)
 extern TaskHandle_t uart_task_handler;
 extern StackType_t uart_task_stack[UART_TASK_STACK];
 extern StaticTask_t uart_task_tcb;
 
-// #define UART_SEND_TASK_STACK                 (1*1024)
+#define UART_SEND_TASK_STACK                 (1*1024)
 extern TaskHandle_t uart_send_task_handler;
-extern StackType_t uart_send_task_stack[1*1024];
+extern StackType_t uart_send_task_stack[UART_SEND_TASK_STACK];
 extern StaticTask_t uart_send_task_tcb;
 
 
index 102ffc7..c8a6a9f 100644 (file)
@@ -1,7 +1,7 @@
 #include "project_common.h"
 
 #define PARA_FLASH_NAME "user_data"
-static const char *FLASH_TAG = "user_flash";
+static const char FLASH_TAG[] = "user_flash";
 
 static const esp_partition_t *partition;
 
index 7f55f6c..29d27ce 100644 (file)
@@ -4,7 +4,27 @@
 #include "user_mqtt.h"
 #if ENABLE_HTTP_FUNC
 #define CLOSE_HTTP_LOG  0
-static const char *HTTP_TAG = "http";
+static const char HTTP_TAG[] = "http";
+
+#define MAX_HTTP_OUTPUT_BUFFER  2*1024 //包含证书下载和获取serialNumber的http请求响应数据
+
+#define HTTP_DOWNLOAD_CER_FUNC_ENABLE 1
+
+#define HTTP_RETRY_NUMBER_MAX (3) //HTTP请求失败重试次数最大值
+
+#define HTTP_GET_MQTT_URL_CER_ENABLE 1 //设备请求下发mqtt地址 证书
+
+#define HTTP_PUT_REPORT_MAC_RSSI_ENABLE   1 //PUT请求上报mac和rssi信息
+#define HTTP_GET_SERIAL_NUMBER_ENBALE 1 //GET请求获取serialNumber,服务器根据设备token mac pid等信息返回对应的serialNumber=deviceID等信息
+
+#define HTTP_CONTENT_MAX_LEN  2048
+#define USE_MALLOC_DATA_SIZE  0
+#define HTTP_CHUNK_SIZE     512
+
+static char http_download_cache[MAX_HTTP_OUTPUT_BUFFER+1] = {0};
+
+
+HTTP_GET_CERT_TASK_PARAM_T http_get_cert_task_param;
 /*
  * 函数功能: http请求回调函数
  *
@@ -37,20 +57,10 @@ static esp_err_t _http_event_handler(esp_http_client_event_t *evt)
     }
     return ESP_OK;
 }
-#define MAX_HTTP_OUTPUT_BUFFER  2*1024 //包含证书下载和获取serialNumber的http请求响应数据
-static char http_download_cache[MAX_HTTP_OUTPUT_BUFFER+1] = {0};
-#define HTTP_DOWNLOAD_CER_FUNC_ENABLE 1
-
-#define HTTP_RETRY_NUMBER_MAX (3) //HTTP请求失败重试次数最大值
 
-#define HTTP_GET_MQTT_URL_CER_ENABLE 1 //设备请求下发mqtt地址 证书
 
-#define HTTP_PUT_REPORT_MAC_RSSI_ENABLE   1 //PUT请求上报mac和rssi信息
-#define HTTP_GET_SERIAL_NUMBER_ENBALE 1 //GET请求获取serialNumber,服务器根据设备token mac pid等信息返回对应的serialNumber=deviceID等信息
 
 
-
-HTTP_GET_CERT_TASK_PARAM_T http_get_cert_task_param;
 /*
 函数功能: https下载证书task回调函数
 */
@@ -276,14 +286,9 @@ void Http_Download_Task(void){
 /*
  * 函数功能: 获取绑定结果task回调函数
  */
-static uint8_t get_bind_flag = 0;
+
 void Http_Get_Bind_Result(void *pvParameters)
 {
-    if(get_bind_flag == 1)
-    {
-        return;
-    }
-    get_bind_flag=1;
     esp_http_client_config_t http_config = 
     {
         .event_handler = _http_event_handler,
@@ -294,7 +299,7 @@ void Http_Get_Bind_Result(void *pvParameters)
     esp_http_client_handle_t http_client = NULL;
     int result = 0;
     ESP_LOGI(HTTP_TAG, "\nget bing");
-    static char url[255]={0};
+    char url[255]={0};
     for(uint8_t retry=0;retry<HTTP_RETRY_NUMBER_MAX;retry++)
     {
         result = 0;
@@ -368,7 +373,6 @@ void Http_Get_Bind_Result(void *pvParameters)
         //恢复出厂开始广播配网了 
        Esp32_Bind_Fail_Reset();
     }
-    get_bind_flag=0;
     vTaskDelete(NULL);
     
 }
@@ -385,9 +389,7 @@ void Http_Get_Bind_Result_Task(void){
         ESP_LOGI(HTTP_TAG,"BIND TASK ok\n");
     }
 }
-#define HTTP_CONTENT_MAX_LEN  2048
-#define USE_MALLOC_DATA_SIZE  0
-#define HTTP_CHUNK_SIZE     512
+
 
 /*
  * 函数功能: 请求下发serialNumber 上传设备信息
@@ -430,7 +432,7 @@ static void Http_Get_Url_Task_Cb(void *pvParameters)
         }
 
         ///iot/device/generateSerialNumberByPid
-        const char path[] = "/iot/device/generateSerialNumberByPid?pid=";// "/iot/device/getDeviceByPid/";//;//;
+        // const char path[] = "/iot/device/generateSerialNumberByPid?pid=";// "/iot/device/getDeviceByPid/";//;//;
         memset(url, 0x00, sizeof(url));
         
         uint8_t mac[6] = {0};
@@ -676,14 +678,18 @@ static void Http_Get_Url_Task_Cb(void *pvParameters)
         // cJSON_AddNumberToObject(pRoot, "userId", common_setting.net_pairing_info.user_id);
 
         char *post_data = cJSON_Print(pRoot);
-        if(strlen(post_data) == 0 && post_data == NULL)//数据为空说明cJSON构建失败
+        if(post_data == NULL)//数据为空说明cJSON构建失败
         {
             cJSON_Delete(pRoot);
-            // ESP_LOGE(HTTP_TAG, "cJSON_Print fail");
-            // ESP_LOGI(HTTP_TAG, "\ncJSON_Print fail.....%d %" PRIu32 " bytes",result,esp_get_free_heap_size());
             vTaskDelay(50);
             continue; 
         }
+        if(strlen(post_data) == 0)
+        {
+            cJSON_Delete(pRoot);
+            vTaskDelay(50);
+            continue;
+        }
         // ESP_LOGI(HTTP_TAG, "put data:%s\n", post_data);
         ESP_LOGI(HTTP_TAG, "post_data = %s", post_data);
         
@@ -964,7 +970,6 @@ static void Http_Get_Url_Task_Cb(void *pvParameters)
     if(pClientCrt != NULL && pClientkey != NULL && pServerCrt != NULL && pMqttSubUrl != NULL && result == 1)
     {
         result = 0;
-        // ESP_LOGI(HTTP_TAG, "\nCER.....HTTP%d %" PRIu32 " bytes",result,esp_get_free_heap_size());
         xEventGroupSetBits(common_event_group, COMMON_HTTP_DOWNLOAD_BIT);
     }
     else
@@ -990,7 +995,6 @@ void Http_Get_Url_Task(void){
 void Http_Download_Status_Check(void)
 {
     EventBits_t http_bits = xEventGroupWaitBits(common_event_group, COMMON_HTTP_DOWNLOAD_BIT | COMMON_HTTP_BIND_BIT |COMMON_HTTP_GET_URL_BIT, pdTRUE, pdFALSE, 0);
-    // ESP_LOGI(HTTP_TAG, "[APP] Free memory http1 on: %" PRIu32 " bytes", esp_get_free_heap_size());
     if((http_bits & COMMON_HTTP_DOWNLOAD_BIT) != 0)
     {
         xEventGroupClearBits(common_event_group, COMMON_HTTP_DOWNLOAD_BIT);
index 202a86f..9b58d34 100644 (file)
@@ -19,6 +19,6 @@ extern HTTP_GET_CERT_TASK_PARAM_T http_get_cert_task_param;;
 void Http_Download_Status_Check(void);
 void Http_Get_Url_Task(void);
 void Http_Download_Task(void);
-void Http_Get_Bing_Result_Task(void);
+void Http_Get_Bind_Result_Task(void);
 #endif
 #endif
index 879305a..e78a13e 100644 (file)
 #define     ERR_UART_SEND_ERR_CODE          600 //串口发送失败  
 
 
+#define QOS    (1)
 #define REPLY_STATUS_OK 1
 #define REPLY_STATUS_FAIL 0
 
-static const char *MQTT_TAG = "MQTT";
+#define REPORT_INTERVAL_CNT  2
+static const char MQTT_TAG[] = "MQTT";
 // static esp_mqtt_client_config_t mqtt_cfg = {0};
-static esp_mqtt_client_handle_t mqtt_client_handle = NULL;
-
-// 发送队列静态内存
-uint8_t  mqtt_dev_report_queue_storage[MQTT_SEND_QUEUE_LENGTH * sizeof(mqtt_dev_report_msg_t)];
-QueueHandle_t mqtt_dev_report_queue_handle=NULL;
-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;
-
-
-// MQTT 任务
-TaskHandle_t mqtt_connect_task_handler;
-StackType_t mqtt_connect_task_stack[MQTT_CONNECT_TASK_STACK];
-StaticTask_t mqtt_connect_task_tcb;
-
-mqtt_pulish_data_t publish_data;
 
-//多任务安全标识
-// 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 enum{
+    MODE_ACK=0, //应答
+    MODE_REPORT, //主动上报
 
+}ACK_POST;
 
 typedef struct {
 
@@ -62,27 +44,49 @@ typedef struct {
     esp_mqtt_client_config_t mqtt_cfg; //mqtt的客户端的配置集合
 
 }__attribute__((packed)) mqtt_ctx_t;
+
+// WiFi RSSI上报控制参数结构体
+typedef struct {
+    int duration_second;    // 总持续上报时长 s
+    int interval_second;    // 单次上报间隔 s,最小2s
+    bool enable_flag;       // 上报总开关标识(原is_report_rssi_info_flag)
+    int duration_s;         // 当前已计时秒数
+    int32_t rssi_tmp;       // RSSI缓存,用于对比变化触发上报
+} __attribute__((packed)) MQTT_WIFI_REPORT_CTRL_T;
+
+//多任务安全标识
+// 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
+
+
 static mqtt_ctx_t mqtt_ctx = {0};
 
-static void log_error_if_nonzero(const char *message, int error_code)
-{
-    if (error_code != 0) {
-        ESP_LOGE(MQTT_TAG, "Last error %s: 0x%x", message, error_code);
-    }
-}
+// 全局统一控制实例,替代一堆零散static
+static MQTT_WIFI_REPORT_CTRL_T g_wifi_report_ctrl = {0};
 
-/*
- * @brief Event handler registered to receive MQTT events
- *
- *  This function is called by the MQTT client event loop.
- *
- * @param handler_args user data registered to the event.
- * @param base Event base for the handler(always MQTT Base in this example).
- * @param event_id The id for the received event.
- * @param event_data The data for the event, esp_mqtt_event_handle_t.
- */
+
+static esp_mqtt_client_handle_t mqtt_client_handle = NULL;
+
+// 发送队列静态内存
+uint8_t  mqtt_dev_report_queue_storage[MQTT_SEND_QUEUE_LENGTH * sizeof(mqtt_dev_report_msg_t)];
+QueueHandle_t mqtt_dev_report_queue_handle=NULL;
+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;
 
 
+// MQTT 任务
+TaskHandle_t mqtt_connect_task_handler;
+StackType_t mqtt_connect_task_stack[MQTT_CONNECT_TASK_STACK];
+StaticTask_t mqtt_connect_task_tcb;
+
+mqtt_pulish_data_t publish_data;
+
 /*
     * 函数功能:获取发布topic
     * 设备根据不同的topic区分数据上报
@@ -129,18 +133,15 @@ char* get_publish_topic(TOPIC_TYPE type)
     return pub_topic;
 }
 
-const char *service_property_set="thing.service.property.set";
-const char *property_set_replay="thing.service.property.set.reply";
-
-const char *service_property_get="thing.service.property.get";
-const char *service_property_get_replay="thing.service.property.get.reply";
-
-const char *property_post = "thing.event.property.post";
-const char *service_invoke = "thing.service.calibration.invoke";
-const char *dev_invoke_replay = "thing.service.calibration.invoke.reply";
-
-const char *service_property_get_wifi = "thing.service.property.get.wifi";;
-const char *service_property_get_wifi_reply = "thing.service.property.get.wifi.reply";
+#define TOPIC_SET          "thing.service.property.set"
+#define TOPIC_SET_REPLY    "thing.service.property.set.reply"
+#define TOPIC_GET          "thing.service.property.get"
+#define TOPIC_GET_REPLY    "thing.service.property.get.reply"
+#define TOPIC_POST         "thing.event.property.post"
+#define TOPIC_CALIB_INVOKE "thing.service.calibration.invoke"
+#define TOPIC_CALIB_REPLY  "thing.service.calibration.invoke.reply"
+#define TOPIC_GET_WIFI     "thing.service.property.get.wifi"
+#define TOPIC_GET_WIFI_REPLY "thing.service.property.get.wifi.reply"
 
 /*
  * 函数功能:获取方式请求mode
@@ -152,33 +153,33 @@ const char *get_methon(uint8_t methon_mode)
     {
         //设备属性设置应答和服务属性下发设置
         case METHON_SERVICE_PROPERTY_SET:
-            return service_property_set;
+            return TOPIC_SET;
 
         case METHON_SERVICE_PROPERTY_SET_REPLAY:
-            return property_set_replay;
+            return TOPIC_SET_REPLY;
         
         case METHON_SERVICE_PROPERTY_GET:
-            return service_property_get;
+            return TOPIC_GET;
 
         case METHON_SERVICE_PROPERTY_GET_REPLAY:
-            return service_property_get_replay;
+            return TOPIC_GET_REPLY;
 
         //设备事件主动上报methon
         case METHON_EVENT_PROPERTY_POST:
-            return property_post;
+            return TOPIC_POST;
 
         //服务事件下发和设备事件应答服务
         case METHON_SERVICE_CALIBRATION_INVOKE:
-            return service_invoke;
+            return TOPIC_CALIB_INVOKE;
         case METHON_SERVICE_CALIBRATION_INVOKE_REPLAY:
-            return dev_invoke_replay;
+            return TOPIC_CALIB_REPLY;
         
 
         //属性下发获取wifi信息
         case METHON_SERVICE_PROPERTY_GET_WIFI:
-            return service_property_get_wifi;
+            return TOPIC_GET_WIFI;
         case METHON_SERVICE_PROPERTY_GET_WIFI_REPLAY:
-            return service_property_get_wifi_reply;
+            return TOPIC_GET_WIFI_REPLY;
 
         default:
             return "";
@@ -202,10 +203,10 @@ static void mqtt_event_on_connected(esp_mqtt_event_handle_t event)
     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);
+    msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_PROPERTY_SET), QOS);
     
     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);
+    msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE), QOS);
 
     if(common_setting.ble_status.is_bind == 1) // 连接mqtt请求一次http绑定接口做绑定 绑定失败配网失败 绑定成功配网成功
     {
@@ -238,6 +239,8 @@ static void mqtt_event_on_data(esp_mqtt_event_handle_t event)
 /*****************mqtt*****事件错误回调******************************************************** */
 static void mqtt_event_on_error(esp_mqtt_event_handle_t event)
 {
+    mqtt_ctx.connect_flag = 0;
+    mqtt_ctx.sub_flag = 0;
     ESP_LOGI(MQTT_TAG, "MQTT_EVENT_ERROR");
     if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT)
     {
@@ -290,7 +293,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
         break;
 
     case MQTT_EVENT_PUBLISHED:
-        ESP_LOGI(MQTT_TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
+        // ESP_LOGI(MQTT_TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
         break;
 
     case MQTT_EVENT_DATA:
@@ -334,11 +337,13 @@ void mqtt_check_isconnect(void)
  */
 void Mqtt_Send_Connect_Status(void)//包含心跳
 {
+    #if 0
     uint8_t heart_beat_data[2] = {0};//数据
     heart_beat_data[0]=COMMON_MQTT_STATUS;//状态类型
     
     heart_beat_data[1]=Mqtt_Get_Connect_Flag() ? 0x01:0x00;//mqtt连接状态
     Uart_Reply_Send(COMMON_UART_HEART_CMD, heart_beat_data, sizeof(heart_beat_data), HOST_TYPE_UART);//(cmd, pbuf, len, host_type);
+    #endif
 }
 
 /*
@@ -380,7 +385,6 @@ static esp_err_t Mqtt_App_Init(void)
     // 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", common_storage.net_info.mqtt_sub_url);
     mqtt_ctx.mqtt_cfg.broker.address.uri = (const char *)common_storage.net_info.mqtt_sub_url;
 
@@ -400,7 +404,7 @@ static esp_err_t Mqtt_App_Init(void)
     mqtt_ctx.mqtt_cfg.credentials.client_id = (const char *)common_storage.net_info.serial_number;
     #endif
     ESP_LOGI(MQTT_TAG, "Mqtt init %s %s",pid_str,common_storage.net_info.serial_number);
-    mqtt_ctx.mqtt_cfg.task.priority = 4;
+    mqtt_ctx.mqtt_cfg.task.priority = 7;
     mqtt_client_handle = esp_mqtt_client_init(&mqtt_ctx.mqtt_cfg);
     if(mqtt_client_handle == NULL){
         ESP_LOGI(MQTT_TAG, "client_handle null\n");
@@ -444,18 +448,23 @@ void Mqtt_Recv_Data(char *topic, int topic_len, char *data, int data_len)
     // 1. 长度判断
     
     
-    if (topic_len > MQTT_TOPIC_LEN || data_len > MQTT_DATA_BUF_SIZE || data_len <= 0 || topic_len <= 0) {
-        ESP_LOGI(MQTT_TAG, "len large :%d\n",data_len);
+    if (topic_len > MQTT_TOPIC_LEN || data_len <= 0 || topic_len <= 0) {
+        ESP_LOGI(MQTT_TAG, "data_len less < 0 = %d topic_len=%d\n",data_len,topic_len);
         return;
     }
+    //如果是升级文件 长度限制小于1024
     if (topic_len == strlen(get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE)) && 
-        strcmp(get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE), topic) == 0)
+        strcmp(get_publish_topic(TOPIC_FIRMWARE_OTA_UPGRADE), topic) == 0 && data_len < 1024)
     {
         ESP_LOGI(MQTT_TAG, "ota json");
         mqtt_parse_recv_data(topic, topic_len, data, data_len);
         return;
     }
-    
+    if(data_len > MQTT_DATA_BUF_SIZE )
+    {
+        ESP_LOGI(MQTT_TAG, "data len :%d\n",data_len);
+        return;
+    }
     // 2. 构建消息
     mqtt_recv_msg_t msg;
     memset(&msg, 0, sizeof(mqtt_recv_msg_t));
@@ -492,6 +501,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 == 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\
@@ -502,7 +512,7 @@ void Mqtt_Connect_Status_Check(void)
     #endif
     {
         xEventGroupClearBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
-        vTaskDelay(50 / portTICK_PERIOD_MS);
+        vTaskDelay(50);
         if(common_setting.wifi_status.wifi_state == 1)
         {
             int ret = Mqtt_App_Init();
@@ -515,13 +525,15 @@ void Mqtt_Connect_Status_Check(void)
             }
             else
             {
-                if(mqtt_init <= 5) //连续多次初始化都失败就不初始化了
+                if(mqtt_init <= 3) //连续多次初始化都失败就不初始化了
                 {
                     xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
                     mqtt_init+=1;
                 }
                 ESP_LOGI(MQTT_TAG, "Mqtt init fail");
             }
+        }else{
+            mqtt_init = 0;
         }
     }
 
@@ -644,8 +656,8 @@ uint8_t Mqtt_Send_Mcu_Info(void)
         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);
+    snprintf(pid_str, sizeof(pid_str), "0x%04X", common_setting.mcu_info.pid);
+    snprintf(vid_str, sizeof(vid_str), "0x%04X", common_setting.mcu_info.vid);
 
     
     
@@ -676,9 +688,6 @@ uint8_t Mqtt_Send_Mcu_Info(void)
     
     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;
@@ -699,17 +708,11 @@ uint8_t Mqtt_Send_Mcu_Info(void)
         post_data=NULL;
         return 0;
     }
-    // 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, "mcu info:%d", strlen(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");
+        ESP_LOGI(MQTT_TAG, "mqtt_dev_report_queue_handle is null\n");
         return 0;
     }
     int ret = user_esp_mqtt_client_publish(post_data,strlen(post_data),get_publish_topic(TOPIC_PROPERTY_ACK_POST));
@@ -739,26 +742,26 @@ static void Report_Wifi_Infor_Json(void)
     char *topic = get_publish_topic(TOPIC_PROPERTY_ACK_POST);
     cJSON_AddStringToObject(root, "topic", (const char *)topic);
     cJSON_AddStringToObject(root, "msgId", (const char *)get_devMsgId());
-    cJSON_AddStringToObject(root, "method", (const char *)methon);
+    cJSON_AddStringToObject(root, "method", methon);
     time_t Time_Sec = Get_Time_Stamp();
-    static char time_str[16] = {0};
+    char time_str[16] = {0};
     int64_to_str((int64_t)Time_Sec,time_str,sizeof(time_str)-1);
     cJSON_AddStringToObject(root, "time", (const char *)time_str);
 
     cJSON_AddStringToObject(root, "serialNumber", (char *)common_storage.net_info.serial_number);
     char pid[8] = {0};
-    snprintf(pid, sizeof(pid), "%04X", common_setting.mcu_info.pid);
+    snprintf(pid, sizeof(pid), "0x%04X", common_setting.mcu_info.pid);
     cJSON_AddStringToObject(root, "productId",pid);
 
     // 3. 创建 params 数组
     cJSON *params_arr = cJSON_CreateArray();
 
-    // 数组第一个元素 {"765":1}
+    // 数组第一个元素 {"10":1}//wifi名称
     cJSON *item1 = cJSON_CreateObject();
     cJSON_AddStringToObject(item1, "10", (char *)common_storage.net_info.wifi_ssid);
     cJSON_AddItemToArray(params_arr, item1);
 
-    // 数组第二个元素 {"786":89}
+    // 数组第二个元素 {"11":89} wifi信号值 rssi
     cJSON *item2 = cJSON_CreateObject();
     cJSON_AddNumberToObject(item2, "11", common_setting.wifi_status.rssi);
     cJSON_AddItemToArray(params_arr, item2);
@@ -786,8 +789,6 @@ static void Report_Wifi_Infor_Json(void)
     {
         ESP_LOGI(MQTT_TAG,"repott wifi failed!");
     }
-    // 内存释放,必须做,否则内存泄漏
-    // 释放顺序:先释放打印字符串 → 销毁JSON树 → 释放动态topic
     if(json_str)
     {
         free(json_str);
@@ -798,19 +799,8 @@ static void Report_Wifi_Infor_Json(void)
 }
 
 
-// WiFi RSSI上报控制参数结构体
-typedef struct {
-    int duration_second;    // 总持续上报时长 s
-    int interval_second;    // 单次上报间隔 s,最小2s
-    bool enable_flag;       // 上报总开关标识(原is_report_rssi_info_flag)
-    int duration_s;         // 当前已计时秒数
-    int32_t rssi_tmp;       // RSSI缓存,用于对比变化触发上报
-} __attribute__((packed)) MQTT_WIFI_REPORT_CTRL_T;
 
-// 全局统一控制实例,替代一堆零散static
-static MQTT_WIFI_REPORT_CTRL_T g_wifi_report_ctrl = {0};
 
-#define REPORT_INTERVAL_CNT  2
 /*
  *函数功能:设置上报wifi信息参数
  *参数:
@@ -877,7 +867,7 @@ void mqtt_get_dev_test(uint16_t *dpid,uint8_t num);
  * 函数功能:mcu信息
  *
  */
-void Mqtt_Send_Data(void)
+void Mqtt_Report_Device_Base_Info(void)
 {
     // return;
     // ESP_LOGI(MQTT_TAG,"HR!!!!!",mqtt_send_data_count);
@@ -1323,17 +1313,11 @@ void mqtt_ota_data_parse(const cJSON *array)
  */
 void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len)
 {
-    // // 安全判断
-    // if (buf == NULL || data_len <= 0 || data_len > MQTT_DATA_BUF_SIZE) {
-    //     ESP_LOGW(MQTT_TAG, "parese too len:%d\n",data_len);
-    //     return;
-    // }
     if(data == NULL)return;
     // ========== cJSON 解析(安全版)==========
     cJSON *root = cJSON_Parse(data);
     if (root == NULL) {
         ESP_LOGI(MQTT_TAG, "JSON error");
-
         return;
     }
     //请求方式
@@ -1994,18 +1978,9 @@ uint8_t mqtt_send_publish_json(TOPIC_TYPE topic_type,cJSON *root)
     // 转成无格式字符串(最小长度)
     char *json_str = cJSON_PrintUnformatted(root);
     if (!json_str) return 0;
-    
-    // 
-    // static char data_buf[MQTT_SEND_RECV_DATA_BUF_SIZE] = {0};
-    // static mqtt_send_msg_t send_msg={0};
 
-    // memcpy(data_buf,json_str,strlen(json_str));
-    // data_buf[strlen(json_str)]='\0';
     ESP_LOGI(MQTT_TAG, "ack:%d topic=%s",strlen(json_str),topic);
-    
 
-    // memcpy(send_msg.topic,topic,strlen(topic));
-    
     if(mqtt_dev_report_queue_handle==NULL)
     {
         return 0;
@@ -2026,8 +2001,9 @@ uint8_t mqtt_send_publish_json(TOPIC_TYPE topic_type,cJSON *root)
         ESP_LOGI(MQTT_TAG, "faile \n");
         return 0;
     }else{
-        return 1;
         ESP_LOGI(MQTT_TAG, "OK\n");
+        return 1;
+        
     }
     
 }
@@ -2064,12 +2040,12 @@ cJSON* create_publish_ack_json(const char* msgId,const char* service_id, const c
     {
         return NULL;
     }
-    static char pid_str[8] = {0};
-    snprintf(pid_str, sizeof(pid_str)-1, "%04X", common_setting.mcu_info.pid);
+    char pid_str[8] = {0};
+    snprintf(pid_str, sizeof(pid_str)-1, "0x%04X", common_setting.mcu_info.pid);
 
     // 
     time_t Time_Sec = Get_Time_Stamp();
-    static char time_str[24] = {0};
+    char time_str[24] = {0};
     int64_to_str((int64_t)Time_Sec,time_str,sizeof(time_str)-1);
 
     cJSON_AddStringToObject(root, "msgId", msgId);
@@ -2109,12 +2085,12 @@ cJSON* create_publish_post_json(const char* msgId,const char* service_id, const
     }else{
         return NULL;
     }
-    static char pid_str[8] = {0};
-    snprintf(pid_str, sizeof(pid_str)-1, "%04X", common_setting.mcu_info.pid);
+    char pid_str[8] = {0};
+    snprintf(pid_str, sizeof(pid_str)-1, "0x%04X", common_setting.mcu_info.pid);
 
     // 
     time_t Time_Sec = Get_Time_Stamp();
-    static char time_str[24] = {0};
+    char time_str[24] = {0};
     int64_to_str((int64_t)Time_Sec,time_str,sizeof(time_str)-1);
 
     cJSON_AddStringToObject(root, "msgId", msgId);
@@ -2152,12 +2128,12 @@ cJSON* pid_serialNumber_replay_json(const char* msgId,const char* service_id, co
     }else{
         return NULL;
     }
-    static char pid_str[8] = {0};
-    snprintf(pid_str, sizeof(pid_str)-1, "%04X", common_setting.mcu_info.pid);
+    char pid_str[8] = {0};
+    snprintf(pid_str, sizeof(pid_str)-1, "0x%04X", common_setting.mcu_info.pid);
 
     // 
     time_t Time_Sec = Get_Time_Stamp();
-    static char time_str[24] = {0};
+    char time_str[24] = {0};
     int64_to_str((int64_t)Time_Sec,time_str,sizeof(time_str)-1);
 
     cJSON_AddStringToObject(root, "msgId", msgId);
@@ -2178,11 +2154,6 @@ cJSON* pid_serialNumber_replay_json(const char* msgId,const char* service_id, co
     cJSON_AddNumberToObject(root, "code", code);       // 注意:code 是数字!
     return root;
 }
-typedef enum{
-    MODE_ACK=0, //应答
-    MODE_REPORT, //主动上报
-
-}ACK_POST;
 
 /*
  *函数功能:设备生成MSGID规则 dev_设备MAC后四位_时间戳
@@ -2322,7 +2293,6 @@ uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len)
             // 销毁旧包,建新包
             cJSON_Delete(root);
             root = NULL;
-            uint32_t old = 0;
             switch (mode)
             {
                 case MODE_ACK:
@@ -2442,7 +2412,8 @@ int user_esp_mqtt_client_publish(char *data,uint16_t data_len,char *topic)
         return -1;
     }
     int ret = esp_mqtt_client_publish(mqtt_client_handle,topic,\
-    data,data_len,0, 0 );
+    data,data_len,QOS, 0 );
+    ESP_LOGI(MQTT_TAG, "Pret=%d", ret);
     return ret;
 
 }
@@ -2498,7 +2469,7 @@ void Mqtt_Init_Task(void)
     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);
     
-    mqtt_connect_task_handler = xTaskCreateStatic(Mqtt_Init_Task_Cb, "mqtt_task", MQTT_CONNECT_TASK_STACK, NULL, 4, mqtt_connect_task_stack, &mqtt_connect_task_tcb);
+    mqtt_connect_task_handler = xTaskCreateStatic(Mqtt_Init_Task_Cb, "mqtt_task", MQTT_CONNECT_TASK_STACK, NULL, 7, mqtt_connect_task_stack, &mqtt_connect_task_tcb);
 }
 
 
index bc8bc56..37bf856 100644 (file)
@@ -4,6 +4,27 @@
 #define ENABLE_MQTT_FUNC 1
 #if ENABLE_MQTT_FUNC
 #define UART_REPLY_MAX_PAYLOAD_MQTT     UART_REPLY_MAX_PAYLOAD-8
+
+#define MQTT_SEND_RECV_DATA_BUF_SIZE 512
+
+#define MQTT_TOPIC  "sdk/test/python"
+#define MQTT_TOPIC_LEN    64
+#define MQTT_MAX_TRY_CONNECT_TIME   10
+#define MQTT_DATA_BUF_SIZE  MQTT_SEND_RECV_DATA_BUF_SIZE
+#define MQTT_RECV_QUEUE_LENGTH 10
+
+#define MQTT_SEND_QUEUE_LENGTH  6
+#define MQTT_SEND_MSG_MAX_LEN   MQTT_SEND_RECV_DATA_BUF_SIZE
+
+
+#define MQTT_PUT_URL_LEN 255
+
+#define MSGID_STR_MAX 64
+#define METHON_STR_MAX 64
+#define SERVICEID_STR_MAX 64
+
+#define MQTT_CONNECT_TASK_STACK            (4*1024)
+
 typedef enum{
     METHON_SERVICE_PROPERTY_SET=0,//服务属性设置下发methon
     METHON_SERVICE_PROPERTY_SET_REPLAY, //设备属性设置应答
@@ -37,28 +58,11 @@ typedef enum {
 } TOPIC_TYPE;
 
 
-#define MQTT_SEND_RECV_DATA_BUF_SIZE 512
-
-#define MQTT_TOPIC  "sdk/test/python"
-#define MQTT_TOPIC_LEN    64
-#define MQTT_MAX_TRY_CONNECT_TIME   10
-#define MQTT_DATA_BUF_SIZE  MQTT_SEND_RECV_DATA_BUF_SIZE
-#define MQTT_RECV_QUEUE_LENGTH 10
 
-#define MQTT_SEND_QUEUE_LENGTH  6
-#define MQTT_SEND_MSG_MAX_LEN   MQTT_SEND_RECV_DATA_BUF_SIZE
 
-
-#define MQTT_PUT_URL_LEN 255
-
-#define MSGID_STR_MAX 64
-#define METHON_STR_MAX 64
-#define SERVICEID_STR_MAX 64
 typedef struct {
     char msgid[MSGID_STR_MAX+1];          // msgid
-    char methon[METHON_STR_MAX+1];       // methon
     char serviceId[SERVICEID_STR_MAX+1];       // serviceId
-    
 } __attribute__((packed)) mqtt_pulish_data_t;
 extern mqtt_pulish_data_t publish_data; //发布的集合
 
@@ -81,7 +85,7 @@ typedef struct {
 
 #define MQTT_RECV_QUEUE_ITEM_SIZE   sizeof(mqtt_recv_msg_t)
 #define MQTT_SEND_QUEUE_ITEM_SIZE   sizeof(mqtt_dev_report_msg_t)
-#define MQTT_CONNECT_TASK_STACK            (4*1024)
+
 extern TaskHandle_t mqtt_connect_task_handler;
 extern StackType_t mqtt_connect_task_stack[MQTT_CONNECT_TASK_STACK];
 extern StaticTask_t mqtt_connect_task_tcb;
@@ -96,7 +100,7 @@ extern StaticQueue_t mqtt_dev_report_queue_buffer;
 
 int user_esp_mqtt_client_publish(char *data,uint16_t data_len,char *topic);
 void Mqtt_Connect_Status_Check(void);
-void Mqtt_Send_Data(void);
+void Mqtt_Report_Device_Base_Info(void);
 void Mqtt_Recv_Data(char *topic,int topic_len,char *data,int data_len);
 void Mqtt_Init_Task(void);
 void Mqtt_Disconnect(void);
index ecfe5c1..c22ff35 100644 (file)
 #include "esp_timer.h"
 #include "gap.h"
 
+/* The event group allows multiple bits for each event, but we only care about two events:
+ * - we are connected to the AP with an IP
+ * - we failed to connect after the maximum amount of retries */
+#define WIFI_CONNECTED_BIT      BIT0
+#define WIFI_FAIL_BIT           BIT1
+#define WIFI_AP_NOT_FOUND_BIT   BIT2
+#define WIFI_PWD_FAIL_BIT       BIT3
+#define WIFI_CONNECT_TIMEOUT_BIT BIT4
+
+
+#define SEC_TO_US(sec)   ((sec) * 1000000ULL)
+
+#define WIFI_SCAN_PACKET_WIFI_NUM        6
+#define DEFAULT_SCAN_LIST_SIZE          15
+
+#define WIFI_CONNECT_TIMEOUT_CNT  18 //用于配网相当于5s重连一次 总共是18*5 = 80s 现在wifi重连机制时间是5s重连一次
+
+
+typedef struct
+{
+    uint8_t cnt;
+    char ssid[32];
+    int8_t rssi;
+    uint8_t auth_mode;
+    uint8_t pairwise_cipher;
+    uint8_t group_cipher;
+    uint8_t channel;
+}WIFI_SCAN_UNIT_T;
+
+
 
 /* FreeRTOS event group to signal when we are connected*/
 static EventGroupHandle_t s_wifi_event_group;
@@ -34,22 +64,17 @@ uint8_t wifi_queue_write[WIFI_QUEUE_ITEM_SIZE] = {0};
 esp_netif_t *wifi_sta_netif = NULL;
 
 
-/* The event group allows multiple bits for each event, but we only care about two events:
- * - we are connected to the AP with an IP
- * - we failed to connect after the maximum amount of retries */
-#define WIFI_CONNECTED_BIT      BIT0
-#define WIFI_FAIL_BIT           BIT1
-#define WIFI_AP_NOT_FOUND_BIT   BIT2
-#define WIFI_PWD_FAIL_BIT       BIT3
-#define WIFI_CONNECT_TIMEOUT_BIT BIT4
-
-static const char *WIFI_TAG = "wifi station";
+static const char WIFI_TAG[] = "wifi station";
 
 static int s_retry_num = 0;
 static uint32_t wifi_connect_retry_max = 0xFFFFFFFF;//EXAMPLE_ESP_MAXIMUM_RETRY;
 
 SemaphoreHandle_t wifi_scan_mutex;
 static volatile uint8_t wifi_connect_timeout_flag = 0;  // 1=超时
+
+static esp_timer_handle_t wifi_one_timer_handle;//创建定时器handle
+static uint8_t wifi_one_timer_flag = 0;//0:未创建  1:已创建
+static uint8_t wifi_ps_flag = 0;
 /*
  * 函数功能: 获取wifi的rssi事件处理函数
 */
@@ -57,6 +82,7 @@ static void esp_bss_rssi_low_handler(void* arg, esp_event_base_t event_base, int
 {
     wifi_event_bss_rssi_low_t *event = event_data;
     common_setting.wifi_status.rssi = event->rssi;
+    // ESP_LOGE(WIFI_TAG, "esp_bss_rssi_low_handler %d\n", event->rssi);
 }
 
 /*
@@ -74,8 +100,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
             {
                 if(1)//
                 {
-                    esp_wifi_set_ps(WIFI_PS_NONE);
-                    esp_wifi_set_inactive_time(WIFI_IF_STA, 60); // 延长超时时间
+                   
                     esp_wifi_connect();
                     common_setting.wifi_status.wifi_state = 0;
                 }
@@ -200,14 +225,14 @@ void Wifi_Sta_Init(void)
     
 }
 
-#define SEC_TO_US(sec)   ((sec) * 1000000ULL)
+
 /*
  * 函数功能: wifi连接超时回调函数
  * 该函数是一个定时器回调函数,当WiFi连接超时时会被调用。在函数中,可以执行一些操作来处理连接超时的情况,例如发送网络连接超时的消息、重置WiFi连接状态等。通过调用该函数,可以实现对WiFi连接超时事件的处理,以便在连接失败时能够及时做出响应。
 */
 
-static esp_timer_handle_t wifi_one_timer_handle;//创建定时器handle
-static uint8_t wifi_one_timer_flag = 0;//0:未创建  1:已创建
+
+
 
 void Wifi_Connect_Timerout_Stop(void)
 {
@@ -222,13 +247,6 @@ void Wifi_Start(void)
 {
     memset(common_setting.wifi_config.sta.ssid, 0x00, sizeof(common_setting.wifi_config.sta.ssid));
     memset(common_setting.wifi_config.sta.password, 0x00, sizeof(common_setting.wifi_config.sta.password));
-    // memcpy(common_setting.wifi_config.sta.ssid, "H0110W", 7);
-    // memcpy(common_setting.wifi_config.sta.password, "264222264222", 13);
-    // memcpy(common_setting.wifi_config.sta.ssid, "PXN@SZoffice", 13);
-    // memcpy(common_setting.wifi_config.sta.password, "PXN201807", 10);
-    // memcpy(common_setting.wifi_config.sta.ssid, "ChinaNet-LaDe", 13);
-    // memcpy(common_setting.wifi_config.sta.password, "9596w3wq", 9);
-    // ESP_LOGI(WIFI_TAG, "[APP] Free memory  wifi len on: %d %d\n",common_setting.wifi_status.cur_ssid_len,common_setting.wifi_status.cur_pwd_len);
     if(common_setting.wifi_status.cur_ssid_len == 0 || common_setting.wifi_status.cur_pwd_len == 0)
     {
         ESP_LOGI(WIFI_TAG, "[APP] Free memory not wifi on: %" PRIu32 " bytes", esp_get_free_heap_size());
@@ -251,6 +269,13 @@ void Wifi_Start(void)
             wifi_connect_timeout_flag = 1;
         }
     }
+   
+    if(!wifi_ps_flag)
+    {
+        wifi_ps_flag=1;
+        esp_wifi_set_ps(WIFI_PS_NONE);//禁止省电 防止认证失败的问题进入 休眠之后可能认证会失败
+        esp_wifi_set_inactive_time(WIFI_IF_STA, 60); // 延长超时时间
+    }
     ESP_ERROR_CHECK(esp_wifi_start());
     common_setting.wifi_status.wifi_mode = 2;
     
@@ -267,8 +292,11 @@ void Wifi_Stop(void)
         xSemaphoreGive(wifi_scan_mutex);
         common_setting.wifi_status.wifi_mode = 0;
         common_setting.wifi_status.wifi_state = 0;
+        wifi_ps_flag=0;
     }
 }
+
+#if 0
 static void print_auth_mode(int authmode)
 {
     switch (authmode) {
@@ -386,19 +414,9 @@ static void print_cipher_type(int pairwise_cipher, int group_cipher)
     }
 }
 
-typedef struct
-{
-    uint8_t cnt;
-    char ssid[32];
-    int8_t rssi;
-    uint8_t auth_mode;
-    uint8_t pairwise_cipher;
-    uint8_t group_cipher;
-    uint8_t channel;
-}WIFI_SCAN_UNIT_T;
+#endif
+
 
-#define WIFI_SCAN_PACKET_WIFI_NUM        6
-#define DEFAULT_SCAN_LIST_SIZE          24
 /*
  * 函数功能: wifi扫描任务
  * 
@@ -460,11 +478,6 @@ void Wifi_Scan_Task(void *pvParameters)
                 break;
             }
             ESP_LOGI(WIFI_TAG, "SSID \t\t%s", ap_info[i].ssid);
-            // ESP_LOGI(WIFI_TAG, "RSSI \t\t%d", ap_info[i].rssi);
-            // print_auth_mode(ap_info[i].authmode);
-            // if (ap_info[i].authmode != WIFI_AUTH_WEP) {
-            //     print_cipher_type(ap_info[i].pairwise_cipher, ap_info[i].group_cipher);
-            // }
             ESP_LOGI(WIFI_TAG, "Channel \t\t%d\n", ap_info[i].primary);
             WIFI_SCAN_UNIT_T unit = {0};
             unit.cnt = i+1;//number = 13 i=12+1
@@ -499,7 +512,7 @@ void Wifi_Scan_Task(void *pvParameters)
                 wifi_scan_reply.content[1] = start_idx;
                 wifi_scan_reply.content[2] = cur_count;
                 wifi_scan_reply.len = HEADER + cur_count * UNIT_SIZE;
-
+                wifi_scan_reply.cmd = 0x14;
                 Ble_Pairing_Reply(wifi_scan_reply.cmd, wifi_scan_reply.content, wifi_scan_reply.len, HOST_TYPE_BLE);
                 vTaskDelay(20 / portTICK_PERIOD_MS);
 
@@ -508,7 +521,7 @@ void Wifi_Scan_Task(void *pvParameters)
                 cur_count = 0;
                 pos = HEADER + cur_count * UNIT_SIZE;
                 memset(&wifi_scan_reply, 0, sizeof(BLE_SEND_REPLY_T));
-                wifi_scan_reply.cmd = 0x14;
+                
             }
 
             // ==============================================
@@ -575,7 +588,7 @@ void Wifi_Queue_Send(uint8_t host, uint8_t enable, uint8_t net_pair_encrypt_prof
  *
 */
 
-#define WIFI_CONNECT_TIMEOUT_CNT  18 //用于配网相当于5s重连一次 总共是18*5 = 80s 现在wifi重连机制时间是5s重连一次
+
 void Wifi_Enable_Status_Check(void)
 {
     static uint8_t wifi_queue_read[WIFI_QUEUE_ITEM_SIZE] = {0}; // byte0:0:本地   2:串口   3:蓝牙     byte1:1:厂测连接   2:用户配网   3:正常连接   4:扫描wifi列表   0xFF:关闭
index 1e004b6..6a89482 100644 (file)
 #define WIFI_RSSI_THRESHOLD     (-1)
 #define WIFI_QUEUE_LENGTH         2
 #define WIFI_QUEUE_ITEM_SIZE      8
+
+#define WIFI_SCAN_TASK_STACK            (1*1024+512)
+
 extern uint8_t wifi_queue_storage[WIFI_QUEUE_LENGTH*WIFI_QUEUE_ITEM_SIZE];
 extern QueueHandle_t wifi_queue_handle;
 extern StaticQueue_t wifi_queue_buffer;
 extern uint8_t wifi_queue_write[WIFI_QUEUE_ITEM_SIZE];
 
-#define WIFI_SCAN_TASK_STACK            (1*1024+512)
+
 extern TaskHandle_t wifi_scan_task_handler;
 extern StackType_t wifi_scan_task_stack[WIFI_SCAN_TASK_STACK];
 extern StaticTask_t wifi_scan_task_tcb;
@@ -36,7 +39,6 @@ void Wifi_Stop(void);
 void Wifi_Queue_Send(uint8_t host, uint8_t enable, uint8_t net_pair_encrypt_profile);
 void Wifi_Enable_Status_Check(void);
 
-void Wifi_Connect_Timerout_Start(void);
 void Wifi_Connect_Timerout_Stop(void);
 #endif
 #endif