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