wifi.c中函数分析: :::int wifi_connect_to_supplicant()

本文介绍如何检查WPA Supplicant服务是否运行,获取Wi-Fi接口名称,判断配置文件是否存在,初始化两个socket连接,并进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /* Make sure supplicant is running */

查看属性,保存在supp_status

判断是否运行:否返回-1

    if (!property_get(SUPP_PROP_NAME, supp_status, NULL)
            || strcmp(supp_status, "running") != 0) {
        LOGE("Supplicant not running, cannot connect");
        return -1;
    }

获得属性:wifi.interface值,保存在iface中

在该环境中:iface=wlan0

    property_get("wifi.interface", iface, WIFI_TEST_INTERFACE);


判断文件IFACE_DIR = "/data/system/wpa_supplicant";是否存在,

    存在:ifname="/data/system/wpa_supplicant/wlan0";

    不存在:ifname=wlan0

if (access(IFACE_DIR, F_OK) == 0) {
        snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface);
    } else {
        strlcpy(ifname, iface, sizeof(ifname));
    }

创建两个socket,并链接由ifname,生成的des.sun_path

 ctrl_conn = wpa_ctrl_open(ifname);

  monitor_conn = wpa_ctrl_open(ifname);

测试链接是否成功:

if (wpa_ctrl_attach(monitor_conn) != 0) {
        wpa_ctrl_close(monitor_conn);
        wpa_ctrl_close(ctrl_conn);
        ctrl_conn = monitor_conn = NULL;
        return -1;
    }
    return 0;

wpa_ctrl_open()

wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)

在wifi.c中:传入参数

ctrl_path=ifname="/data/system/wpa_supplicant/wlan0";

也就是wpa_supplicant的soket。

如果定义android

则生成自己的soket文件路径组合如下:

%s/%s%d-%d", local_socket_dir, local_socket_prefix, getpid(), counter++);

其中:local_socket_dir= "/data/misc/wifi/sockets";

local_socket_prefix = "wpa_ctrl_";

在wpa_ctrl.c中定义。

生成后绑定本地soket文件,修改权限及所属组。



