Skip to content

Commit 7cef2e2

Browse files
committed
Add initial IPv6 Support
1 parent 22f0577 commit 7cef2e2

File tree

8 files changed

+406
-0
lines changed

8 files changed

+406
-0
lines changed

cores/esp32/IPv6Address.cpp

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
IPv6Address.cpp - Base class that provides IPv6Address
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <Arduino.h>
21+
#include <IPv6Address.h>
22+
#include <Print.h>
23+
24+
IPv6Address::IPv6Address()
25+
{
26+
memset(_address.bytes, 0, sizeof(_address.bytes));
27+
}
28+
29+
IPv6Address::IPv6Address(const uint8_t *address)
30+
{
31+
memcpy(_address.bytes, address, sizeof(_address.bytes));
32+
}
33+
34+
IPv6Address::IPv6Address(const uint32_t *address)
35+
{
36+
memcpy(_address.bytes, (const uint8_t *)address, sizeof(_address.bytes));
37+
}
38+
39+
IPv6Address& IPv6Address::operator=(const uint8_t *address)
40+
{
41+
memcpy(_address.bytes, address, sizeof(_address.bytes));
42+
return *this;
43+
}
44+
45+
bool IPv6Address::operator==(const uint8_t* addr) const
46+
{
47+
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
48+
}
49+
50+
size_t IPv6Address::printTo(Print& p) const
51+
{
52+
size_t n = 0;
53+
for(int i = 0; i < 16; i+=2) {
54+
if(i){
55+
n += p.print(':');
56+
}
57+
n += p.printf("%02x", _address.bytes[i]);
58+
n += p.printf("%02x", _address.bytes[i+1]);
59+
60+
}
61+
return n;
62+
}
63+
64+
String IPv6Address::toString() const
65+
{
66+
char szRet[40];
67+
sprintf(szRet,"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
68+
_address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3],
69+
_address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7],
70+
_address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11],
71+
_address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]);
72+
return String(szRet);
73+
}
74+
75+
bool IPv6Address::fromString(const char *address)
76+
{
77+
//format 0011:2233:4455:6677:8899:aabb:ccdd:eeff
78+
if(strlen(address) != 39){
79+
return false;
80+
}
81+
char * pos = (char *)address;
82+
size_t i = 0;
83+
for(i = 0; i < 16; i+=2) {
84+
if(!sscanf(pos, "%2hhx", &_address.bytes[i]) || !sscanf(pos+2, "%2hhx", &_address.bytes[i+1])){
85+
return false;
86+
}
87+
pos += 5;
88+
}
89+
return true;
90+
}

