3
3
#include < Print.h>
4
4
5
5
// Default constructor, blank mac address.
6
- MacAddress::MacAddress () {
7
- _mac.val = 0 ;
8
- }
6
+ MacAddress::MacAddress () : MacAddress(MAC6){}
9
7
10
- MacAddress::MacAddress (uint64_t mac) {
8
+ MacAddress::MacAddress (MACType mac_type){
9
+ _type = mac_type;
10
+ memset (_mac.bytes , 0 , sizeof (_mac.bytes ));
11
+ }
12
+ MacAddress::MacAddress (MACType mac_type, uint64_t mac) {
11
13
_mac.val = mac;
14
+ _type = mac_type;
12
15
}
13
16
14
- MacAddress::MacAddress (const uint8_t *macbytearray) {
15
- memcpy (_mac.bytes , macbytearray, sizeof (_mac.bytes ));
17
+ MacAddress::MacAddress (MACType mac_type, const uint8_t *macbytearray) {
18
+ // 6-bytes MacAddress only
19
+ memset (_mac.bytes , 0 , sizeof (_mac.bytes ));
20
+ if (mac_type == MAC6) {
21
+ memcpy (_mac.bytes , macbytearray, 6 );
22
+ } else {
23
+ memcpy (_mac.bytes , macbytearray, 8 );
24
+ }
25
+ _type = mac_type;
26
+ }
27
+
28
+ MacAddress::MacAddress (const char *macstr){
29
+ fromString (macstr);
16
30
}
17
31
18
32
MacAddress::MacAddress (uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6) {
33
+ memset (_mac.bytes , 0 , sizeof (_mac.bytes ));
34
+ _mac.bytes [0 ] = b1;
35
+ _mac.bytes [1 ] = b2;
36
+ _mac.bytes [2 ] = b3;
37
+ _mac.bytes [3 ] = b4;
38
+ _mac.bytes [4 ] = b5;
39
+ _mac.bytes [5 ] = b6;
40
+ _type = MAC6;
41
+ }
42
+
43
+ MacAddress::MacAddress (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
44
_mac.bytes [0 ] = b1;
20
45
_mac.bytes [1 ] = b2;
21
46
_mac.bytes [2 ] = b3;
22
47
_mac.bytes [3 ] = b4;
23
48
_mac.bytes [4 ] = b5;
24
49
_mac.bytes [5 ] = b6;
50
+ _mac.bytes [6 ] = b7;
51
+ _mac.bytes [7 ] = b8;
52
+ _type = MAC8;
25
53
}
26
54
27
55
// Parse user entered string into MAC address
28
- bool MacAddress::fromCStr (const char *buf) {
29
- char cs[18 ];
30
- char *token;
31
- char *next; // Unused but required
32
- int i;
33
-
34
- strncpy (cs, buf, sizeof (cs)); // strtok modifies the buffer: copy to working buffer.
35
-
36
- for (i=0 ; i<sizeof (_mac.bytes ); i++) {
37
- token = strtok ((i==0 ) ? cs : NULL , " :" ); // Find first or next token
38
- if (!token) { // No more tokens found
39
- return false ;
56
+ bool MacAddress::fromString (const char *buf) {
57
+ if (strlen (buf) == 17 ) {
58
+ return fromString6 (buf);
59
+ } else if (strlen (buf) == 23 ) {
60
+ return fromString8 (buf);
40
61
}
41
- _mac.bytes [i] = strtol (token, &next, 16 );
42
- }
43
- return true ;
62
+ return false ;
44
63
}
45
64
46
65
// Parse user entered string into MAC address
47
- bool MacAddress::fromString (const String &macstr) {
48
- return fromCStr (macstr.c_str ());
66
+ bool MacAddress::fromString6 (const char *buf) {
67
+ char cs[18 ];
68
+ char *token;
69
+ char *next; // Unused but required
70
+ int i;
71
+
72
+ strncpy (cs, buf, sizeof (cs)); // strtok modifies the buffer: copy to working buffer.
73
+
74
+ for (i = 0 ; i < 6 ; i++) {
75
+ token = strtok ((i==0 ) ? cs : NULL , " :" ); // Find first or next token
76
+ if (!token) { // No more tokens found
77
+ return false ;
78
+ }
79
+ _mac.bytes [i] = strtol (token, &next, 16 );
80
+ }
81
+ _type = MAC6;
82
+ return true ;
49
83
}
50
84
51
- // Copy MAC into 6 byte array
85
+ bool MacAddress::fromString8 (const char *buf) {
86
+ char cs[24 ];
87
+ char *token;
88
+ char *next; // Unused but required
89
+ int i;
90
+
91
+ strncpy (cs, buf, sizeof (cs)); // strtok modifies the buffer: copy to working buffer.
92
+
93
+ for (i = 0 ; i < 8 ; i++) {
94
+ token = strtok ((i==0 ) ? cs : NULL , " :" ); // Find first or next token
95
+ if (!token) { // No more tokens found
96
+ return false ;
97
+ }
98
+ _mac.bytes [i] = strtol (token, &next, 16 );
99
+ }
100
+ _type = MAC8;
101
+ return true ;
102
+ }
103
+
104
+ // Copy MAC into byte array
52
105
void MacAddress::toBytes (uint8_t *buf) {
53
- memcpy (buf, _mac.bytes , sizeof (_mac.bytes ));
106
+ if (_type == MAC6) {
107
+ memcpy (buf, _mac.bytes , 6 );
108
+ } else {
109
+ memcpy (buf, _mac.bytes , sizeof (_mac.bytes ));
110
+ }
54
111
}
55
112
56
113
// Print MAC address into a C string.
57
114
// MAC: Buffer must be at least 18 chars
58
- int MacAddress::toCStr (char *buf) {
59
- return sprintf (buf, " %02X:%02X:%02X:%02X:%02X:%02X" ,
60
- _mac.bytes [0 ], _mac.bytes [1 ], _mac.bytes [2 ],
61
- _mac.bytes [3 ], _mac.bytes [4 ], _mac.bytes [5 ]);
115
+ int MacAddress::toString (char *buf) {
116
+ if (_type == MAC6) {
117
+ return sprintf (buf, " %02X:%02X:%02X:%02X:%02X:%02X" ,
118
+ _mac.bytes [0 ], _mac.bytes [1 ], _mac.bytes [2 ],
119
+ _mac.bytes [3 ], _mac.bytes [4 ], _mac.bytes [5 ]);
120
+ } else {
121
+ return sprintf (buf, " %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X" ,
122
+ _mac.bytes [0 ], _mac.bytes [1 ], _mac.bytes [2 ],
123
+ _mac.bytes [3 ], _mac.bytes [4 ], _mac.bytes [5 ],
124
+ _mac.bytes [6 ], _mac.bytes [7 ]);
125
+ }
62
126
}
63
127
64
128
String MacAddress::toString () const {
65
- char buf[18 ];
66
- sprintf (buf, " %02X:%02X:%02X:%02X:%02X:%02X" ,
67
- _mac.bytes [0 ], _mac.bytes [1 ], _mac.bytes [2 ],
68
- _mac.bytes [3 ], _mac.bytes [4 ], _mac.bytes [5 ]);
69
- return String (buf);
129
+ if (_type == MAC6) {
130
+ char buf[18 ];
131
+ sprintf (buf, " %02X:%02X:%02X:%02X:%02X:%02X" ,
132
+ _mac.bytes [0 ], _mac.bytes [1 ], _mac.bytes [2 ],
133
+ _mac.bytes [3 ], _mac.bytes [4 ], _mac.bytes [5 ]);
134
+ return String (buf);
135
+ } else {
136
+ char buf[24 ];
137
+ sprintf (buf, " %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X" ,
138
+ _mac.bytes [0 ], _mac.bytes [1 ], _mac.bytes [2 ],
139
+ _mac.bytes [3 ], _mac.bytes [4 ], _mac.bytes [5 ],
140
+ _mac.bytes [6 ], _mac.bytes [7 ]);
141
+ return String (buf);
142
+ }
70
143
}
71
144
72
145
uint64_t MacAddress::Value () {
@@ -87,19 +160,24 @@ uint8_t& MacAddress::operator[](int index) {
87
160
88
161
// Overloaded copy operator: init MacAddress object from byte array
89
162
MacAddress& MacAddress::operator =(const uint8_t *macbytearray) {
90
- memcpy (_mac.bytes , macbytearray, sizeof (_mac.bytes ));
163
+ // 6-bytes MacAddress only
164
+ _type = MAC6;
165
+ memset (_mac.bytes , 0 , sizeof (_mac.bytes ));
166
+ memcpy (_mac.bytes , macbytearray, 6 );
91
167
return *this ;
92
168
}
93
169
94
170
// Overloaded copy operator: init MacAddress object from uint64_t
95
171
MacAddress& MacAddress::operator =(uint64_t macval) {
172
+ // 6-bytes MacAddress only
173
+ _type = MAC6;
96
174
_mac.val = macval;
97
175
return *this ;
98
176
}
99
177
100
178
// Compare class to byte array
101
179
bool MacAddress::operator ==(const uint8_t *macbytearray) const {
102
- return !memcmp (_mac.bytes , macbytearray, sizeof (_mac. bytes ) );
180
+ return !memcmp (_mac.bytes , macbytearray, 6 );
103
181
}
104
182
105
183
// Allow comparing value of two classes
@@ -124,8 +202,9 @@ MacAddress::operator const uint64_t*() const {
124
202
125
203
size_t MacAddress::printTo (Print& p) const
126
204
{
205
+ uint8_t bytes = (_type == MAC6) ? 6 : 8 ;
127
206
size_t n = 0 ;
128
- for (int i = 0 ; i < 6 ; i++) {
207
+ for (int i = 0 ; i < bytes ; i++) {
129
208
if (i){
130
209
n += p.print (' :' );
131
210
}
@@ -139,8 +218,14 @@ int MacAddress::EnforceIndexBounds(int i) const {
139
218
if (i < 0 ) {
140
219
return 0 ;
141
220
}
142
- if (i >= sizeof (_mac.bytes )) {
143
- return sizeof (_mac.bytes )-1 ;
221
+ if (_type == MAC6) {
222
+ if (i >= 6 ) {
223
+ return 5 ;
224
+ }
225
+ } else {
226
+ if (i >= 8 ) {
227
+ return 7 ;
228
+ }
144
229
}
145
230
return i;
146
231
}
0 commit comments