Skip to content

Commit e004ab0

Browse files
Added new classes MacAddress and MacAddress8
In the same style as class IPAddress. Based on Apache License.
1 parent ce2cd11 commit e004ab0

File tree

5 files changed

+295
-0
lines changed

5 files changed

+295
-0
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ set(CORE_SRCS
5252
cores/esp32/libb64/cdecode.c
5353
cores/esp32/libb64/cencode.c
5454
cores/esp32/main.cpp
55+
cores/esp32/MacAddress.cpp
56+
cores/esp32/MacAddress8.cpp
5557
cores/esp32/MD5Builder.cpp
5658
cores/esp32/Print.cpp
5759
cores/esp32/stdlib_noniso.c

cores/esp32/MacAddress.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <MacAddress.h>
2+
#include <stdio.h>
3+
4+
//Default constructor, blank mac address.
5+
MacAddress::MacAddress() {
6+
_mac.val = 0;
7+
}
8+
9+
MacAddress::MacAddress(uint64_t mac) {
10+
_mac.val = mac;
11+
}
12+
13+
MacAddress::MacAddress(const uint8_t *macbytearray) {
14+
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
15+
}
16+
17+
//Parse user entered string into MAC address
18+
bool MacAddress::fromCStr(const char *buf) {
19+
char cs[18];
20+
char *token;
21+
char *next; //Unused but required
22+
int i;
23+
24+
strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
25+
26+
for(i=0; i<sizeof(_mac.bytes); i++) {
27+
token = strtok((i==0) ? cs : NULL, ":"); //Find first or next token
28+
if(!token) { //No more tokens found
29+
return false;
30+
}
31+
_mac.bytes[i] = strtol(token, &next, 16);
32+
}
33+
return true;
34+
}
35+
36+
//Parse user entered string into MAC address
37+
bool MacAddress::fromString(const String &macstr) {
38+
return fromCStr(macstr.c_str());
39+
}
40+
41+
//Copy MAC into 6 byte array
42+
void MacAddress::toBytes(uint8_t *buf) {
43+
memcpy(buf, _mac.bytes, sizeof(_mac.bytes));
44+
}
45+
46+
//Print MAC address into a C string.
47+
//MAC: Buffer must be at least 18 chars
48+
int MacAddress::toCStr(char *buf) {
49+
return sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
50+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
51+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5]);
52+
}
53+
54+
String MacAddress::toString() const {
55+
char buf[18];
56+
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
57+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
58+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5]);
59+
return String(buf);
60+
}
61+
62+
uint64_t MacAddress::Value() {
63+
return _mac.val;
64+
}
65+
66+
//Implicit conversion object to number [same as .Value()]
67+
MacAddress::operator uint64_t() const
68+
{
69+
return _mac.val;
70+
}
71+
72+
//Overloaded copy operators to allow initialisation of MacAddress objects from other types
73+
MacAddress& MacAddress::operator=(const uint8_t *mac)
74+
{
75+
memcpy(_mac.bytes, mac, sizeof(_mac.bytes));
76+
return *this;
77+
}
78+
79+
MacAddress& MacAddress::operator=(uint64_t macval)
80+
{
81+
_mac.val = macval;
82+
return *this;
83+
}
84+
85+
//Compare class to byte array
86+
bool MacAddress::operator==(const uint8_t *mac) const
87+
{
88+
return !memcmp(_mac.bytes, mac, sizeof(_mac.bytes));
89+
}
90+
91+
//Allow comparing value of two classes
92+
bool MacAddress::operator==(const MacAddress& mac2) const
93+
{
94+
return _mac.val == mac2._mac.val;
95+
}

cores/esp32/MacAddress.h

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 <WString.h>
22+
23+
// A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses.
24+
class MacAddress {
25+
private:
26+
union {
27+
struct {
28+
uint8_t align[2];
29+
uint8_t bytes[6];
30+
};
31+
uint64_t val;
32+
} _mac;
33+
34+
public:
35+
MacAddress();
36+
MacAddress(uint64_t mac);
37+
MacAddress(const uint8_t *macbytearray);
38+
virtual ~MacAddress() {}
39+
bool fromCStr(const char *buf);
40+
bool fromString(const String &macstr);
41+
void toBytes(uint8_t *buf);
42+
int toCStr(char *buf);
43+
String toString() const;
44+
uint64_t Value();
45+
46+
operator uint64_t() const;
47+
MacAddress& operator=(const uint8_t *mac);
48+
MacAddress& operator=(uint64_t macval);
49+
bool operator==(const uint8_t *mac) const;
50+
bool operator==(const MacAddress& mac2) const;
51+
};
52+
53+
#endif

