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 <chrono>
7 #include <condition_variable>
8 #include <iomanip>
9 #include <iostream>
10 #include <sstream>
11 #include <thread>
12 #include <map>
13 #include <algorithm>
14 #include <atomic>
15
16 #include <gtest/gtest.h>
17
18 #include <vsomeip/vsomeip.hpp>
19 #include <vsomeip/internal/logger.hpp>
20
21 #ifdef ANDROID
22 #include "../../configuration/include/internal_android.hpp"
23 #else
24 #include "../../configuration/include/internal.hpp"
25 #endif // ANDROID
26
27 #include "offer_test_globals.hpp"
28
29 enum operation_mode_e {
30 SUBSCRIBE,
31 METHODCALL
32 };
33
34 class offer_test_client {
35 public:
offer_test_client(struct offer_test::service_info _service_info,operation_mode_e _mode)36 offer_test_client(struct offer_test::service_info _service_info, operation_mode_e _mode) :
37 service_info_(_service_info),
38 operation_mode_(_mode),
39 app_(vsomeip::runtime::get()->create_application("offer_test_client")),
40 wait_until_registered_(true),
41 wait_until_service_available_(true),
42 wait_for_stop_(true),
43 last_received_counter_(0),
44 last_received_response_(std::chrono::steady_clock::now()),
45 number_received_responses_(0),
46 stop_thread_(std::bind(&offer_test_client::wait_for_stop, this)),
47 send_thread_(std::bind(&offer_test_client::send, this)) {
48 if (!app_->init()) {
49 ADD_FAILURE() << "Couldn't initialize application";
50 return;
51 }
52 app_->register_state_handler(
53 std::bind(&offer_test_client::on_state, this,
54 std::placeholders::_1));
55
56 app_->register_message_handler(vsomeip::ANY_SERVICE,
57 vsomeip::ANY_INSTANCE, vsomeip::ANY_METHOD,
58 std::bind(&offer_test_client::on_message, this,
59 std::placeholders::_1));
60
61 // register availability for all other services and request their event.
62 app_->register_availability_handler(service_info_.service_id,
63 service_info_.instance_id,
64 std::bind(&offer_test_client::on_availability, this,
65 std::placeholders::_1, std::placeholders::_2,
66 std::placeholders::_3));
67 app_->request_service(service_info_.service_id,
68 service_info_.instance_id);
69
70 if (operation_mode_ == operation_mode_e::SUBSCRIBE) {
71 std::set<vsomeip::eventgroup_t> its_eventgroups;
72 its_eventgroups.insert(service_info_.eventgroup_id);
73 app_->request_event(service_info_.service_id,
74 service_info_.instance_id, service_info_.event_id,
75 its_eventgroups, vsomeip::event_type_e::ET_EVENT,
76 vsomeip::reliability_type_e::RT_BOTH);
77
78 app_->subscribe(service_info_.service_id, service_info_.instance_id,
79 service_info_.eventgroup_id, vsomeip::DEFAULT_MAJOR);
80 }
81
82 app_->start();
83 }
84
~offer_test_client()85 ~offer_test_client() {
86 send_thread_.join();
87 stop_thread_.join();
88 }
89
on_state(vsomeip::state_type_e _state)90 void on_state(vsomeip::state_type_e _state) {
91 VSOMEIP_INFO << "Application " << app_->get_name() << " is "
92 << (_state == vsomeip::state_type_e::ST_REGISTERED ?
93 "registered." : "deregistered.");
94
95 if (_state == vsomeip::state_type_e::ST_REGISTERED) {
96 std::lock_guard<std::mutex> its_lock(mutex_);
97 wait_until_registered_ = false;
98 condition_.notify_one();
99 }
100 }
101
on_availability(vsomeip::service_t _service,vsomeip::instance_t _instance,bool _is_available)102 void on_availability(vsomeip::service_t _service,
103 vsomeip::instance_t _instance, bool _is_available) {
104 VSOMEIP_INFO << "Service [" << std::setw(4)
105 << std::setfill('0') << std::hex << _service << "." << _instance
106 << "] is " << (_is_available ? "available":"not available") << ".";
107 std::lock_guard<std::mutex> its_lock(mutex_);
108 if(_is_available) {
109 wait_until_service_available_ = false;
110 condition_.notify_one();
111 } else {
112 wait_until_service_available_ = true;
113 condition_.notify_one();
114 }
115 }
116
on_message(const std::shared_ptr<vsomeip::message> & _message)117 void on_message(const std::shared_ptr<vsomeip::message> &_message) {
118 if(_message->get_message_type() == vsomeip::message_type_e::MT_NOTIFICATION) {
119 on_notification(_message);
120 } else if (_message->get_message_type() == vsomeip::message_type_e::MT_RESPONSE) {
121 on_response(_message);
122 }
123 }
124
on_notification(const std::shared_ptr<vsomeip::message> & _message)125 void on_notification(const std::shared_ptr<vsomeip::message> &_message) {
126 std::shared_ptr<vsomeip::payload> its_payload(_message->get_payload());
127 EXPECT_EQ(4u, its_payload->get_length());
128 vsomeip::byte_t *d = its_payload->get_data();
129 static std::uint32_t number_received_notifications(0);
130 std::uint32_t counter(0);
131 counter |= static_cast<std::uint32_t>(d[0] << 24);
132 counter |= static_cast<std::uint32_t>(d[0] << 16);
133 counter = counter | static_cast<std::uint32_t>((d[2] << 8));
134 counter = counter | static_cast<std::uint32_t>(d[3]);
135
136 VSOMEIP_DEBUG
137 << "Received a notification with Client/Session [" << std::setw(4)
138 << std::setfill('0') << std::hex << _message->get_client() << "/"
139 << std::setw(4) << std::setfill('0') << std::hex
140 << _message->get_session() << "] from Service/Method ["
141 << std::setw(4) << std::setfill('0') << std::hex
142 << _message->get_service() << "/" << std::setw(4) << std::setfill('0')
143 << std::hex << _message->get_method() <<"] got:" << std::dec << counter;
144
145 ASSERT_GT(counter, last_received_counter_);
146 last_received_counter_ = counter;
147 ++number_received_notifications;
148
149 if(number_received_notifications >= 250) {
150 std::lock_guard<std::mutex> its_lock(stop_mutex_);
151 wait_for_stop_ = false;
152 VSOMEIP_INFO << "going down";
153 stop_condition_.notify_one();
154 }
155 }
156
on_response(const std::shared_ptr<vsomeip::message> & _message)157 void on_response(const std::shared_ptr<vsomeip::message> &_message) {
158 ++number_received_responses_;
159 static bool first(true);
160 if (first) {
161 first = false;
162 last_received_response_ = std::chrono::steady_clock::now();
163 return;
164 }
165 EXPECT_EQ(service_info_.service_id, _message->get_service());
166 EXPECT_EQ(service_info_.method_id, _message->get_method());
167 EXPECT_EQ(service_info_.instance_id, _message->get_instance());
168 ASSERT_LT(std::chrono::duration_cast<std::chrono::milliseconds>(
169 std::chrono::steady_clock::now() - last_received_response_).count(),
170 (std::chrono::milliseconds(VSOMEIP_DEFAULT_WATCHDOG_TIMEOUT)
171 + std::chrono::milliseconds(1000)).count());
172 last_received_response_ = std::chrono::steady_clock::now();
173 std::cout << ".";
174 std::cout.flush();
175 }
176
send()177 void send() {
178 if (operation_mode_ != operation_mode_e::METHODCALL) {
179 return;
180 }
181 std::unique_lock<std::mutex> its_lock(mutex_);
182 while (wait_until_registered_) {
183 condition_.wait(its_lock);
184 }
185
186 while (wait_until_service_available_) {
187 condition_.wait(its_lock);
188 }
189 its_lock.unlock();
190 its_lock.release();
191
192 for (int var = 0; var < offer_test::number_of_messages_to_send; ++var) {
193 bool send(false);
194 {
195 std::lock_guard<std::mutex> its_lock(mutex_);
196 send = !wait_until_service_available_;
197 }
198 if (send) {
199 std::shared_ptr<vsomeip::message> its_req = vsomeip::runtime::get()->create_request();
200 its_req->set_service(service_info_.service_id);
201 its_req->set_instance(service_info_.instance_id);
202 its_req->set_method(service_info_.method_id);
203 app_->send(its_req);
204 std::this_thread::sleep_for(std::chrono::milliseconds(100));
205 } else {
206 std::this_thread::sleep_for(std::chrono::milliseconds(50));
207 }
208 }
209 std::this_thread::sleep_for(std::chrono::milliseconds(100));
210 {
211 std::lock_guard<std::mutex> its_lock(stop_mutex_);
212 wait_for_stop_ = false;
213 VSOMEIP_INFO << "going down. Sent " << offer_test::number_of_messages_to_send
214 << " requests and received " << number_received_responses_
215 << " responses";
216 stop_condition_.notify_one();
217 }
218 }
219
wait_for_stop()220 void wait_for_stop() {
221 std::unique_lock<std::mutex> its_lock(stop_mutex_);
222 while (wait_for_stop_) {
223 stop_condition_.wait(its_lock);
224 }
225 VSOMEIP_INFO << "going down";
226 app_->clear_all_handler();
227 app_->stop();
228 }
229
230 private:
231 struct offer_test::service_info service_info_;
232 operation_mode_e operation_mode_;
233 std::shared_ptr<vsomeip::application> app_;
234
235 bool wait_until_registered_;
236 bool wait_until_service_available_;
237 std::mutex mutex_;
238 std::condition_variable condition_;
239
240 bool wait_for_stop_;
241 std::mutex stop_mutex_;
242 std::condition_variable stop_condition_;
243
244 std::uint32_t last_received_counter_;
245 std::chrono::steady_clock::time_point last_received_response_;
246 std::atomic<std::uint32_t> number_received_responses_;
247 std::thread stop_thread_;
248 std::thread send_thread_;
249 };
250
251 static operation_mode_e passed_mode = operation_mode_e::SUBSCRIBE;
252
TEST(someip_offer_test,subscribe_or_call_method_at_service)253 TEST(someip_offer_test, subscribe_or_call_method_at_service)
254 {
255 offer_test_client its_sample(offer_test::service, passed_mode);
256 }
257
258 #ifndef _WIN32
main(int argc,char ** argv)259 int main(int argc, char** argv)
260 {
261 ::testing::InitGoogleTest(&argc, argv);
262 if(argc < 2) {
263 std::cerr << "Please specify a operation mode, like: " << argv[0] << " SUBSCRIBE" << std::endl;
264 std::cerr << "Valid operation modes are SUBSCRIBE and METHODCALL" << std::endl;
265 return 1;
266 }
267
268 if (std::string("SUBSCRIBE") == std::string(argv[1])) {
269 passed_mode = operation_mode_e::SUBSCRIBE;
270 } else if (std::string("METHODCALL") == std::string(argv[1])) {
271 passed_mode = operation_mode_e::METHODCALL;
272 } else {
273 std::cerr << "Wrong operation mode passed, exiting" << std::endl;
274 std::cerr << "Please specify a operation mode, like: " << argv[0] << " SUBSCRIBE" << std::endl;
275 std::cerr << "Valid operation modes are SUBSCRIBE and METHODCALL" << std::endl;
276 return 1;
277 }
278
279 #if 0
280 if (argc >= 4 && std::string("SAME_SERVICE_ID") == std::string(argv[3])) {
281 use_same_service_id = true;
282 } else {
283 use_same_service_id = false;
284 }
285 #endif
286 return RUN_ALL_TESTS();
287 }
288 #endif
289