• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "local_routing_test_service.hpp"
7 
local_routing_test_service(bool _use_static_routing)8 local_routing_test_service::local_routing_test_service(bool _use_static_routing) :
9                 app_(vsomeip::runtime::get()->create_application()),
10                 is_registered_(false),
11                 use_static_routing_(_use_static_routing),
12                 blocked_(false),
13                 number_of_received_messages_(0),
14                 offer_thread_(std::bind(&local_routing_test_service::run, this))
15 {
16 }
17 
init()18 bool local_routing_test_service::init()
19 {
20     std::lock_guard<std::mutex> its_lock(mutex_);
21 
22     if (!app_->init()) {
23         ADD_FAILURE() << "Couldn't initialize application";
24         return false;
25     }
26     app_->register_message_handler(vsomeip_test::TEST_SERVICE_SERVICE_ID,
27             vsomeip_test::TEST_SERVICE_INSTANCE_ID, vsomeip_test::TEST_SERVICE_METHOD_ID,
28             std::bind(&local_routing_test_service::on_message, this,
29                     std::placeholders::_1));
30 
31     app_->register_state_handler(
32             std::bind(&local_routing_test_service::on_state, this,
33                     std::placeholders::_1));
34 
35     VSOMEIP_INFO << "Static routing " << (use_static_routing_ ? "ON" : "OFF");
36     return true;
37 }
38 
start()39 void local_routing_test_service::start()
40 {
41     VSOMEIP_INFO << "Starting...";
42     app_->start();
43 }
44 
stop()45 void local_routing_test_service::stop()
46 {
47     VSOMEIP_INFO << "Stopping...";
48     app_->clear_all_handler();
49     app_->stop();
50 }
51 
join_offer_thread()52 void local_routing_test_service::join_offer_thread()
53 {
54     offer_thread_.join();
55 }
56 
offer()57 void local_routing_test_service::offer()
58 {
59     app_->offer_service(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID);
60 }
61 
stop_offer()62 void local_routing_test_service::stop_offer()
63 {
64     app_->stop_offer_service(vsomeip_test::TEST_SERVICE_SERVICE_ID, vsomeip_test::TEST_SERVICE_INSTANCE_ID);
65 }
66 
on_state(vsomeip::state_type_e _state)67 void local_routing_test_service::on_state(vsomeip::state_type_e _state)
68 {
69     VSOMEIP_INFO << "Application " << app_->get_name() << " is "
70             << (_state == vsomeip::state_type_e::ST_REGISTERED ? "registered." :
71                     "deregistered.");
72 
73     if(_state == vsomeip::state_type_e::ST_REGISTERED)
74     {
75         if(!is_registered_)
76         {
77             is_registered_ = true;
78             std::lock_guard<std::mutex> its_lock(mutex_);
79             blocked_ = true;
80             // "start" the run method thread
81             condition_.notify_one();
82         }
83     }
84     else
85     {
86         is_registered_ = false;
87     }
88 }
89 
on_message(const std::shared_ptr<vsomeip::message> & _request)90 void local_routing_test_service::on_message(const std::shared_ptr<vsomeip::message>& _request)
91 {
92     VSOMEIP_INFO << "Received a message with Client/Session [" << std::setw(4)
93             << std::setfill('0') << std::hex << _request->get_client() << "/"
94             << std::setw(4) << std::setfill('0') << std::hex
95             << _request->get_session() << "]";
96 
97     number_of_received_messages_++;
98 
99     ASSERT_EQ(_request->get_service(), vsomeip_test::TEST_SERVICE_SERVICE_ID);
100     ASSERT_EQ(_request->get_method(), vsomeip_test::TEST_SERVICE_METHOD_ID);
101 
102     // Check the protocol version this shall be set to 0x01 according to the spec.
103     // TR_SOMEIP_00052
104     ASSERT_EQ(_request->get_protocol_version(), 0x01);
105     // Check the message type this shall be 0xx (REQUEST) according to the spec.
106     // TR_SOMEIP_00055
107     ASSERT_EQ(_request->get_message_type(), vsomeip::message_type_e::MT_REQUEST);
108 
109     // check the session id.
110     ASSERT_EQ(_request->get_session(), static_cast<vsomeip::session_t>(number_of_received_messages_));
111 
112 
113     // send response
114     std::shared_ptr<vsomeip::message> its_response =
115             vsomeip::runtime::get()->create_response(_request);
116 
117     app_->send(its_response);
118 
119     if(number_of_received_messages_ >= vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND)
120     {
121         std::lock_guard<std::mutex> its_lock(mutex_);
122         blocked_ =true;
123         condition_.notify_one();
124     }
125     ASSERT_LT(number_of_received_messages_,
126             vsomeip_test::NUMBER_OF_MESSAGES_TO_SEND + 1);
127 }
128 
run()129 void local_routing_test_service::run()
130 {
131     std::unique_lock<std::mutex> its_lock(mutex_);
132     while (!blocked_)
133         condition_.wait(its_lock);
134 
135     blocked_ = false;
136     if(use_static_routing_)
137     {
138         offer();
139     }
140     while (!blocked_)
141         condition_.wait(its_lock);
142 
143     std::thread t2([](){ std::this_thread::sleep_for(std::chrono::microseconds(1000000 * 5));});
144     t2.join();
145     app_->stop();
146 }
147 
TEST(someip_local_routing_test,receive_ten_messages_over_local_uds_socket)148 TEST(someip_local_routing_test, receive_ten_messages_over_local_uds_socket)
149 {
150     bool use_static_routing = true;
151     local_routing_test_service test_service(use_static_routing);
152     if (test_service.init()) {
153         test_service.start();
154         test_service.join_offer_thread();
155     }
156 }
157 
158 #ifndef _WIN32
main(int argc,char ** argv)159 int main(int argc, char** argv)
160 {
161     ::testing::InitGoogleTest(&argc, argv);
162     return RUN_ALL_TESTS();
163 }
164 #endif
165