Page 1 of 1

how to convert esp_ip4_addr_t to ip_addr_t

Posted: Sat Apr 10, 2021 4:04 pm
by korstiaan
Hi,

I want to ping my gateway.
I get my gateway address from the event handler (IP_EVENT_STA_GOT_IP) and is of type

Code: Select all

esp_ip4_addr_t gw
.
But to use the ping I need a target of type

Code: Select all

ip_addr_t target
.

Can somebody tell me how to assing the gateway ip address from

Code: Select all

esp_ip4_addr_t gw
to

Code: Select all

ip_addr_t target
?

Code: Select all

target.u_addr = ...?
target.type = ...?
Thanks for helping.

Re: how to convert esp_ip4_addr_t to ip_addr_t

Posted: Mon Apr 18, 2022 5:56 am
by nopnop2002
Convert from esp_ip4_addr_t to ip_addr_t:

Code: Select all

// get current STA information
esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info);

ip_addr_t gw_addr;
memcpy((char *)&gw_addr.u_addr.ip4, (char *)&ip_info.gw, sizeof(ip4_addr));
gw_addr.type = IPADDR_TYPE_V4;
ESP_LOGI(TAG, "ip4addr_ntoa(gw_addr.u_addr.ip4)=%s", ip4addr_ntoa(&gw_addr.u_addr.ip4));