cores/esp32/MacAddress8.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <MacAddress8.h>
2+
#include <stdio.h>
3+
4+
//Default constructor, blank mac address.
5+
MacAddress8::MacAddress8() {
6+
_mac.val = 0;
7+
}
8+
9+
MacAddress8::MacAddress8(uint64_t mac) {
10+
_mac.val = mac;
11+
}
12+
13+
MacAddress8::MacAddress8(const uint8_t *macbytearray) {
14+
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
15+
}
16+
17+
//Parse user entered string into MAC address
18+
bool MacAddress8::fromCStr(const char *buf) {
19+
char cs[24];
20+
char *token;
21+
char *next; //Unused but required
22+
int i;
23+
24+
strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
25+
26+
for(i=0; i<sizeof(_mac.bytes); i++) {
27+
token = strtok((i==0) ? cs : NULL, ":"); //Find first or next token
28+
if(!token) { //No more tokens found
29+
return false;
30+
}
31+
_mac.bytes[i] = strtol(token, &next, 16);
32+
}
33+
return true;
34+
}
35+
36+
//Parse user entered string into MAC address
37+
bool MacAddress8::fromString(const String &macstr) {
38+
return fromCStr(macstr.c_str());
39+
}
40+
41+
//Copy MAC into 8 byte array
42+
void MacAddress8::toBytes(uint8_t *buf) {
43+
memcpy(buf, _mac.bytes, sizeof(_mac.bytes));
44+
}
45+
46+
//Print MAC address into a C string.
47+
//EUI-64: Buffer must be at least 24 chars
48+
int MacAddress8::toCStr(char *buf) {
49+
return sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
50+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2], _mac.bytes[3],
51+
_mac.bytes[4], _mac.bytes[5], _mac.bytes[6], _mac.bytes[7]);
52+
}
53+
54+
String MacAddress8::toString() const {
55+
char buf[24];
56+
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
57+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2], _mac.bytes[3],
58+
_mac.bytes[4], _mac.bytes[5], _mac.bytes[6], _mac.bytes[7]);
59+
return String(buf);
60+
}
61+
62+
uint64_t MacAddress8::Value() {
63+
return _mac.val;
64+
}
65+
66+
//Implicit conversion object to number [same as .Value()]
67+
MacAddress8::operator uint64_t() const
68+
{
69+
return _mac.val;
70+
}
71+
72+
//Overloaded copy operators to allow initialisation of MacAddress objects from other types
73+
MacAddress8& MacAddress8::operator=(const uint8_t *mac)
74+
{
75+
memcpy(_mac.bytes, mac, sizeof(_mac.bytes));
76+
return *this;
77+
}
78+
79+
MacAddress8& MacAddress8::operator=(uint64_t macval)
80+
{
81+
_mac.val = macval;
82+
return *this;
83+
}
84+
85+
//Compare class to byte array
86+
bool MacAddress8::operator==(const uint8_t *mac) const
87+
{
88+
return !memcmp(_mac.bytes, mac, sizeof(_mac.bytes));
89+
}
90+
91+
//Allow comparing value of two classes
92+
bool MacAddress8::operator==(const MacAddress8& mac2) const
93+
{
94+
return _mac.val == mac2._mac.val;
95+
}

cores/esp32/MacAddress8.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//-----------------------------------------------------------------------------
2+
// MacAddress8.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 MacAddress8_h
19+
#define MacAddress8_h
20+
21+
#include <WString.h>
22+
23+
// A class to make it easier to handle and pass around 8-byte EUI-64(used for IEEE 802.15.4) addresses. See <esp_mac.h>.
24+
class MacAddress8 {
25+
private:
26+
union {
27+
uint8_t bytes[8];
28+
uint64_t val;
29+
} _mac;
30+
31+
public:
32+
MacAddress8();
33+
MacAddress8(uint64_t mac);
34+
MacAddress8(const uint8_t *macbytearray);
35+
virtual ~MacAddress8() {}
36+
bool fromCStr(const char *buf);
37+
bool fromString(const String &macstr);
38+
void toBytes(uint8_t *buf);
39+
int toCStr(char *buf);
40+
String toString() const;
41+
uint64_t Value();
42+
43+
operator uint64_t() const;
44+
MacAddress8& operator=(const uint8_t *mac);
45+
MacAddress8& operator=(uint64_t macval);
46+
bool operator==(const uint8_t *mac) const;
47+
bool operator==(const MacAddress8& mac2) const;
48+
};
49+
50+
#endif

0 commit comments

Comments
 (0)