1 // Copyright (C) 2015-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 "external_local_routing_test_service.hpp"
7
external_local_routing_test_service(bool _use_static_routing)8 external_local_routing_test_service::external_local_routing_test_service(bool _use_static_routing) :
9 app_(vsomeip::runtime::get()->create_application()),
10 is_registered_(false),
11 use_static_routing_(_use_static_routing),
12 blocked_(false),
13 number_received_messages_local_(0),
14 number_received_messages_external_(0),
15 offer_thread_(std::bind(&external_local_routing_test_service::run, this))
16 {
17 }
18
init()19 bool external_local_routing_test_service::init()
20 {
21 std::lock_guard<std::mutex> its_lock(mutex_);
22
23 if (!app_->init()) {
24 ADD_FAILURE() << "Couldn't initialize application";
25 return false;
26 }
27 app_->register_message_handler(vsomeip_test::TEST_SERVICE_SERVICE_ID,
28 vsomeip_test::TEST_SERVICE_INSTANCE_ID, vsomeip_test::TEST_SERVICE_METHOD_ID,
29 std::bind(&external_local_routing_test_service::on_message, this,
30 std::placeholders::_1));
31
32 app_->register_message_handler(vsomeip_test::TEST_SERVICE_SERVICE_ID,
33 vsomeip_test::TEST_SERVICE_INSTANCE_ID, vsomeip_test::TEST_SERVICE_METHOD_ID,
34 std::bind(&external_local_routing_test_service::on_message, this,
35 std::placeholders::_1));
36
37 app_->register_state_handler(
38 std::bind(&external_local_routing_test_service::on_state, this,
39 std::placeholders::_1));
40
41 VSOMEIP_INFO << "Static routing " << (use_static_routing_ ? "ON" : "OFF");
42 return true;
43 }
44
start()45 void external_local_routing_test_service::start()
46 {
47 VSOMEIP_INFO << "Starting...";
48 app_->start();
49 }
50
stop()51 void external_local_routing_test_service::stop()
52 {
53 VSOMEIP_INFO << "Stopping...";
54 app_->clear_all_handler();
55 app_->stop();
56 }
57
join_offer_thread()58 void external_local_routing_test_service::join_offer_thread()
59 {
60 offer_thread_.join();
61 }
62
offer()63 void external_local_routing_test_service::offer()
64 {
65 app_->offer_service(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID);
66 }
67
stop_offer()68 void external_local_routing_test_service::stop_offer()
69 {
70 app_->stop_offer_service(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID);
71 }
72
on_state(vsomeip::state_type_e _state)73 void external_local_routing_test_service::on_state(vsomeip::state_type_e _state)
74 {
75 VSOMEIP_INFO << "Application " << app_->get_name() << " is "
76 << (_state == vsomeip::state_type_e::ST_REGISTERED ? "registered." :
77 "deregistered.");
78
79 if(_state == vsomeip::state_type_e::ST_REGISTERED)
80 {
81 if(!is_registered_)
82 {
83 std::lock_guard<std::mutex> its_lock(mutex_);
84 is_registered_ = true;
85 blocked_ = true;
86 // "start" the run method thread
87 condition_.notify_one();
88 }
89 }
90 else
91 {
92 is_registered_ = false;
93 }
94 }
95
on_message(const std::shared_ptr<vsomeip::message> & _request)96 void external_local_routing_test_service::on_message(
97 const std::shared_ptr<vsomeip::message>& _request)
98 {
99 VSOMEIP_INFO << "Received a message with Client/Session [" << std::setw(4)
100 << std::setfill('0') << std::hex << _request->get_client() << "/"
101 << std::setw(4) << std::setfill('0') << std::hex
102 << _request->get_session() << "]";
103
104 ASSERT_EQ(_request->get_service(), vsomeip_test::TEST_SERVICE_SERVICE_ID);
105 ASSERT_EQ(_request->get_method(), vsomeip_test::TEST_SERVICE_METHOD_ID);
106
107 // Check the protocol version this shall be set to 0x01 according to the spec.
108 // TR_SOMEIP_00052
109 ASSERT_EQ(_request->get_protocol_version(), 0x01);
110 // Check the message type this shall be 0xx (REQUEST) according to the spec.
111 // TR_SOMEIP_00055
112 ASSERT_EQ(_request->get_message_type(), vsomeip::message_type_e::MT_REQUEST);
113
114 if(_request->get_client() == vsomeip_test::TEST_CLIENT_CLIENT_ID)
115 {
116 number_received_messages_local_++;
117 // check the session id.
118 ASSERT_EQ(_request->get_session(),
119 static_cast<vsomeip::session_t>(number_received_messages_local_));
120 }
121 else if (_request->get_client() == vsomeip_test::TEST_CLIENT_EXTERNAL_CLIENT_ID)
122 {
123 number_received_messages_external_++;
124 // check the session id.
125 ASSERT_EQ(_request->get_session(),
126 static_cast<vsomeip::session_t>(number_received_messages_external_));
127 }
128
129 // send response
130 std::shared_ptr<vsomeip::message> its_response =
131 vsomeip::runtime::get()->create_response(_request);
132
133 app_->send(its_response);
134
135 if(number_received_messages_local_ >= vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND
136 && number_received_messages_external_ >= vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND)
137 {
138 std::lock_guard<std::mutex> its_lock(mutex_);
139 blocked_ = true;
140 condition_.notify_one();
141 }
142 ASSERT_LT(number_received_messages_local_,
143 vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND + 1);
144 ASSERT_LT(number_received_messages_external_,
145 vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND + 1);
146 }
147
run()148 void external_local_routing_test_service::run()
149 {
150 std::unique_lock<std::mutex> its_lock(mutex_);
151 while (!blocked_)
152 condition_.wait(its_lock);
153
154 blocked_ = false;
155 if(use_static_routing_)
156 {
157 offer();
158 }
159
160 while (!blocked_) {
161 condition_.wait(its_lock);
162 }
163
164 std::thread t2([](){ std::this_thread::sleep_for(std::chrono::microseconds(1000000 * 2));});
165 t2.join();
166 app_->stop();
167 }
168
TEST(someip_external_local_routing_test,receive_ten_messages_over_local_and_external_socket)169 TEST(someip_external_local_routing_test, receive_ten_messages_over_local_and_external_socket)
170 {
171 bool use_static_routing = true;
172 external_local_routing_test_service test_service(use_static_routing);
173 if (test_service.init()) {
174 test_service.start();
175 test_service.join_offer_thread();
176 }
177 }
178
179 #ifndef _WIN32
main(int argc,char ** argv)180 int main(int argc, char** argv)
181 {
182 ::testing::InitGoogleTest(&argc, argv);
183 return RUN_ALL_TESTS();
184 }
185 #endif
186