Skip to content

Commit ae78e5e

Browse files
committed
feat(MAC): Rework API to support both 6+8 bytes MacAddress
1 parent 6dbd662 commit ae78e5e

File tree

4 files changed

+163
-198
lines changed

4 files changed

+163
-198
lines changed

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ set(CORE_SRCS
5454
cores/esp32/IPAddress.cpp
5555
cores/esp32/libb64/cdecode.c
5656
cores/esp32/libb64/cencode.c
57-
cores/esp32/main.cpp
5857
cores/esp32/MacAddress.cpp
59-
cores/esp32/MacAddress8.cpp
58+
cores/esp32/main.cpp
6059
cores/esp32/MD5Builder.cpp
6160
cores/esp32/Print.cpp
6261
cores/esp32/SHA1Builder.cpp

cores/esp32/MacAddress.cpp

Lines changed: 124 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,143 @@
33
#include <Print.h>
44

55
//Default constructor, blank mac address.
6-
MacAddress::MacAddress() {
7-
_mac.val = 0;
8-
}
6+
MacAddress::MacAddress() : MacAddress(MAC6){}
97

10-
MacAddress::MacAddress(uint64_t mac) {
8+
MacAddress::MacAddress(MACType mac_type){
9+
_type = mac_type;
10+
memset(_mac.bytes, 0, sizeof(_mac.bytes));
11+
}
12+
MacAddress::MacAddress(MACType mac_type, uint64_t mac) {
1113
_mac.val = mac;
14+
_type = mac_type;
1215
}
1316

14-
MacAddress::MacAddress(const uint8_t *macbytearray) {
15-
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
17+
MacAddress::MacAddress(MACType mac_type, const uint8_t *macbytearray) {
18+
// 6-bytes MacAddress only
19+
memset(_mac.bytes, 0, sizeof(_mac.bytes));
20+
if(mac_type == MAC6) {
21+
memcpy(_mac.bytes, macbytearray, 6);
22+
} else {
23+
memcpy(_mac.bytes, macbytearray, 8);
24+
}
25+
_type = mac_type;
26+
}
27+
28+
MacAddress::MacAddress(const char *macstr){
29+
fromString(macstr);
1630
}
1731

1832
MacAddress::MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6) {
33+
memset(_mac.bytes, 0, sizeof(_mac.bytes));
34+
_mac.bytes[0] = b1;
35+
_mac.bytes[1] = b2;
36+
_mac.bytes[2] = b3;
37+
_mac.bytes[3] = b4;
38+
_mac.bytes[4] = b5;
39+
_mac.bytes[5] = b6;
40+
_type = MAC6;
41+
}
42+
43+
MacAddress::MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8) {
1944
_mac.bytes[0] = b1;
2045
_mac.bytes[1] = b2;
2146
_mac.bytes[2] = b3;
2247
_mac.bytes[3] = b4;
2348
_mac.bytes[4] = b5;
2449
_mac.bytes[5] = b6;
50+
_mac.bytes[6] = b7;
51+
_mac.bytes[7] = b8;
52+
_type = MAC8;
2553
}
2654

2755
//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;
56+
bool MacAddress::fromString(const char *buf) {
57+
if(strlen(buf) == 17) {
58+
return fromString6(buf);
59+
} else if(strlen(buf) == 23) {
60+
return fromString8(buf);
4061
}
41-
_mac.bytes[i] = strtol(token, &next, 16);
42-
}
43-
return true;
62+
return false;
4463
}
4564

4665
//Parse user entered string into MAC address
47-
bool MacAddress::fromString(const String &macstr) {
48-
return fromCStr(macstr.c_str());
66+
bool MacAddress::fromString6(const char *buf) {
67+
char cs[18];
68+
char *token;
69+
char *next; //Unused but required
70+
int i;
71+
72+
strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
73+
74+
for(i = 0; i < 6; i++) {
75+
token = strtok((i==0) ? cs : NULL, ":"); //Find first or next token
76+
if(!token) { //No more tokens found
77+
return false;
78+
}
79+
_mac.bytes[i] = strtol(token, &next, 16);
80+
}
81+
_type = MAC6;
82+
return true;
4983
}
5084

51-
//Copy MAC into 6 byte array
85+
bool MacAddress::fromString8(const char *buf) {
86+
char cs[24];
87+
char *token;
88+
char *next; //Unused but required
89+
int i;
90+
91+
strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
92+
93+
for(i = 0; i < 8; i++) {
94+
token = strtok((i==0) ? cs : NULL, ":"); //Find first or next token
95+
if(!token) { //No more tokens found
96+
return false;
97+
}
98+
_mac.bytes[i] = strtol(token, &next, 16);
99+
}
100+
_type = MAC8;
101+
return true;
102+
}
103+
104+
//Copy MAC into byte array
52105
void MacAddress::toBytes(uint8_t *buf) {
53-
memcpy(buf, _mac.bytes, sizeof(_mac.bytes));
106+
if(_type == MAC6) {
107+
memcpy(buf, _mac.bytes, 6);
108+
} else {
109+
memcpy(buf, _mac.bytes, sizeof(_mac.bytes));
110+
}
54111
}
55112

56113
//Print MAC address into a C string.
57114
//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]);
115+
int MacAddress::toString(char *buf) {
116+
if(_type == MAC6) {
117+
return sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
118+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
119+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5]);
120+
} else {
121+
return sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
122+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
123+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5],
124+
_mac.bytes[6], _mac.bytes[7]);
125+
}
62126
}
63127

