|
| 1 | +#include <MacAddress8.h> |
| 2 | +#include <stdio.h> |
| 3 | + |
| 4 | +//Default constructor, blank mac address. |
| 5 | +MacAddress8::MacAddress8() { |
| 6 | + _mac.val = 0; |
| 7 | +} |
| 8 | + |
| 9 | +MacAddress8::MacAddress8(uint64_t mac) { |
| 10 | + _mac.val = mac; |
| 11 | +} |
| 12 | + |
| 13 | +MacAddress8::MacAddress8(const uint8_t *macbytearray) { |
| 14 | + memcpy(_mac.bytes, macbytearray, sizeof(_mac.bytes)); |
| 15 | +} |
| 16 | + |
| 17 | +//Parse user entered string into MAC address |
| 18 | +bool MacAddress8::fromCStr(const char *buf) { |
| 19 | + char cs[24]; |
| 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 MacAddress8::fromString(const String &macstr) { |
| 38 | + return fromCStr(macstr.c_str()); |
| 39 | +} |
| 40 | + |
| 41 | +//Copy MAC into 8 byte array |
| 42 | +void MacAddress8::toBytes(uint8_t *buf) { |
| 43 | + memcpy(buf, _mac.bytes, sizeof(_mac.bytes)); |
| 44 | +} |
| 45 | + |
| 46 | +//Print MAC address into a C string. |
| 47 | +//EUI-64: Buffer must be at least 24 chars |
| 48 | +int MacAddress8::toCStr(char *buf) { |
| 49 | + return sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", |
| 50 | + _mac.bytes[0], _mac.bytes[1], _mac.bytes[2], _mac.bytes[3], |
| 51 | + _mac.bytes[4], _mac.bytes[5], _mac.bytes[6], _mac.bytes[7]); |
| 52 | +} |
| 53 | + |
| 54 | +String MacAddress8::toString() const { |
| 55 | + char buf[24]; |
| 56 | + sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", |
| 57 | + _mac.bytes[0], _mac.bytes[1], _mac.bytes[2], _mac.bytes[3], |
| 58 | + _mac.bytes[4], _mac.bytes[5], _mac.bytes[6], _mac.bytes[7]); |
| 59 | + return String(buf); |
| 60 | +} |
| 61 | + |
| 62 | +uint64_t MacAddress8::Value() { |
| 63 | + return _mac.val; |
| 64 | +} |
| 65 | + |
| 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 |
| 73 | +MacAddress8& MacAddress8::operator=(const uint8_t *mac) |
| 74 | +{ |
| 75 | + memcpy(_mac.bytes, mac, sizeof(_mac.bytes)); |
| 76 | + return *this; |
| 77 | +} |
| 78 | + |
| 79 | +MacAddress8& MacAddress8::operator=(uint64_t macval) |
| 80 | +{ |
| 81 | + _mac.val = macval; |
| 82 | + return *this; |
| 83 | +} |
| 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 | +} |
0 commit comments