ARP request is crashing on esp32c3
Posted: Wed Apr 03, 2024 9:01 pm
I want to send ARP request and it's crashing at etharp_request() function call.
If I comment ARP function, it does not crash and can successful connection to WiFi.
Tried with both ESP IDF 5.1.2 and 5.1.1.
ESP32c3
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!)
Need to add SSID, PASS and update the ip address for ARP
update: add wifi_connected_flag flag
If I comment ARP function, it does not crash and can successful connection to WiFi.
Tried with both ESP IDF 5.1.2 and 5.1.1.
ESP32c3
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!)
Need to add SSID, PASS and update the ip address for ARP
update: add wifi_connected_flag flag
Code: Select all
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_system.h"
#include "esp_task_wdt.h"
#include "esp_wifi.h"
#include "esp_wifi_types.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "lwip/err.h"
#include "lwip/etharp.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "nvs_flash.h"
#include <string.h>
#include <wifi_provisioning/manager.h>
#include <wifi_provisioning/scheme_ble.h>
#define TAG "app_main"
esp_netif_t * netif;
static EventGroupHandle_t s_wifi_event_group;
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
//Insert SSID & pass
#define SSID ""
#define PASS ""
#define MAC_ADDR_LEN 6
bool wifi_connected_flag = false;
void send_arp(ip4_addr_t * ip_ptr)
{
esp_err_t retval = etharp_request((struct netif *) netif, ip_ptr);
if (retval == ESP_OK)
{
ESP_LOGI(TAG, "retval %d", retval);
}
}
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);
// arp requst
send_arp(&curr_ip4);
vTaskDelay(60000 / portTICK_PERIOD_MS);
}
}
void query_task(void * params)
{
while (1)
{
char arp_src[17];
// change according ex1) 10.0.0.1 -> 10.0.0, ex2) 192.168.1.1 -> 192.168.1
sprintf(arp_src, "10.0.0."); //TODO: make it dynamic based on gateway
send_arp_request(arp_src);
vTaskDelay(60000 / portTICK_PERIOD_MS);
}
}
static void wifi_event_handler(void * arg, esp_event_base_t event_base, int32_t event_id,
void * event_data)
{
if (event_base == WIFI_EVENT)
{
switch (event_id)
{
case WIFI_EVENT_STA_START: {
wifi_connected_flag = true;
esp_wifi_connect();
break;
}
case WIFI_EVENT_STA_DISCONNECTED: {
esp_wifi_connect();
break;
}
default:
break;
}
}
}
/**
* @brief Initialize wifi station mode
*
*/
void init_wifi_sta()
{
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();
char hostname[14]; // custom string esp_XX_XX_XX
memset(hostname, 0, sizeof(hostname));
uint8_t mac_addr[MAC_ADDR_LEN];
esp_efuse_mac_get_default(&mac_addr[0]);
sprintf(hostname, "esp_%02X%02X%02X", mac_addr[3], mac_addr[4], mac_addr[5]);
ESP_LOGI(TAG, "self hostname set to %s", hostname);
// specific hostname not set yet, get_hostname should fail
esp_err_t test_err = esp_netif_set_hostname(netif, hostname);
if (test_err != ESP_OK)
{
ESP_LOGW(TAG, "Unable to set hostname due to error 0x%02X, using default %s", test_err,
CONFIG_LWIP_LOCAL_HOSTNAME);
}
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
ESP_ERROR_CHECK(
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
wifi_country_t country_config = {
.cc = "01",
.schan = 1,
.nchan = 11,
.policy = WIFI_COUNTRY_POLICY_AUTO,
};
wifi_config_t wifi_config = {
.sta =
{
.ssid = SSID,
.password = PASS,
},
};
ESP_ERROR_CHECK(esp_wifi_set_country(&country_config));
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());
/* 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) */
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE,
255);
}
void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init()); // initializing NVS (Non-Volatile Storage)
init_wifi_sta();
while(wifi_connected_flag == false)
{
//wait util wifi connects
}
xTaskCreate(query_task, "QUERY", 4096, NULL, tskIDLE_PRIORITY, NULL);
}