|
| 1 | +#include <MacAddress.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <Print.h> |
| 4 | + |
| 5 | +//Default constructor, blank mac address. |
| 6 | +MacAddress::MacAddress() : MacAddress(MAC6){} |
| 7 | + |
| 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) { |
| 13 | + _type = mac_type; |
| 14 | + _mac.val = mac; |
| 15 | +} |
| 16 | + |
| 17 | +MacAddress::MacAddress(MACType mac_type, const uint8_t *macbytearray) { |
| 18 | + _type = mac_type; |
| 19 | + memset(_mac.bytes, 0, sizeof(_mac.bytes)); |
| 20 | + if(_type == MAC6) { |
| 21 | + memcpy(_mac.bytes, macbytearray, 6); |
| 22 | + } else { |
| 23 | + memcpy(_mac.bytes, macbytearray, 8); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +MacAddress::MacAddress(const char *macstr){ |
| 28 | + fromString(macstr); |
| 29 | +} |
| 30 | + |
| 31 | +MacAddress::MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6) { |
| 32 | + _type = MAC6; |
| 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 | +} |
| 41 | + |
| 42 | +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) { |
| 43 | + _type = MAC8; |
| 44 | + _mac.bytes[0] = b1; |
| 45 | + _mac.bytes[1] = b2; |
| 46 | + _mac.bytes[2] = b3; |
| 47 | + _mac.bytes[3] = b4; |
| 48 | + _mac.bytes[4] = b5; |
| 49 | + _mac.bytes[5] = b6; |
| 50 | + _mac.bytes[6] = b7; |
| 51 | + _mac.bytes[7] = b8; |
| 52 | +} |
| 53 | + |
| 54 | +//Parse user entered string into MAC address |
| 55 | +bool MacAddress::fromString(const char *buf) { |
| 56 | + if(strlen(buf) == 17) { |
| 57 | + return fromString6(buf); |
| 58 | + } else if(strlen(buf) == 23) { |
| 59 | + return fromString8(buf); |
| 60 | + } |
| 61 | + return false; |
| 62 | +} |
| 63 | + |
| 64 | +//Parse user entered string into MAC address |
| 65 | +bool MacAddress::fromString6(const char *buf) { |
| 66 | + char cs[18]; |
| 67 | + char *token; |
| 68 | + char *next; //Unused but required |
| 69 | + int i; |
| 70 | + |
| 71 | + strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer. |
| 72 | + |
| 73 | + for(i = 0; i < 6; i++) { |
| 74 | + token = strtok((i==0) ? cs : NULL, ":"); //Find first or next token |
| 75 | + if(!token) { //No more tokens found |
| 76 | + return false; |
| 77 | + } |
| 78 | + _mac.bytes[i] = strtol(token, &next, 16); |
| 79 | + } |
| 80 | + _type = MAC6; |
| 81 | + return true; |
| 82 | +} |
| 83 | + |
| 84 | +bool MacAddress::fromString8(const char *buf) { |
| 85 | + char cs[24]; |
| 86 | + char *token; |
| 87 | + char *next; //Unused but required |
| 88 | + int i; |
| 89 | + |
| 90 | + strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer. |
| 91 | + |
| 92 | + for(i = 0; i < 8; i++) { |
| 93 | + token = strtok((i==0) ? cs : NULL, ":"); //Find first or next token |
| 94 | + if(!token) { //No more tokens found |
| 95 | + return false; |
| 96 | + } |
| 97 | + _mac.bytes[i] = strtol(token, &next, 16); |
| 98 | + } |
| 99 | + _type = MAC8; |
| 100 | + return true; |
| 101 | +} |
| 102 | + |
| 103 | +//Copy MAC into byte array |
| 104 | +void MacAddress::toBytes(uint8_t *buf) { |
| 105 | + if(_type == MAC6) { |
| 106 | + memcpy(buf, _mac.bytes, 6); |
| 107 | + } else { |
| 108 | + memcpy(buf, _mac.bytes, sizeof(_mac.bytes)); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +//Print MAC address into a C string. |
| 113 | +//MAC: Buffer must be at least 18 chars |
| 114 | +int MacAddress::toString(char *buf) { |
| 115 | + if(_type == MAC6) { |
| 116 | + return sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 117 | + _mac.bytes[0], _mac.bytes[1], _mac.bytes[2], |
| 118 | + _mac.bytes[3], _mac.bytes[4], _mac.bytes[5]); |
| 119 | + } else { |
| 120 | + return sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", |
| 121 | + _mac.bytes[0], _mac.bytes[1], _mac.bytes[2], |
| 122 | + _mac.bytes[3], _mac.bytes[4], _mac.bytes[5], |
| 123 | + _mac.bytes[6], _mac.bytes[7]); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +String MacAddress::toString() const { |
| 128 | + uint8_t bytes = (_type == MAC6) ? 18 : 24; |
| 129 | + char buf[bytes]; |
| 130 | + if(_type == MAC6) { |
| 131 | + snprintf(buf, sizeof(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 | + } else { |
| 135 | + snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", |
| 136 | + _mac.bytes[0], _mac.bytes[1], _mac.bytes[2], |
| 137 | + _mac.bytes[3], _mac.bytes[4], _mac.bytes[5], |
| 138 | + _mac.bytes[6], _mac.bytes[7]); |
| 139 | + } |
| 140 | + return String(buf); |
| 141 | +} |
| 142 | + |
| 143 | +uint64_t MacAddress::Value() { |
| 144 | + return _mac.val; |
| 145 | +} |
| 146 | + |
| 147 | +//Allow getting individual octets of the address. e.g. uint8_t b0 = ma[0]; |
| 148 | +uint8_t MacAddress::operator[](int index) const { |
| 149 | + index = EnforceIndexBounds(index); |
| 150 | + return _mac.bytes[index]; |
| 151 | +} |
| 152 | + |
| 153 | +//Allow setting individual octets of the address. e.g. ma[2] = 255; |
| 154 | +uint8_t& MacAddress::operator[](int index) { |
| 155 | + index = EnforceIndexBounds(index); |
| 156 | + return _mac.bytes[index]; |
| 157 | +} |
| 158 | + |
| 159 | +//Overloaded copy operator: init MacAddress object from byte array |
| 160 | +MacAddress& MacAddress::operator=(const uint8_t *macbytearray) { |
| 161 | + // 6-bytes MacAddress only |
| 162 | + _type = MAC6; |
| 163 | + memset(_mac.bytes, 0, sizeof(_mac.bytes)); |
| 164 | + memcpy(_mac.bytes, macbytearray, 6); |
| 165 | + return *this; |
| 166 | +} |
| 167 | + |
| 168 | +//Overloaded copy operator: init MacAddress object from uint64_t |
| 169 | +MacAddress& MacAddress::operator=(uint64_t macval) { |
| 170 | + // 6-bytes MacAddress only |
| 171 | + _type = MAC6; |
| 172 | + _mac.val = macval; |
| 173 | + return *this; |
| 174 | +} |
| 175 | + |
| 176 | +//Compare class to byte array |
| 177 | +bool MacAddress::operator==(const uint8_t *macbytearray) const { |
| 178 | + return !memcmp(_mac.bytes, macbytearray, 6); |
| 179 | +} |
| 180 | + |
| 181 | +//Allow comparing value of two classes |
| 182 | +bool MacAddress::operator==(const MacAddress& mac2) const { |
| 183 | + return _mac.val == mac2._mac.val; |
| 184 | +} |
| 185 | + |
| 186 | +//Type converter object to uint64_t [same as .Value()] |
| 187 | +MacAddress::operator uint64_t() const { |
| 188 | + return _mac.val; |
| 189 | +} |
| 190 | + |
| 191 | +//Type converter object to read only pointer to mac bytes. e.g. const uint8_t *ip_8 = ma; |
| 192 | +MacAddress::operator const uint8_t*() const { |
| 193 | + return _mac.bytes; |
| 194 | +} |
| 195 | + |
| 196 | +//Type converter object to read only pointer to mac value. e.g. const uint32_t *ip_64 = ma; |
| 197 | +MacAddress::operator const uint64_t*() const { |
| 198 | + return &_mac.val; |
| 199 | +} |
| 200 | + |
| 201 | +size_t MacAddress::printTo(Print& p) const |
| 202 | +{ |
| 203 | + uint8_t bytes = (_type == MAC6) ? 6 : 8; |
| 204 | + size_t n = 0; |
| 205 | + for(int i = 0; i < bytes; i++) { |
| 206 | + if(i){ |
| 207 | + n += p.print(':'); |
| 208 | + } |
| 209 | + n += p.printf("%02X", _mac.bytes[i]); |
| 210 | + } |
| 211 | + return n; |
| 212 | +} |
| 213 | + |
| 214 | +//Bounds checking |
| 215 | +int MacAddress::EnforceIndexBounds(int i) const { |
| 216 | + if(i < 0) { |
| 217 | + return 0; |
| 218 | + } |
| 219 | + if(_type == MAC6) { |
| 220 | + if(i >= 6) { |
| 221 | + return 5; |
| 222 | + } |
| 223 | + } else { |
| 224 | + if(i >= 8) { |
| 225 | + return 7; |
| 226 | + } |
| 227 | + } |
| 228 | + return i; |
| 229 | +} |
0 commit comments