cores/esp32/IPv6Address.h

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
IPv6Address.h - Base class that provides IPv6Address
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef IPv6Address_h
21+
#define IPv6Address_h
22+
23+
#include <stdint.h>
24+
#include <WString.h>
25+
#include <Printable.h>
26+
27+
// A class to make it easier to handle and pass around IP addresses
28+
29+
class IPv6Address: public Printable
30+
{
31+
private:
32+
union {
33+
uint8_t bytes[16]; // IPv4 address
34+
uint32_t dword[4];
35+
} _address;
36+
37+
// Access the raw byte array containing the address. Because this returns a pointer
38+
// to the internal structure rather than a copy of the address this function should only
39+
// be used when you know that the usage of the returned uint8_t* will be transient and not
40+
// stored.
41+
uint8_t* raw_address()
42+
{
43+
return _address.bytes;
44+
}
45+
46+
public:
47+
// Constructors
48+
IPv6Address();
49+
IPv6Address(const uint8_t *address);
50+
IPv6Address(const uint32_t *address);
51+
virtual ~IPv6Address() {}
52+
53+
bool fromString(const char *address);
54+
bool fromString(const String &address) { return fromString(address.c_str()); }
55+
56+
operator const uint8_t*() const
57+
{
58+
return _address.bytes;
59+
}
60+
operator const uint32_t*() const
61+
{
62+
return _address.dword;
63+
}
64+
bool operator==(const IPv6Address& addr) const
65+
{
66+
return (_address.dword[0] == addr._address.dword[0])
67+
&& (_address.dword[1] == addr._address.dword[1])
68+
&& (_address.dword[2] == addr._address.dword[2])
69+
&& (_address.dword[3] == addr._address.dword[3]);
70+
}
71+
bool operator==(const uint8_t* addr) const;
72+
73+
// Overloaded index operator to allow getting and setting individual octets of the address
74+
uint8_t operator[](int index) const
75+
{
76+
return _address.bytes[index];
77+
}
78+
uint8_t& operator[](int index)
79+
{
80+
return _address.bytes[index];
81+
}
82+
83+
// Overloaded copy operators to allow initialisation of IPv6Address objects from other types
84+
IPv6Address& operator=(const uint8_t *address);
85+
86+
virtual size_t printTo(Print& p) const;
87+
String toString() const;
88+
89+
friend class UDP;
90+
friend class Client;
91+
friend class Server;
92+
};
93+
94+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include "WiFi.h"
2+
3+
#define STA_SSID "nbis-test"
4+
#define STA_PASS "1234567890"
5+
#define AP_SSID "esp32"
6+
7+
static volatile bool wifi_connected = false;
8+
9+
WiFiUDP ntpClient;
10+
11+
void wifiOnConnect(){
12+
Serial.println("STA Connected");
13+
Serial.print("STA IPv4: ");
14+
Serial.println(WiFi.localIP());
15+
16+
ntpClient.begin(2390);
17+
}
18+
19+
void wifiOnDisconnect(){
20+
Serial.println("STA Disconnected");
21+
delay(1000);
22+
WiFi.begin(STA_SSID, STA_PASS);
23+
}
24+
25+
void wifiConnectedLoop(){
26+
//lets check the time
27+
const int NTP_PACKET_SIZE = 48;
28+
byte ntpPacketBuffer[NTP_PACKET_SIZE];
29+
30+
IPAddress address;
31+
WiFi.hostByName("time.nist.gov", address);
32+
memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
33+
ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
34+
ntpPacketBuffer[1] = 0; // Stratum, or type of clock
35+
ntpPacketBuffer[2] = 6; // Polling Interval
36+
ntpPacketBuffer[3] = 0xEC; // Peer Clock Precision
37+
// 8 bytes of zero for Root Delay & Root Dispersion
38+
ntpPacketBuffer[12] = 49;
39+
ntpPacketBuffer[13] = 0x4E;
40+
ntpPacketBuffer[14] = 49;
41+
ntpPacketBuffer[15] = 52;
42+
ntpClient.beginPacket(address, 123); //NTP requests are to port 123
43+
ntpClient.write(ntpPacketBuffer, NTP_PACKET_SIZE);
44+
ntpClient.endPacket();
45+
46+
delay(1000);
47+
48+
int packetLength = ntpClient.parsePacket();
49+
if (packetLength){
50+
if(packetLength >= NTP_PACKET_SIZE){
51+
ntpClient.read(ntpPacketBuffer, NTP_PACKET_SIZE);
52+
}
53+
ntpClient.flush();
54+
uint32_t secsSince1900 = (uint32_t)ntpPacketBuffer[40] << 24 | (uint32_t)ntpPacketBuffer[41] << 16 | (uint32_t)ntpPacketBuffer[42] << 8 | ntpPacketBuffer[43];
55+
//Serial.printf("Seconds since Jan 1 1900: %u\n", secsSince1900);
56+
uint32_t epoch = secsSince1900 - 2208988800UL;
57+
//Serial.printf("EPOCH: %u\n", epoch);
58+
uint8_t h = (epoch % 86400L) / 3600;
59+
uint8_t m = (epoch % 3600) / 60;
60+
uint8_t s = (epoch % 60);
61+
Serial.printf("UTC: %02u:%02u:%02u (GMT)\n", h, m, s);
62+
}
63+
64+
delay(9000);
65+
}
66+
67+
void WiFiEvent(WiFiEvent_t event){
68+
switch(event) {
69+
70+
case SYSTEM_EVENT_AP_START:
71+
//can set ap hostname here
72+
WiFi.softAPsetHostname(AP_SSID);
73+
//enable ap ipv6 here
74+
WiFi.softAPenableIpV6();
75+
break;
76+
77+
case SYSTEM_EVENT_STA_START:
78+
//set sta hostname here
79+
WiFi.setHostname(AP_SSID);
80+
break;
81+
case SYSTEM_EVENT_STA_CONNECTED:
82+
//enable sta ipv6 here
83+
WiFi.enableIpV6();
84+
break;
85+
case SYSTEM_EVENT_AP_STA_GOT_IP6:
86+
//both interfaces get the same event
87+
Serial.print("STA IPv6: ");
88+
Serial.println(WiFi.localIPv6());
89+
Serial.print("AP IPv6: ");
90+
Serial.println(WiFi.softAPIPv6());
91+
break;
92+
case SYSTEM_EVENT_STA_GOT_IP:
93+
wifiOnConnect();
94+
wifi_connected = true;
95+
break;
96+
case SYSTEM_EVENT_STA_DISCONNECTED:
97+
wifi_connected = false;
98+
wifiOnDisconnect();
99+
break;
100+
default:
101+
break;
102+
}
103+
}
104+
105+
void setup(){
106+
Serial.begin(115200);
107+
WiFi.disconnect(true);
108+
WiFi.onEvent(WiFiEvent);
109+
WiFi.mode(WIFI_MODE_APSTA);
110+
WiFi.softAP(AP_SSID);
111+
WiFi.begin(STA_SSID, STA_PASS);
112+
}
113+
114+
void loop(){
115+
if(wifi_connected){
116+
wifiConnectedLoop();
117+
}
118+
while(Serial.available()) Serial.write(Serial.read());
119+
}

