Skip to content

Commit 44aa51e

Browse files
Cleanup and added another comparison
Renamed MacAddress6 to MacAddress. Added class comparison to byte array. Moved operator implementation from .h to .cpp file. Renamed a couple variables.
1 parent 9b83858 commit 44aa51e

File tree

4 files changed

+134
-116
lines changed

4 files changed

+134
-116
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,95 @@
1-
#include <MacAddress6.h>
2-
#include <stdio.h>
3-
4-
//Default constructor, blank mac address.
5-
MacAddress6::MacAddress6() {
6-
_mac.val = 0;
7-
}
8-
9-
MacAddress6::MacAddress6(uint64_t mac) {
10-
_mac.val = mac;
11-
}
12-
13-
MacAddress6::MacAddress6(const uint8_t *macbytearray) {
14-
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
15-
}
16-
17-
//Parse user entered string into MAC address
18-
bool MacAddress6::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 MacAddress6::fromString(const String &address) {
38-
return fromCStr(address.c_str());
39-
}
40-
41-
//Copy MAC into 6 byte array
42-
void MacAddress6::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 MacAddress6::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 MacAddress6::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 MacAddress6::Value() {
63-
return _mac.val;
64-
}
65-
66-
MacAddress6& MacAddress6::operator=(const uint8_t *mac)
67-
{
68-
memcpy(_mac.bytes, mac, sizeof(_mac.bytes));
69-
return *this;
70-
}
71-
72-
MacAddress6& MacAddress6::operator=(uint64_t macval)
73-
{
74-
_mac.val = macval;
75-
return *this;
76-
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//-----------------------------------------------------------------------------
2-
// MacAddress6.h - class to make it easier to handle BSSID and MAC addresses.
2+
// MacAddress.h - class to make it easier to handle BSSID and MAC addresses.
33
//
44
// Copyright 2022 David McCurley
55
// Licensed under the Apache License, Version 2.0 (the "License").
@@ -15,13 +15,13 @@
1515
// limitations under the License.
1616
//-----------------------------------------------------------------------------
1717

18-
#ifndef MacAddress6_h
19-
#define MacAddress6_h
18+
#ifndef MacAddress_h
19+
#define MacAddress_h
2020

2121
#include <WString.h>
2222

2323
// A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses.
24-
class MacAddress6 {
24+
class MacAddress {
2525
private:
2626
union {
2727
struct {
@@ -32,32 +32,22 @@ class MacAddress6 {
3232
} _mac;
3333

3434
public:
35-
MacAddress6();
36-
MacAddress6(uint64_t mac);
37-
MacAddress6(const uint8_t *macbytearray);
38-
virtual ~MacAddress6() {}
35+
MacAddress();
36+
MacAddress(uint64_t mac);
37+
MacAddress(const uint8_t *macbytearray);
38+
virtual ~MacAddress() {}
3939
bool fromCStr(const char *buf);
40-
bool fromString(const String &address);
40+
bool fromString(const String &macstr);
4141
void toBytes(uint8_t *buf);
4242
int toCStr(char *buf);
4343
String toString() const;
4444
uint64_t Value();
4545

46-
// Overloaded copy operators to allow initialisation of MacAddress6 objects from other types
47-
MacAddress6& operator=(const uint8_t *mac);
48-
MacAddress6& operator=(uint64_t macval);
49-
50-
// Implicit conversion object to number [same as .Value()]
51-
operator uint64_t() const
52-
{
53-
return _mac.val;
54-
}
55-
56-
// Allow comparing value of two classes
57-
bool operator==(const MacAddress6& mac2) const
58-
{
59-
return _mac.val == mac2._mac.val;
60-
}
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;
6151
};
6252

6353
#endif

cores/esp32/MacAddress8.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ bool MacAddress8::fromCStr(const char *buf) {
3434
}
3535

3636
//Parse user entered string into MAC address
37-
bool MacAddress8::fromString(const String &address) {
38-
return fromCStr(address.c_str());
37+
bool MacAddress8::fromString(const String &macstr) {
38+
return fromCStr(macstr.c_str());
3939
}
4040

4141
//Copy MAC into 8 byte array
@@ -63,6 +63,13 @@ uint64_t MacAddress8::Value() {
6363
return _mac.val;
6464
}
6565

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
6673
MacAddress8& MacAddress8::operator=(const uint8_t *mac)
6774
{
6875
memcpy(_mac.bytes, mac, sizeof(_mac.bytes));
@@ -74,3 +81,15 @@ MacAddress8& MacAddress8::operator=(uint64_t macval)
7481
_mac.val = macval;
7582
return *this;
7683
}
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

+4-14
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,17 @@ class MacAddress8 {
3434
MacAddress8(const uint8_t *macbytearray);
3535
virtual ~MacAddress8() {}
3636
bool fromCStr(const char *buf);
37-
bool fromString(const String &address);
37+
bool fromString(const String &macstr);
3838
void toBytes(uint8_t *buf);
3939
int toCStr(char *buf);
4040
String toString() const;
4141
uint64_t Value();
4242

43-
// Overloaded copy operators to allow initialisation of MacAddress6 objects from other types
43+
operator uint64_t() const;
4444
MacAddress8& operator=(const uint8_t *mac);
4545
MacAddress8& operator=(uint64_t macval);
46-
47-
// Implicit conversion object to number [same as .Value()]
48-
operator uint64_t() const
49-
{
50-
return _mac.val;
51-
}
52-
53-
// Allow comparing value of two classes
54-
bool operator==(const MacAddress8& mac2) const
55-
{
56-
return _mac.val == mac2._mac.val;
57-
}
46+
bool operator==(const uint8_t *mac) const;
47+
bool operator==(const MacAddress8& mac2) const;
5848
};
5949

6050
#endif

0 commit comments

Comments
 (0)