mirror of
https://github.com/esphome/esphome.git
synced 2025-10-01 17:42:22 +01:00
Refactor ip address representation (#5252)
This commit is contained in:
@@ -437,9 +437,9 @@ bool WiFiComponent::wifi_sta_ip_config_(optional<ManualIP> manual_ip) {
|
||||
}
|
||||
|
||||
esp_netif_ip_info_t info; // struct of ip4_addr_t with ip, netmask, gw
|
||||
info.ip.addr = static_cast<uint32_t>(manual_ip->static_ip);
|
||||
info.gw.addr = static_cast<uint32_t>(manual_ip->gateway);
|
||||
info.netmask.addr = static_cast<uint32_t>(manual_ip->subnet);
|
||||
info.ip = manual_ip->static_ip;
|
||||
info.gw = manual_ip->gateway;
|
||||
info.netmask = manual_ip->subnet;
|
||||
err = esp_netif_dhcpc_stop(s_sta_netif);
|
||||
if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
|
||||
ESP_LOGV(TAG, "esp_netif_dhcpc_stop failed: %s", esp_err_to_name(err));
|
||||
@@ -452,12 +452,12 @@ bool WiFiComponent::wifi_sta_ip_config_(optional<ManualIP> manual_ip) {
|
||||
}
|
||||
|
||||
esp_netif_dns_info_t dns;
|
||||
if (uint32_t(manual_ip->dns1) != 0) {
|
||||
dns.ip.u_addr.ip4.addr = static_cast<uint32_t>(manual_ip->dns1);
|
||||
if (manual_ip->dns1.is_set()) {
|
||||
dns.ip = manual_ip->dns1;
|
||||
esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_MAIN, &dns);
|
||||
}
|
||||
if (uint32_t(manual_ip->dns2) != 0) {
|
||||
dns.ip.u_addr.ip4.addr = static_cast<uint32_t>(manual_ip->dns2);
|
||||
if (manual_ip->dns2.is_set()) {
|
||||
dns.ip = manual_ip->dns2;
|
||||
esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_BACKUP, &dns);
|
||||
}
|
||||
|
||||
@@ -471,9 +471,10 @@ network::IPAddress WiFiComponent::wifi_sta_ip() {
|
||||
esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGV(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
// TODO: do something smarter
|
||||
// return false;
|
||||
}
|
||||
return {ip.ip.addr};
|
||||
return network::IPAddress(&ip.ip);
|
||||
}
|
||||
|
||||
bool WiFiComponent::wifi_apply_hostname_() {
|
||||
@@ -769,13 +770,13 @@ bool WiFiComponent::wifi_ap_ip_config_(optional<ManualIP> manual_ip) {
|
||||
|
||||
esp_netif_ip_info_t info;
|
||||
if (manual_ip.has_value()) {
|
||||
info.ip.addr = static_cast<uint32_t>(manual_ip->static_ip);
|
||||
info.gw.addr = static_cast<uint32_t>(manual_ip->gateway);
|
||||
info.netmask.addr = static_cast<uint32_t>(manual_ip->subnet);
|
||||
info.ip = manual_ip->static_ip;
|
||||
info.gw = manual_ip->gateway;
|
||||
info.netmask = manual_ip->subnet;
|
||||
} else {
|
||||
info.ip.addr = static_cast<uint32_t>(network::IPAddress(192, 168, 4, 1));
|
||||
info.gw.addr = static_cast<uint32_t>(network::IPAddress(192, 168, 4, 1));
|
||||
info.netmask.addr = static_cast<uint32_t>(network::IPAddress(255, 255, 255, 0));
|
||||
info.ip = network::IPAddress(192, 168, 4, 1);
|
||||
info.gw = network::IPAddress(192, 168, 4, 1);
|
||||
info.netmask = network::IPAddress(255, 255, 255, 0);
|
||||
}
|
||||
|
||||
err = esp_netif_dhcpc_stop(s_sta_netif);
|
||||
@@ -792,12 +793,12 @@ bool WiFiComponent::wifi_ap_ip_config_(optional<ManualIP> manual_ip) {
|
||||
|
||||
dhcps_lease_t lease;
|
||||
lease.enable = true;
|
||||
network::IPAddress start_address = info.ip.addr;
|
||||
start_address[3] += 99;
|
||||
lease.start_ip.addr = static_cast<uint32_t>(start_address);
|
||||
network::IPAddress start_address = network::IPAddress(&info.ip);
|
||||
start_address += 99;
|
||||
lease.start_ip = start_address;
|
||||
ESP_LOGV(TAG, "DHCP server IP lease start: %s", start_address.str().c_str());
|
||||
start_address[3] += 100;
|
||||
lease.end_ip.addr = static_cast<uint32_t>(start_address);
|
||||
start_address += 100;
|
||||
lease.end_ip = start_address;
|
||||
ESP_LOGV(TAG, "DHCP server IP lease end: %s", start_address.str().c_str());
|
||||
err = esp_netif_dhcps_option(s_sta_netif, ESP_NETIF_OP_SET, ESP_NETIF_REQUESTED_IP_ADDRESS, &lease, sizeof(lease));
|
||||
|
||||
@@ -855,7 +856,7 @@ bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
|
||||
network::IPAddress WiFiComponent::wifi_soft_ap_ip() {
|
||||
esp_netif_ip_info_t ip;
|
||||
esp_netif_get_ip_info(s_sta_netif, &ip);
|
||||
return {ip.ip.addr};
|
||||
return network::IPAddress(&ip.ip);
|
||||
}
|
||||
bool WiFiComponent::wifi_disconnect_() { return esp_wifi_disconnect(); }
|
||||
|
||||
@@ -907,7 +908,7 @@ network::IPAddress WiFiComponent::wifi_subnet_mask_() {
|
||||
ESP_LOGW(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
|
||||
return {};
|
||||
}
|
||||
return {ip.netmask.addr};
|
||||
return network::IPAddress(&ip.netmask);
|
||||
}
|
||||
network::IPAddress WiFiComponent::wifi_gateway_ip_() {
|
||||
esp_netif_ip_info_t ip;
|
||||
@@ -916,15 +917,11 @@ network::IPAddress WiFiComponent::wifi_gateway_ip_() {
|
||||
ESP_LOGW(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
|
||||
return {};
|
||||
}
|
||||
return {ip.gw.addr};
|
||||
return network::IPAddress(&ip.gw);
|
||||
}
|
||||
network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
|
||||
const ip_addr_t *dns_ip = dns_getserver(num);
|
||||
#if LWIP_IPV6
|
||||
return {dns_ip->u_addr.ip4.addr};
|
||||
#else
|
||||
return {dns_ip->addr};
|
||||
#endif
|
||||
return network::IPAddress(dns_ip);
|
||||
}
|
||||
|
||||
} // namespace wifi
|
||||
|
Reference in New Issue
Block a user