Skip to content

Commit f63a58a

Browse files
Adding MacAddress, MacAddress8
Adding New Classes MacAddress, MacAddress8 in the style of IPAddress. Note I didn't include Printable, as I don't understand how that works. If someone else wants to add it later, they are welcome to do so. This is my first time contributing to this repository, so please feel free to point out any mistakes. This library originated on arduino-esp32 (espressif/arduino-esp32#6667). It was suggested that I contribute to the Arduino core as well :)
1 parent e03b653 commit f63a58a

File tree

4 files changed

+305
-0
lines changed

4 files changed

+305
-0
lines changed

api/MacAddress.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <stdio.h>
2+
#include "MacAddress.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+
}

api/MacAddress.h

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 "String.h"
23+
24+
namespace arduino {
25+
26+
// A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses.
27+
class MacAddress {
28+
private:
29+
union {
30+
struct {
31+
uint8_t align[2];
32+
uint8_t bytes[6];
33+
};
34+
uint64_t val;
35+
} _mac;
36+
37+
public:
38+
MacAddress();
39+
MacAddress(uint64_t mac);
40+
MacAddress(const uint8_t *macbytearray);
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+
operator uint64_t() const;
50+
MacAddress& operator=(const uint8_t *mac);
51+
MacAddress& operator=(uint64_t macval);
52+
bool operator==(const uint8_t *mac) const;
53+
bool operator==(const MacAddress& mac2) const;
54+
};
55+
56+
}
57+
using arduino::MacAddress;
58+
59+
#endif

api/MacAddress8.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <stdio.h>
2+
#include "MacAddress8.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+
}

api/MacAddress8.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//-----------------------------------------------------------------------------
2+
// MacAddress8.h - class to make it easier to handle 8-byte EUI-64 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 <stdint.h>
22+
#include "String.h"
23+
24+
namespace arduino {
25+
26+
// 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>.
27+
class MacAddress8 {
28+
private:
29+
union {
30+
uint8_t bytes[8];
31+
uint64_t val;
32+
} _mac;
33+
34+
public:
35+
MacAddress8();
36+
MacAddress8(uint64_t mac);
37+
MacAddress8(const uint8_t *macbytearray);
38+
virtual ~MacAddress8() {}
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+
MacAddress8& operator=(const uint8_t *mac);
48+
MacAddress8& operator=(uint64_t macval);
49+
bool operator==(const uint8_t *mac) const;
50+
bool operator==(const MacAddress8& mac2) const;
51+
};
52+
53+
}
54+
using arduino::MacAddress8;
55+
56+
#endif

0 commit comments

Comments
 (0)