|
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 | +} |
0 commit comments