1 // Copyright (C) 2014-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 <boost/asio/ip/tcp.hpp>
7 #include <boost/asio/ip/udp.hpp>
8 #include <boost/asio/ip/udp_ext.hpp>
9 #include <boost/asio/local/stream_protocol.hpp>
10 #include <boost/asio/local/stream_protocol_ext.hpp>
11 
12 #include <vsomeip/constants.hpp>
13 #include <vsomeip/defines.hpp>
14 #include <vsomeip/internal/logger.hpp>
15 
16 #include "../include/endpoint_host.hpp"
17 #include "../../routing/include/routing_host.hpp"
18 #include "../include/endpoint_impl.hpp"
19 
20 namespace vsomeip_v3 {
21 
22 template<typename Protocol>
endpoint_impl(const std::shared_ptr<endpoint_host> & _endpoint_host,const std::shared_ptr<routing_host> & _routing_host,const endpoint_type & _local,boost::asio::io_service & _io,std::uint32_t _max_message_size,configuration::endpoint_queue_limit_t _queue_limit,const std::shared_ptr<configuration> & _configuration)23 endpoint_impl<Protocol>::endpoint_impl(
24         const std::shared_ptr<endpoint_host>& _endpoint_host,
25         const std::shared_ptr<routing_host>& _routing_host,
26         const endpoint_type& _local,
27         boost::asio::io_service &_io,
28         std::uint32_t _max_message_size,
29         configuration::endpoint_queue_limit_t _queue_limit,
30         const std::shared_ptr<configuration>& _configuration)
31     : service_(_io),
32       endpoint_host_(_endpoint_host),
33       routing_host_(_routing_host),
34       is_supporting_magic_cookies_(false),
35       has_enabled_magic_cookies_(false),
36       max_message_size_(_max_message_size),
37       use_count_(0),
38       sending_blocked_(false),
39       local_(_local),
40       queue_limit_(_queue_limit),
41       configuration_(_configuration),
42       is_supporting_someip_tp_(false) {
43 }
44 
45 template<typename Protocol>
~endpoint_impl()46 endpoint_impl<Protocol>::~endpoint_impl() {
47 }
48 
49 template<typename Protocol>
enable_magic_cookies()50 void endpoint_impl<Protocol>::enable_magic_cookies() {
51     has_enabled_magic_cookies_ = is_supporting_magic_cookies_;
52 }
53 
54 template<typename Protocol>
find_magic_cookie(byte_t * _buffer,size_t _size)55 uint32_t endpoint_impl<Protocol>::find_magic_cookie(
56         byte_t *_buffer, size_t _size) {
57     bool is_found(false);
58     uint32_t its_offset = 0xFFFFFFFF;
59 
60     uint8_t its_cookie_identifier, its_cookie_type;
61 
62     if (is_client()) {
63         its_cookie_identifier =
64                 static_cast<uint8_t>(MAGIC_COOKIE_SERVICE_MESSAGE);
65         its_cookie_type =
66                 static_cast<uint8_t>(MAGIC_COOKIE_SERVICE_MESSAGE_TYPE);
67     } else {
68         its_cookie_identifier =
69                 static_cast<uint8_t>(MAGIC_COOKIE_CLIENT_MESSAGE);
70         its_cookie_type =
71                 static_cast<uint8_t>(MAGIC_COOKIE_CLIENT_MESSAGE_TYPE);
72     }
73 
74     do {
75         its_offset++; // --> first loop has "its_offset = 0"
76         if (_size > its_offset + 16) {
77             is_found = (_buffer[its_offset] == 0xFF
78                      && _buffer[its_offset + 1] == 0xFF
79                      && _buffer[its_offset + 2] == its_cookie_identifier
80                      && _buffer[its_offset + 3] == 0x00
81                      && _buffer[its_offset + 4] == 0x00
82                      && _buffer[its_offset + 5] == 0x00
83                      && _buffer[its_offset + 6] == 0x00
84                      && _buffer[its_offset + 7] == 0x08
85                      && _buffer[its_offset + 8] == 0xDE
86                      && _buffer[its_offset + 9] == 0xAD
87                      && _buffer[its_offset + 10] == 0xBE
88                      && _buffer[its_offset + 11] == 0xEF
89                      && _buffer[its_offset + 12] == 0x01
90                      && _buffer[its_offset + 13] == 0x01
91                      && _buffer[its_offset + 14] == its_cookie_type
92                      && _buffer[its_offset + 15] == 0x00);
93         } else {
94             break;
95         }
96 
97     } while (!is_found);
98 
99     return (is_found ? its_offset : 0xFFFFFFFF);
100 }
101 
102 template<typename Protocol>
add_default_target(service_t,const std::string &,uint16_t)103 void endpoint_impl<Protocol>::add_default_target(
104         service_t, const std::string &, uint16_t) {
105 }
106 
107 template<typename Protocol>
remove_default_target(service_t)108 void endpoint_impl<Protocol>::remove_default_target(service_t) {
109 }
110 
111 template<typename Protocol>
increment_use_count()112 void endpoint_impl<Protocol>::increment_use_count() {
113     use_count_++;
114 }
115 
116 template<typename Protocol>
decrement_use_count()117 void endpoint_impl<Protocol>::decrement_use_count() {
118     if (use_count_ > 0)
119         use_count_--;
120 }
121 
122 template<typename Protocol>
get_use_count()123 uint32_t endpoint_impl<Protocol>::get_use_count() {
124     return use_count_;
125 }
126 
127 template<typename Protocol>
register_error_handler(error_handler_t _error_handler)128 void endpoint_impl<Protocol>::register_error_handler(error_handler_t _error_handler) {
129     std::lock_guard<std::mutex> its_lock(error_handler_mutex_);
130     this->error_handler_ = _error_handler;
131 }
132 
133 // Instantiate template
134 #ifndef _WIN32
135 template class endpoint_impl<boost::asio::local::stream_protocol>;
136 template class endpoint_impl<boost::asio::local::stream_protocol_ext>;
137 #endif
138 template class endpoint_impl<boost::asio::ip::tcp>;
139 template class endpoint_impl<boost::asio::ip::udp>;
140 template class endpoint_impl<boost::asio::ip::udp_ext>;
141 
142 } // namespace vsomeip_v3
143