Skip to content

Commit 6dbd662

Browse files
committed
Merge @mrengineer7777s changes from 'mrengineer7777:patch-2' into feature/mac-adress-refactor
2 parents e18d8b9 + d05cd87 commit 6dbd662

File tree

5 files changed

+438
-0
lines changed

5 files changed

+438
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ set(CORE_SRCS
5555
cores/esp32/libb64/cdecode.c
5656
cores/esp32/libb64/cencode.c
5757
cores/esp32/main.cpp
58+
cores/esp32/MacAddress.cpp
59+
cores/esp32/MacAddress8.cpp
5860
cores/esp32/MD5Builder.cpp
5961
cores/esp32/Print.cpp
6062
cores/esp32/SHA1Builder.cpp

cores/esp32/MacAddress.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#include <MacAddress.h>
2+
#include <stdio.h>
3+
#include <Print.h>
4+
5+
//Default constructor, blank mac address.
6+
MacAddress::MacAddress() {
7+
_mac.val = 0;
8+
}
9+
10+
MacAddress::MacAddress(uint64_t mac) {
11+
_mac.val = mac;
12+
}
13+
14+
MacAddress::MacAddress(const uint8_t *macbytearray) {
15+
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
16+
}
17+
18+
MacAddress::MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6) {
19+
_mac.bytes[0] = b1;
20+
_mac.bytes[1] = b2;
21+
_mac.bytes[2] = b3;
22+
_mac.bytes[3] = b4;
23+
_mac.bytes[4] = b5;
24+
_mac.bytes[5] = b6;
25+
}
26+
27+
//Parse user entered string into MAC address
28+
bool MacAddress::fromCStr(const char *buf) {
29+
char cs[18];
30+
char *token;
31+
char *next; //Unused but required
32+
int i;
33+
34+
strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
35+
36+
for(i=0; i<sizeof(_mac.bytes); i++) {
37+
token = strtok((i==0) ? cs : NULL, ":"); //Find first or next token
38+
if(!token) { //No more tokens found
39+
return false;
40+
}
41+
_mac.bytes[i] = strtol(token, &next, 16);
42+
}
43+
return true;
44+
}
45+
46+
//Parse user entered string into MAC address
47+
bool MacAddress::fromString(const String &macstr) {
48+
return fromCStr(macstr.c_str());
49+
}
50+
51+
//Copy MAC into 6 byte array
52+
void MacAddress::toBytes(uint8_t *buf) {
53+
memcpy(buf, _mac.bytes, sizeof(_mac.bytes));
54+
}
55+
56+
//Print MAC address into a C string.
57+
//MAC: Buffer must be at least 18 chars
58+
int MacAddress::toCStr(char *buf) {
59+
return sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
60+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
61+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5]);
62+
}
63+
64+
String MacAddress::toString() const {
65+
char buf[18];
66+
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
67+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
68+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5]);
69+
return String(buf);
70+
}
71+
72+
uint64_t MacAddress::Value() {
73+
return _mac.val;
74+
}
75+
76+
//Allow getting individual octets of the address. e.g. uint8_t b0 = ma[0];
77+
uint8_t MacAddress::operator[](int index) const {
78+
index = EnforceIndexBounds(index);
79+
return _mac.bytes[index];
80+
}
81+
82+
//Allow setting individual octets of the address. e.g. ma[2] = 255;
83+
uint8_t& MacAddress::operator[](int index) {
84+
index = EnforceIndexBounds(index);
85+
return _mac.bytes[index];
86+
}
87+
88+
//Overloaded copy operator: init MacAddress object from byte array
89+
MacAddress& MacAddress::operator=(const uint8_t *macbytearray) {
90+
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
91+
return *this;
92+
}
93+
94+
//Overloaded copy operator: init MacAddress object from uint64_t
95+
MacAddress& MacAddress::operator=(uint64_t macval) {
96+
_mac.val = macval;
97+
return *this;
98+
}
99+
100+
//Compare class to byte array
101+
bool MacAddress::operator==(const uint8_t *macbytearray) const {
102+
return !memcmp(_mac.bytes, macbytearray, sizeof(_mac.bytes));
103+
}
104+
105+
//Allow comparing value of two classes
106+
bool MacAddress::operator==(const MacAddress& mac2) const {
107+
return _mac.val == mac2._mac.val;
108+
}
109+
110+
//Type converter object to uint64_t [same as .Value()]
111+
MacAddress::operator uint64_t() const {
112+
return _mac.val;
113+
}
114+
115+
//Type converter object to read only pointer to mac bytes. e.g. const uint8_t *ip_8 = ma;
116+
MacAddress::operator const uint8_t*() const {
117+
return _mac.bytes;
118+
}
119+
120+
//Type converter object to read only pointer to mac value. e.g. const uint32_t *ip_64 = ma;
121+
MacAddress::operator const uint64_t*() const {
122+
return &_mac.val;
123+
}
124+
125+
size_t MacAddress::printTo(Print& p) const
126+
{
127+
size_t n = 0;
128+
for(int i = 0; i < 6; i++) {
129+
if(i){
130+
n += p.print(':');
131+
}
132+
n += p.printf("%02X", _mac.bytes[i]);
133+
}
134+
return n;
135+
}
136+
137+
//Bounds checking
138+
int MacAddress::EnforceIndexBounds(int i) const {
139+
if(i < 0) {
140+
return 0;
141+
}
142+
if(i >= sizeof(_mac.bytes)) {
143+
return sizeof(_mac.bytes)-1;
144+
}
145+
return i;
146+
}

