From f9e33ed8796312abf8470e78a47607fe5ab52ba3 Mon Sep 17 00:00:00 2001 From: Virens Date: Sat, 16 Mar 2024 22:00:25 +0800 Subject: [PATCH 1/4] add "toString" function with IPAddress --- cores/arduino/IPAddress.cpp | 6 ++++++ cores/arduino/IPAddress.h | 1 + 2 files changed, 7 insertions(+) diff --git a/cores/arduino/IPAddress.cpp b/cores/arduino/IPAddress.cpp index 68439d1a5a..d0b82f3adb 100644 --- a/cores/arduino/IPAddress.cpp +++ b/cores/arduino/IPAddress.cpp @@ -107,3 +107,9 @@ size_t IPAddress::printTo(Print &p) const return n; } +String IPAddress::toString() const +{ + char szRet[16]; + sprintf(szRet, "%u.%u.%u.%u", _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3]); + return String(szRet); +} diff --git a/cores/arduino/IPAddress.h b/cores/arduino/IPAddress.h index c73f011982..b8b1092d3b 100644 --- a/cores/arduino/IPAddress.h +++ b/cores/arduino/IPAddress.h @@ -82,6 +82,7 @@ class IPAddress : public Printable { IPAddress &operator=(uint32_t address); virtual size_t printTo(Print &p) const; + String toString() const; friend class EthernetClass; friend class UDP; From a8c8c5036ec32069d9c7aee286d35a7ac5c5d2d4 Mon Sep 17 00:00:00 2001 From: Virens Date: Sat, 16 Mar 2024 22:15:56 +0800 Subject: [PATCH 2/4] add IPv6Address with IPV6 --- cores/arduino/IPv6Address.cpp | 90 +++++++++++++++++++++++++++++++++ cores/arduino/IPv6Address.h | 93 +++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 cores/arduino/IPv6Address.cpp create mode 100644 cores/arduino/IPv6Address.h diff --git a/cores/arduino/IPv6Address.cpp b/cores/arduino/IPv6Address.cpp new file mode 100644 index 0000000000..7d3c0de5f5 --- /dev/null +++ b/cores/arduino/IPv6Address.cpp @@ -0,0 +1,90 @@ +/* + IPv6Address.cpp - Base class that provides IPv6Address + Copyright (c) 2011 Adrian McEwen. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +IPv6Address::IPv6Address() +{ + memset(_address.bytes, 0, sizeof(_address.bytes)); +} + +IPv6Address::IPv6Address(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); +} + +IPv6Address::IPv6Address(const uint32_t *address) +{ + memcpy(_address.bytes, (const uint8_t *)address, sizeof(_address.bytes)); +} + +IPv6Address& IPv6Address::operator=(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); + return *this; +} + +bool IPv6Address::operator==(const uint8_t* addr) const +{ + return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; +} + +size_t IPv6Address::printTo(Print& p) const +{ + size_t n = 0; + for(int i = 0; i < 16; i+=2) { + if(i){ + n += p.print(':'); + } + n += p.printf("%02x", _address.bytes[i]); + n += p.printf("%02x", _address.bytes[i+1]); + + } + return n; +} + +String IPv6Address::toString() const +{ + char szRet[40]; + sprintf(szRet,"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", + _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3], + _address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7], + _address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11], + _address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]); + return String(szRet); +} + +bool IPv6Address::fromString(const char *address) +{ + //format 0011:2233:4455:6677:8899:aabb:ccdd:eeff + if(strlen(address) != 39){ + return false; + } + char * pos = (char *)address; + size_t i = 0; + for(i = 0; i < 16; i+=2) { + if(!sscanf(pos, "%2hhx", &_address.bytes[i]) || !sscanf(pos+2, "%2hhx", &_address.bytes[i+1])){ + return false; + } + pos += 5; + } + return true; +} diff --git a/cores/arduino/IPv6Address.h b/cores/arduino/IPv6Address.h new file mode 100644 index 0000000000..a45a8c34d6 --- /dev/null +++ b/cores/arduino/IPv6Address.h @@ -0,0 +1,93 @@ +/* + IPv6Address.h - Base class that provides IPv6Address + Copyright (c) 2011 Adrian McEwen. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef IPv6Address_h +#define IPv6Address_h + +#include +#include +#include + +// A class to make it easier to handle and pass around IP addresses + +class IPv6Address: public Printable +{ +private: + union { + uint8_t bytes[16]; // IPv4 address + uint32_t dword[4]; + } _address; + + // Access the raw byte array containing the address. Because this returns a pointer + // to the internal structure rather than a copy of the address this function should only + // be used when you know that the usage of the returned uint8_t* will be transient and not + // stored. + uint8_t* raw_address() + { + return _address.bytes; + } + +public: + // Constructors + IPv6Address(); + IPv6Address(const uint8_t *address); + IPv6Address(const uint32_t *address); + + bool fromString(const char *address); + bool fromString(const String &address) { return fromString(address.c_str()); } + + operator const uint8_t*() const + { + return _address.bytes; + } + operator const uint32_t*() const + { + return _address.dword; + } + bool operator==(const IPv6Address& addr) const + { + return (_address.dword[0] == addr._address.dword[0]) + && (_address.dword[1] == addr._address.dword[1]) + && (_address.dword[2] == addr._address.dword[2]) + && (_address.dword[3] == addr._address.dword[3]); + } + bool operator==(const uint8_t* addr) const; + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const + { + return _address.bytes[index]; + } + uint8_t& operator[](int index) + { + return _address.bytes[index]; + } + + // Overloaded copy operators to allow initialisation of IPv6Address objects from other types + IPv6Address& operator=(const uint8_t *address); + + virtual size_t printTo(Print& p) const; + String toString() const; + + friend class UDP; + friend class Client; + friend class Server; +}; + +#endif From 4d95273ace999704a4e3aaf0a241d909d313e433 Mon Sep 17 00:00:00 2001 From: Virens Date: Sat, 16 Mar 2024 22:26:17 +0800 Subject: [PATCH 3/4] format code --- cores/arduino/IPv6Address.cpp | 68 ++++++++++---------- cores/arduino/IPv6Address.h | 116 +++++++++++++++++----------------- 2 files changed, 93 insertions(+), 91 deletions(-) diff --git a/cores/arduino/IPv6Address.cpp b/cores/arduino/IPv6Address.cpp index 7d3c0de5f5..3080287077 100644 --- a/cores/arduino/IPv6Address.cpp +++ b/cores/arduino/IPv6Address.cpp @@ -23,68 +23,68 @@ IPv6Address::IPv6Address() { - memset(_address.bytes, 0, sizeof(_address.bytes)); + memset(_address.bytes, 0, sizeof(_address.bytes)); } IPv6Address::IPv6Address(const uint8_t *address) { - memcpy(_address.bytes, address, sizeof(_address.bytes)); + memcpy(_address.bytes, address, sizeof(_address.bytes)); } IPv6Address::IPv6Address(const uint32_t *address) { - memcpy(_address.bytes, (const uint8_t *)address, sizeof(_address.bytes)); + memcpy(_address.bytes, (const uint8_t *)address, sizeof(_address.bytes)); } IPv6Address& IPv6Address::operator=(const uint8_t *address) { - memcpy(_address.bytes, address, sizeof(_address.bytes)); - return *this; + memcpy(_address.bytes, address, sizeof(_address.bytes)); + return *this; } bool IPv6Address::operator==(const uint8_t* addr) const { - return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; + return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; } size_t IPv6Address::printTo(Print& p) const { - size_t n = 0; - for(int i = 0; i < 16; i+=2) { - if(i){ - n += p.print(':'); - } - n += p.printf("%02x", _address.bytes[i]); - n += p.printf("%02x", _address.bytes[i+1]); - + size_t n = 0; + for(int i = 0; i < 16; i+=2) { + if(i){ + n += p.print(':'); } - return n; + n += p.printf("%02x", _address.bytes[i]); + n += p.printf("%02x", _address.bytes[i+1]); + + } + return n; } String IPv6Address::toString() const { - char szRet[40]; - sprintf(szRet,"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", - _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3], - _address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7], - _address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11], - _address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]); - return String(szRet); + char szRet[40]; + sprintf(szRet,"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", + _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3], + _address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7], + _address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11], + _address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]); + return String(szRet); } bool IPv6Address::fromString(const char *address) { - //format 0011:2233:4455:6677:8899:aabb:ccdd:eeff - if(strlen(address) != 39){ - return false; - } - char * pos = (char *)address; - size_t i = 0; - for(i = 0; i < 16; i+=2) { - if(!sscanf(pos, "%2hhx", &_address.bytes[i]) || !sscanf(pos+2, "%2hhx", &_address.bytes[i+1])){ - return false; - } - pos += 5; + //format 0011:2233:4455:6677:8899:aabb:ccdd:eeff + if(strlen(address) != 39){ + return false; + } + char * pos = (char *)address; + size_t i = 0; + for(i = 0; i < 16; i+=2) { + if(!sscanf(pos, "%2hhx", &_address.bytes[i]) || !sscanf(pos+2, "%2hhx", &_address.bytes[i+1])){ + return false; } - return true; + pos += 5; + } + return true; } diff --git a/cores/arduino/IPv6Address.h b/cores/arduino/IPv6Address.h index a45a8c34d6..1009dea0c6 100644 --- a/cores/arduino/IPv6Address.h +++ b/cores/arduino/IPv6Address.h @@ -29,65 +29,67 @@ class IPv6Address: public Printable { private: - union { - uint8_t bytes[16]; // IPv4 address - uint32_t dword[4]; - } _address; - - // Access the raw byte array containing the address. Because this returns a pointer - // to the internal structure rather than a copy of the address this function should only - // be used when you know that the usage of the returned uint8_t* will be transient and not - // stored. - uint8_t* raw_address() - { - return _address.bytes; - } + union { + uint8_t bytes[16]; // IPv4 address + uint32_t dword[4]; + } _address; + + // Access the raw byte array containing the address. Because this returns a pointer + // to the internal structure rather than a copy of the address this function should only + // be used when you know that the usage of the returned uint8_t* will be transient and not + // stored. + uint8_t* raw_address() + { + return _address.bytes; + } public: - // Constructors - IPv6Address(); - IPv6Address(const uint8_t *address); - IPv6Address(const uint32_t *address); - - bool fromString(const char *address); - bool fromString(const String &address) { return fromString(address.c_str()); } - - operator const uint8_t*() const - { - return _address.bytes; - } - operator const uint32_t*() const - { - return _address.dword; - } - bool operator==(const IPv6Address& addr) const - { - return (_address.dword[0] == addr._address.dword[0]) - && (_address.dword[1] == addr._address.dword[1]) - && (_address.dword[2] == addr._address.dword[2]) - && (_address.dword[3] == addr._address.dword[3]); - } - bool operator==(const uint8_t* addr) const; - - // Overloaded index operator to allow getting and setting individual octets of the address - uint8_t operator[](int index) const - { - return _address.bytes[index]; - } - uint8_t& operator[](int index) - { - return _address.bytes[index]; - } - - // Overloaded copy operators to allow initialisation of IPv6Address objects from other types - IPv6Address& operator=(const uint8_t *address); - - virtual size_t printTo(Print& p) const; - String toString() const; - - friend class UDP; - friend class Client; - friend class Server; + // Constructors + IPv6Address(); + IPv6Address(const uint8_t *address); + IPv6Address(const uint32_t *address); + + bool fromString(const char *address); + bool fromString(const String &address) { + return fromString(address.c_str()); + } + + operator const uint8_t*() const + { + return _address.bytes; + } + operator const uint32_t*() const + { + return _address.dword; + } + bool operator==(const IPv6Address& addr) const + { + return (_address.dword[0] == addr._address.dword[0]) + && (_address.dword[1] == addr._address.dword[1]) + && (_address.dword[2] == addr._address.dword[2]) + && (_address.dword[3] == addr._address.dword[3]); + } + bool operator==(const uint8_t* addr) const; + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const + { + return _address.bytes[index]; + } + uint8_t& operator[](int index) + { + return _address.bytes[index]; + } + + // Overloaded copy operators to allow initialisation of IPv6Address objects from other types + IPv6Address& operator=(const uint8_t *address); + + virtual size_t printTo(Print& p) const; + String toString() const; + + friend class UDP; + friend class Client; + friend class Server; }; #endif From 5154c09647754621581332d98d9bd2845bf23908 Mon Sep 17 00:00:00 2001 From: Virens Date: Sun, 17 Mar 2024 00:58:40 +0800 Subject: [PATCH 4/4] format code with astyle error --- cores/arduino/IPv6Address.cpp | 28 ++++---- cores/arduino/IPv6Address.h | 128 +++++++++++++++++----------------- 2 files changed, 78 insertions(+), 78 deletions(-) diff --git a/cores/arduino/IPv6Address.cpp b/cores/arduino/IPv6Address.cpp index 3080287077..c3d3efbf3e 100644 --- a/cores/arduino/IPv6Address.cpp +++ b/cores/arduino/IPv6Address.cpp @@ -36,26 +36,26 @@ IPv6Address::IPv6Address(const uint32_t *address) memcpy(_address.bytes, (const uint8_t *)address, sizeof(_address.bytes)); } -IPv6Address& IPv6Address::operator=(const uint8_t *address) +IPv6Address &IPv6Address::operator=(const uint8_t *address) { memcpy(_address.bytes, address, sizeof(_address.bytes)); return *this; } -bool IPv6Address::operator==(const uint8_t* addr) const +bool IPv6Address::operator==(const uint8_t *addr) const { return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; } -size_t IPv6Address::printTo(Print& p) const +size_t IPv6Address::printTo(Print &p) const { size_t n = 0; - for(int i = 0; i < 16; i+=2) { - if(i){ + for (int i = 0; i < 16; i += 2) { + if(i) { n += p.print(':'); } n += p.printf("%02x", _address.bytes[i]); - n += p.printf("%02x", _address.bytes[i+1]); + n += p.printf("%02x", _address.bytes[i + 1]); } return n; @@ -64,24 +64,24 @@ size_t IPv6Address::printTo(Print& p) const String IPv6Address::toString() const { char szRet[40]; - sprintf(szRet,"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", - _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3], - _address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7], - _address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11], - _address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]); + sprintf(szRet, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", + _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3], + _address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7], + _address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11], + _address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]); return String(szRet); } bool IPv6Address::fromString(const char *address) { //format 0011:2233:4455:6677:8899:aabb:ccdd:eeff - if(strlen(address) != 39){ + if (strlen(address) != 39) { return false; } char * pos = (char *)address; size_t i = 0; - for(i = 0; i < 16; i+=2) { - if(!sscanf(pos, "%2hhx", &_address.bytes[i]) || !sscanf(pos+2, "%2hhx", &_address.bytes[i+1])){ + for (i = 0; i < 16; i += 2) { + if (!sscanf(pos, "%2hhx", &_address.bytes[i]) || !sscanf(pos + 2, "%2hhx", &_address.bytes[i + 1])) { return false; } pos += 5; diff --git a/cores/arduino/IPv6Address.h b/cores/arduino/IPv6Address.h index 1009dea0c6..3f22ca3945 100644 --- a/cores/arduino/IPv6Address.h +++ b/cores/arduino/IPv6Address.h @@ -26,70 +26,70 @@ // A class to make it easier to handle and pass around IP addresses -class IPv6Address: public Printable -{ -private: - union { - uint8_t bytes[16]; // IPv4 address - uint32_t dword[4]; - } _address; - - // Access the raw byte array containing the address. Because this returns a pointer - // to the internal structure rather than a copy of the address this function should only - // be used when you know that the usage of the returned uint8_t* will be transient and not - // stored. - uint8_t* raw_address() - { - return _address.bytes; - } - -public: - // Constructors - IPv6Address(); - IPv6Address(const uint8_t *address); - IPv6Address(const uint32_t *address); - - bool fromString(const char *address); - bool fromString(const String &address) { - return fromString(address.c_str()); - } - - operator const uint8_t*() const - { - return _address.bytes; - } - operator const uint32_t*() const - { - return _address.dword; - } - bool operator==(const IPv6Address& addr) const - { - return (_address.dword[0] == addr._address.dword[0]) - && (_address.dword[1] == addr._address.dword[1]) - && (_address.dword[2] == addr._address.dword[2]) - && (_address.dword[3] == addr._address.dword[3]); - } - bool operator==(const uint8_t* addr) const; - - // Overloaded index operator to allow getting and setting individual octets of the address - uint8_t operator[](int index) const - { - return _address.bytes[index]; - } - uint8_t& operator[](int index) - { - return _address.bytes[index]; - } - - // Overloaded copy operators to allow initialisation of IPv6Address objects from other types - IPv6Address& operator=(const uint8_t *address); - - virtual size_t printTo(Print& p) const; - String toString() const; - - friend class UDP; - friend class Client; - friend class Server; +class IPv6Address: public Printable { + private: + union { + uint8_t bytes[16]; // IPv4 address + uint32_t dword[4]; + } _address; + + // Access the raw byte array containing the address. Because this returns a pointer + // to the internal structure rather than a copy of the address this function should only + // be used when you know that the usage of the returned uint8_t* will be transient and not + // stored. + uint8_t *raw_address() + { + return _address.bytes; + } + + public: + // Constructors + IPv6Address(); + IPv6Address(const uint8_t *address); + IPv6Address(const uint32_t *address); + + bool fromString(const char *address); + bool fromString(const String &address) + { + return fromString(address.c_str()); + } + + operator const uint8_t *() const + { + return _address.bytes; + } + operator const uint32_t *() const + { + return _address.dword; + } + bool operator==(const IPv6Address &addr) const + { + return (_address.dword[0] == addr._address.dword[0]) + && (_address.dword[1] == addr._address.dword[1]) + && (_address.dword[2] == addr._address.dword[2]) + && (_address.dword[3] == addr._address.dword[3]); + } + bool operator==(const uint8_t *addr) const; + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const + { + return _address.bytes[index]; + } + uint8_t &operator[](int index) + { + return _address.bytes[index]; + } + + // Overloaded copy operators to allow initialisation of IPv6Address objects from other types + IPv6Address &operator=(const uint8_t *address); + + virtual size_t printTo(Print &p) const; + String toString() const; + + friend class UDP; + friend class Client; + friend class Server; }; #endif