Vanetza
Loading...
Searching...
No Matches
mac_address.hpp
1#ifndef MAC_ADDRESS_HPP_FDINBLBS
2#define MAC_ADDRESS_HPP_FDINBLBS
3
4#include <vanetza/common/serialization.hpp>
5#include <boost/operators.hpp>
6#include <boost/optional.hpp>
7#include <array>
8#include <cstddef>
9#include <cstdint>
10#include <initializer_list>
11#include <functional>
12#include <ostream>
13#include <string>
14
15namespace vanetza
16{
17
19{
20public:
21 static constexpr std::size_t length_bytes = 6;
22
23 MacAddress();
24 MacAddress(std::initializer_list<uint8_t> args);
25 MacAddress(const MacAddress&) = default;
26 MacAddress& operator=(const MacAddress&) = default;
27 MacAddress(MacAddress&&) = default;
28 MacAddress& operator=(MacAddress&&) = default;
29
30 std::array<uint8_t, length_bytes> octets;
31};
32
33extern const MacAddress cBroadcastMacAddress;
34bool operator==(const MacAddress& lhs, const MacAddress& rhs);
35bool operator<(const MacAddress& lhs, const MacAddress& rhs);
36std::ostream& operator<<(std::ostream& os, const MacAddress&);
37
38/**
39 * Try to parse MAC address from string
40 * \param str source string with "XX:XX:XX:XX:XX:XX" format
41 * \param addr pass parsed address by reference
42 * \return true if successfully parsed
43 */
44bool parse_mac_address(const std::string& str, MacAddress& addr);
45
46/**
47 * Try to parse MAC address from string
48 * \param str source string with "XX:XX:XX:XX:XX:XX" format
49 * \return parsed address if successful
50 */
51boost::optional<MacAddress> parse_mac_address(const std::string& str);
52
53/**
54 * Derive a MAC address from an arbitrary integral value.
55 * \param value used to derive MAC address, it's size does not matter
56 * \return New MAC address
57 */
58template<typename T>
59MacAddress create_mac_address(T value)
60{
61 MacAddress mac;
62 const std::size_t octets = std::min(mac.octets.size(), sizeof(T));
63 for (std::size_t i = 0; i < octets; ++i) {
64 mac.octets[i] = value >> (8 * (octets - i - 1)) & 0xff;
65 }
66 return mac;
67}
68
69/**
70 * Serialize MAC address
71 * \param out output sink
72 * \param addr MAC address
73 */
74void serialize(OutputArchive& out, const MacAddress& addr);
75
76/**
77 * Deserialize MAC address
78 * \param in input source
79 * \param addr deserialize into this object
80 */
81void deserialize(InputArchive& in, MacAddress& addr);
82
83} // namespace vanetza
84
85// specialization
86namespace std {
87
88template<>
89struct hash<vanetza::MacAddress>
90{
91 size_t operator()(const vanetza::MacAddress& addr) const
92 {
93 size_t tmp = 0;
94 for (auto octet : addr.octets) {
95 tmp ^= hash<decltype(octet)>()(octet);
96 }
97 return tmp;
98 }
99};
100
101} // namespace std
102
103#endif /* MAC_ADDRESS_HPP_FDINBLBS */
static constexpr T mask
static constexpr std::size_t bits
Link layer header used by ITS-G5 stations.
Logical Link Control header with SNAP extension.
Frame Control field in IEEE 802.11 MAC header.
QoS Control field in IEEE 802.11 MAC header.
Sequence Control field in IEEE 802.11 MAC header.