1 // Copyright (C) 2016-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 
6 #include <cstring>
7 
8 #include "../include/header.hpp"
9 #include "../../endpoints/include/endpoint.hpp"
10 #include "../../endpoints/include/client_endpoint.hpp"
11 #include "../../utility/include/byteorder.hpp"
12 
13 namespace vsomeip_v3 {
14 namespace trace {
15 
prepare(const std::shared_ptr<endpoint> & _endpoint,bool _is_sending,instance_t _instance)16 bool header::prepare(const std::shared_ptr<endpoint> &_endpoint,
17         bool _is_sending, instance_t _instance) {
18     return prepare(_endpoint.get(), _is_sending, _instance);
19 }
20 
prepare(const endpoint * _endpoint,bool _is_sending,instance_t _instance)21 bool header::prepare(const endpoint *_endpoint, bool _is_sending,
22         instance_t _instance) {
23     boost::asio::ip::address its_address;
24     unsigned short its_port(0);
25     protocol_e its_protocol(protocol_e::unknown);
26 
27     if (_endpoint) {
28         const client_endpoint* its_client_endpoint =
29                 dynamic_cast<const client_endpoint*>(_endpoint);
30         if (its_client_endpoint) {
31 
32             its_client_endpoint->get_remote_address(its_address);
33             if (its_address.is_v6()) {
34                 return false;
35             }
36 
37             its_port = its_client_endpoint->get_remote_port();
38 
39             if (_endpoint->is_local()) {
40                 its_protocol = protocol_e::local;
41             } else {
42                 if (_endpoint->is_reliable()) {
43                     its_protocol = protocol_e::tcp;
44                 } else {
45                     its_protocol = protocol_e::udp;
46                 }
47             }
48         }
49     }
50     prepare(its_address.to_v4(), its_port, its_protocol, _is_sending, _instance);
51     return true;
52 }
53 
prepare(const boost::asio::ip::address_v4 & _address,std::uint16_t _port,protocol_e _protocol,bool _is_sending,instance_t _instance)54 void header::prepare(const boost::asio::ip::address_v4 &_address,
55         std::uint16_t _port, protocol_e _protocol,
56         bool _is_sending, instance_t _instance) {
57     unsigned long its_address_as_long = _address.to_ulong();
58     data_[0] = VSOMEIP_LONG_BYTE3(its_address_as_long);
59     data_[1] = VSOMEIP_LONG_BYTE2(its_address_as_long);
60     data_[2] = VSOMEIP_LONG_BYTE1(its_address_as_long);
61     data_[3] = VSOMEIP_LONG_BYTE0(its_address_as_long);
62     data_[4] = VSOMEIP_WORD_BYTE1(_port);
63     data_[5] = VSOMEIP_WORD_BYTE0(_port);
64     data_[6] = static_cast<byte_t>(_protocol);
65     data_[7] = static_cast<byte_t>(_is_sending);
66     data_[8] = VSOMEIP_WORD_BYTE1(_instance);
67     data_[9] = VSOMEIP_WORD_BYTE0(_instance);
68 }
69 
70 } // namespace trace
71 } // namespace vsomeip_v3
72