cores/esp32/MacAddress.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//-----------------------------------------------------------------------------
2+
// MacAddress.h - class to make it easier to handle BSSID and MAC addresses.
3+
//
4+
// Copyright 2022 David McCurley
5+
// Licensed under the Apache License, Version 2.0 (the "License").
6+
// You may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//-----------------------------------------------------------------------------
17+
18+
#ifndef MacAddress_h
19+
#define MacAddress_h
20+
21+
#include <stdint.h>
22+
#include <WString.h>
23+
#include <Printable.h>
24+
25+
// A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses.
26+
class MacAddress : public Printable {
27+
private:
28+
union {
29+
struct {
30+
uint8_t align[2];
31+
uint8_t bytes[6];
32+
};
33+
uint64_t val;
34+
} _mac;
35+
36+
public:
37+
MacAddress();
38+
MacAddress(uint64_t mac);
39+
MacAddress(const uint8_t *macbytearray);
40+
MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6);
41+
virtual ~MacAddress() {}
42+
bool fromCStr(const char *buf);
43+
bool fromString(const String &macstr);
44+
void toBytes(uint8_t *buf);
45+
int toCStr(char *buf);
46+
String toString() const;
47+
uint64_t Value();
48+
49+
uint8_t operator[](int index) const;
50+
uint8_t& operator[](int index);
51+
MacAddress& operator=(const uint8_t *macbytearray);
52+
MacAddress& operator=(uint64_t macval);
53+
bool operator==(const uint8_t *macbytearray) const;
54+
bool operator==(const MacAddress& mac2) const;
55+
operator uint64_t() const;
56+
operator const uint8_t*() const;
57+
operator const uint64_t*() const;
58+
59+
virtual size_t printTo(Print& p) const;
60+
61+
// future use in Arduino Networking
62+
friend class EthernetClass;
63+
friend class UDP;
64+
friend class Client;
65+
friend class Server;
66+
friend class DhcpClass;
67+
friend class DNSClient;
68+
69+
private:
70+
int EnforceIndexBounds(int i) const;
71+
};
72+
73+
#endif

