diff --git a/CMakeLists.txt b/CMakeLists.txt index 851d1488eca..6935a1ad652 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,8 @@ set(CORE_SRCS cores/esp32/libb64/cdecode.c cores/esp32/libb64/cencode.c cores/esp32/main.cpp + cores/esp32/MacAddress.cpp + cores/esp32/MacAddress8.cpp cores/esp32/MD5Builder.cpp cores/esp32/Print.cpp cores/esp32/stdlib_noniso.c diff --git a/cores/esp32/MacAddress.cpp b/cores/esp32/MacAddress.cpp new file mode 100644 index 00000000000..0498b1f93e6 --- /dev/null +++ b/cores/esp32/MacAddress.cpp @@ -0,0 +1,146 @@ +#include +#include +#include + +//Default constructor, blank mac address. +MacAddress::MacAddress() { + _mac.val = 0; +} + +MacAddress::MacAddress(uint64_t mac) { + _mac.val = mac; +} + +MacAddress::MacAddress(const uint8_t *macbytearray) { + memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes)); +} + +MacAddress::MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6) { + _mac.bytes[0] = b1; + _mac.bytes[1] = b2; + _mac.bytes[2] = b3; + _mac.bytes[3] = b4; + _mac.bytes[4] = b5; + _mac.bytes[5] = b6; +} + +//Parse user entered string into MAC address +bool MacAddress::fromCStr(const char *buf) { + char cs[18]; + char *token; + char *next; //Unused but required + int i; + + strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer. + + for(i=0; i= sizeof(_mac.bytes)) { + return sizeof(_mac.bytes)-1; + } + return i; +} diff --git a/cores/esp32/MacAddress.h b/cores/esp32/MacAddress.h new file mode 100644 index 00000000000..de335a957a1 --- /dev/null +++ b/cores/esp32/MacAddress.h @@ -0,0 +1,73 @@ +//----------------------------------------------------------------------------- +// MacAddress.h - class to make it easier to handle BSSID and MAC addresses. +// +// Copyright 2022 David McCurley +// Licensed under the Apache License, Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//----------------------------------------------------------------------------- + +#ifndef MacAddress_h +#define MacAddress_h + +#include +#include +#include + +// A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses. +class MacAddress : public Printable { +private: + union { + struct { + uint8_t align[2]; + uint8_t bytes[6]; + }; + uint64_t val; + } _mac; + +public: + MacAddress(); + MacAddress(uint64_t mac); + MacAddress(const uint8_t *macbytearray); + MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6); + virtual ~MacAddress() {} + bool fromCStr(const char *buf); + bool fromString(const String &macstr); + void toBytes(uint8_t *buf); + int toCStr(char *buf); + String toString() const; + uint64_t Value(); + + uint8_t operator[](int index) const; + uint8_t& operator[](int index); + MacAddress& operator=(const uint8_t *macbytearray); + MacAddress& operator=(uint64_t macval); + bool operator==(const uint8_t *macbytearray) const; + bool operator==(const MacAddress& mac2) const; + operator uint64_t() const; + operator const uint8_t*() const; + operator const uint64_t*() const; + + virtual size_t printTo(Print& p) const; + + // future use in Arduino Networking + friend class EthernetClass; + friend class UDP; + friend class Client; + friend class Server; + friend class DhcpClass; + friend class DNSClient; + +private: + int EnforceIndexBounds(int i) const; +}; + +#endif diff --git a/cores/esp32/MacAddress8.cpp b/cores/esp32/MacAddress8.cpp new file mode 100644 index 00000000000..7406bee8a42 --- /dev/null +++ b/cores/esp32/MacAddress8.cpp @@ -0,0 +1,147 @@ +#include +#include +#include + +//Default constructor, blank mac address. +MacAddress8::MacAddress8() { + _mac.val = 0; +} + +MacAddress8::MacAddress8(uint64_t mac) { + _mac.val = mac; +} + +MacAddress8::MacAddress8(const uint8_t *macbytearray) { + memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes)); +} + +MacAddress8::MacAddress8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8) { + _mac.bytes[0] = b1; + _mac.bytes[1] = b2; + _mac.bytes[2] = b3; + _mac.bytes[3] = b4; + _mac.bytes[4] = b5; + _mac.bytes[5] = b6; + _mac.bytes[6] = b7; + _mac.bytes[7] = b8; +} + +//Parse user entered string into MAC address +bool MacAddress8::fromCStr(const char *buf) { + char cs[24]; + char *token; + char *next; //Unused but required + int i; + + strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer. + + for(i=0; i= sizeof(_mac.bytes)) { + return sizeof(_mac.bytes)-1; + } + return i; +} diff --git a/cores/esp32/MacAddress8.h b/cores/esp32/MacAddress8.h new file mode 100644 index 00000000000..360ce8c9fe7 --- /dev/null +++ b/cores/esp32/MacAddress8.h @@ -0,0 +1,70 @@ +//----------------------------------------------------------------------------- +// MacAddress8.h - class to make it easier to handle 8-byte EUI-64 addresses. +// +// Copyright 2022 David McCurley +// Licensed under the Apache License, Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//----------------------------------------------------------------------------- + +#ifndef MacAddress8_h +#define MacAddress8_h + +#include +#include +#include + +// A class to make it easier to handle and pass around 8-byte EUI-64(used for IEEE 802.15.4) addresses. See . +class MacAddress8 : public Printable { +private: + union { + uint8_t bytes[8]; + uint64_t val; + } _mac; + +public: + MacAddress8(); + MacAddress8(uint64_t mac); + MacAddress8(const uint8_t *macbytearray); + MacAddress8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8); + virtual ~MacAddress8() {} + bool fromCStr(const char *buf); + bool fromString(const String &macstr); + void toBytes(uint8_t *buf); + int toCStr(char *buf); + String toString() const; + uint64_t Value(); + + uint8_t operator[](int index) const; + uint8_t& operator[](int index); + MacAddress8& operator=(const uint8_t *macbytearray); + MacAddress8& operator=(uint64_t macval); + bool operator==(const uint8_t *macbytearray) const; + bool operator==(const MacAddress8& mac2) const; + operator uint64_t() const; + operator const uint8_t*() const; + operator const uint64_t*() const; + + virtual size_t printTo(Print& p) const; + + // future use in Arduino Networking + friend class EthernetClass; + friend class UDP; + friend class Client; + friend class Server; + friend class DhcpClass; + friend class DNSClient; + +private: + int EnforceIndexBounds(int i) const; +}; + +#endif