Hi I'm trying to get this code block to run in the mainline of one of the examples provided with ESP-IDF (denoted by arrows)
void app_main(void)
{
ESP_ERROR_CHECK( nvs_flash_init() );
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*/
ESP_ERROR_CHECK(example_connect());
// VVV
esp_netif_t netif;
esp_err_t esp_netif_get_netif_impl_name(&netif, "STA");
esp_netif_dhcpc_stop(&netif);
esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(&netif, &ip_info);
esp_netif_set_ip4_addr(&ip_info.netmask, 255,255,255,255);
esp_netif_set_ip_info(&netif, &ip_info);
esp_netif_destroy(&netif);
// ^^^
ESP_LOGI(TAG, "IP4 address set successfuly");
xTaskCreate(&http_get_task, "http_get_task", 4096, NULL, 5, NULL);
}
Here are the error logs:
../main/http_request_example_main.c: In function 'app_main':
../main/http_request_example_main.c:141:17: error: storage size of 'netif' isn't known
esp_netif_t netif;
^~~~~
../main/http_request_example_main.c:142:45: error: expected declaration specifiers or '...' before '&' token
esp_err_t esp_netif_get_netif_impl_name(&netif, "STA");
^
../main/http_request_example_main.c:142:53: error: expected declaration specifiers or '...' before string constant
esp_err_t esp_netif_get_netif_impl_name(&netif, "STA");
^~~~~
../main/http_request_example_main.c:141:17: warning: unused variable 'netif' [-Wunused-variable]
esp_netif_t netif;
^~~~~
ninja: build stopped: subcommand failed.
ninja failed with exit code 1
What I'm attempting to do is; get the esp32's to talk to each other through wifi directly so that I can see what's happening through Wireshark.
The netif is initialised in example_connect() and esp_err_t esp_netif_get_netif_impl_name(&netif, "STA"); calls that same implementation name so that some work can be done on it
I will appreciate any comments or answers. Thank you!
The root cause of this error is that the struct esp_netif_t
is a private type in the netif implementation. You're not supposed to create any objects of this type, only pass around handles (pointers) to it.
I suspect you've misunderstood what esp_netif_get_netif_impl_name()
does. According to the API doc it takes a handle to a esp_netif_t
and returns the name corresponding to this interface.
What you seem to be after is retrieving the handle to your interface in the first place. For this the (rather terse) API doc suggest a few other functions, e.g. esp_netif_get_handle_from_ifkey(...)
for searching for interfaces using something called interface key (no idea, sorry, but google helps there) or esp_netif_next(...)
for iterating over interfaces.
According to this forum post you might want to try something like this:
esp_netif_t* netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
if (netif) {
esp_netif_dhcpc_stop(netif);
// ... rest of your code
}