1 // Copyright (C) 2015-2019 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 #ifndef NPDUTESTSERVICE_HPP_
7 #define NPDUTESTSERVICE_HPP_
8 #include <gtest/gtest.h>
9 
10 #include <vsomeip/vsomeip.hpp>
11 
12 #include <thread>
13 #include <mutex>
14 #include <condition_variable>
15 #include <functional>
16 #include <chrono>
17 #include <deque>
18 
19 class npdu_test_service
20 {
21 public:
22     npdu_test_service(vsomeip::service_t _service_id,
23                             vsomeip::instance_t _instance_id,
24                             std::array<vsomeip::method_t, 4> _method_ids,
25                             std::array<std::chrono::nanoseconds, 4> _debounce_times,
26                             std::array<std::chrono::nanoseconds, 4> _max_retention_times);
27     void init();
28     void start();
29     void stop();
30     void offer();
31     void stop_offer();
32     void join_shutdown_thread();
33     void on_state(vsomeip::state_type_e _state);
34     template<int method_idx> void on_message(const std::shared_ptr<vsomeip::message> &_request);
35     void on_message_shutdown(const std::shared_ptr<vsomeip::message> &_request);
36     void run();
37 
38 private:
39     template<int method_idx> void check_times();
40     template <int method_idx> void register_message_handler();
41 
42 private:
43     std::shared_ptr<vsomeip::application> app_;
44     bool is_registered_;
45     std::array<vsomeip::method_t, 4> method_ids_;
46     std::array<std::chrono::nanoseconds, 4> debounce_times_;
47     std::array<std::chrono::nanoseconds, 4> max_retention_times_;
48     std::array<std::chrono::steady_clock::time_point, 4> timepoint_last_received_message_;
49     std::array<std::mutex, 4> timepoint_mutexes_;
50     std::deque<std::chrono::microseconds> undershot_debounce_times_;
51     vsomeip::service_t service_id_;
52     vsomeip::instance_t instance_id_;
53     std::mutex mutex_;
54     std::condition_variable condition_;
55     bool blocked_;
56     std::mutex shutdown_mutex_;
57     std::condition_variable shutdown_condition_;
58     bool allowed_to_shutdown_;
59     std::uint32_t number_of_received_messages_;
60     std::thread offer_thread_;
61     std::thread shutdown_thread_;
62 };
63 
64 #endif /* NPDUTESTSERVICE_HPP_ */
65