Skip to content

Commit 7f90683

Browse files
committed
fix(mac): Add initializer list constructor
1 parent fb49b6d commit 7f90683

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

cores/esp32/MacAddress.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ MacAddress::MacAddress(const String &macstr) {
3232
fromString(macstr.c_str());
3333
}
3434

35+
#ifdef __GXX_EXPERIMENTAL_CXX0X__
36+
MacAddress::MacAddress(std::initializer_list<uint8_t> list) {
37+
size_t size = list.size();
38+
memset(_mac.bytes, 0, sizeof(_mac.bytes));
39+
if (size == 6) {
40+
_type = MAC6;
41+
} else if (size == 8) {
42+
_type = MAC8;
43+
} else {
44+
// Default to MAC6 and keep the rest of the bytes as 0
45+
_type = MAC6;
46+
return;
47+
}
48+
49+
memcpy(_mac.bytes, list.begin(), size);
50+
}
51+
#endif
52+
3553
MacAddress::MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6) {
3654
_type = MAC6;
3755
memset(_mac.bytes, 0, sizeof(_mac.bytes));

cores/esp32/MacAddress.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <stdint.h>
2424
#include <WString.h>
2525
#include <Printable.h>
26+
#include <initializer_list>
2627

2728
enum MACType {
2829
MAC6,
@@ -56,6 +57,12 @@ class MacAddress : public Printable {
5657
MacAddress(const char *macstr);
5758
MacAddress(const String &macstr);
5859

60+
#ifdef __GXX_EXPERIMENTAL_CXX0X__
61+
// Initializer list constructor for {0xAA, 0xBB, ...} syntax
62+
// This has higher priority than String conversion, preventing ambiguity
63+
MacAddress(std::initializer_list<uint8_t> list);
64+
#endif
65+
5966
virtual ~MacAddress() {}
6067

6168
bool fromString(const char *buf);

0 commit comments

Comments
 (0)