Skip to content

Commit 4d739b9

Browse files
committed
Network and mDNS fixes
1 parent 4393270 commit 4d739b9

File tree

4 files changed

+259
-140
lines changed

4 files changed

+259
-140
lines changed

wled00/src/dependencies/network/Network.cpp

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "esp_netif.h"
44
#include "lwip/dns.h"
55
#include "lwip/netdb.h"
6+
#include "mdns.h"
67

78
IPAddress NetworkClass::localIP() {
89
esp_netif_ip_info_t ip_info;
@@ -19,17 +20,45 @@ IPAddress NetworkClass::localIP() {
1920

2021
// Try primary interface first
2122
if (primary && esp_netif_get_ip_info(primary, &ip_info) == ESP_OK && ip_info.ip.addr != 0) {
23+
mdns_netif_action(primary, MDNS_EVENT_DISABLE_IP6);
24+
mdns_netif_action(secondary, MDNS_EVENT_DISABLE_IP4);
25+
mdns_netif_action(secondary, MDNS_EVENT_DISABLE_IP6);
2226
return IPAddress(ip_info.ip.addr);
2327
}
2428

2529
// Fall back to secondary interface
2630
if (secondary && esp_netif_get_ip_info(secondary, &ip_info) == ESP_OK && ip_info.ip.addr != 0) {
31+
mdns_netif_action(secondary, MDNS_EVENT_DISABLE_IP6);
32+
mdns_netif_action(primary, MDNS_EVENT_DISABLE_IP4);
33+
mdns_netif_action(primary, MDNS_EVENT_DISABLE_IP6);
2734
return IPAddress(ip_info.ip.addr);
2835
}
2936

3037
return INADDR_NONE;
3138
}
3239

40+
IPAddress NetworkClass::getWiFiIP() {
41+
esp_netif_ip_info_t ip_info;
42+
esp_netif_t* wifi_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
43+
44+
if (wifi_netif && esp_netif_get_ip_info(wifi_netif, &ip_info) == ESP_OK && ip_info.ip.addr != 0) {
45+
return IPAddress(ip_info.ip.addr);
46+
} else {
47+
return INADDR_NONE;
48+
}
49+
}
50+
51+
IPAddress NetworkClass::getEthernetIP() {
52+
esp_netif_ip_info_t ip_info;
53+
esp_netif_t* eth_netif = esp_netif_get_handle_from_ifkey("ETH_DEF");
54+
55+
if (eth_netif && esp_netif_get_ip_info(eth_netif, &ip_info) == ESP_OK && ip_info.ip.addr != 0) {
56+
return IPAddress(ip_info.ip.addr);
57+
} else {
58+
return INADDR_NONE;
59+
}
60+
}
61+
3362
IPAddress NetworkClass::softAPIP() {
3463
esp_netif_ip_info_t ip_info;
3564
esp_netif_t* ap_netif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
@@ -77,18 +106,6 @@ void NetworkClass::localMAC(uint8_t* MAC) {
77106
return;
78107
}
79108

80-
// bool NetworkClass::isConnected() {
81-
// esp_netif_t* netif = esp_netif_get_default_netif();
82-
// if (netif == NULL) {
83-
// return false;
84-
// }
85-
// esp_netif_ip_info_t ip_info;
86-
// if (esp_netif_get_ip_info(netif, &ip_info) == ESP_OK) {
87-
// return (ip_info.ip.addr != 0);
88-
// }
89-
// return false;
90-
// }
91-
92109
bool NetworkClass::isConnected() {
93110
esp_netif_t* netif = esp_netif_get_default_netif();
94111
if (netif == NULL) {
@@ -137,20 +154,21 @@ String NetworkClass::format_mac_address(const uint8_t* mac) {
137154
}
138155

139156
esp_err_t NetworkClass::get_hardware_mac_address(uint8_t* mac_addr) {
157+
140158
// This gets the MAC from the hardware, before any network service init happens.
141159
esp_err_t err = ESP_FAIL;
160+
142161
#if defined(WLED_USE_ETHERNET)
143-
if (eth_handle != NULL) {
144-
// Investigate esp_efuse_mac_get_default() in case we don't even need the eth_handle.
145-
// Just need to check with one this returns, assuming Ethernet on the P4.
146-
err = esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
147-
}
162+
err = esp_read_mac(mac_addr, ESP_MAC_ETH);
148163
if (err == ESP_OK) {
149164
return ESP_OK;
150165
}
151-
#elif !defined(WLED_USE_ETHERNET_ONLY)
152-
err = esp_wifi_get_mac(WIFI_IF_STA, mac_addr);
153166
#endif
167+
err = esp_read_mac(mac_addr, ESP_MAC_WIFI_STA);
168+
if (err == ESP_OK) {
169+
return ESP_OK;
170+
}
171+
USER_PRINTLN("Failed to read MAC");
154172
return err;
155173
}
156174

@@ -178,6 +196,21 @@ bool NetworkClass::isEthernet() {
178196
return false;
179197
}
180198

199+
bool NetworkClass::isWiFi() {
200+
esp_netif_t* default_netif = esp_netif_get_default_netif();
201+
if (default_netif == NULL) {
202+
return false; // No default interface is active
203+
}
204+
esp_netif_t* wifi_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
205+
esp_netif_t* eth_netif = esp_netif_get_handle_from_ifkey("ETH_DEF");
206+
if (default_netif == wifi_netif) {
207+
return true;
208+
} else if (default_netif == eth_netif) {
209+
return false;
210+
}
211+
return false;
212+
}
213+
181214
bool NetworkClass::setHostname(const char* hostname) {
182215
esp_netif_t* netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
183216
if (netif == NULL) {

wled00/src/dependencies/network/Network.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ class NetworkClass
1919
IPAddress softAPIP();
2020
IPAddress subnetMask();
2121
IPAddress gatewayIP();
22+
IPAddress getWiFiIP();
23+
IPAddress getEthernetIP();
2224
void localMAC(uint8_t* MAC);
2325
bool isConnected();
2426
bool isEthernet();
27+
bool isWiFi();
2528
IPAddress hostByName(const char* hostname);
2629
String format_mac_address(const uint8_t* mac);
2730
esp_err_t get_hardware_mac_address(uint8_t* mac_addr);

0 commit comments

Comments
 (0)