libraries/WiFi/src/WiFi.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "Print.h"
2828
#include "IPAddress.h"
29+
#include "IPv6Address.h"
2930

3031
#include "WiFiType.h"
3132
#include "WiFiSTA.h"

libraries/WiFi/src/WiFiAP.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,48 @@ String WiFiAPClass::softAPmacAddress(void)
232232
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
233233
return String(macStr);
234234
}
235+
236+
/**
237+
* Get the softAP interface Host name.
238+
* @return char array hostname
239+
*/
240+
const char * WiFiAPClass::softAPgetHostname()
241+
{
242+
const char * hostname;
243+
if(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_AP, &hostname)) {
244+
return NULL;
245+
}
246+
return hostname;
247+
}
248+
249+
/**
250+
* Set the softAP interface Host name.
251+
* @param hostname pointer to const string
252+
* @return true on success
253+
*/
254+
bool WiFiAPClass::softAPsetHostname(const char * hostname)
255+
{
256+
return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_AP, hostname) == 0;
257+
}
258+
259+
/**
260+
* Enable IPv6 on the softAP interface.
261+
* @return true on success
262+
*/
263+
bool WiFiAPClass::softAPenableIpV6()
264+
{
265+
return tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_AP) == 0;
266+
}
267+
268+
/**
269+
* Get the softAP interface IPv6 address.
270+
* @return IPv6Address softAP IPv6
271+
*/
272+
IPv6Address WiFiAPClass::softAPIPv6()
273+
{
274+
static ip6_addr_t addr;
275+
if(tcpip_adapter_get_ip6_linklocal(TCPIP_ADAPTER_IF_AP, &addr)) {
276+
return IPv6Address();
277+
}
278+
return IPv6Address(addr.addr);
279+
}

libraries/WiFi/src/WiFiAP.h

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class WiFiAPClass
4545

4646
IPAddress softAPIP();
4747

48+
bool softAPenableIpV6();
49+
IPv6Address softAPIPv6();
50+
51+
const char * softAPgetHostname();
52+
bool softAPsetHostname(const char * hostname);
53+
4854
uint8_t* softAPmacAddress(uint8_t* mac);
4955
String softAPmacAddress(void);
5056

0 commit comments

Comments
 (0)