1 // Copyright (C) 2014-2018 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 <iostream>
9 #include <sstream>
10 #include <thread>
11 #include <map>
12 #include <atomic>
13
14 #include <gtest/gtest.h>
15
16 #include <vsomeip/vsomeip.hpp>
17 #include <vsomeip/internal/logger.hpp>
18
19 #include "event_test_globals.hpp"
20
21 class event_test_service {
22 public:
event_test_service(struct event_test::service_info _service_info,bool _use_tcp)23 event_test_service(struct event_test::service_info _service_info, bool _use_tcp) :
24 service_info_(_service_info),
25 test_mode_(event_test::test_mode_e::UNKNOWN),
26 app_(vsomeip::runtime::get()->create_application("event_test_service")),
27 wait_until_registered_(true),
28 wait_until_notify_method_called_(true),
29 wait_until_shutdown_method_called_(true),
30 client_subscribed_(false),
31 notifications_to_send_(0),
32 offer_thread_(std::bind(&event_test_service::run, this)),
33 use_tcp_(_use_tcp) {
34 if (!app_->init()) {
35 ADD_FAILURE() << "Couldn't initialize application";
36 return;
37 }
38 app_->register_state_handler(
39 std::bind(&event_test_service::on_state, this,
40 std::placeholders::_1));
41
42 std::set<vsomeip::eventgroup_t> its_eventgroups;
43 its_eventgroups.insert(_service_info.eventgroup_id);
44 app_->offer_event(service_info_.service_id, service_info_.instance_id,
45 service_info_.event_id, its_eventgroups,
46 vsomeip::event_type_e::ET_EVENT, std::chrono::milliseconds::zero(),
47 false, true, nullptr,
48 (use_tcp_ ? vsomeip::reliability_type_e::RT_RELIABLE : vsomeip::reliability_type_e::RT_UNRELIABLE));
49 app_->register_message_handler(service_info_.service_id,
50 service_info_.instance_id, service_info_.shutdown_method_id,
51 std::bind(&event_test_service::on_shutdown_method_called, this,
52 std::placeholders::_1));
53 app_->register_message_handler(service_info_.service_id,
54 service_info_.instance_id, service_info_.notify_method_id,
55 std::bind(&event_test_service::on_message, this,
56 std::placeholders::_1));
57 app_->register_subscription_handler(service_info_.service_id,
58 service_info_.instance_id, service_info_.eventgroup_id,
59 std::bind(&event_test_service::subscription_handler,
60 this, std::placeholders::_1, std::placeholders::_2,
61 std::placeholders::_3, std::placeholders::_4));
62
63 app_->start();
64 }
65
~event_test_service()66 ~event_test_service() {
67 offer_thread_.join();
68 }
69
offer()70 void offer() {
71 app_->offer_service(service_info_.service_id, service_info_.instance_id);
72 }
73
stop()74 void stop() {
75 app_->stop_offer_service(service_info_.service_id, service_info_.instance_id);
76 app_->clear_all_handler();
77 app_->stop();
78 }
79
on_state(vsomeip::state_type_e _state)80 void on_state(vsomeip::state_type_e _state) {
81 VSOMEIP_INFO << "Application " << app_->get_name() << " is "
82 << (_state == vsomeip::state_type_e::ST_REGISTERED ?
83 "registered." : "deregistered.");
84
85 if (_state == vsomeip::state_type_e::ST_REGISTERED) {
86 std::lock_guard<std::mutex> its_lock(mutex_);
87 wait_until_registered_ = false;
88 condition_.notify_one();
89 }
90 }
91
on_shutdown_method_called(const std::shared_ptr<vsomeip::message> & _message)92 void on_shutdown_method_called(const std::shared_ptr<vsomeip::message> &_message) {
93 app_->send(vsomeip::runtime::get()->create_response(_message));
94 VSOMEIP_WARNING << "************************************************************";
95 VSOMEIP_WARNING << "Shutdown method called -> going down!";
96 VSOMEIP_WARNING << "************************************************************";
97 std::lock_guard<std::mutex> its_lock(mutex_);
98 wait_until_shutdown_method_called_ = false;
99 condition_.notify_one();
100 }
101
on_message(const std::shared_ptr<vsomeip::message> & _message)102 void on_message(const std::shared_ptr<vsomeip::message> &_message) {
103 EXPECT_EQ(service_info_.service_id, _message->get_service());
104 EXPECT_EQ(service_info_.instance_id, _message->get_instance());
105 EXPECT_EQ(service_info_.notify_method_id, _message->get_method());
106 auto its_payload = _message->get_payload();
107 ASSERT_EQ(2u, its_payload->get_length());
108 test_mode_ = static_cast<event_test::test_mode_e>(its_payload->get_data()[0]);
109 notifications_to_send_ = static_cast<std::uint32_t>(its_payload->get_data()[1]);
110 std::lock_guard<std::mutex> its_lock(mutex_);
111 wait_until_notify_method_called_ = false;
112 condition_.notify_one();
113 }
114
run()115 void run() {
116 VSOMEIP_DEBUG << "[" << std::setw(4) << std::setfill('0') << std::hex
117 << service_info_.service_id << "] Running";
118 std::unique_lock<std::mutex> its_lock(mutex_);
119 while (wait_until_registered_) {
120 condition_.wait(its_lock);
121 }
122
123 VSOMEIP_DEBUG << "[" << std::setw(4) << std::setfill('0') << std::hex
124 << service_info_.service_id << "] Offering";
125 offer();
126
127 while (wait_until_notify_method_called_) {
128 condition_.wait(its_lock);
129 }
130 VSOMEIP_INFO << "notify";
131 notify();
132
133
134 while (wait_until_shutdown_method_called_) {
135 condition_.wait(its_lock);
136 }
137 its_lock.unlock();
138 stop();
139 }
140
notify()141 void notify() {
142 EXPECT_TRUE(client_subscribed_);
143 auto its_payload = vsomeip::runtime::get()->create_payload();
144 for (std::uint32_t i = 0; i < notifications_to_send_; i++) {
145 if (test_mode_ == event_test::test_mode_e::PAYLOAD_FIXED) {
146 its_payload->set_data(std::vector<vsomeip::byte_t>(event_test::payload_fixed_length, 0x44));
147 } else if (test_mode_ == event_test::test_mode_e::PAYLOAD_DYNAMIC) {
148 its_payload->set_data(std::vector<vsomeip::byte_t>(i+1, 0x55));
149 }
150 app_->notify(service_info_.service_id, service_info_.instance_id,
151 service_info_.event_id, its_payload, false);
152 }
153 }
154
subscription_handler(vsomeip::client_t _client,std::uint32_t _uid,std::uint32_t _gid,bool _subscribed)155 bool subscription_handler(vsomeip::client_t _client, std::uint32_t _uid, std::uint32_t _gid, bool _subscribed) {
156 (void)_uid;
157 (void)_gid;
158 VSOMEIP_INFO << __func__ << ": client: 0x" << std::hex << _client
159 << ((_subscribed) ? " subscribed" : "unsubscribed");
160 client_subscribed_ = _subscribed;
161 return true;
162 }
163
164 private:
165 struct event_test::service_info service_info_;
166 event_test::test_mode_e test_mode_;
167 std::shared_ptr<vsomeip::application> app_;
168
169 bool wait_until_registered_;
170 bool wait_until_notify_method_called_;
171 bool wait_until_shutdown_method_called_;
172 std::atomic<bool> client_subscribed_;
173 std::uint32_t notifications_to_send_;
174 std::mutex mutex_;
175 std::condition_variable condition_;
176 std::thread offer_thread_;
177 bool use_tcp_;
178 };
179
180 static bool use_tcp = false;
181
TEST(someip_event_test,send_events)182 TEST(someip_event_test, send_events)
183 {
184 event_test_service its_sample(event_test::service, use_tcp);
185 }
186
187
188 #ifndef _WIN32
main(int argc,char ** argv)189 int main(int argc, char** argv)
190 {
191 ::testing::InitGoogleTest(&argc, argv);
192
193 if (std::string("TCP")== std::string(argv[1])) {
194 use_tcp = true;
195 } else if (std::string("UDP")== std::string(argv[1])) {
196 use_tcp = false;
197 }
198
199 return RUN_ALL_TESTS();
200 }
201 #endif
202