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
15 #include <gtest/gtest.h>
16
17 #include <vsomeip/vsomeip.hpp>
18 #include <vsomeip/internal/logger.hpp>
19
20 #include "offer_test_globals.hpp"
21
22
23 class offer_test_service_availability_checker {
24 public:
offer_test_service_availability_checker(struct offer_test::service_info _service_info)25 offer_test_service_availability_checker(struct offer_test::service_info _service_info) :
26 service_info_(_service_info),
27 app_(vsomeip::runtime::get()->create_application()),
28 wait_until_registered_(true),
29 wait_for_stop_(true),
30 stop_thread_(std::bind(&offer_test_service_availability_checker::wait_for_stop, this)) {
31 if (!app_->init()) {
32 ADD_FAILURE() << "Couldn't initialize application";
33 return;
34 }
35 app_->register_state_handler(
36 std::bind(&offer_test_service_availability_checker::on_state, this,
37 std::placeholders::_1));
38
39 // register availability for all other services and request their event.
40 app_->register_availability_handler(service_info_.service_id,
41 service_info_.instance_id,
42 std::bind(&offer_test_service_availability_checker::on_availability, this,
43 std::placeholders::_1, std::placeholders::_2,
44 std::placeholders::_3));
45 app_->request_service(service_info_.service_id,
46 service_info_.instance_id);
47
48 app_->start();
49 }
50
~offer_test_service_availability_checker()51 ~offer_test_service_availability_checker() {
52 stop_thread_.join();
53 }
54
on_state(vsomeip::state_type_e _state)55 void on_state(vsomeip::state_type_e _state) {
56 VSOMEIP_INFO << "MY Application " << app_->get_name() << " is "
57 << (_state == vsomeip::state_type_e::ST_REGISTERED ?
58 "registered." : "deregistered.");
59
60 if (_state == vsomeip::state_type_e::ST_REGISTERED) {
61 std::lock_guard<std::mutex> its_lock(mutex_);
62 wait_until_registered_ = false;
63 condition_.notify_one();
64 }
65 }
66
on_availability(vsomeip::service_t _service,vsomeip::instance_t _instance,bool _is_available)67 void on_availability(vsomeip::service_t _service,
68 vsomeip::instance_t _instance, bool _is_available) {
69 VSOMEIP_INFO << "MY Service [" << std::setw(4)
70 << std::setfill('0') << std::hex << _service << "." << _instance
71 << "] is " << (_is_available ? "available":"not available") << ".";
72 std::lock_guard<std::mutex> its_lock(mutex_);
73 if(_is_available) {
74 wait_for_stop_ = false;
75 stop_condition_.notify_one();
76 }
77 }
78
wait_for_stop()79 void wait_for_stop() {
80 VSOMEIP_INFO << " MY offer_test_service_availability_check wait_for_stop() ";
81 std::unique_lock<std::mutex> its_lock(stop_mutex_);
82 while (wait_for_stop_) {
83 stop_condition_.wait(its_lock);
84 }
85 //VSOMEIP_INFO << "[" << std::setw(4) << std::setfill('0') << std::hex
86 // << client_number_ << "] all services are available. Going down";
87 VSOMEIP_INFO << " MY offer_test_service_availability_check is going down ";
88 app_->clear_all_handler();
89 app_->stop();
90 }
91
92 private:
93 struct offer_test::service_info service_info_;
94 std::shared_ptr<vsomeip::application> app_;
95
96 bool wait_until_registered_;
97 std::mutex mutex_;
98 std::condition_variable condition_;
99
100 bool wait_for_stop_;
101 std::mutex stop_mutex_;
102 std::condition_variable stop_condition_;
103 std::thread stop_thread_;
104 };
105
TEST(someip_offer_test_external,wait_for_availability_and_exit)106 TEST(someip_offer_test_external, wait_for_availability_and_exit)
107 {
108 offer_test_service_availability_checker its_sample(
109 offer_test::service);
110 }
111
112 #ifndef _WIN32
main(int argc,char ** argv)113 int main(int argc, char** argv)
114 {
115 ::testing::InitGoogleTest(&argc, argv);
116 return RUN_ALL_TESTS();
117 }
118 #endif
119