cores/esp32/MacAddress8.cpp

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#include <MacAddress8.h>
2+
#include <stdio.h>
3+
#include <Print.h>
4+
5+
//Default constructor, blank mac address.
6+
MacAddress8::MacAddress8() {
7+
_mac.val = 0;
8+
}
9+
10+
MacAddress8::MacAddress8(uint64_t mac) {
11+
_mac.val = mac;
12+
}
13+
14+
MacAddress8::MacAddress8(const uint8_t *macbytearray) {
15+
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
16+
}
17+
18+
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) {
19+
_mac.bytes[0] = b1;
20+
_mac.bytes[1] = b2;
21+
_mac.bytes[2] = b3;
22+
_mac.bytes[3] = b4;
23+
_mac.bytes[4] = b5;
24+
_mac.bytes[5] = b6;
25+
_mac.bytes[6] = b7;
26+
_mac.bytes[7] = b8;
27+
}
28+
29+
//Parse user entered string into MAC address
30+
bool MacAddress8::fromCStr(const char *buf) {
31+
char cs[24];
32+
char *token;
33+
char *next; //Unused but required
34+
int i;
35+
36+
strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
37+
38+
for(i=0; i<sizeof(_mac.bytes); i++) {
39+
token = strtok((i==0) ? cs : NULL, ":"); //Find first or next token
40+
if(!token) { //No more tokens found
41+
return false;
42+
}
43+
_mac.bytes[i] = strtol(token, &next, 16);
44+
}
45+
return true;
46+
}
47+
48+
//Parse user entered string into MAC address
49+
bool MacAddress8::fromString(const String &macstr) {
50+
return fromCStr(macstr.c_str());
51+
}
52+
53+
//Copy MAC into 8 byte array
54+
void MacAddress8::toBytes(uint8_t *buf) {
55+
memcpy(buf, _mac.bytes, sizeof(_mac.bytes));
56+
}
57+
58+
//Print MAC address into a C string.
59+
//EUI-64: Buffer must be at least 24 chars
60+
int MacAddress8::toCStr(char *buf) {
61+
return sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
62+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2], _mac.bytes[3],
63+
_mac.bytes[4], _mac.bytes[5], _mac.bytes[6], _mac.bytes[7]);
64+
}
65+
66+
String MacAddress8::toString() const {
67+
char buf[24];
68+
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
69+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2], _mac.bytes[3],
70+
_mac.bytes[4], _mac.bytes[5], _mac.bytes[6], _mac.bytes[7]);
71+
return String(buf);
72+
}
73+
74+
uint64_t MacAddress8::Value() {
75+
return _mac.val;
76+
}
77+
78+
uint8_t MacAddress8::operator[](int index) const {
79+
index = EnforceIndexBounds(index);
80+
return _mac.bytes[index];
81+
}
82+
83+
//Allow setting individual octets of the address. e.g. ma[2] = 255;
84+
uint8_t& MacAddress8::operator[](int index) {
85+
index = EnforceIndexBounds(index);
86+
return _mac.bytes[index];
87+
}
88+
89+
//Overloaded copy operator: init MacAddress object from byte array
90+
MacAddress8& MacAddress8::operator=(const uint8_t *macbytearray) {
91+
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
92+
return *this;
93+
}
94+
95+
//Overloaded copy operator: init MacAddress object from uint64_t
96+
MacAddress8& MacAddress8::operator=(uint64_t macval) {
97+
_mac.val = macval;
98+
return *this;
99+
}
100+
101+
//Compare class to byte array
102+
bool MacAddress8::operator==(const uint8_t *macbytearray) const {
103+
return !memcmp(_mac.bytes, macbytearray, sizeof(_mac.bytes));
104+
}
105+
106+
//Allow comparing value of two classes
107+
bool MacAddress8::operator==(const MacAddress8& mac2) const {
108+
return _mac.val == mac2._mac.val;
109+
}
110+
111+
//Type converter object to uint64_t [same as .Value()]
112+
MacAddress8::operator uint64_t() const {
113+
return _mac.val;
114+
}
115+
116+
//Type converter object to read only pointer to mac bytes. e.g. const uint8_t *ip_8 = ma;
117+
MacAddress8::operator const uint8_t*() const {
118+
return _mac.bytes;
119+
}
120+
121+
//Type converter object to read only pointer to mac value. e.g. const uint32_t *ip_64 = ma;
122+
MacAddress8::operator const uint64_t*() const {
123+
return &_mac.val;
124+
}
125+
126+
size_t MacAddress8::printTo(Print& p) const
127+
{
128+
size_t n = 0;
129+
for(int i = 0; i < 8; i++) {
130+
if(i){
131+
n += p.print(':');
132+
}
133+
n += p.printf("%02X", _mac.bytes[i]);
134+
}
135+
return n;
136+
}
137+
138+
//Bounds checking
139+
int MacAddress8::EnforceIndexBounds(int i) const {
140+
if(i < 0) {
141+
return 0;
142+
}
143+
if(i >= sizeof(_mac.bytes)) {
144+
return sizeof(_mac.bytes)-1;
145+
}
146+
return i;
147+
}

0 commit comments

Comments
 (0)