Skip to content

Commit b4e644f

Browse files
committed
Align with latest ArduinoCore
1 parent 290ac95 commit b4e644f

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

Diff for: cores/esp32/IPAddress.cpp

+30-11
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ IPAddress::IPAddress(IPType ip_type, const uint8_t *address)
8989
}
9090
}
9191

92+
IPAddress::IPAddress(const char *address)
93+
{
94+
fromString(address);
95+
}
96+
9297
IPAddress& IPAddress::operator=(const uint8_t *address)
9398
{
9499
// IPv4 only conversion from byte pointer
@@ -98,6 +103,12 @@ IPAddress& IPAddress::operator=(const uint8_t *address)
98103
return *this;
99104
}
100105

106+
IPAddress& IPAddress::operator=(const char *address)
107+
{
108+
fromString(address);
109+
return *this;
110+
}
111+
101112
IPAddress& IPAddress::operator=(uint32_t address)
102113
{
103114
// IPv4 conversion
@@ -200,22 +211,30 @@ size_t IPAddress::printTo(Print& p) const
200211
return n;
201212
}
202213

203-
String IPAddress::toString() const
214+
String IPAddress::toString4() const
204215
{
205-
if (_type == IPv6)
206-
{
207-
StreamString s;
208-
s.reserve(40);
209-
printTo(s);
210-
return s;
211-
}
212-
213-
// IPv4
214216
char szRet[16];
215-
sprintf(szRet,"%u.%u.%u.%u", _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3]);
217+
snprintf(szRet, sizeof(szRet), "%u.%u.%u.%u", _address.bytes[IPADDRESS_V4_BYTES_INDEX], _address.bytes[IPADDRESS_V4_BYTES_INDEX + 1], _address.bytes[IPADDRESS_V4_BYTES_INDEX + 2], _address.bytes[IPADDRESS_V4_BYTES_INDEX + 3]);
216218
return String(szRet);
217219
}
218220

221+
String IPAddress::toString6() const
222+
{
223+
StreamString s;
224+
s.reserve(40);
225+
printTo(s);
226+
return s;
227+
}
228+
229+
String IPAddress::toString() const
230+
{
231+
if (_type == IPv4) {
232+
return toString4();
233+
} else {
234+
return toString6();
235+
}
236+
}
237+
219238
bool IPAddress::fromString(const char *address)
220239
{
221240
if (!fromString4(address))

Diff for: cores/esp32/IPAddress.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class IPAddress: public Printable
6262
IPAddress(uint32_t address);
6363
IPAddress(const uint8_t *address);
6464
IPAddress(IPType ip_type, const uint8_t *address);
65+
// If IPv4 fails tries IPv6 see fromString function
66+
IPAddress(const char *address);
6567
virtual ~IPAddress() {}
6668

6769
bool fromString(const char *address);
@@ -84,11 +86,13 @@ class IPAddress: public Printable
8486
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
8587
IPAddress& operator=(const uint8_t *address);
8688
IPAddress& operator=(uint32_t address);
89+
// If IPv4 fails tries IPv6 see fromString function
90+
IPAddress& operator=(const char *address);
8791

8892
virtual size_t printTo(Print& p) const;
8993
String toString() const;
9094

91-
IPType type() { return _type; }
95+
IPType type() const { return _type; }
9296

9397
friend class EthernetClass;
9498
friend class UDP;
@@ -100,6 +104,8 @@ class IPAddress: public Printable
100104
protected:
101105
bool fromString4(const char *address);
102106
bool fromString6(const char *address);
107+
String toString4() const;
108+
String toString6() const;
103109
};
104110

105111
// changed to extern because const declaration creates copies in BSS of INADDR_NONE for each CPP unit that includes it

0 commit comments

Comments
 (0)