64128
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);
129+
if(_type == MAC6) {
130+
char buf[18];
131+
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
132+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
133+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5]);
134+
return String(buf);
135+
} else {
136+
char buf[24];
137+
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
138+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
139+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5],
140+
_mac.bytes[6], _mac.bytes[7]);
141+
return String(buf);
142+
}
70143
}
71144

72145
uint64_t MacAddress::Value() {
@@ -87,19 +160,24 @@ uint8_t& MacAddress::operator[](int index) {
87160

88161
//Overloaded copy operator: init MacAddress object from byte array
89162
MacAddress& MacAddress::operator=(const uint8_t *macbytearray) {
90-
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
163+
// 6-bytes MacAddress only
164+
_type = MAC6;
165+
memset(_mac.bytes, 0, sizeof(_mac.bytes));
166+
memcpy(_mac.bytes, macbytearray, 6);
91167
return *this;
92168
}
93169

94170
//Overloaded copy operator: init MacAddress object from uint64_t
95171
MacAddress& MacAddress::operator=(uint64_t macval) {
172+
// 6-bytes MacAddress only
173+
_type = MAC6;
96174
_mac.val = macval;
97175
return *this;
98176
}
99177

100178
//Compare class to byte array
101179
bool MacAddress::operator==(const uint8_t *macbytearray) const {
102-
return !memcmp(_mac.bytes, macbytearray, sizeof(_mac.bytes));
180+
return !memcmp(_mac.bytes, macbytearray, 6);
103181
}
104182

105183
//Allow comparing value of two classes
@@ -124,8 +202,9 @@ MacAddress::operator const uint64_t*() const {
124202

125203
size_t MacAddress::printTo(Print& p) const
126204
{
205+
uint8_t bytes = (_type == MAC6) ? 6 : 8;
127206
size_t n = 0;
128-
for(int i = 0; i < 6; i++) {
207+
for(int i = 0; i < bytes; i++) {
129208
if(i){
130209
n += p.print(':');
131210
}
@@ -139,8 +218,14 @@ int MacAddress::EnforceIndexBounds(int i) const {
139218
if(i < 0) {
140219
return 0;
141220
}
142-
if(i >= sizeof(_mac.bytes)) {
143-
return sizeof(_mac.bytes)-1;
221+
if(_type == MAC6) {
222+
if(i >= 6) {
223+
return 5;
224+
}
225+
} else {
226+
if(i >= 8) {
227+
return 7;
228+
}
144229
}
145230
return i;
146231
}

cores/esp32/MacAddress.h

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// MacAddress.h - class to make it easier to handle BSSID and MAC addresses.
33
//
44
// Copyright 2022 David McCurley
5+
// Modified by Espressif Systems 2024
6+
//
57
// Licensed under the Apache License, Version 2.0 (the "License").
68
// You may not use this file except in compliance with the License.
79
// You may obtain a copy of the License at
@@ -22,34 +24,54 @@
2224
#include <WString.h>
2325
#include <Printable.h>
2426

25-
// A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses.
27+
enum MACType {
28+
MAC6,
29+
MAC8
30+
};
31+
32+
// A class to make it easier to handle and pass around MAC addresses, supporting both 6-byte and 8-byte MAC addresses.
2633
class MacAddress : public Printable {
2734
private:
2835
union {
29-
struct {
30-
uint8_t align[2];
31-
uint8_t bytes[6];
32-
};
36+
uint8_t bytes[8];
3337
uint64_t val;
3438
} _mac;
39+
MACType _type;
3540

3641
public:
42+
//Default MAC6
3743
MacAddress();
38-
MacAddress(uint64_t mac);
39-
MacAddress(const uint8_t *macbytearray);
44+
45+
MacAddress(MACType mac_type);
4046
MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6);
47+
MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8);
48+
49+
MacAddress(MACType mac_type, uint64_t mac);
50+
MacAddress(MACType mac_type, const uint8_t *macbytearray);
51+
52+
//Default MAC6
53+
MacAddress(uint64_t mac) : MacAddress(MAC6, mac) {}
54+
MacAddress(const uint8_t *macbytearray) : MacAddress(MAC6, macbytearray) {}
55+
56+
MacAddress(const char *macstr);
57+
4158
virtual ~MacAddress() {}
42-
bool fromCStr(const char *buf);
43-
bool fromString(const String &macstr);
59+
60+
bool fromString(const char *buf);
61+
bool fromString(const String &macstr) { return fromString(macstr.c_str()); }
62+
4463
void toBytes(uint8_t *buf);
45-
int toCStr(char *buf);
64+
int toString(char *buf);
4665
String toString() const;
4766
uint64_t Value();
4867

4968
uint8_t operator[](int index) const;
5069
uint8_t& operator[](int index);
70+
71+
//MAC6 only
5172
MacAddress& operator=(const uint8_t *macbytearray);
5273
MacAddress& operator=(uint64_t macval);
74+
5375
bool operator==(const uint8_t *macbytearray) const;
5476
bool operator==(const MacAddress& mac2) const;
5577
operator uint64_t() const;
@@ -59,12 +81,18 @@ class MacAddress : public Printable {
5981
virtual size_t printTo(Print& p) const;
6082

6183
// future use in Arduino Networking
84+
/*
6285
friend class EthernetClass;
6386
friend class UDP;
6487
friend class Client;
6588
friend class Server;
6689
friend class DhcpClass;
6790
friend class DNSClient;
91+
*/
92+
93+
protected:
94+
bool fromString6(const char *buf);
95+
bool fromString8(const char *buf);
6896

6997
private:
7098
int EnforceIndexBounds(int i) const;

0 commit comments

Comments
 (0)