Vanetza
 
Loading...
Searching...
No Matches
validity_restriction.hpp
1#ifndef VALIDITY_RESTRICTION_HPP_LMCUHYLJ
2#define VALIDITY_RESTRICTION_HPP_LMCUHYLJ
3
4#include <vanetza/common/bit_number.hpp>
5#include <vanetza/security/v2/region.hpp>
6#include <vanetza/security/v2/serialization.hpp>
7#include <boost/variant/variant.hpp>
8#include <chrono>
9
10namespace vanetza
11{
12namespace security
13{
14namespace v2
15{
16
17using Time32 = uint32_t;
18using Time64 = uint64_t;
19
21{
22public:
23 enum class Units
24 {
25 Seconds = 0x0,
26 Minutes = 0x1,
27 Hours = 0x2,
28 Sixty_Hour_Blocks = 0x3,
29 Years = 0x4
30 };
31
32 Duration();
34 Duration(uint16_t raw);
35
36 uint16_t raw() const
37 {
38 return m_raw;
39 }
40
41 /**
42 * Get duration's unit.
43 * \return unit part of raw value
44 */
45 Units unit() const
46 {
47 return static_cast<Units>(m_raw >> 13);
48 }
49
50 /**
51 * Get duration's ticks value
52 * \return value part of raw value
53 */
54 uint16_t value() const
55 {
56 return m_raw & 0x1FFF; // mask upper 3 bit
57 }
58
59 /**
60 * Convert duration to seconds.
61 * \note std::chrono::seconds is wide enough to represent 2^13 years
62 * \return duration in seconds
63 */
64 std::chrono::seconds to_seconds() const;
65
66private:
67 uint16_t m_raw;
68};
69
70size_t get_size(const Duration&);
71
72enum class ValidityRestrictionType : uint8_t
73{
74 Time_End = 0, // end_validity
75 Time_Start_And_End = 1, // start_and_end_valididty
76 Time_Start_And_Duration = 2, // start_and_duration_validity
77 Region = 3 // GeographicRegion
78};
79
81{
82 StartAndEndValidity() = default;
83 StartAndEndValidity(Time32 start, Time32 end);
84
85 Time32 start_validity;
86 Time32 end_validity;
87};
88
90{
91 StartAndDurationValidity() = default;
93
94 Time32 start_validity;
95 Duration duration;
96};
97
98using EndValidity = Time32;
99
100typedef boost::variant<EndValidity, StartAndEndValidity, StartAndDurationValidity, GeographicRegion> ValidityRestriction;
101
102/**
103 * Determines ValidityRestrictionType to a given ValidityRestriction
104 * \param ValidityRestriction
105 */
106ValidityRestrictionType get_type(const ValidityRestriction&);
107
108/**
109 * Calculates size of an object
110 * \param Object
111 * \return size_t containing the number of octets needed to serialize the object
112 */
113size_t get_size(const StartAndEndValidity&);
114size_t get_size(const StartAndDurationValidity&);
115size_t get_size(const ValidityRestriction&);
116size_t get_size(const Time32&);
117size_t get_size(const Time64&);
118size_t get_size(const Duration&);
119
120/**
121 * Deserializes a ValidityRestriction list from a binary archive
122 * \param archive with a serialized ValidityRestriction list at the beginning
123 * \param ValidityRestriction list to deserialize
124 * \return size of the ValidityRestriction list
125 */
126size_t deserialize(InputArchive&, ValidityRestriction&);
127
128/**
129 * Serializes a ValidityRestriction list into a binary archive
130 * \param archive to serialize in
131 * \param ValidityRestriction list to serialize
132 */
133void serialize(OutputArchive&, const ValidityRestriction&);
134
135namespace detail
136{
137
138template<ValidityRestrictionType>
140
141template<>
142struct validity_restriction_type<ValidityRestrictionType::Time_End>
143{
144 using type = EndValidity;
145};
146
147template<>
148struct validity_restriction_type<ValidityRestrictionType::Time_Start_And_End>
149{
151};
152
153template<>
154struct validity_restriction_type<ValidityRestrictionType::Time_Start_And_Duration>
155{
157};
158
159template<>
160struct validity_restriction_type<ValidityRestrictionType::Region>
161{
162 using type = GeographicRegion;
163};
164
165} // namespace details
166
167/**
168 * \brief resolve type for matching ValidityRestrictionType
169 *
170 * This is kind of the reverse function of get_type(const ValidityRestrictionType&)
171 */
172template<ValidityRestrictionType T>
173using validity_restriction_type = typename detail::validity_restriction_type<T>::type;
174
175} // namespace v2
176} // namespace security
177} // namespace vanetza
178
179#endif /* VALIDITY_RESTRICTION_HPP_LMCUHYLJ */
std::chrono::seconds to_seconds() const