1 // Copyright (C) 2020 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 "e2e_profile_04_test_common.hpp"
7 #include "e2e_profile_04_test_service.hpp"
8
9 static bool is_remote_test = false;
10 static bool remote_client_allowed = true;
11
12 std::vector<std::vector<vsomeip::byte_t> > responses_;
13 std::vector<std::vector<vsomeip::byte_t> > events_;
14
15 std::map<vsomeip::method_t, uint32_t> counters_;
16
e2e_profile_04_test_service()17 e2e_profile_04_test_service::e2e_profile_04_test_service()
18 : app_(vsomeip::runtime::get()->create_application()),
19 is_registered_(false),
20 blocked_(false),
21 offer_thread_(std::bind(&e2e_profile_04_test_service::run, this)),
22 received_(0) {
23 }
24
25 bool
init()26 e2e_profile_04_test_service::init() {
27
28 std::lock_guard<std::mutex> its_lock(mutex_);
29
30 if (!app_->init()) {
31 ADD_FAILURE() << __func__ << ": Cannot initialize application.";
32 return false;
33 }
34
35 app_->register_message_handler(PROFILE_04_SERVICE, PROFILE_04_INSTANCE,
36 PROFILE_04_METHOD,
37 std::bind(&e2e_profile_04_test_service::on_message, this,
38 std::placeholders::_1));
39
40 app_->register_message_handler(PROFILE_04_SERVICE, PROFILE_04_INSTANCE,
41 PROFILE_04_SHUTDOWN,
42 std::bind(&e2e_profile_04_test_service::on_message_shutdown, this,
43 std::placeholders::_1));
44
45 app_->register_state_handler(
46 std::bind(&e2e_profile_04_test_service::on_state, this,
47 std::placeholders::_1));
48
49 // E2E Profile 04: Event 8001
50 app_->offer_event(PROFILE_04_SERVICE, PROFILE_04_INSTANCE,
51 PROFILE_04_EVENT, { PROFILE_04_EVENTGROUP },
52 vsomeip::event_type_e::ET_FIELD, std::chrono::milliseconds::zero(),
53 false, true, nullptr, vsomeip::reliability_type_e::RT_UNRELIABLE);
54
55 // Initialize the attribute
56 auto its_payload = vsomeip::runtime::get()->create_payload();
57 vsomeip::byte_t its_data[] = {
58 0x00, 0x50, 0x8f, 0x80, 0x01, 0x00, 0x00, 0x2d,
59 0xf3, 0x2a, 0x8c, 0x89, 0x05, 0x04, 0xcc, 0x46,
60 0x00, 0x00, 0x09, 0x3d, 0x00, 0x01, 0x06, 0xfe,
61 0x01, 0x3e, 0x4c, 0xcc, 0xcd, 0x80, 0x3f, 0xb2,
62 0x40, 0xb1, 0x3e, 0xba, 0xc4, 0x76, 0x3f, 0xb3,
63 0x7b, 0x03, 0xbd, 0x95, 0x74, 0x53, 0x3d, 0x32,
64 0x4b, 0x9d, 0xbd, 0xbc, 0xd6, 0x3b, 0x3f, 0x80,
65 0x00, 0x00, 0xfc, 0x01, 0x01, 0x3c, 0x1f, 0xf5,
66 0xf6, 0x01, 0x01, 0x3c, 0x2b, 0xb1, 0xa2, 0x00
67 };
68 its_payload->set_data(its_data, sizeof(its_data));
69
70 app_->notify(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID,
71 static_cast<vsomeip::event_t>(0x8001), its_payload);
72
73 return (true);
74 }
75
76 void
start()77 e2e_profile_04_test_service::start() {
78
79 VSOMEIP_INFO << __func__ << ": Starting...";
80 app_->start();
81 }
82
83 void
stop()84 e2e_profile_04_test_service::stop() {
85
86 VSOMEIP_INFO << __func__ << ": Stopping...";
87 app_->clear_all_handler();
88 app_->stop();
89 }
90
91 void
join_offer_thread()92 e2e_profile_04_test_service::join_offer_thread() {
93
94 if (offer_thread_.joinable()) {
95 offer_thread_.join();
96 }
97 }
98
99 void
offer()100 e2e_profile_04_test_service::offer() {
101
102 app_->offer_service(PROFILE_04_SERVICE, PROFILE_04_INSTANCE,
103 PROFILE_04_MAJOR, PROFILE_04_MINOR);
104 }
105
106 void
stop_offer()107 e2e_profile_04_test_service::stop_offer() {
108
109 app_->stop_offer_service(PROFILE_04_SERVICE, PROFILE_04_INSTANCE,
110 PROFILE_04_MAJOR, PROFILE_04_MINOR);
111 }
112
113 void
on_state(vsomeip::state_type_e _state)114 e2e_profile_04_test_service::on_state(vsomeip::state_type_e _state) {
115
116 VSOMEIP_INFO << __func__ << ": Application "
117 << app_->get_name() << " is "
118 << (_state == vsomeip::state_type_e::ST_REGISTERED ?
119 "registered." : "deregistered.");
120
121 if (_state == vsomeip::state_type_e::ST_REGISTERED) {
122 if (!is_registered_) {
123 is_registered_ = true;
124
125 std::lock_guard<std::mutex> its_lock(mutex_);
126 blocked_ = true;
127
128 // "start" the run method thread
129 condition_.notify_one();
130 }
131 } else {
132 is_registered_ = false;
133 }
134 }
135
136 void
on_message(const std::shared_ptr<vsomeip::message> & _request)137 e2e_profile_04_test_service::on_message(
138 const std::shared_ptr<vsomeip::message> &_request) {
139
140 ASSERT_EQ(PROFILE_04_SERVICE, _request->get_service());
141 ASSERT_EQ(PROFILE_04_INSTANCE, _request->get_instance());
142
143 VSOMEIP_INFO << "Received a message with Client/Session ["
144 << std::setw(4) << std::setfill('0') << std::hex
145 << _request->get_client() << "/" << _request->get_session()
146 << "] method: " << _request->get_method() ;
147
148 std::shared_ptr<vsomeip::message> its_response =
149 vsomeip::runtime::get()->create_response(_request);
150 std::shared_ptr< vsomeip::payload > its_response_payload =
151 vsomeip::runtime::get()->create_payload();
152 std::shared_ptr<vsomeip::payload> its_event_payload =
153 vsomeip::runtime::get()->create_payload();
154
155 // send fixed payload for profile 01 CRC8
156 if (PROFILE_04_METHOD == _request->get_method()) {
157 its_response_payload->set_data(responses_[counters_[PROFILE_04_METHOD] % PROFILE_O4_NUM_MESSAGES]);
158 its_response->set_payload(its_response_payload);
159 app_->send(its_response);
160
161 counters_[PROFILE_04_METHOD]++;
162
163 // set value to field which gets filled by e2e protection with CRC on sending
164 its_event_payload->set_data(events_[counters_[PROFILE_04_EVENT] % PROFILE_O4_NUM_MESSAGES]);
165 app_->notify(PROFILE_04_SERVICE, PROFILE_04_INSTANCE, PROFILE_04_EVENT, its_event_payload);
166
167 counters_[PROFILE_04_EVENT]++;
168 }
169
170 received_++;
171 if (received_ == PROFILE_O4_NUM_MESSAGES) {
172 VSOMEIP_INFO << __func__ << ": Received all messages!";
173 }
174 }
175
176 void
on_message_shutdown(const std::shared_ptr<vsomeip::message> & _request)177 e2e_profile_04_test_service::on_message_shutdown(
178 const std::shared_ptr<vsomeip::message> &_request) {
179
180 (void)_request;
181 VSOMEIP_INFO << __func__ << ": Shutdown method was called, going down now.";
182 stop();
183 }
184
185 void
run()186 e2e_profile_04_test_service::run() {
187
188 std::unique_lock<std::mutex> its_lock(mutex_);
189 while (!blocked_)
190 condition_.wait(its_lock);
191
192 offer();
193 }
194
TEST(someip_e2e_profile_04_test,basic_subscribe_request_response)195 TEST(someip_e2e_profile_04_test, basic_subscribe_request_response) {
196 e2e_profile_04_test_service test_service;
197 if (test_service.init()) {
198 test_service.start();
199 test_service.join_offer_thread();
200 }
201 }
202
203 #ifndef _WIN32
main(int argc,char ** argv)204 int main(int argc, char** argv) {
205
206
207 counters_[PROFILE_04_METHOD] = 0;
208 counters_[PROFILE_04_EVENT] = 0;
209
210 std::string test_remote("--remote");
211 std::string test_local("--local");
212 std::string test_allow_remote_client("--allow");
213 std::string test_deny_remote_client("--deny");
214 std::string help("--help");
215
216 int i = 1;
217 while (i < argc)
218 {
219 if(test_remote == argv[i])
220 {
221 is_remote_test = true;
222 }
223 else if(test_local == argv[i])
224 {
225 is_remote_test = false;
226 }
227 else if(test_allow_remote_client == argv[i])
228 {
229 remote_client_allowed = true;
230 }
231 else if(test_deny_remote_client == argv[i])
232 {
233 remote_client_allowed = false;
234 }
235 else if(help == argv[i])
236 {
237 VSOMEIP_INFO << "Parameters:\n"
238 << "--remote: Run test between two hosts\n"
239 << "--local: Run test locally\n"
240 << "--allow: test is started with a policy that allows remote messages sent by this test client to the service\n"
241 << "--deny: test is started with a policy that denies remote messages sent by this test client to the service\n"
242 << "--help: print this help";
243 }
244 i++;
245 }
246
247 // Payloads (without counter, data id and crc)
248 responses_ = {
249 {
250 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
251 0x00, 0x00, 0x00, 0x00, 0x08, 0xb7, 0xf4, 0x4c,
252 0x00, 0x00, 0x09, 0x3d, 0x00, 0x01, 0x06, 0xfe,
253 0x01, 0x3e, 0x4c, 0xcc, 0xcd, 0x80, 0x3f, 0xb2,
254 0x3d, 0x83, 0x3e, 0xba, 0x68, 0xed, 0x3f, 0xb3,
255 0x7a, 0xf2, 0xbd, 0x96, 0xc1, 0x42, 0x3d, 0x25,
256 0x1a, 0x62, 0xbd, 0xae, 0x77, 0xf3, 0x3f, 0x80,
257 0x00, 0x00, 0xfc, 0x01, 0x01, 0x3c, 0x1d, 0xbd,
258 0x4e, 0x01, 0x01, 0x3c, 0x2b, 0x87, 0xed, 0x00
259 },
260 {
261 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
262 0x00, 0x00, 0x00, 0x00, 0x0c, 0x69, 0x02, 0x1c,
263 0x00, 0x00, 0x09, 0x3d, 0x00, 0x01, 0x06, 0xfe,
264 0x01, 0x3e, 0x4c, 0xcc, 0xcd, 0x80, 0x3f, 0xb2,
265 0x3c, 0x2f, 0x3e, 0xba, 0x46, 0x81, 0x3f, 0xb3,
266 0x73, 0x8d, 0xbd, 0x93, 0xcb, 0xae, 0x3c, 0xf7,
267 0xd2, 0x58, 0xbd, 0xa2, 0x6e, 0xcd, 0x3f, 0x80,
268 0x00, 0x00, 0xfc, 0x01, 0x01, 0x3c, 0x1c, 0x89,
269 0x24, 0x01, 0x01, 0x3c, 0x2b, 0x24, 0x45, 0x00
270 },
271 {
272 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273 0x00, 0x00, 0x00, 0x00, 0x10, 0x1b, 0x28, 0xae,
274 0x00, 0x00, 0x09, 0x3d, 0x00, 0x01, 0x06, 0xfe,
275 0x01, 0x3e, 0x4c, 0xcc, 0xcd, 0x80, 0x3f, 0xb2,
276 0x3e, 0xf3, 0x3e, 0xba, 0x97, 0x45, 0x3f, 0xb3,
277 0x86, 0x81, 0xbd, 0x8a, 0xda, 0xc2, 0x3c, 0xf6,
278 0x00, 0x7a, 0xbd, 0xb4, 0xf9, 0xb9, 0x3f, 0x80,
279 0x00, 0x00, 0xfc, 0x01, 0x01, 0x3c, 0x1c, 0x1b,
280 0x72, 0x01, 0x01, 0x3c, 0x2a, 0x9e, 0x1f, 0x00
281 }
282 };
283
284 // Payloads (full data with counter, data id and crc to be sent raw)
285 events_ = {
286 {
287 0x00, 0x50, 0x8f, 0x81, 0x01, 0x00, 0x00, 0x2d,
288 0xed, 0x6e, 0x78, 0x8d, 0x08, 0xb7, 0xf4, 0x4c,
289 0x00, 0x00, 0x09, 0x3d, 0x00, 0x01, 0x06, 0xfe,
290 0x01, 0x3e, 0x4c, 0xcc, 0xcd, 0x80, 0x3f, 0xb2,
291 0x3d, 0x83, 0x3e, 0xba, 0x68, 0xed, 0x3f, 0xb3,
292 0x7a, 0xf2, 0xbd, 0x96, 0xc1, 0x42, 0x3d, 0x25,
293 0x1a, 0x62, 0xbd, 0xae, 0x77, 0xf3, 0x3f, 0x80,
294 0x00, 0x00, 0xfc, 0x01, 0x01, 0x3c, 0x1d, 0xbd,
295 0x4e, 0x01, 0x01, 0x3c, 0x2b, 0x87, 0xed, 0x00
296 },
297 {
298 0x00, 0x50, 0x8f, 0x82, 0x01, 0x00, 0x00, 0x2d,
299 0x9d, 0xbb, 0x49, 0x3f, 0x0c, 0x69, 0x02, 0x1c,
300 0x00, 0x00, 0x09, 0x3d, 0x00, 0x01, 0x06, 0xfe,
301 0x01, 0x3e, 0x4c, 0xcc, 0xcd, 0x80, 0x3f, 0xb2,
302 0x3c, 0x2f, 0x3e, 0xba, 0x46, 0x81, 0x3f, 0xb3,
303 0x73, 0x8d, 0xbd, 0x93, 0xcb, 0xae, 0x3c, 0xf7,
304 0xd2, 0x58, 0xbd, 0xa2, 0x6e, 0xcd, 0x3f, 0x80,
305 0x00, 0x00, 0xfc, 0x01, 0x01, 0x3c, 0x1c, 0x89,
306 0x24, 0x01, 0x01, 0x3c, 0x2b, 0x24, 0x45, 0x00
307 },
308 {
309 0x00, 0x50, 0x8f, 0x83, 0x01, 0x00, 0x00, 0x2d,
310 0x13, 0x04, 0xf8, 0x81, 0x10, 0x1b, 0x28, 0xae,
311 0x00, 0x00, 0x09, 0x3d, 0x00, 0x01, 0x06, 0xfe,
312 0x01, 0x3e, 0x4c, 0xcc, 0xcd, 0x80, 0x3f, 0xb2,
313 0x3e, 0xf3, 0x3e, 0xba, 0x97, 0x45, 0x3f, 0xb3,
314 0x86, 0x81, 0xbd, 0x8a, 0xda, 0xc2, 0x3c, 0xf6,
315 0x00, 0x7a, 0xbd, 0xb4, 0xf9, 0xb9, 0x3f, 0x80,
316 0x00, 0x00, 0xfc, 0x01, 0x01, 0x3c, 0x1c, 0x1b,
317 0x72, 0x01, 0x01, 0x3c, 0x2a, 0x9e, 0x1f, 0x00
318 }
319 };
320
321 ::testing::InitGoogleTest(&argc, argv);
322 return RUN_ALL_TESTS();
323 }
324 #endif
325