Page 1 of 1

Why esp_netif_get_ip_info() does not seem to be working

Posted: Mon Dec 07, 2020 10:09 am
by CharlesSeartech
Hello,

I'm trying to get the IP settings of a softAP access point.
Using the examples\wifi\getting_started\softAP\main\softap_example_main.c I added a wifi_event_handler() and waited for WIFI_EVENT_AP_STACONNECTED before calling esp_netif_get_ip_info(). Unfortunately I get the following result:
I (13483) wifi softAP: AP IP:192.168.4.1, mask:192.168.4.1, gw:192.168.4.1
using the following code:
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "Connected");

esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(esp_netif_ap, &ip_info);
ESP_LOGI(TAG, "AP IP:%s, mask:%s, gw:%s", ip4addr_ntoa((ip4_addr_t*)&ip_info.ip),
ip4addr_ntoa((ip4_addr_t*)&ip_info.netmask),
ip4addr_ntoa((ip4_addr_t*)&ip_info.gw));
}

I then added an ip_event_handler() and retried waiting until I got the IP_EVENT_AP_STAIPASSIGNED event but get the same result:
I (13523) wifi softAP: AP Assigned IP:192.168.4.1, mask:192.168.4.1, gw:192.168.4.1
using the following code:
if (bits & IP_AP_STAIPASSIGNED) {
esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(esp_netif_ap, &ip_info);
ESP_LOGI(TAG, "AP Assigned IP:%s, mask:%s, gw:%s", ip4addr_ntoa((ip4_addr_t*)&ip_info.ip),
ip4addr_ntoa((ip4_addr_t*)&ip_info.netmask),
ip4addr_ntoa((ip4_addr_t*)&ip_info.gw));
}

Can someone please let me know why the netmask is 192.168.4.1 and not a netmask representation like 255.255.255.0 at least?
I understand that the gateway is 192.168.4.1 as that is the AP address.

The reason I require the info is that we supply the network information when the device is a station but if the device is set-up as an AP (softAP) then I don't get the information to supply to the user.

Best regards,
Charles

Re: Why esp_netif_get_ip_info() does not seem to be working

Posted: Mon Dec 07, 2020 10:28 am
by boarchuz
ip4addr_ntoa uses an internal buffer, overwriting the previous result each time. You're giving ESP_LOGI the same pointer x3 to this buffer.

If all you need is to dump the addresses over serial, the IPSTR / IP2STR macros are convenient and efficient, eg:
https://github.com/espressif/esp-idf/bl ... ers.c#L101

If you really need the string representations to stick around you can use ip4addr_ntoa_r with your own buffers.

Re: Why esp_netif_get_ip_info() does not seem to be working

Posted: Mon Dec 07, 2020 11:30 am
by CharlesSeartech
Great, thanks boarchuz.

The downfalls of copy and paste!!!

Regards,
Charles