Vanetza
 
Loading...
Searching...
No Matches
address.cpp
1#include "address.hpp"
2#include "serialization.hpp"
3#include <boost/functional/hash.hpp>
4
5namespace vanetza
6{
7namespace geonet
8{
9
10constexpr std::size_t Address::length_bytes;
11
12namespace // anonymous namespace for local constants
13{
14constexpr uint16_t manually_configured_mask = 0x8000;
15constexpr uint16_t station_type_mask = 0x7c00;
16constexpr uint16_t country_code_mask = 0x03ff;
17constexpr unsigned station_type_shift = 10;
18} // namespace
19
20Address::Address() :
21 m_manually_configured(false),
22 m_station_type(StationType::Unknown),
23 m_country_code(0)
24{
25}
26
27Address::Address(const MacAddress& addr) :
28 m_manually_configured(false),
29 m_station_type(StationType::Unknown),
30 m_country_code(0),
31 m_mid(addr)
32{
33}
34
35bool Address::operator==(const Address& other) const
36{
37 return (this->m_manually_configured == other.m_manually_configured &&
38 this->m_station_type == other.m_station_type &&
39 this->m_country_code == other.m_country_code &&
40 this->m_mid == other.m_mid);
41}
42
43bool Address::operator!=(const Address& other) const
44{
45 return !(*this == other);
46}
47
48void serialize(const Address& addr, OutputArchive& ar)
49{
50 uint16_t manuallyConfiguredAndTypeAndCountryCode = addr.country_code().raw();
51 manuallyConfiguredAndTypeAndCountryCode |=
52 (static_cast<uint16_t>(addr.station_type()) << station_type_shift) & station_type_mask;
53 manuallyConfiguredAndTypeAndCountryCode |=
54 addr.is_manually_configured() ? manually_configured_mask : 0x0000;
55 serialize(host_cast(manuallyConfiguredAndTypeAndCountryCode), ar);
56 serialize(ar, addr.mid());
57}
58
59void deserialize(Address& addr, InputArchive& ar)
60{
61 uint16_t tmp;
62 deserialize(tmp, ar);
63 addr.is_manually_configured((tmp & manually_configured_mask) != 0);
64 addr.country_code(tmp & country_code_mask);
65 addr.station_type(static_cast<StationType>((tmp & station_type_mask) >> station_type_shift));
66 MacAddress mid;
67 deserialize(ar, mid);
68 addr.mid(mid);
69}
70
71} // namespace geonet
72} // namespace vanetza
73
74namespace std
75{
76
77namespace gn = vanetza::geonet;
78size_t hash<gn::Address>::operator()(const gn::Address& addr) const
79{
80 size_t seed = 0;
81 boost::hash_combine(seed, addr.is_manually_configured());
82 boost::hash_combine(seed, addr.station_type());
83 boost::hash_combine(seed, addr.country_code().raw());
84 boost::hash_range(seed, addr.mid().octets.begin(), addr.mid().octets.end());
85 return seed;
86}
87
88} // namespace std
STL namespace.