Vanetza
 
Loading...
Searching...
No Matches
lifetime.cpp
1#include "lifetime.hpp"
2#include "serialization.hpp"
3#include <vanetza/common/byte_order.hpp>
4#include <boost/units/cmath.hpp>
5#include <stdexcept>
6
7namespace vanetza
8{
9namespace geonet
10{
11
12const Lifetime Lifetime::zero()
13{
14 return Lifetime();
15}
16
17Lifetime::Lifetime()
18{
19 set(Base::Fifty_Milliseconds, 0);
20}
21
22Lifetime::Lifetime(Base base, BitNumber<uint8_t, 6> multiplier)
23{
24 set(base, multiplier);
25}
26
27void Lifetime::set(Base base, BitNumber<uint8_t, 6> multiplier)
28{
29 m_lifetime = multiplier.raw() << 2 | (static_cast<uint8_t>(base) & base_mask);
30}
31
32bool Lifetime::operator<(const Lifetime& other) const
33{
34 return this->decode() < other.decode();
35}
36
37bool Lifetime::operator==(const Lifetime& other) const
38{
39 const units::Duration diff = this->decode() - other.decode();
40 // 50 ms is the smallest non-zero value Lifetime can represent
41 const auto min_value = 0.050 * units::si::seconds;
42 return abs(diff) < min_value;
43}
44
45void Lifetime::encode(units::Duration duration)
46{
47 double seconds = duration / boost::units::si::seconds;
48 if (seconds >= 630.0) {
49 set(Base::Hundred_Seconds, std::lround(seconds / 100.0));
50 } else if (seconds >= 63.0) {
51 set(Base::Ten_Seconds, std::lround(seconds / 10.0));
52 } else if (seconds >= 3.15) {
53 set(Base::One_Second, std::lround(seconds));
54 } else {
55 set(Base::Fifty_Milliseconds, std::lround(seconds / 0.050));
56 }
57}
58
59units::Duration Lifetime::decode() const
60{
61 using vanetza::units::si::seconds;
62 Base base = static_cast<Base>(m_lifetime & base_mask);
63 const double multiplier = (m_lifetime & multiplier_mask) >> 2;
64 units::Duration unit;
65
66 switch (base) {
67 case Base::Fifty_Milliseconds:
68 unit = 0.050 * seconds;
69 break;
70 case Base::One_Second:
71 unit = 1.0 * seconds;
72 // already done
73 break;
74 case Base::Ten_Seconds:
75 unit = 10.0 * seconds;
76 break;
77 case Base::Hundred_Seconds:
78 unit = 100.0 * seconds;
79 break;
80 default:
81 throw std::runtime_error("Decoding of Lifetime::Base failed");
82 break;
83 };
84
85 return multiplier * unit;
86}
87
88void serialize(const Lifetime& lifetime, OutputArchive& ar)
89{
90 serialize(host_cast(lifetime.raw()), ar);
91}
92
93void deserialize(Lifetime& lifetime, InputArchive& ar)
94{
95 uint8_t raw;
96 deserialize(raw, ar);
97 lifetime.raw(raw);
98}
99
100} // namespace geonet
101} // namespace vanetza
102
void encode(units::Duration)
Definition: lifetime.cpp:45
units::Duration decode() const
Definition: lifetime.cpp:59