I took station example as reference and add piece regarding ARPing. Having same failure.
Code: Select all
/* WiFi station Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "esp_event.h"
#include "esp_log.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "lwip/etharp.h"
#include "nvs_flash.h"
#include <string.h>
#include "lwip/err.h"
#include "lwip/sys.h"
esp_netif_t *netif;
EventBits_t bits;
/* The examples use WiFi configuration that you can set via project
configuration menu
If you'd rather not, just change the below entries to strings with
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
#define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
#if CONFIG_ESP_WPA3_SAE_PWE_HUNT_AND_PECK
#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_HUNT_AND_PECK
#define EXAMPLE_H2E_IDENTIFIER ""
#elif CONFIG_ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT
#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_HASH_TO_ELEMENT
#define EXAMPLE_H2E_IDENTIFIER CONFIG_ESP_WIFI_PW_ID
#elif CONFIG_ESP_WPA3_SAE_PWE_BOTH
#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_BOTH
#define EXAMPLE_H2E_IDENTIFIER CONFIG_ESP_WIFI_PW_ID
#endif
#if CONFIG_ESP_WIFI_AUTH_OPEN
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
#elif CONFIG_ESP_WIFI_AUTH_WEP
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
#elif CONFIG_ESP_WIFI_AUTH_WPA_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA2_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA_WPA2_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA3_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
#elif CONFIG_ESP_WIFI_AUTH_WPA2_WPA3_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
#elif CONFIG_ESP_WIFI_AUTH_WAPI_PSK
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
#endif
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
/* 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
static const char *TAG = "wifi station";
static int s_retry_num = 0;
static void event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT &&
event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG, "connect to the AP fail");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void wifi_init_sta(void) {
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
netif = esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(
WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
wifi_config_t wifi_config = {
.sta =
{
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
/* Authmode threshold resets to WPA2 as default if password
* matches WPA2 standards (pasword len => 8). If you want to
* connect the device to deprecated WEP/WPA networks, Please set
* the threshold value to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set
* the password with length and format matching to
* WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
*/
.threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
.sae_pwe_h2e = ESP_WIFI_SAE_MODE,
.sae_h2e_identifier = EXAMPLE_H2E_IDENTIFIER,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_sta finished.");
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or
* connection failed for the maximum number of re-tries (WIFI_FAIL_BIT). The
* bits are set by event_handler() (see above) */
bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE, pdFALSE, portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we
* can test which event actually happened. */
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap");
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
}
void send_arp(ip4_addr_t *ip_ptr) {
// tcpip_adapter_get_netif(0, &t_netif);
// struct netif * netif_interface = (struct netif *) netif;
// ESP_LOGI(TAG, "out %d", netif_interface->hwaddr_len);
// if (netif_interface->hwaddr_len == 6)
// {
// esp_netif_t * esp_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
// if (esp_netif)
// {
// esp_netif_t * t_netif = esp_netif_get_netif_impl_index(esp_netif);
esp_err_t retval = etharp_request((struct netif *)netif, ip_ptr);
// arp requst
if (retval == ESP_OK) {
ESP_LOGI(TAG, "retval %d", retval);
}
// }
// netif * netif = netif_list;
// while (netif)
// {
// if (netif->hwaddr_len == 6)
// {
// etharp_request((char *) (netif), (char *) netif_ip4_addr(netif));
// }
// netif = netif->next;
// }
}
static void send_arp_request(char *cut_ip_ptr) {
uint8_t sent_count = 0;
for (uint8_t i = 0; i < 255; i++) {
char curr_ip_str[17];
ip4_addr_t curr_ip4;
sprintf(curr_ip_str, "%s%d", cut_ip_ptr, i);
ip4addr_aton(curr_ip_str, &curr_ip4);
if (bits & WIFI_CONNECTED_BIT) {
// arp requst
send_arp(&curr_ip4);
}
vTaskDelay(60000 / portTICK_PERIOD_MS);
}
}
void query_task(void *params) {
while (1) {
char arp_src[17];
sprintf(arp_src, "10.0.0.");
send_arp_request(arp_src);
vTaskDelay(60000 / portTICK_PERIOD_MS);
}
}
void app_main(void) {
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta();
xTaskCreate(query_task, "QUERY", 4096, NULL, tskIDLE_PRIORITY, NULL);
}
Terminal Output
Code: Select all
I (1689) esp_netif_handlers: sta ip: 10.0.0.130, mask: 255.255.255.0, gw: 10.0.0.1
I (1689) wifi station: got ip:10.0.0.130
I (1689) wifi station: connected to ap
I (1689) main_task: Returned from app_main()
assert failed: etharp_raw /IDF/components/lwip/lwip/src/core/ipv4/etharp.c:1145 (netif->hwaddr_len must be the same as ETH_HWADDR_LEN for etharp!)
Core 0 register dump:
MEPC : 0x40380700 RA : 0x40385aec SP : 0x3fca7bf0 GP : 0x3fc90a00
TP : 0x3fc895a8 T0 : 0x37363534 T1 : 0x7271706f T2 : 0x33323130
S0/FP : 0x00000035 S1 : 0x00000001 A0 : 0x3fca7c2c A1 : 0x3fc91d11
A2 : 0x00000001 A3 : 0x00000029 A4 : 0x00000001 A5 : 0x3fc97000
A6 : 0x7a797877 A7 : 0x76757473 S2 : 0x00000009 S3 : 0x3fca7d92
S4 : 0x3fc91d10 S5 : 0x3fc9ccdc S6 : 0x3fc9ccd8 S7 : 0x3fc9cdb0
S8 : 0x3c098974 S9 : 0x00000001 S10 : 0x00000000 S11 : 0x00000000
Stack dump detected
0x40380700: panic_abort at /home/test/Documents/esp-idf-v5.1.2/components/esp_system/panic.c:452
0x40385aec: __ubsan_include at /home/test/Documents/esp-idf-v5.1.2/components/esp_system/ubsan.c:313
T3 : 0x6e6d6c6b T4 : 0x6a696867 T5 : 0x66656463 T6 : 0x62613938
MSTATUS : 0x00001881 MTVEC : 0x40380001 MCAUSE : 0x00000007 MTVAL : 0x00000000
0x40380001: _vector_table at ??:?
MHARTID : 0x00000000
Backtrace:
panic_abort (details=details@entry=0x3fca7c2c "assert failed: etharp_raw /IDF/components/lwip/lwip/src/core/ipv4/etharp.c:1145 (netif->hwaddr_len must be the same as ETH_HWADDR_LEN for etharp!)") at /home/test/Documents/esp-idf-v5.1.2/components/esp_system/panic.c:452
452 *((volatile int *) 0) = 0; // NOLINT(clang-analyzer-core.NullDereference) should be an invalid operation on targets
#0 panic_abort (details=details@entry=0x3fca7c2c "assert failed: etharp_raw /IDF/components/lwip/lwip/src/core/ipv4/etharp.c:1145 (netif->hwaddr_len must be the same as ETH_HWADDR_LEN for etharp!)") at /home/test/Documents/esp-idf-v5.1.2/components/esp_system/panic.c:452
#1 0x40385aec in esp_system_abort (details=details@entry=0x3fca7c2c "assert failed: etharp_raw /IDF/components/lwip/lwip/src/core/ipv4/etharp.c:1145 (netif->hwaddr_len must be the same as ETH_HWADDR_LEN for etharp!)") at /home/test/Documents/esp-idf-v5.1.2/components/esp_system/port/esp_system_chip.c:84
#2 0x4038cb9c in __assert_func (file=file@entry=0x3c098608 "", line=line@entry=1145, func=<optimized out>, func@entry=0x3c098830 <__func__.3> "", expr=expr@entry=0x3c09870c "") at /home/test/Documents/esp-idf-v5.1.2/components/newlib/assert.c:81
#3 0x420106a6 in etharp_raw (netif=0x3fc9ccd8, ethsrc_addr=0x3fc9cdb0, ethdst_addr=0x3c098974 <ethbroadcast>, hwsrc_addr=0x3fc9cdb0, ipsrc_addr=0x3fc9ccdc, hwdst_addr=0x3c09896c <ethzero>, ipdst_addr=ipdst_addr@entry=0x3fca7d88, opcode=opcode@entry=1) at /home/test/Documents/esp-idf-v5.1.2/components/lwip/lwip/src/core/ipv4/etharp.c:1145
#4 0x420106c8 in etharp_request_dst (netif=<optimized out>, ipaddr=ipaddr@entry=0x3fca7d88, hw_dst_addr=<optimized out>) at /home/test/Documents/esp-idf-v5.1.2/components/lwip/lwip/src/core/ipv4/etharp.c:1199
#5 0x4201085c in etharp_request (netif=<optimized out>, ipaddr=ipaddr@entry=0x3fca7d88) at /home/test/Documents/esp-idf-v5.1.2/components/lwip/lwip/src/core/ipv4/etharp.c:1217
#6 0x420082ea in send_arp (ip_ptr=ip_ptr@entry=0x3fca7d88) at /home/test/Documents/esp-idf-v5.1.2/examples/wifi/getting_started/station/main/station_example_main.c:170
#7 0x42008350 in send_arp_request (cut_ip_ptr=cut_ip_ptr@entry=0x3fca7dbc "10.0.0.") at /home/test/Documents/esp-idf-v5.1.2/examples/wifi/getting_started/station/main/station_example_main.c:197
#8 0x42008394 in query_task (params=<error reading variable: value has been optimized out>) at /home/test/Documents/esp-idf-v5.1.2/examples/wifi/getting_started/station/main/station_example_main.c:206
#9 0x403882a0 in vPortTaskWrapper (pxCode=<optimized out>, pvParameters=<optimized out>) at /home/test/Documents/esp-idf-v5.1.2/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c:202
ELF file SHA256: 66f40d2887a83879
Rebooting...
ESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0xc (SPI_FAST_FLASH_BOOT)
Saved PC:0x40048b82
0x40048b82: software_reset in ROM
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5820,len:0x170c
load:0x403cc710,len:0x968
load:0x403ce710,len:0x2f9c
entry 0x403cc710
MicroController wrote: ↑Wed Apr 03, 2024 10:42 pm
ray1050 wrote: ↑Wed Apr 03, 2024 10:23 pm
I added a flag check set when wifi connects and check before calling query task. It has same issue.
Probably because the flag check doesn't work the way you think it would. I suggest following the code from the WiFi examples, setting the event bits in the event handler, using a big-enough timeout value, and checking the result of xEventGroupWaitBits().