Vanetza
 
Loading...
Searching...
No Matches
common_header.cpp
1#include "common_header.hpp"
2#include "data_request.hpp"
3#include "serialization.hpp"
4#include <stdexcept>
5
6namespace vanetza
7{
8namespace geonet
9{
10
11constexpr std::size_t CommonHeader::length_bytes;
12
13CommonHeader::CommonHeader() :
14 next_header(NextHeaderCommon::Any),
15 reserved1(0),
16 header_type(HeaderType::Any),
17 flags(0),
18 payload(0),
19 maximum_hop_limit(0),
20 reserved2(0)
21{
22}
23
24CommonHeader::CommonHeader(const MIB& mib) :
25 next_header(NextHeaderCommon::Any),
26 reserved1(0),
27 header_type(HeaderType::Any),
28 traffic_class(mib.itsGnDefaultTrafficClass),
29 flags(mib.itsGnIsMobile ? 0x80 : 0x00),
30 payload(0),
31 maximum_hop_limit(mib.itsGnDefaultHopLimit),
32 reserved2(0)
33{
34}
35
36CommonHeader::CommonHeader(const DataRequest& request, const MIB& mib) :
37 CommonHeader(mib)
38{
39 switch (request.upper_protocol) {
40 case UpperProtocol::BTP_A:
41 next_header = NextHeaderCommon::BTP_A;
42 break;
43 case UpperProtocol::BTP_B:
44 next_header = NextHeaderCommon::BTP_B;
45 break;
46 case UpperProtocol::IPv6:
47 next_header = NextHeaderCommon::IPv6;
48 break;
49 default:
50 throw std::runtime_error("Unhandled upper protocol");
51 break;
52 }
53
54 traffic_class = request.traffic_class;
55 maximum_hop_limit = request.max_hop_limit;
56}
57
58CommonHeader::CommonHeader(const ShbDataRequest& request, const MIB& mib) :
59 CommonHeader(static_cast<const DataRequest&>(request), mib)
60{
61 header_type = HeaderType::TSB_Single_Hop;
62 maximum_hop_limit = 1;
63}
64
65void serialize(const CommonHeader& hdr, OutputArchive& ar)
66{
67 uint8_t nextHeaderAndReserved = static_cast<uint8_t>(hdr.next_header);
68 nextHeaderAndReserved <<= 4;
69 nextHeaderAndReserved |= hdr.reserved1.raw();
70 serialize(host_cast(nextHeaderAndReserved), ar);
71 serialize(host_cast(static_cast<std::underlying_type<HeaderType>::type>(hdr.header_type)), ar);
72 serialize(hdr.traffic_class, ar);
73 serialize(host_cast(hdr.flags), ar);
74 serialize(host_cast(hdr.payload), ar);
75 serialize(host_cast(hdr.maximum_hop_limit), ar);
76 serialize(host_cast(hdr.reserved2), ar);
77}
78
79void deserialize(CommonHeader& hdr, InputArchive& ar)
80{
81 uint8_t nextHeaderAndReserved;
82 deserialize(nextHeaderAndReserved, ar);
83 hdr.next_header = static_cast<NextHeaderCommon>(nextHeaderAndReserved >> 4);
84 hdr.reserved1 = nextHeaderAndReserved & 0x0f;
85 typename std::underlying_type<HeaderType>::type headerType;
86 deserialize(headerType, ar);
87 hdr.header_type = static_cast<HeaderType>(headerType);
88 deserialize(hdr.traffic_class, ar);
89 deserialize(hdr.flags, ar);
90 deserialize(hdr.payload, ar);
91 deserialize(hdr.maximum_hop_limit, ar);
92 deserialize(hdr.reserved2, ar);
93}
94
95} // namespace geonet
96} // namespace vanetza
97