From e58bccf42c93e975001cf08381cbb596f0682a2a Mon Sep 17 00:00:00 2001 From: Xavier horwood <39760456+Xavierhorwood@users.noreply.github.com> Date: Fri, 23 May 2025 05:24:48 +1000 Subject: [PATCH 1/2] Add IPv6 support trunk fmt --- src/mesh/wifi/WiFiAPClient.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/mesh/wifi/WiFiAPClient.cpp b/src/mesh/wifi/WiFiAPClient.cpp index 789f8ac44..8aff5c52f 100644 --- a/src/mesh/wifi/WiFiAPClient.cpp +++ b/src/mesh/wifi/WiFiAPClient.cpp @@ -306,6 +306,24 @@ bool initWifi() } #ifdef ARCH_ESP32 +#if ESP_ARDUINO_VERSION <= ESP_ARDUINO_VERSION_VAL(3, 0, 0) +// Most of the next 12 lines of code are adapted from espressif/arduino-esp32 +// Licensed under the GNU Lesser General Public License v2.1 +// https://github.com/espressif/arduino-esp32/blob/1f038677eb2eaf5e9ca6b6074486803c15468bed/libraries/WiFi/src/WiFiSTA.cpp#L755 +esp_netif_t *get_esp_interface_netif(esp_interface_t interface); +IPv6Address GlobalIPv6() +{ + esp_ip6_addr_t addr; + if (WiFiGenericClass::getMode() == WIFI_MODE_NULL) { + return IPv6Address(); + } + if (esp_netif_get_ip6_global(get_esp_interface_netif(ESP_IF_WIFI_STA), &addr)) { + return IPv6Address(); + } + return IPv6Address(addr.addr); +} +#endif + // Called by the Espressif SDK to static void WiFiEvent(WiFiEvent_t event) { @@ -327,6 +345,13 @@ static void WiFiEvent(WiFiEvent_t event) break; case ARDUINO_EVENT_WIFI_STA_CONNECTED: LOG_INFO("Connected to access point"); +#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0) + WiFi.enableIPv6(); +#else + if (!WiFi.enableIpV6()) { + LOG_WARN("Failed to enable IPv6"); + } +#endif break; case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: LOG_INFO("Disconnected from WiFi access point"); @@ -349,7 +374,8 @@ static void WiFiEvent(WiFiEvent_t event) LOG_INFO("Obtained Local IP6 address: %s", WiFi.linkLocalIPv6().toString().c_str()); LOG_INFO("Obtained GlobalIP6 address: %s", WiFi.globalIPv6().toString().c_str()); #else - LOG_INFO("Obtained IP6 address: %s", WiFi.localIPv6().toString().c_str()); + LOG_INFO("Obtained Local IP6 address: %s", WiFi.localIPv6().toString().c_str()); + LOG_INFO("Obtained GlobalIP6 address: %s", GlobalIPv6().toString().c_str()); #endif break; case ARDUINO_EVENT_WIFI_STA_LOST_IP: @@ -474,4 +500,4 @@ uint8_t getWifiDisconnectReason() { return wifiDisconnectReason; } -#endif \ No newline at end of file +#endif From 935c4360ddc83425a3b142312f7c9468ed8dce47 Mon Sep 17 00:00:00 2001 From: Xavier horwood <39760456+Xavierhorwood@users.noreply.github.com> Date: Fri, 23 May 2025 18:33:39 +1000 Subject: [PATCH 2/2] add Config Option --- src/mesh/Default.h | 1 + src/mesh/NodeDB.cpp | 6 ++++++ src/mesh/wifi/WiFiAPClient.cpp | 10 ++++++---- userPrefs.jsonc | 1 + 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/mesh/Default.h b/src/mesh/Default.h index 208f992c8..3fcbe50c7 100644 --- a/src/mesh/Default.h +++ b/src/mesh/Default.h @@ -22,6 +22,7 @@ #define min_node_info_broadcast_secs 60 * 60 // No regular broadcasts of more than once an hour #define min_neighbor_info_broadcast_secs 4 * 60 * 60 #define default_map_publish_interval_secs 60 * 60 +#define default_network_ipv6_enabled false #define default_mqtt_address "mqtt.meshtastic.org" #define default_mqtt_username "meshdev" diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 28af7d308..05a987e9c 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -654,6 +654,12 @@ void NodeDB::installDefaultConfig(bool preserveKey = false) strncpy(config.network.wifi_psk, USERPREFS_NETWORK_WIFI_PSK, sizeof(config.network.wifi_psk)); #endif +#if defined(ARCH_ESP32) && defined(USERPREFS_NETWORK_IPV6_ENABLED) + config.network.ipv6_enabled = USERPREFS_NETWORK_IPV6_ENABLED; +#else + config.network.ipv6_enabled = default_network_ipv6_enabled; +#endif + #ifdef DISPLAY_FLIP_SCREEN config.display.flip_screen = true; #endif diff --git a/src/mesh/wifi/WiFiAPClient.cpp b/src/mesh/wifi/WiFiAPClient.cpp index 8aff5c52f..e336f8766 100644 --- a/src/mesh/wifi/WiFiAPClient.cpp +++ b/src/mesh/wifi/WiFiAPClient.cpp @@ -345,13 +345,15 @@ static void WiFiEvent(WiFiEvent_t event) break; case ARDUINO_EVENT_WIFI_STA_CONNECTED: LOG_INFO("Connected to access point"); + if (config.network.ipv6_enabled) { #if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0) - WiFi.enableIPv6(); + WiFi.enableIPv6(); #else - if (!WiFi.enableIpV6()) { - LOG_WARN("Failed to enable IPv6"); - } + if (!WiFi.enableIpV6()) { + LOG_WARN("Failed to enable IPv6"); + } #endif + } break; case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: LOG_INFO("Disconnected from WiFi access point"); diff --git a/userPrefs.jsonc b/userPrefs.jsonc index a349a5700..6ddf7f6de 100644 --- a/userPrefs.jsonc +++ b/userPrefs.jsonc @@ -51,5 +51,6 @@ // "USERPREFS_MQTT_ENCRYPTION_ENABLED": "true", // "USERPREFS_MQTT_TLS_ENABLED": "false", // "USERPREFS_MQTT_ROOT_TOPIC": "event/REPLACEME", + // "USERPREFS_NETWORK_IPV6_ENABLED": "1", "USERPREFS_TZ_STRING": "tzplaceholder " }