Files
ESP8266lib/net/MAC.h
2017-05-23 21:42:01 +02:00

79 lines
1.5 KiB
C++

#ifndef MAC_H
#define MAC_H
#include <string>
#include <cstdint>
#include <string.h>
namespace WiFiRaw {
struct MACAddress {
uint8_t a;
uint8_t b;
uint8_t c;
uint8_t d;
uint8_t e;
uint8_t f;
/** empty ctor */
MACAddress() {;}
/** ctor from distinct values */
MACAddress(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d, const uint8_t e, const uint8_t f) :
a(a), b(b), c(c), d(d), e(e), f(f) {;}
/** ctor from memory region */
MACAddress(const uint8_t* data) {
memcpy(this, data, 6);
}
/** convert to pointer */
const uint8_t* asPtr() const {
return (uint8_t*) this;
}
/** equal to the given mac? */
bool operator == (const MACAddress& o) const {
return (a == o.a) && (b == o.b) && (c == o.c) && (d == o.d) && (e == o.e) && (f == o.f);
}
std::string asString() const {
std::string mac; mac.resize(17);
mac[0] = toHex(a >> 4);
mac[1] = toHex(a >> 0);
mac[2] = ':';
mac[3] = toHex(b >> 4);
mac[4] = toHex(b >> 0);
mac[5] = ':';
mac[6] = toHex(c >> 4);
mac[7] = toHex(c >> 0);
mac[8] = ':';
mac[9] = toHex(d >> 4);
mac[10] = toHex(d >> 0);
mac[11] = ':';
mac[12] = toHex(e >> 4);
mac[13] = toHex(e >> 0);
mac[14] = ':';
mac[15] = toHex(f >> 4);
mac[16] = toHex(f >> 0);
return mac;
}
private:
inline char toHex(uint8_t c) const {
const uint8_t val = c & 0xF;
return (val >= 10) ? ('A' + val - 10) : ('0' + val);
}
};
}
#endif // MAC_H