|
25 | 25 | #include <string> |
26 | 26 | #include <cstring> // strncmp |
27 | 27 |
|
28 | | -namespace hw { |
29 | | -// MAC address |
30 | | -union MAC_addr { |
31 | | - // no. parts in a MAC address |
32 | | - static constexpr size_t PARTS_LEN = 6; |
33 | | - // The parts of the MAC address |
34 | | - uint8_t part[PARTS_LEN]; |
35 | | - |
36 | | - struct { |
37 | | - uint16_t minor; |
38 | | - uint32_t major; |
39 | | - } __attribute__((packed)); |
40 | | - |
41 | | - MAC_addr() noexcept : part{} {} |
42 | | - |
43 | | - MAC_addr(const uint8_t a, const uint8_t b, const uint8_t c, |
44 | | - const uint8_t d, const uint8_t e, const uint8_t f) noexcept |
45 | | - : part{a,b,c,d,e,f} |
46 | | - {} |
47 | | - |
48 | | - MAC_addr& operator=(const MAC_addr cpy) noexcept { |
49 | | - minor = cpy.minor; |
50 | | - major = cpy.major; |
51 | | - return *this; |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * @brief Hex representation of a MAC address |
56 | | - * |
57 | | - * @return hex string representation |
58 | | - */ |
59 | | - std::string hex_str() const { |
60 | | - char hex_addr[18]; |
61 | | - snprintf(hex_addr, sizeof(hex_addr), "%02x:%02x:%02x:%02x:%02x:%02x", |
62 | | - part[0], part[1], part[2], |
63 | | - part[3], part[4], part[5]); |
64 | | - return hex_addr; |
65 | | - } |
66 | | - |
67 | | - /** |
68 | | - * @brief String representation of a MAC address |
69 | | - * @note default is hex |
70 | | - * @return string representation of the MAC address |
71 | | - */ |
72 | | - std::string str() const |
73 | | - { return hex_str(); } |
74 | | - |
75 | | - std::string to_string() const |
76 | | - { return str(); } |
77 | | - |
78 | | - operator std::string () const |
79 | | - { return str(); } |
80 | | - |
81 | | - /** Check for equality */ |
82 | | - bool operator==(const MAC_addr mac) const noexcept |
83 | | - { |
84 | | - return strncmp( |
85 | | - reinterpret_cast<const char*>(part), |
86 | | - reinterpret_cast<const char*>(mac.part), |
87 | | - PARTS_LEN) == 0; |
88 | | - } |
89 | | - |
90 | | -} __attribute__((packed)); //< union addr |
| 28 | +namespace MAC { |
| 29 | + |
| 30 | + // MAC address |
| 31 | + union Addr { |
| 32 | + // no. parts in a MAC address |
| 33 | + static constexpr size_t PARTS_LEN = 6; |
| 34 | + // The parts of the MAC address |
| 35 | + uint8_t part[PARTS_LEN]; |
| 36 | + |
| 37 | + struct { |
| 38 | + uint16_t minor; |
| 39 | + uint32_t major; |
| 40 | + } __attribute__((packed)); |
| 41 | + |
| 42 | + Addr() noexcept : part{} {} |
| 43 | + |
| 44 | + Addr(const uint8_t a, const uint8_t b, const uint8_t c, |
| 45 | + const uint8_t d, const uint8_t e, const uint8_t f) noexcept |
| 46 | + : part{a,b,c,d,e,f} |
| 47 | + {} |
| 48 | + |
| 49 | + Addr& operator=(const Addr cpy) noexcept { |
| 50 | + minor = cpy.minor; |
| 51 | + major = cpy.major; |
| 52 | + return *this; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + /** |
| 57 | + * @brief Hex representation of a MAC address |
| 58 | + * |
| 59 | + * @return hex string representation |
| 60 | + */ |
| 61 | + std::string hex_str() const { |
| 62 | + char hex_addr[18]; |
| 63 | + snprintf(hex_addr, sizeof(hex_addr), "%02x:%02x:%02x:%02x:%02x:%02x", |
| 64 | + part[0], part[1], part[2], |
| 65 | + part[3], part[4], part[5]); |
| 66 | + return hex_addr; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @brief String representation of a MAC address |
| 71 | + * @note default is hex |
| 72 | + * @return string representation of the MAC address |
| 73 | + */ |
| 74 | + std::string str() const |
| 75 | + { return hex_str(); } |
| 76 | + |
| 77 | + std::string to_string() const |
| 78 | + { return str(); } |
| 79 | + |
| 80 | + operator std::string () const |
| 81 | + { return str(); } |
| 82 | + |
| 83 | + /** Check for equality */ |
| 84 | + bool operator==(const Addr mac) const noexcept |
| 85 | + { |
| 86 | + return strncmp( |
| 87 | + reinterpret_cast<const char*>(part), |
| 88 | + reinterpret_cast<const char*>(mac.part), |
| 89 | + PARTS_LEN) == 0; |
| 90 | + } |
| 91 | + |
| 92 | + bool operator!=(const Addr mac) const noexcept |
| 93 | + { return not(*this == mac); } |
| 94 | + |
| 95 | + } __attribute__((packed)); //< union addr |
| 96 | + |
| 97 | + const Addr EMPTY {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
| 98 | + |
| 99 | + // uint16_t(0xFFFF), uint32_t(0xFFFFFFFF) |
| 100 | + const Addr BROADCAST {0xff,0xff,0xff,0xff,0xff,0xff}; |
| 101 | + |
| 102 | + // uint16_t(0x0000), uint32_t(0x01000000) |
| 103 | + const Addr MULTICAST {0,0,0x01,0,0,0}; |
| 104 | + |
| 105 | + // uint16_t(0x3333), uint32_t(0x01000000) |
| 106 | + const Addr IPv6mcast_01 {0x33,0x33,0x01,0,0,0}; |
| 107 | + |
| 108 | + // uint16_t(0x3333), uint32_t(0x02000000) |
| 109 | + const Addr IPv6mcast_02 {0x33,0x33,0x02,0,0,0}; |
| 110 | + |
| 111 | + |
91 | 112 | } |
92 | 113 |
|
93 | 114 | #endif |
0 commit comments