Vanetza
 
Loading...
Searching...
No Matches
port_dispatcher.cpp
1#include "port_dispatcher.hpp"
2#include "data_indication.hpp"
3#include <vanetza/geonet/data_indication.hpp>
4#include <algorithm>
5#include <cassert>
6
7namespace vanetza
8{
9namespace btp
10{
11
12boost::optional<DataIndication> parse_btp_header(const geonet::DataIndication& gn_ind, PacketVariant& packet)
13{
14 boost::optional<DataIndication> indication;
15
16 switch (gn_ind.upper_protocol) {
17 case geonet::UpperProtocol::BTP_A: {
18 HeaderA hdr = parse_btp_a(packet);
19 indication = DataIndication(gn_ind, hdr);
20 }
21 break;
22 case geonet::UpperProtocol::BTP_B: {
23 HeaderB hdr = parse_btp_b(packet);
24 indication = DataIndication(gn_ind, hdr);
25 }
26 break;
27 default:
28 // drop non-BTP packet
29 break;
30 }
31
32 return indication;
33}
34
36{
37 if (hook != nullptr) {
38 auto it = std::find(m_promiscuous_hooks.begin(), m_promiscuous_hooks.end(), hook);
39 if (it == m_promiscuous_hooks.end()) {
40 m_promiscuous_hooks.push_back(hook);
41 }
42 }
43}
44
46{
47 m_promiscuous_hooks.remove(hook);
48}
49
51 port_type port,
52 IndicationInterface* handler)
53{
54 m_interactive_handlers[port] = handler;
55}
56
58 port_type port,
59 IndicationInterface* handler)
60{
61 m_non_interactive_handlers[port] = handler;
62}
63
64void PortDispatcher::indicate(
65 const geonet::DataIndication& gn_ind,
66 std::unique_ptr<UpPacket> packet)
67{
68 assert(packet);
69 boost::optional<DataIndication> btp_ind = parse_btp_header(gn_ind, *packet);
70 IndicationInterface* handler = nullptr;
71
72 if (btp_ind) {
73 if (btp_ind->source_port) {
74 handler = m_interactive_handlers[btp_ind->destination_port];
75 } else {
76 handler = m_non_interactive_handlers[btp_ind->destination_port];
77 }
78
79 for (PromiscuousHook* hook : m_promiscuous_hooks) {
80 hook->tap_packet(*btp_ind, *packet);
81 }
82
83 if (handler) {
84 handler->indicate(*btp_ind, std::move(packet));
85 } else {
86 hook_undispatched(gn_ind, &btp_ind.get());
87 }
88 } else {
89 hook_undispatched(gn_ind, nullptr);
90 }
91}
92
93} // namespace btp
94} // namespace vanetza
void add_promiscuous_hook(PromiscuousHook *hook)
void remove_promiscuous_hook(PromiscuousHook *)
void set_interactive_handler(port_type, IndicationInterface *)
Hook< const geonet::DataIndication &, const btp::DataIndication * > hook_undispatched
void set_non_interactive_handler(port_type, IndicationInterface *)