Skip to content

Commit 718fd11

Browse files
Add IPv6 Support for esp32 (meshtastic#6866)
* Update Default.h * Update NodeDB.cpp * Update WiFiAPClient.cpp * Update userPrefs.jsonc * set ipv6 to off by default * Trunk fix --------- Co-authored-by: Tom Fifield <tom@tomfifield.net>
1 parent 75f7ded commit 718fd11

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/mesh/Default.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#else
3030
#define default_ringtone_nag_secs 15
3131
#endif
32+
#define default_network_ipv6_enabled false
3233

3334
#define default_mqtt_address "mqtt.meshtastic.org"
3435
#define default_mqtt_username "meshdev"

src/mesh/NodeDB.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,12 @@ void NodeDB::installDefaultConfig(bool preserveKey = false)
718718
strncpy(config.network.wifi_psk, USERPREFS_NETWORK_WIFI_PSK, sizeof(config.network.wifi_psk));
719719
#endif
720720

721+
#if defined(USERPREFS_NETWORK_IPV6_ENABLED)
722+
config.network.ipv6_enabled = USERPREFS_NETWORK_IPV6_ENABLED;
723+
#else
724+
config.network.ipv6_enabled = default_network_ipv6_enabled;
725+
#endif
726+
721727
#ifdef DISPLAY_FLIP_SCREEN
722728
config.display.flip_screen = true;
723729
#endif

src/mesh/wifi/WiFiAPClient.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,23 @@ bool initWifi()
334334
}
335335

336336
#ifdef ARCH_ESP32
337+
#if ESP_ARDUINO_VERSION <= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
338+
// Most of the next 12 lines of code are adapted from espressif/arduino-esp32
339+
// Licensed under the GNU Lesser General Public License v2.1
340+
// https://github.com/espressif/arduino-esp32/blob/1f038677eb2eaf5e9ca6b6074486803c15468bed/libraries/WiFi/src/WiFiSTA.cpp#L755
341+
esp_netif_t *get_esp_interface_netif(esp_interface_t interface);
342+
IPv6Address GlobalIPv6()
343+
{
344+
esp_ip6_addr_t addr;
345+
if (WiFiGenericClass::getMode() == WIFI_MODE_NULL) {
346+
return IPv6Address();
347+
}
348+
if (esp_netif_get_ip6_global(get_esp_interface_netif(ESP_IF_WIFI_STA), &addr)) {
349+
return IPv6Address();
350+
}
351+
return IPv6Address(addr.addr);
352+
}
353+
#endif
337354
// Called by the Espressif SDK to
338355
static void WiFiEvent(WiFiEvent_t event)
339356
{
@@ -355,6 +372,17 @@ static void WiFiEvent(WiFiEvent_t event)
355372
break;
356373
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
357374
LOG_INFO("Connected to access point");
375+
if (config.network.ipv6_enabled) {
376+
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
377+
if (!WiFi.enableIPv6()) {
378+
LOG_WARN("Failed to enable IPv6");
379+
}
380+
#else
381+
if (!WiFi.enableIpV6()) {
382+
LOG_WARN("Failed to enable IPv6");
383+
}
384+
#endif
385+
}
358386
#ifdef WIFI_LED
359387
digitalWrite(WIFI_LED, HIGH);
360388
#endif
@@ -383,7 +411,8 @@ static void WiFiEvent(WiFiEvent_t event)
383411
LOG_INFO("Obtained Local IP6 address: %s", WiFi.linkLocalIPv6().toString().c_str());
384412
LOG_INFO("Obtained GlobalIP6 address: %s", WiFi.globalIPv6().toString().c_str());
385413
#else
386-
LOG_INFO("Obtained IP6 address: %s", WiFi.localIPv6().toString().c_str());
414+
LOG_INFO("Obtained Local IP6 address: %s", WiFi.localIPv6().toString().c_str());
415+
LOG_INFO("Obtained GlobalIP6 address: %s", GlobalIPv6().toString().c_str());
387416
#endif
388417
break;
389418
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
@@ -514,4 +543,4 @@ uint8_t getWifiDisconnectReason()
514543
{
515544
return wifiDisconnectReason;
516545
}
517-
#endif // HAS_WIFI
546+
#endif // HAS_WIFI

userPrefs.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@
5656
// "USERPREFS_MQTT_ROOT_TOPIC": "event/REPLACEME",
5757
// "USERPREFS_RINGTONE_NAG_SECS": "60",
5858
"USERPREFS_RINGTONE_RTTTL": "24:d=32,o=5,b=565:f6,p,f6,4p,p,f6,p,f6,2p,p,b6,p,b6,p,b6,p,b6,p,b,p,b,p,b,p,b,p,b,p,b,p,b,p,b,1p.,2p.,p",
59+
// "USERPREFS_NETWORK_IPV6_ENABLED": "1",
5960
"USERPREFS_TZ_STRING": "tzplaceholder "
6061
}

0 commit comments

Comments
 (0)