修改wifi扫描的dump问题
authord15521469561 <1731678236@qq.com>
Thu, 23 Apr 2026 10:44:08 +0000 (18:44 +0800)
committerd15521469561 <1731678236@qq.com>
Thu, 23 Apr 2026 10:44:08 +0000 (18:44 +0800)
main/gap.h
main/gatt_svc.c
main/project_common.c
main/project_common.h
main/uart.c
main/user_http.c
main/wifi.c
main/wifi.h

index f5bf4cc..931a85c 100644 (file)
@@ -21,5 +21,6 @@
 void start_advertising(void);
 void adv_init(void);
 int gap_init(void);
+extern uint16_t ble_mtu_max;
 #endif
 #endif // GAP_SVC_H
index d0ec6cb..e346a5e 100644 (file)
@@ -103,7 +103,7 @@ static int gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
             // MODLOG_DFLT(INFO, "Notification/Indication scheduled for "
             //             "all subscribed peers.\n");
             ESP_LOGI(GATT_TAG,"write %d, %d, %d:\n", ctxt->om->om_flags, ctxt->om->om_pkthdr_len, ctxt->om->om_len);
-            for(size_t i=0;i<20;i++)
+            for(size_t i=0;i<ctxt->om->om_len;i++)
             {
                 ESP_LOGI(GATT_TAG,"%02x ", ctxt->om->om_data[i]);
             }
@@ -184,7 +184,7 @@ void send_comm_2_indication(void)
         if(xQueueReceive(ble_notify_queue_handle, &ble_notify, 0) == pdTRUE)
         {
             ESP_LOGI(GATT_TAG, "ble send");
-            ESP_LOG_BUFFER_HEX(GATT_TAG, ble_notify.data, ble_notify.length);
+            // ESP_LOG_BUFFER_HEX(GATT_TAG, ble_notify.data, ble_notify.length);
             ble_gatts_notify(comm_2_chr_conn_handle, comm_2_chr_val_handle);
         }
 
