Skip to content

Commit a740b1c

Browse files
Improvements from arduino-esp32 repo
Added printTo(). Added index operators and type converters. See test project here: https://github.com/mrengineer7777/MA_Test. No further changes expected.
1 parent f63a58a commit a740b1c

File tree

4 files changed

+184
-53
lines changed

4 files changed

+184
-53
lines changed

Diff for: api/MacAddress.cpp

+67-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#include <MacAddress.h>
12
#include <stdio.h>
2-
#include "MacAddress.h"
3+
#include <Print.h>
34

45
//Default constructor, blank mac address.
56
MacAddress::MacAddress() {
@@ -14,6 +15,15 @@ MacAddress::MacAddress(const uint8_t *macbytearray) {
1415
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
1516
}
1617

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+
1727
//Parse user entered string into MAC address
1828
bool MacAddress::fromCStr(const char *buf) {
1929
char cs[18];
@@ -63,33 +73,74 @@ uint64_t MacAddress::Value() {
6373
return _mac.val;
6474
}
6575

66-
//Implicit conversion object to number [same as .Value()]
67-
MacAddress::operator uint64_t() const
68-
{
69-
return _mac.val;
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];
7080
}
7181

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));
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));
7691
return *this;
7792
}
7893

79-
MacAddress& MacAddress::operator=(uint64_t macval)
80-
{
94+
//Overloaded copy operator: init MacAddress object from uint64_t
95+
MacAddress& MacAddress::operator=(uint64_t macval) {
8196
_mac.val = macval;
8297
return *this;
8398
}
8499

85100
//Compare class to byte array
86-
bool MacAddress::operator==(const uint8_t *mac) const
87-
{
88-
return !memcmp(_mac.bytes, mac, sizeof(_mac.bytes));
101+
bool MacAddress::operator==(const uint8_t *macbytearray) const {
102+
return !memcmp(_mac.bytes, macbytearray, sizeof(_mac.bytes));
89103
}
90104

91105
//Allow comparing value of two classes
92-
bool MacAddress::operator==(const MacAddress& mac2) const
93-
{
106+
bool MacAddress::operator==(const MacAddress& mac2) const {
94107
return _mac.val == mac2._mac.val;
95108
}
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+
}

Diff for: api/MacAddress.h

+24-10
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
#define MacAddress_h
2020

2121
#include <stdint.h>
22-
#include "String.h"
23-
24-
namespace arduino {
22+
#include <WString.h>
23+
#include <Printable.h>
2524

2625
// A class to make it easier to handle and pass around 6-byte BSSID and MAC addresses.
27-
class MacAddress {
26+
class MacAddress : public Printable {
2827
private:
2928
union {
3029
struct {
@@ -38,6 +37,7 @@ class MacAddress {
3837
MacAddress();
3938
MacAddress(uint64_t mac);
4039
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);
4141
virtual ~MacAddress() {}
4242
bool fromCStr(const char *buf);
4343
bool fromString(const String &macstr);
@@ -46,14 +46,28 @@ class MacAddress {
4646
String toString() const;
4747
uint64_t Value();
4848

49-
operator uint64_t() const;
50-
MacAddress& operator=(const uint8_t *mac);
49+
uint8_t operator[](int index) const;
50+
uint8_t& operator[](int index);
51+
MacAddress& operator=(const uint8_t *macbytearray);
5152
MacAddress& operator=(uint64_t macval);
52-
bool operator==(const uint8_t *mac) const;
53+
bool operator==(const uint8_t *macbytearray) const;
5354
bool operator==(const MacAddress& mac2) const;
54-
};
55+
operator uint64_t() const;
56+
operator const uint8_t*() const;
57+
operator const uint64_t*() const;
5558

56-
}
57-
using arduino::MacAddress;
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+
};
5872

5973
#endif

Diff for: api/MacAddress8.cpp

+68-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#include <MacAddress8.h>
12
#include <stdio.h>
2-
#include "MacAddress8.h"
3+
#include <Print.h>
34

45
//Default constructor, blank mac address.
56
MacAddress8::MacAddress8() {
@@ -14,6 +15,17 @@ MacAddress8::MacAddress8(const uint8_t *macbytearray) {
1415
memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes));
1516
}
1617

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+
1729
//Parse user entered string into MAC address
1830
bool MacAddress8::fromCStr(const char *buf) {
1931
char cs[24];
@@ -63,33 +75,73 @@ uint64_t MacAddress8::Value() {
6375
return _mac.val;
6476
}
6577

66-
//Implicit conversion object to number [same as .Value()]
67-
MacAddress8::operator uint64_t() const
68-
{
69-
return _mac.val;
78+
uint8_t MacAddress8::operator[](int index) const {
79+
index = EnforceIndexBounds(index);
80+
return _mac.bytes[index];
7081
}
7182

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));
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));
7692
return *this;
7793
}
7894

79-
MacAddress8& MacAddress8::operator=(uint64_t macval)
80-
{
95+
//Overloaded copy operator: init MacAddress object from uint64_t
96+
MacAddress8& MacAddress8::operator=(uint64_t macval) {
8197
_mac.val = macval;
8298
return *this;
8399
}
84100

85101
//Compare class to byte array
86-
bool MacAddress8::operator==(const uint8_t *mac) const
87-
{
88-
return !memcmp(_mac.bytes, mac, sizeof(_mac.bytes));
102+
bool MacAddress8::operator==(const uint8_t *macbytearray) const {
103+
return !memcmp(_mac.bytes, macbytearray, sizeof(_mac.bytes));
89104
}
90105

91106
//Allow comparing value of two classes
92-
bool MacAddress8::operator==(const MacAddress8& mac2) const
93-
{
107+
bool MacAddress8::operator==(const MacAddress8& mac2) const {
94108
return _mac.val == mac2._mac.val;
95109
}
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+
}

Diff for: api/MacAddress8.h

+25-11
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
#define MacAddress8_h
2020

2121
#include <stdint.h>
22-
#include "String.h"
23-
24-
namespace arduino {
22+
#include <WString.h>
23+
#include <Printable.h>
2524

2625
// 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 {
26+
class MacAddress8 : public Printable {
2827
private:
2928
union {
3029
uint8_t bytes[8];
@@ -35,22 +34,37 @@ class MacAddress8 {
3534
MacAddress8();
3635
MacAddress8(uint64_t mac);
3736
MacAddress8(const uint8_t *macbytearray);
37+
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);
3838
virtual ~MacAddress8() {}
3939
bool fromCStr(const char *buf);
4040
bool fromString(const String &macstr);
4141
void toBytes(uint8_t *buf);
42-
int toCStr(char *buf);
42+
int toCStr(char *buf);
4343
String toString() const;
4444
uint64_t Value();
4545

46-
operator uint64_t() const;
47-
MacAddress8& operator=(const uint8_t *mac);
46+
uint8_t operator[](int index) const;
47+
uint8_t& operator[](int index);
48+
MacAddress8& operator=(const uint8_t *macbytearray);
4849
MacAddress8& operator=(uint64_t macval);
49-
bool operator==(const uint8_t *mac) const;
50+
bool operator==(const uint8_t *macbytearray) const;
5051
bool operator==(const MacAddress8& mac2) const;
51-
};
52+
operator uint64_t() const;
53+
operator const uint8_t*() const;
54+
operator const uint64_t*() const;
5255

53-
}
54-
using arduino::MacAddress8;
56+
virtual size_t printTo(Print& p) const;
57+
58+
// future use in Arduino Networking
59+
friend class EthernetClass;
60+
friend class UDP;
61+
friend class Client;
62+
friend class Server;
63+
friend class DhcpClass;
64+
friend class DNSClient;
65+
66+
private:
67+
int EnforceIndexBounds(int i) const;
68+
};
5569

5670
#endif

0 commit comments

Comments
 (0)