--- 0x420133f2: omac1_aes_128 at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c:920 --- 0x420135d6: ccmp_decrypt at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/src/crypto/ccmp.c:141 --- 0x42013684: ccmp_encrypt at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/src/crypto/ccmp.c:193 --- 0x42012f3e: esp_aes_gmac at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/src/crypto/crypto_ops.c:24 --- 0x42013060: sha256_vector at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c:89 3fc9e3c0: 0x0000000a 0x00000020 0x00000001 0x00000000 0x00000020 0x00000000 0x00000005 0x00000000 3fc9e3e0: 0x00000000 0x00000001 0x00000001 0x00000000 0x00000001 0x00000000 0x00000006 0x00000000 3fc9e400: 0x000002f0 0x00000020 0x000000a1 0x00000000 0x00001801 0x00000007 0x00000001 0x40380c00 --- 0x40380c00: esp_ptr_in_rtc_dram_fast at E:/espidf/v5.4.1/esp-idf/components/esp_hw_support/include/esp_memory_utils.h:121 --- (inlined by) dram_alloc_to_iram_addr at E:/espidf/v5.4.1/esp-idf/components/heap/heap_caps_base.c:41 --- (inlined by) heap_caps_aligned_alloc_base at E:/espidf/v5.4.1/esp-idf/components/heap/heap_caps_base.c:150 3fc9e420: 0x1f2f3f4f 0x00001800 0x77770006 0x77777777 0x6c6c756e 0x69666977 0x00000000 0x00000000 3fc9e440: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000008 0x38000000 0x38383838 0x00383838 3fc9e460: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e480: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x38383838 0x38383838 3fc9e4a0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e4c0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000005 0x3c0d8554 3fc9e4e0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x3c0c4000 0x420b35a8 --- 0x420b35a8: main_task at E:/espidf/v5.4.1/esp-idf/components/freertos/app_startup.c:209 3fc9e500: 0x00000000 0x00001388 0x00000001 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e520: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 3fc9e540: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0x00000150 0x3fc9dd80 0x00000002 0x3fc957c4 3fc9e560: 0x3fc957c4 0x3fc9e554 0x3fc957bc 0x00000018 0x3fca4640 0x3fca4640 0x3fc9e554 0x00000000 3fc9e580: 0x00000001 0x3fc9d550 0x6e69616d 0x00000000 0x00000000 0x00000000 0x3fc9e540 0x00000001 3fc9e5a0: 0x00000000 0x3fca2a90 0x42004e7c 0x00000000 0x3fc9be80 0x3fc9bee8 0x3fc9bf50 0x00000000 --- 0x42004e7c: pthread_cleanup_thread_specific_data_callback at E:/espidf/v5.4.1/esp-idf/components/pthread/pthread_local_storage.c:126 3fc9e5c0: 0x00000000 0x00000001 0x00000000 0x00000000 0x00000000 0x42004706 0x00000000 0x00000000 --- 0x42004706: esp_cleanup_r at E:/espidf/v5.4.1/esp-idf/components/newlib/newlib_init.c:43 ELF file SHA256: 04a1caf88 Rebooting... ESP-ROM:esp32c3-api1-20210207 Build:Feb 7 2021 rst:0xc (RTC_SW_CPU_RST),boot:0xd (SPI_FAST_FLASH_BOOT) Saved PC:0x403807f4 --- 0x403807f4: esp_restart_noos at E:/espidf/v5.4.1/esp-idf/components/esp_system/port/soc/esp32c3/system_internal.c:112 SPIWP:0xee mode:DIO, clock div:1 load:0x3fcd5820,len:0x1574 load:0x403cc710,len:0xc30 load:0x403ce710,len:0x2f64 entry 0x403cc71a I (24) boot: ESP-IDF v5.4.1 2nd stage bootloader I (24) boot: compile time Jun 27 2025 16:28:05 I (24) boot: chip revision: v0.4 I (25) boot: efuse block revision: v1.3 I (27) boot.esp32c3: SPI Speed : 80MHz I (31) boot.esp32c3: SPI Mode : DIO I (35) boot.esp32c3: SPI Flash Size : 2MB I (39) boot: Enabling RNG early entropy source... I (43) boot: Partition Table: I (46) boot: ## Label Usage Type ST Offset Length I (52) boot: 0 nvs WiFi data 01 02 00009000 00006000 I (59) boot: 1 phy_init RF data 01 01 0000f000 00001000 I (65) boot: 2 factory factory app 00 00 00010000 00100000 I (72) boot: End of partition table I (75) esp_image: segment 0: paddr=00010020 vaddr=3c0c0020 size=22a38h (141880) map I (105) esp_image: segment 1: paddr=00032a60 vaddr=3fc92800 size=02df8h ( 11768) load I (108) esp_image: segment 2: paddr=00035860 vaddr=40380000 size=0a7b8h ( 42936) load I (117) esp_image: segment 3: paddr=00040020 vaddr=42000020 size=b4088h (737416) map I (235) esp_image: segment 4: paddr=000f40b0 vaddr=4038a7b8 size=07f0ch ( 32524) load I (241) esp_image: segment 5: paddr=000fbfc4 vaddr=50000200 size=0001ch ( 28) load I (248) boot: Loaded app from partition at offset 0x10000 I (248) boot: Disabling RNG early entropy source... I (260) cpu_start: Unicore app I (268) cpu_start: Pro cpu start user code I (268) cpu_start: cpu freq: 160000000 Hz I (269) app_init: Application information: I (269) app_init: Project name: internal_communication I (274) app_init: App version: 1 I (277) app_init: Compile time: Jun 27 2025 16:27:06 I (282) app_init: ELF file SHA256: 04a1caf88... I (286) app_init: ESP-IDF: v5.4.1 I (290) efuse_init: Min chip rev: v0.3 I (294) efuse_init: Max chip rev: v1.99 I (298) efuse_init: Chip rev: v0.4 I (302) heap_init: Initializing. RAM available for dynamic allocation: I (308) heap_init: At 3FC9B840 len 000247C0 (145 KiB): RAM I (313) heap_init: At 3FCC0000 len 0001C710 (113 KiB): Retention RAM I (319) heap_init: At 3FCDC710 len 00002950 (10 KiB): Retention RAM I (325) heap_init: At 5000021C len 00001DCC (7 KiB): RTCRAM I (331) spi_flash: detected chip: generic I (334) spi_flash: flash io: dio W (337) spi_flash: Detected size(8192k) larger than the size in the binary image header(2048k). Using the size in the binary image header. I (350) sleep_gpio: Configure to isolate all GPIO pins in sleep state I (356) sleep_gpio: Enable automatic switching of GPIO sleep configuration I (362) main_task: Started on CPU0 I (362) main_task: Calling app_main() I (372) pp: pp rom version: 9387209 I (372) net80211: net80211 rom version: 9387209 I (382) wifi:wifi driver task: 3fca44c0, prio:23, stack:6656, core=0 I (382) wifi:wifi firmware version: 79fa3f41ba I (382) wifi:wifi certification version: v7.0 I (382) wifi:config NVS flash: enabled I (392) wifi:config nano formatting: disabled I (392) wifi:Init data frame dynamic rx buffer num: 32 I (402) wifi:Init static rx mgmt buffer num: 5 I (402) wifi:Init management short buffer num: 32 I (402) wifi:Init dynamic tx buffer num: 32 I (412) wifi:Init static tx FG buffer num: 2 I (412) wifi:Init static rx buffer size: 1600 I (422) wifi:Init static rx buffer num: 10 I (422) wifi:Init dynamic rx buffer num: 32 I (422) wifi_init: rx ba win: 6 I (432) wifi_init: accept mbox: 6 I (432) wifi_init: tcpip mbox: 32 I (432) wifi_init: udp mbox: 6 I (442) wifi_init: tcp mbox: 6 I (442) wifi_init: tcp tx win: 5760 I (442) wifi_init: tcp rx win: 5760 I (442) wifi_init: tcp mss: 1440 I (452) wifi_init: WiFi IRAM OP enabled I (452) wifi_init: WiFi RX IRAM OP enabled I (462) phy_init: phy_version 1200,2b7123f9,Feb 18 2025,15:22:21 I (492) phy_init: Saving new calibration data due to checksum failure or outdated calibration data, mode(0) I (522) wifi:mode : sta (34:cd:b0:97:58:bc) + softAP (34:cd:b0:97:58:bd) I (522) wifi:enable tsf I (522) wifi:Total power save buffer number: 16 I (522) wifi:Init max length of beacon: 752/752 I (522) wifi:Init max length of beacon: 752/752 I (532) mesh: <nvs>read layer:0, err:0x1102 I (532) mesh: <nvs>read assoc:0, err:0x1102 I (532) wifi:Set ps type: 1, coexist: 0 I (542) wifi:Total power save buffer number: 16 I (792) mesh: [IO]disable self-organizing<reconnect> I (792) mesh: [CONFIG]connect to router:nullwifi, 00:00:00:00:00:00 I (792) mesh_main: <MESH_EVENT_MESH_STARTED>ID:77:77:77:77:77:77 I (802) mesh_main: mesh starts successfully, heap:161708, root fixed<0>(tree), ps:1 assert failed: xTaskCreatePinnedToCore freertos_tasks_c_additions.h:163 (( ( ( ( BaseType_t ) xCoreID ) >= 0 && ( ( BaseType_t ) xCoreID ) < 1 ) ? ( ( BaseType_t ) 1 ) : ( ( BaseType_t ) 0 ) ) == ( ( Core 0 register dump: MEPC : 0x40380828 RA : 0x4038692c SP : 0x3fc9e1e0 GP : 0x3fc93000 --- 0x40380828: panic_abort at E:/espidf/v5.4.1/esp-idf/components/esp_system/panic.c:468 --- 0x4038692c: __ubsan_include at E:/espidf/v5.4.1/esp-idf/components/esp_system/ubsan.c:311 TP : 0x3fc9e540 T0 : 0x37363534 T1 : 0x7271706f T2 : 0x33323130 S0/FP : 0x0000007e S1 : 0x3fc9e3c7 A0 : 0x3fc9e238 A1 : 0x3c0c41f2 A2 : 0x0000007e A3 : 0x00000028 A4 : 0x00000001 A5 : 0x3fc9b000 A6 : 0x7a797877 A7 : 0x76757473 S2 : 0x00000000 S3 : 0x3fc9e230 S4 : 0x3fc9e238 S5 : 0x00000000 S6 : 0x00000003 S7 : 0x00000000 S8 : 0x00000000 S9 : 0x00000000 S10 : 0x00000000 S11 : 0x00000000 T3 : 0x6e6d6c6b T4 : 0x6a696867 T5 : 0x66656463 T6 : 0x62613938 MSTATUS : 0x00001881 MTVEC : 0x40380001 MCAUSE : 0x00000002 MTVAL : 0x00000000 --- 0x40380001: _vector_table at E:/espidf/v5.4.1/esp-idf/components/riscv/vectors_intc.S:54 MHARTID : 0x00000000 Stack memory: 3fc9e1e0: 0x00000000 0x00060000 0x3c0d726c 0x4038db14 0x00000000 0x00060000 0xc0380000 0x3c0c4174 --- 0x4038db14: newlib_include_assert_impl at E:/espidf/v5.4.1/esp-idf/components/newlib/assert.c:92 3fc9e200: 0x00333631 0x00000000 0x00000000 0x00000000 0x00000000 0x3fc947f0 0x3c0d726c 0x3fc94e2c 3fc9e220: 0x3c0c4258 0x3fc94800 0x3fc9e200 0x3fc94804 0x3c0c4174 0x3fc94a58 0x65737361 0x66207472 3fc9e240: 0x656c6961 0x78203a64 0x6b736154 0x61657243 0x69506574 0x64656e6e 0x6f436f54 0x66206572 3fc9e260: 0x72656572 0x5f736f74 0x6b736174 0x5f635f73 0x69646461 0x6e6f6974 0x3a682e73 0x20333631 3fc9e280: 0x28202828 0x28202820 0x73614220 0x70795465 0x20745f65 0x43782029 0x4965726f 0x20292044 3fc9e2a0: 0x30203d3e 0x20262620 0x20282028 0x65736142 0x65707954 0x2920745f 0x6f437820 0x44496572 3fc9e2c0: 0x3c202920 0x29203120 0x28203f20 0x42202820 0x54657361 0x5f657079 0x20292074 0x20292031 3fc9e2e0: 0x2028203a 0x61422028 0x79546573 0x745f6570 0x30202920 0x29202920 0x203d3d20 0x00282028 3fc9e300: 0x00000000 0x00000001 0x3c0c4a14 0x4200b2e0 0x00001000 0x3c0c5000 0x00000001 0x403893ee --- 0x4200b2e0: uart_event_task at D:/esp32_C3/internal_communication_client_0.1/main/uart.c:16 --- 0x403893ee: xTaskCreatePinnedToCore at E:/espidf/v5.4.1/esp-idf/components/freertos/esp_additions/freertos_tasks_c_additions.h:231 3fc9e320: 0x00000000 0x00000000 0x04c4b400 0x00000000 0x00000000 0x00000000 0x00000000 0x000277ac 3fc9e340: 0x00000003 0x3c0c5000 0x00000001 0x4200b510 0x0001c200 0x00000003 0x00000000 0x00000001 --- 0x4200b510: uart_init at D:/esp32_C3/internal_communication_client_0.1/main/uart.c:80 3fc9e360: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000322 0x3c0c5560 0x3c0c553c 0x4200b2b0 --- 0x4200b2b0: app_main at D:/esp32_C3/internal_communication_client_0.1/main/mesh_main.c:349 3fc9e380: 0x3c0c5560 0x00000001 0xa5a5a5a5 0xa5a5a5a5 0x3fc92b38 0x0000002c 0x00000001 0x4201306e --- 0x4201306e: hmac_sha256_vector at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c:342 3fc9e3a0: 0x420132de 0x420131c2 0x42013224 0x420133f2 0x420135d6 0x42013684 0x42012f3e 0x42013060 --- 0x420132de: pbkdf2_sha1 at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c:752 --- 0x420131c2: aes_128_cbc_encrypt at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c:445 --- 0x42013224: aes_128_cbc_decrypt at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c:467 --- 0x420133f2: omac1_aes_128 at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c:920 --- 0x420135d6: ccmp_decrypt at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/src/crypto/ccmp.c:141 --- 0x42013684: ccmp_encrypt at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/src/crypto/ccmp.c:193 --- 0x42012f3e: esp_aes_gmac at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/src/crypto/crypto_ops.c:24 --- 0x42013060: sha256_vector at E:/espidf/v5.4.1/esp-idf/components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c:89 3fc9e3c0: 0x0000000a 0x00000020 0x00000001 0x00000000 0x00000020 0x00000000 0x00000005 0x00000000 3fc9e3e0: 0x00000000 0x00000001 0x00000001 0x00000000 0x00000001 0x00000000 0x00000006 0x00000000 3fc9e400: 0x000002f0 0x00000020 0x000000a1 0x00000000 0x00001801 0x00000007 0x00000001 0x40380c00 --- 0x40380c00: esp_ptr_in_rtc_dram_fast at E:/espidf/v5.4.1/esp-idf/components/esp_hw_support/include/esp_memory_utils.h:121 --- (inlined by) dram_alloc_to_iram_addr at E:/espidf/v5.4.1/esp-idf/components/heap/heap_caps_base.c:41 --- (inlined by) heap_caps_aligned_alloc_base at E:/espidf/v5.4.1/esp-idf/components/heap/heap_caps_base.c:150 3fc9e420: 0x1f2f3f4f 0x00001800 0x77770006 0x77777777 0x6c6c756e 0x69666977 0x00000000 0x00000000 3fc9e440: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000008 0x38000000 0x38383838 0x00383838 3fc9e460: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e480: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x38383838 0x38383838 3fc9e4a0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e4c0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000005 0x3c0d8554 3fc9e4e0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x3c0c4000 0x420b35a8 --- 0x420b35a8: main_task at E:/espidf/v5.4.1/esp-idf/components/freertos/app_startup.c:209 3fc9e500: 0x00000000 0x00001388 0x00000001 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e520: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 3fc9e540: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0x00000150 0x3fc9dd80 0x00000002 0x3fc957c4 3fc9e560: 0x3fc957c4 0x3fc9e554 0x3fc957bc 0x00000018 0x3fca4640 0x3fca4640 0x3fc9e554 0x00000000 3fc9e580: 0x00000001 0x3fc9d550 0x6e69616d 0x00000000 0x00000000 0x00000000 0x3fc9e540 0x00000001 3fc9e5a0: 0x00000000 0x3fca2a90 0x42004e7c 0x00000000 0x3fc9be80 0x3fc9bee8 0x3fc9bf50 0x00000000 --- 0x42004e7c: pthread_cleanup_thread_specific_data_callback at E:/espidf/v5.4.1/esp-idf/components/pthread/pthread_local_storage.c:126 3fc9e5c0: 0x00000000 0x00000001 0x00000000 0x00000000 0x00000000 0x42004706 0x00000000 0x00000000 --- 0x42004706: esp_cleanup_r at E:/espidf/v5.4.1/esp-idf/components/newlib/newlib_init.c:43 ELF file SHA256: 04a1caf88 Rebooting... ESP-ROM:esp32c3-api1-20210207 Build:Feb 7 2021 rst:0xc (RTC_SW_CPU_RST),boot:0xd (SPI_FAST_FLASH_BOOT) Saved PC:0x403807f4 --- 0x403807f4: esp_restart_noos at E:/espidf/v5.4.1/esp-idf/components/esp_system/port/soc/esp32c3/system_internal.c:112 SPIWP:0xee mode:DIO, clock div:1 load:0x3fcd5820,len:0x1574 load:0x403cc710,len:0xc30 load:0x403ce710,len:0x2f64 entry 0x403cc71a I (24) boot: ESP-IDF v5.4.1 2nd stage bootloader I (24) boot: compile time Jun 27 2025 16:28:05 I (24) boot: chip revision: v0.4 I (25) boot: efuse block revision: v1.3 I (27) boot.esp32c3: SPI Speed : 80MHz I (31) boot.esp32c3: SPI Mode : DIO I (35) boot.esp32c3: SPI Flash Size : 2MB I (39) boot: Enabling RNG early entropy source... I (43) boot: Partition Table: I (46) boot: ## Label Usage Type ST Offset Length I (52) boot: 0 nvs WiFi data 01 02 00009000 00006000 I (59) boot: 1 phy_init RF data 01 01 0000f000 00001000 I (65) boot: 2 factory factory app 00 00 00010000 00100000 I (72) boot: End of partition table I (75) esp_image: segment 0: paddr=00010020 vaddr=3c0c0020 size=22a38h (141880) map I (105) esp_image: segment 1: paddr=00032a60 vaddr=3fc92800 size=02df8h ( 11768) load I (108) esp_image: segment 2: paddr=00035860 vaddr=40380000 size=0a7b8h ( 42936) load I (117) esp_image: segment 3: paddr=00040020 vaddr=42000020 size=b4088h (737416) map I (235) esp_image: segment 4: paddr=000f40b0 vaddr=4038a7b8 size=07f0ch ( 32524) load I (241) esp_image: segment 5: paddr=000fbfc4 vaddr=50000200 size=0001ch ( 28) load I (248) boot: Loaded app from partition at offset 0x10000 I (248) boot: Disabling RNG early entropy source... I (260) cpu_start: Unicore app I (268) cpu_start: Pro cpu start user code I (268) cpu_start: cpu freq: 160000000 Hz I (269) app_init: Application information: I (269) app_init: Project name: internal_communication I (273) app_init: App version: 1 I (277) app_init: Compile time: Jun 27 2025 16:27:06 I (282) app_init: ELF file SHA256: 04a1caf88... I (286) app_init: ESP-IDF: v5.4.1 I (290) efuse_init: Min chip rev: v0.3 I (294) efuse_init: Max chip rev: v1.99 I (298) efuse_init: Chip rev: v0.4 I (302) heap_init: Initializing. RAM available for dynamic allocation: I (308) heap_init: At 3FC9B840 len 000247C0 (145 KiB): RAM I (313) heap_init: At 3FCC0000 len 0001C710 (113 KiB): Retention RAM I (319) heap_init: At 3FCDC710 len 00002950 (10 KiB): Retention RAM I (325) heap_init: At 5000021C len 00001DCC (7 KiB): RTCRAM I (331) spi_flash: detected chip: generic I (334) spi_flash: flash io: dio W (337) spi_flash: Detected size(8192k) larger than the size in the binary image header(2048k). Using the size in the binary image header. I (350) sleep_gpio: Configure to isolate all GPIO pins in sleep state I (356) sleep_gpio: Enable automatic switching of GPIO sleep configuration I (362) main_task: Started on CPU0 I (362) main_task: Calling app_main() I (372) pp: pp rom version: 9387209 I (372) net80211: net80211 rom version: 9387209 I (382) wifi:wifi driver task: 3fca44c0, prio:23, stack:6656, core=0 I (382) wifi:wifi firmware version: 79fa3f41ba I (382) wifi:wifi certification version: v7.0 I (382) wifi:config NVS flash: enabled I (392) wifi:config nano formatting: disabled I (392) wifi:Init data frame dynamic rx buffer num: 32 I (402) wifi:Init static rx mgmt buffer num: 5 I (402) wifi:Init management short buffer num: 32 I (402) wifi:Init dynamic tx buffer num: 32 I (412) wifi:Init static tx FG buffer num: 2 I (412) wifi:Init static rx buffer size: 1600 I (422) wifi:Init static rx buffer num: 10 I (422) wifi:Init dynamic rx buffer num: 32 I (422) wifi_init: rx ba win: 6 I (432) wifi_init: accept mbox: 6 I (432) wifi_init: tcpip mbox: 32 I (432) wifi_init: udp mbox: 6 I (442) wifi_init: tcp mbox: 6 I (442) wifi_init: tcp tx win: 5760 I (442) wifi_init: tcp rx win: 5760 I (442) wifi_init: tcp mss: 1440 I (452) wifi_init: WiFi IRAM OP enabled I (452) wifi_init: WiFi RX IRAM OP enabled I (462) phy_init: phy_version 1200,2b7123f9,Feb 18 2025,15:22:21 I (492) phy_init: Saving new calibration data due to checksum failure or outdated calibration data, mode(0) I (572) wifi:mode : sta (34:cd:b0:97:58:bc) + softAP (34:cd:b0:97:58:bd) I (572) wifi:enable tsf I (572) wifi:Total power save buffer number: 16 I (572) wifi:Init max length of beacon: 752/752 I (582) wifi:Init max length of beacon: 752/752 I (582) mesh: <nvs>read layer:0, err:0x1102 I (582) mesh: <nvs>read assoc:0, err:0x1102 I (592) wifi:Set ps type: 1, coexist: 0
最新发布
06-28
<think>我们遇到了一个断言失败(assert)错误,错误发生在`freertos_tasks_c_additions.h`文件的第163行。错误信息表明在调用`xTaskCreatePinnedToCore`函数时,传入的核心ID参数无效。根据错误信息,核心ID必须大于等于0且小于1(因为ESP32-C3是单核处理器,所以核心ID只能是0)。错误信息中显示传入的核心ID是1,这超出了有效范围(0)。在ESP-IDF中,对于单核处理器(如ESP32-C3),只能使用核心0。因此,在创建任务时,必须将核心ID设置为0或`tskNO_AFFINITY`(允许任务在任何核心上运行,但这里只有一个核心)。 查看堆栈跟踪,我们看到创建任务的调用来自`uart_init`函数(位于`uart.c`的第80行),然后从`app_main`(位于`mesh_main.c`的第349行)调用。 因此,我们需要检查在`uart_init`函数中调用`xTaskCreatePinnedToCore`时,传入的核心ID参数是否正确。###解决方案1.**检查`uart_init`函数**:在`uart_init`函数中,找到创建任务的地方,并确保传入的核心ID是0(因为ESP32-C3是单核)。2.**修改代码**:将创建任务的核心ID参数改为0(或`tskNO_AFFINITY`,但通常指定0即可)。 ###代码示例假设在`uart_init`函数中创建任务的代码如下: ```cxTaskCreatePinnedToCore(uart_event_task,"uart_event_task",2048, NULL,10,&uart_event_task_handle,1); //注意最后一个参数是核心ID,这里传入了1```修改为:```cxTaskCreatePinnedToCore(uart_event_task, "uart_event_task",2048,NULL,10, &uart_event_task_handle,0); //将核心ID改为0```###解释-`xTaskCreatePinnedToCore`函数的最后一个参数是指定任务运行的核心。对于单核ESP32-C3,核心ID只能是0。 -如果传入的核心ID无效(如1),则会触发断言失败。 ###其他可能的问题如果代码中其他地方也有创建任务并指定核心ID为1的情况,也需要修改。但根据堆栈跟踪,当前问题出现在`uart_init`函数中。 ###完整步骤1.打开`uart.c`文件,找到`uart_init`函数。2.函数中找到调用`xTaskCreatePinnedToCore`的地方。 3.将核心ID参数(最后一个参数)改为0。4.重新编译并烧录程序。###预防措施在编写多核相关的代码时,可以使用宏来适配不同的芯片: ```c#if CONFIG_FREERTOS_UNICORE#defineUART_TASK_CORE_ID0#else#define UART_TASK_CORE_ID1 //或者根据需求选择#endif//然后使用xTaskCreatePinnedToCore(...,UART_TASK_CORE_ID);``` 但注意,ESP32-C3是单核,所以`CONFIG_FREERTOS_UNICORE`被定义。因此,在单核情况下,所有任务都必须运行在核心0上。###相关问题§§相关问题§§: 1. ESP32-C3的任务创建与多核处理有何不同? 2.如何根据芯片类型(单核/多核)编写可移植的任务创建代码?3.FreeRTOS中`xTaskCreatePinnedToCore`函数的参数详解。4.在ESP-IDF中,如何处理任务核心绑定错误?</think># 解决ESP32-C3任务创建断言失败问题 您遇到的断言失败错误发生在`xTaskCreatePinnedToCore`函数调用中,核心问题是尝试在单核处理器上创建任务时使用了无效的核心ID(1)。ESP32-C3是单核处理器(仅核心0),因此核心ID参数必须为0或`tskNO_AFFINITY`。 ## 问题分析 ### 错误关键点: ``` assert failed: xTaskCreatePinnedToCore freertos_tasks_c_additions.h:163 (( ( ( ( BaseType_t ) xCoreID ) >= 0 && ( ( BaseType_t ) xCoreID ) < 1 ) ? ... ) ``` ### 根本原因: - ESP32-C3是单核处理器(仅核心0) - 代码中尝试在核心1上创建任务(`xCoreID = 1`) - 调用栈显示错误发生在`uart_init`函数中创建任务的位置 ## 完整解决方案 ### 1. 修正任务创建代码(uart.c) ```c #include "freertos/FreeRTOS.h" #include "freertos/task.h" // 在uart_init函数中找到任务创建代码 void uart_init(void) { // ... 其他初始化代码 ... // 错误代码示例(可能导致断言失败): // xTaskCreatePinnedToCore(uart_event_task, "uart_task", 2048, NULL, 10, NULL, 1); // 修正为(使用核心0): BaseType_t core_id = 0; // ESP32-C3仅支持核心0 xTaskCreatePinnedToCore( uart_event_task, // 任务函数 "uart_task", // 任务名称 2048, // 堆栈大小 NULL, // 参数 configMAX_PRIORITIES - 1, // 优先级 NULL, // 任务句柄(可选) core_id // 核心ID(必须为0) ); // 或者使用tskNO_AFFINITY(推荐): xTaskCreate( uart_event_task, // 任务函数 "uart_task", // 任务名称 2048, // 堆栈大小 NULL, // 参数 configMAX_PRIORITIES - 1, // 优先级 NULL // 任务句柄(可选) ); } ``` ### 2. 添加芯片核心检测宏(通用解决方案) 在项目头文件中添加以下宏定义: ```c // chip_utils.h #pragma once #include "soc/soc_caps.h" #if SOC_CPU_CORES_NUM == 1 #define TASK_CREATE_CORE_ID tskNO_AFFINITY #else #define TASK_CREATE_CORE_ID 1 // 对于多核芯片使用核心1 #endif // 安全任务创建宏 #define SAFE_TASK_CREATE(task_func, task_name, stack_size, param, priority) \ do { \ BaseType_t err = xTaskCreate( \ task_func, \ task_name, \ stack_size, \ param, \ priority, \ NULL \ ); \ if (err != pdPASS) { \ ESP_LOGE("TASK", "Failed to create task %s", task_name); \ } \ } while(0) ``` ### 3. 在代码中使用安全任务创建 ```c #include "chip_utils.h" void app_main() { // 使用安全宏创建任务 SAFE_TASK_CREATE( uart_event_task, "uart_task", 2048, NULL, configMAX_PRIORITIES - 1 ); // 或者使用显式核心ID(仅当需要指定核心时) #if SOC_CPU_CORES_NUM > 1 xTaskCreatePinnedToCore( mesh_task, "mesh_task", 4096, NULL, configMAX_PRIORITIES - 2, NULL, 1 // 仅在多核芯片上使用核心1 ); #else xTaskCreate( mesh_task, "mesh_task", 4096, NULL, configMAX_PRIORITIES - 2, NULL ); #endif } ``` ### 4. 添加断言检查(可选但推荐) ```c // 在任务创建前添加核心ID检查 void safe_task_create_pinned_to_core( TaskFunction_t pxTaskCode, const char * const pcName, const uint32_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask, const BaseType_t xCoreID ) { // 检查核心ID是否有效 if (xCoreID >= SOC_CPU_CORES_NUM) { ESP_LOGE("TASK", "Invalid core ID %d for %s", xCoreID, pcName); xCoreID = tskNO_AFFINITY; } BaseType_t result = xTaskCreatePinnedToCore( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask, xCoreID ); if (result != pdPASS) { ESP_LOGE("TASK", "Failed to create task %s on core %d", pcName, xCoreID); } } ``` ## 关键修正点解释 ### 1. 理解ESP32-C3核心架构 - ESP32-C3是单核RISC-V处理器(仅核心0) - 尝试在核心1上创建任务会导致断言失败 - 解决方案:始终使用核心0或`tskNO_AFFINITY` ### 2. 任务创建函数区别 - `xTaskCreate()`:不指定核心,由FreeRTOS调度器分配 - `xTaskCreatePinnedToCore()`:显式指定任务运行的核心 - 在单核芯片上,两者功能等效 ### 3. SOC_CPU_CORES_NUM宏 - ESP-IDF提供此宏用于检测芯片核心数量 - 对于ESP32-C3:`SOC_CPU_CORES_NUM == 1` - 对于ESP32:`SOC_CPU_CORES_NUM == 2` ## 预防措施 ### 1. 创建任务的最佳实践 ```c // 使用此模板创建任务 xTaskCreate( task_function, // 任务函数指针 "TaskName", // 描述性任务名称(有助于调试) 3072, // 足够大的堆栈大小(根据需求调整) task_param, // 传递给任务的参数 priority, // 合理的优先级(通常5-10之间) &task_handle // 保存任务句柄以便管理 ); ``` ### 2. 堆栈大小计算 使用FreeRTOS的堆栈溢出检测功能: ```c // 在FreeRTOSConfig.h中启用堆栈溢出检测 #define configCHECK_FOR_STACK_OVERFLOW 2 // 在任务中添加堆栈检查 void task_function(void *param) { for (;;) { // 任务代码 UBaseType_t free_stack = uxTaskGetStackHighWaterMark(NULL); if (free_stack < 100) { ESP_LOGW("TASK", "Low stack: %d bytes", free_stack); } } } ``` ### 3. 优先级管理 - 避免优先级反转问题 - 系统任务优先级:`configMAX_PRIORITIES - 1` - 高优先级任务:`configMAX_PRIORITIES - 2` 到 `configMAX_PRIORITIES - 4` - 普通任务:`configMAX_PRIORITIES - 5` 及以下 ## 完整工作流程验证 1. **修改代码**:更新所有`xTaskCreatePinnedToCore`调用 2. **清理项目**: ```bash idf.py fullclean ``` 3. **编译并烧录**: ```bash idf.py build idf.py -p PORT flash ``` 4. **监控日志**: ```bash idf.py -p PORT monitor ``` ## 总结 通过以下步骤解决断言失败问题: 1. **修正任务核心分配**:在单核芯片上使用核心0或`tskNO_AFFINITY` 2. **使用安全任务创建宏**:自动适配不同芯片的核心数量 3. **添加运行时检查**:防止无效核心ID 4. **优化任务管理**:使用标准任务创建API并监控资源 这些修改确保您的代码在ESP32-C3上正确运行,同时保持与多核芯片的兼容性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值