index e2e0543..a278039 100644 (file)
@@ -126,6 +126,7 @@ void Ble_Pairing_Reply(uint8_t cmd, uint8_t* pbuf, uint16_t len, uint8_t host_ty
     uint8_t crc = Crc8_ROHC_Cal(&net_pairing_reply.buffer[1], len + 3);
     net_pairing_reply.buffer[len + 4] = crc;
     ESP_LOGI(COMMON_TAG, "Ble_Pairing_Reply crc %02x len %u", crc, len);
+    // ESP_LOG_BUFFER_HEX(COMMON_TAG, net_pairing_reply.buffer, net_pairing_reply.size);
     #if ENABLE_BLE_FUNC
     xQueueSend(ble_notify_queue_handle, &net_pairing_reply, 0);
     #endif
@@ -138,11 +139,6 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
         ESP_LOGI(COMMON_TAG, "ble recv %d bytes:cmd %0x\n", len, data[1]);
         // ESP_LOG_BUFFER_HEX(COMMON_TAG, data, len);
         static BLE_SEND_REPLY_T net_pairing_reply = {0};
-        // net_pairing_reply = (BLE_SEND_REPLY_T *)pvPortMalloc(sizeof(BLE_SEND_REPLY_T));
-        // if(net_pairing_reply == NULL)        {
-        //     ESP_LOGE(COMMON_TAG, "malloc failed for net_pairing_reply");
-        //     return;
-        // }
         memset(&net_pairing_reply, 0x00, sizeof(BLE_SEND_REPLY_T));
 
         // Process
@@ -218,7 +214,6 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
             case COMMON_BLE_PID_MAC_CMD:
             {
                 net_pairing_reply.len = 8;
-                common_setting.mcu_info.pid=0xA00B;
                 memcpy(&net_pairing_reply.content[0], (uint16_t*)&common_setting.mcu_info.pid, 2);
                 esp_read_mac(&net_pairing_reply.content[2], ESP_MAC_BT);
                 
@@ -944,7 +939,8 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
     else if(data[0] == FRAMEHEAD_APP_TO_ESP)
     {
         //发送调试数据不需要crc校验
-        ESP_LOGI(COMMON_TAG, "no need checkcrc recv %d bytes:cmd %0x",len, data[1]);
+        uint8_t crc_error = Crc8_ROHC_Cal(&data[1], len-2);
+        ESP_LOGI(COMMON_TAG, "no need checkcrc recv %d bytes:cmd %0x crc_error:0x%02x",len, data[1],crc_error);
         ESP_LOG_BUFFER_HEX(COMMON_TAG, data, len);
         BLE_SEND_REPLY_T net_pairing_reply = {0};
         memset(&net_pairing_reply, 0x00, sizeof(BLE_SEND_REPLY_T));
index fe3663b..bba936c 100644 (file)
@@ -353,9 +353,11 @@ typedef struct
     // MQTT
     esp_mqtt_client_config_t mqtt_cfg;
     esp_mqtt_client_handle_t mqtt_client_handle;
+    MQTT_CONFIG_INFO_T mqtt_user_cfg;
 
+    //https ota
     HTTPS_OTA_CONFIG_INFO_T https_ota_cfg;
-    MQTT_CONFIG_INFO_T mqtt_user_cfg;
+    
 }COMMON_SETTING_T;
 
 
index 38913bd..5b53dab 100644 (file)
@@ -215,7 +215,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                         }
                         else
                         {
-                            ESP_LOGI(COMMON_TAG, "%s mcu info name len out of range, data len %d, want %d\n",__func__, len, (12 + name_len));
+                            ESP_LOGI(COMMON_TAG, "%s  data len %d, want %d\n",__func__, len, (12 + name_len));
                             name_len = 0;
                             uart_pairing_reply.content[0]=0x01;
                         }
@@ -405,7 +405,7 @@ void Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_type)
 {
     if (len > UART_REPLY_MAX_PAYLOAD-4) // 4 = header(1) + cmd(1) + len(1) + crc(1) 暂时len为1个字节
     {
-        ESP_LOGE(COMMON_TAG, "Uart_Reply_Send length too large: %d\n", len);
+        ESP_LOGE(COMMON_TAG, "Uart too large: %d\n", len);
         return;
     }
     static UART_RECV_DATA_T uart_pairing_reply = { 0 };
index f5b251b..240678c 100644 (file)
@@ -88,7 +88,7 @@ static void Http_Download_Task_Cb(void *pvParameters)
         esp_http_client_handle_t http_client = NULL; // 提前定义
         for(uint8_t retry=0;retry<HTTP_RETRY_NUMBER_MAX;retry++)
         {
-            ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "[APP] Free memory get cer on: %" PRIu32 " bytes", esp_get_free_heap_size());
+            ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "get cer on: %" PRIu32 " bytes", esp_get_free_heap_size());
             // printf("剩余堆内存: %" PRIu32, esp_get_free_heap_size());
             http_client = esp_http_client_init(&http_config);
             if(http_client == NULL)
@@ -159,7 +159,7 @@ static void Http_Download_Task_Cb(void *pvParameters)
                     }
                     else{
                         err = ESP_FAIL;
-                        ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "http filed size too large\n");
+                        ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "http filed large\n");
                     }
                 }
                 else
@@ -210,7 +210,7 @@ static void Http_Download_Task_Cb(void *pvParameters)
         xEventGroupSetBits(ble_event_group, BLE_OFF_BIT);//关闭蓝牙配网
         vTaskDelay(100);
         #endif
-        ESP_LOGI(HTTP_TAG, "download clientcer clientkey mqtt \n");
+        ESP_LOGI(HTTP_TAG, "download mqtt \n");
         common_setting.mqtt_user_cfg.mqtt_start_flag=1;
         #if ENABLE_HTTPS_OTA_FUNC
         xEventGroupSetBits(common_event_group, COMMON_HTTPS_OTA_BIT);
index f9fc73e..b70f976 100644 (file)
@@ -15,6 +15,7 @@
 #include "https_ota.h"
 #include "user_http.h"
 #include "esp_timer.h"
+#include "gap.h"
 // #include "freertos/FreeRTOS.h"
 // #include "freertos/task.h"
 // #include "freertos/event_groups.h"
