This is a function that gets the IP address provided an interface, It works as expected but only the fist time it executes, as soon as the device connects to an access point and receives an address it returns garbage.
uint32_t WifiController::getIpv4() {
esp_netif_ip_info_t output;
if (const auto error = esp_netif_get_ip_info(this->station_netif, &output); error != ESP_OK) {
ESP_LOGE(this->debuggingTag, "Ip address retrieval failed, error: %d", (uint8_t)error);
return 0;
}
return output.ip.addr;
}
Furthermore if I make the function const
which should not be a problem since no modification of any class value is taking place, the chip crashes entirely when it gets to that point.
I’m assumed this happens due to the function esp_netif_get_ip_info()
modifying the supplied station_netif
but as seen bellow it doesn’t.
esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info) {
ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif);
if (esp_netif == NULL || ip_info == NULL) {
return ESP_ERR_INVALID_ARG;
}
struct netif *p_netif = esp_netif->lwip_netif;
if (p_netif != NULL && netif_is_up(p_netif)) {
ip4_addr_set(&ip_info->ip, ip_2_ip4(&p_netif->ip_addr));
ip4_addr_set(&ip_info->netmask, ip_2_ip4(&p_netif->netmask));
ip4_addr_set(&ip_info->gw, ip_2_ip4(&p_netif->gw));
return ESP_OK;
}
memcpy(ip_info, esp_netif->ip_info, sizeof(esp_netif_ip_info_t));
return ESP_OK;
}
- Does anyone know what could be causing the
esp_netif_get_ip_info()
to return garbage? - What could be causing the function to crash when it’s declared constant?
Clarifications
- The function is declared constant like this
uint32_t WifiController::getIpv4() const {/* code */}
Any and all feedback on code is welcome 🙂