'netif' 的 ESP32 存储大小未知,如何解决此错误?
ESP32 storage size of 'netif' isn't known, how to fix this error?
您好,我正在尝试将此代码块添加到 ESP-IDF 提供的示例之一的主线中 运行(用箭头表示)
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);
}
错误日志如下:
../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
我想做的是;让 esp32 直接通过 wifi 相互通信,这样我就可以通过 Wireshark 看到发生了什么。
netif 在 example_connect() 中初始化
和 esp_err_t esp_netif_get_netif_impl_name(&netif, "STA");调用相同的实现名称,以便可以在其上完成一些工作
如有任何意见或答复,我将不胜感激。谢谢!
这个错误的根本原因是struct esp_netif_t
是netif 实现中的私有类型。你不应该创建任何这种类型的对象,只传递句柄(指针)给它。
我怀疑您误解了 esp_netif_get_netif_impl_name()
的作用。根据 API doc 它需要一个 esp_netif_t
的句柄和 returns 与此接口对应的名称。
您似乎首先要检索您的界面句柄。为此,(相当简洁的)API 文档建议了一些其他功能,例如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(...)
用于迭代接口。
根据 this forum post 你可能想尝试这样的事情:
esp_netif_t* netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
if (netif) {
esp_netif_dhcpc_stop(netif);
// ... rest of your code
}
您好,我正在尝试将此代码块添加到 ESP-IDF 提供的示例之一的主线中 运行(用箭头表示)
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);
}
错误日志如下:
../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
我想做的是;让 esp32 直接通过 wifi 相互通信,这样我就可以通过 Wireshark 看到发生了什么。
netif 在 example_connect() 中初始化 和 esp_err_t esp_netif_get_netif_impl_name(&netif, "STA");调用相同的实现名称,以便可以在其上完成一些工作
如有任何意见或答复,我将不胜感激。谢谢!
这个错误的根本原因是struct esp_netif_t
是netif 实现中的私有类型。你不应该创建任何这种类型的对象,只传递句柄(指针)给它。
我怀疑您误解了 esp_netif_get_netif_impl_name()
的作用。根据 API doc 它需要一个 esp_netif_t
的句柄和 returns 与此接口对应的名称。
您似乎首先要检索您的界面句柄。为此,(相当简洁的)API 文档建议了一些其他功能,例如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(...)
用于迭代接口。
根据 this forum post 你可能想尝试这样的事情:
esp_netif_t* netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
if (netif) {
esp_netif_dhcpc_stop(netif);
// ... rest of your code
}