@@ -491,81 +492,148 @@ typedef struct
 #define DEFAULT_SCAN_LIST_SIZE          24
 void Wifi_Scan_Task(void *pvParameters)
 {
-    // wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
-    // ESP_ERROR_CHECK(esp_wifi_init(&cfg));
-    static BLE_SEND_REPLY_T wifi_scan_reply={0};
+    static BLE_SEND_REPLY_T wifi_scan_reply = {0};
     memset(&wifi_scan_reply, 0x00, sizeof(BLE_SEND_REPLY_T));
     wifi_scan_reply.send_flag = 1;
     wifi_scan_reply.cmd = 0x14;
 
-    // uint8_t host_type = *(uint8_t*)pvParameters;
-
     uint16_t number = DEFAULT_SCAN_LIST_SIZE;
-    wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
+    static wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
     uint16_t ap_count = 0;
+
     memset(ap_info, 0, sizeof(ap_info));
     common_setting.wifi_status.wifi_mode = 1;
+
     ESP_ERROR_CHECK(esp_netif_set_hostname(wifi_sta_netif, LWIP_LOCAL_HOSTNAME));
     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
     ESP_ERROR_CHECK(esp_wifi_start());
     esp_wifi_disconnect();
-    if(esp_wifi_scan_start(NULL, true) == ESP_OK)
+
+    if (esp_wifi_scan_start(NULL, true) == ESP_OK)
     {
-        ESP_LOGI(WIFI_TAG, "Max AP number ap_info can hold = %u", number);
-        ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
-        ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
-        ESP_LOGI(WIFI_TAG, "Total APs scanned = %u, actual AP number ap_info holds = %u\n", ap_count, number);
+        esp_wifi_scan_get_ap_num(&ap_count);
+        esp_wifi_scan_get_ap_records(&number, ap_info);
+
+        if (number > DEFAULT_SCAN_LIST_SIZE)
+            number = DEFAULT_SCAN_LIST_SIZE;
+
+        const int HEADER = 3;
+        const int UNIT_SIZE = sizeof(WIFI_SCAN_UNIT_T);
+        const int MAX_PER_PACKET = 6;
+        
+        int max_payload = ble_mtu_max - 5;
+        int max_per_pkt = (max_payload - HEADER) / UNIT_SIZE;
+
+        if (max_per_pkt > MAX_PER_PACKET) max_per_pkt = MAX_PER_PACKET;
+        if (max_per_pkt <= 0) max_per_pkt = 1;
+
+        uint8_t total_wifi = number > 0xFF ? 0xFF : number;
+        uint8_t cur_count = 0;
+        uint8_t start_idx = 1;  // 从1开
+
+        memset(&wifi_scan_reply, 0, sizeof(BLE_SEND_REPLY_T));
+        wifi_scan_reply.cmd = 0x14;
 
-        // 填装配网回复
         for (int i = 0; i < number; i++)
         {
-            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);
+            if((UNIT_SIZE + 3 + 5) > ble_mtu_max)//mtu小于38+3+5 不够一包的数据退出
+            {
+                wifi_scan_reply.len = 3;
+                wifi_scan_reply.content[0] = 0;
+                wifi_scan_reply.content[1] = 0;
+                wifi_scan_reply.content[2] = 0;
+                Ble_Pairing_Reply(wifi_scan_reply.cmd, wifi_scan_reply.content, wifi_scan_reply.len, HOST_TYPE_BLE);
+                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 wifi_scan_unit = {0};
-            wifi_scan_unit.cnt = i;
-            memcpy(wifi_scan_unit.ssid, ap_info[i].ssid, sizeof(wifi_scan_unit.ssid));
-            wifi_scan_unit.rssi = ap_info[i].rssi;
-            wifi_scan_unit.auth_mode = ap_info[i].authmode;
-            wifi_scan_unit.pairwise_cipher = ap_info[i].pairwise_cipher;
-            wifi_scan_unit.group_cipher = ap_info[i].group_cipher;
-            wifi_scan_unit.channel = ap_info[i].primary;
-
-            memcpy(&wifi_scan_reply.content[3+(i%WIFI_SCAN_PACKET_WIFI_NUM)*sizeof(wifi_scan_unit.ssid)], &wifi_scan_unit, sizeof(wifi_scan_unit));
+            WIFI_SCAN_UNIT_T unit = {0};
+            unit.cnt = i+1;//number = 13 i=12+1
+           
+            memcpy(unit.ssid,ap_info[i].ssid,sizeof(unit.ssid)-1);
             
-            if((i+1) >= number || (i+1) % WIFI_SCAN_PACKET_WIFI_NUM == 0)
+            if(strlen((const char *)ap_info[i].ssid) > sizeof(unit.ssid)-1)//3132-21
             {
-                wifi_scan_reply.len = 3+((i%WIFI_SCAN_PACKET_WIFI_NUM)+1)*sizeof(wifi_scan_unit.ssid);
+                unit.ssid[sizeof(unit.ssid)-1]='\0';
+            }
+            else{
+                unit.ssid[strlen((const char *)ap_info[i].ssid)]='\0';
+            }
+            ESP_LOGI(WIFI_TAG, "unit.ssid \t\t%s", unit.ssid);
+            
+            unit.rssi = ap_info[i].rssi;
+            unit.auth_mode = ap_info[i].authmode;
+            unit.pairwise_cipher = ap_info[i].pairwise_cipher;
+            unit.group_cipher = ap_info[i].group_cipher;
+            unit.channel = ap_info[i].primary;
+
+            // 计算要存放的位置
+            int pos = HEADER + cur_count * UNIT_SIZE;
+
+            // ==============================================
+            // 关键:如果装不下 → 先把当前的包发出去
+            // ==============================================
+            if (pos + UNIT_SIZE > sizeof(wifi_scan_reply.content))
+            {
+                // 发送
+                wifi_scan_reply.content[0] = total_wifi;
+                wifi_scan_reply.content[1] = start_idx;
+                wifi_scan_reply.content[2] = cur_count;
+                wifi_scan_reply.len = HEADER + cur_count * UNIT_SIZE;
+
+                Ble_Pairing_Reply(wifi_scan_reply.cmd, wifi_scan_reply.content, wifi_scan_reply.len, HOST_TYPE_BLE);
+                vTaskDelay(20 / portTICK_PERIOD_MS);
+
+                // 
+                start_idx = i + 1;
+                cur_count = 0;
+                pos = HEADER + cur_count * UNIT_SIZE;
+                memset(&wifi_scan_reply, 0, sizeof(BLE_SEND_REPLY_T));
+                wifi_scan_reply.cmd = 0x14;
+            }
 
-                wifi_scan_reply.content[0] = number > 0xFF ? 0xFF:number;
-                wifi_scan_reply.content[1] = i/WIFI_SCAN_PACKET_WIFI_NUM;
-                wifi_scan_reply.content[2] = i%WIFI_SCAN_PACKET_WIFI_NUM;
+            // ==============================================
+            // 
+            // ==============================================
+            memcpy(&wifi_scan_reply.content[pos], &unit, UNIT_SIZE);
+            cur_count++;  // 
+
+            // ==============================================
+            // 
+            // ==============================================
+            if (cur_count >= max_per_pkt || i == number - 1)
+            {
+                wifi_scan_reply.content[0] = total_wifi;
+                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);
-                // Check_Host_Type(wifi_scan_reply.cmd, wifi_scan_reply.content, wifi_scan_reply.len, host_type);
-                memset(&wifi_scan_reply, 0x00, sizeof(BLE_SEND_REPLY_T));
+                vTaskDelay(20 / portTICK_PERIOD_MS);
+
+                // 重置
+                start_idx = i + 1;
+                cur_count = 0;
+                memset(&wifi_scan_reply, 0, sizeof(BLE_SEND_REPLY_T));
             }
         }
     }
     else
     {
-        ESP_LOGI(WIFI_TAG, "esp_wifi_scan_start fail\n");
         wifi_scan_reply.len = 3;
         wifi_scan_reply.content[0] = 0;
         wifi_scan_reply.content[1] = 0;
         wifi_scan_reply.content[2] = 0;
         Ble_Pairing_Reply(wifi_scan_reply.cmd, wifi_scan_reply.content, wifi_scan_reply.len, HOST_TYPE_BLE);
-        // Check_Host_Type(wifi_scan_reply.cmd, wifi_scan_reply.content, wifi_scan_reply.len, host_type);
     }
-    
+
     Wifi_Stop();
     vTaskDelete(NULL);
-    return;
 }
 
 void Wifi_Queue_Send(uint8_t host, uint8_t enable, uint8_t net_pair_encrypt_profile)
index b22d3bb..14d13f4 100644 (file)
@@ -19,7 +19,7 @@ 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)
+#define WIFI_SCAN_TASK_STACK            (2*1024)
 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;