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 <vsomeip/defines.hpp>
7 
8 #include "../include/application_impl.hpp"
9 #include "../include/runtime_impl.hpp"
10 #include "../../message/include/message_impl.hpp"
11 #include "../../message/include/payload_impl.hpp"
12 
13 namespace vsomeip_v3 {
14 
15 std::map<std::string, std::string> runtime_impl::properties_;
16 
get_property(const std::string & _name)17 std::string runtime_impl::get_property(const std::string &_name) {
18     auto found_property = properties_.find(_name);
19     if (found_property != properties_.end())
20         return found_property->second;
21     return "";
22 }
23 
set_property(const std::string & _name,const std::string & _value)24 void runtime_impl::set_property(const std::string &_name, const std::string &_value) {
25     properties_[_name] = _value;
26 }
27 
get()28 std::shared_ptr<runtime> runtime_impl::get() {
29     static std::shared_ptr<runtime> the_runtime_ = std::make_shared<runtime_impl>();
30     return the_runtime_;
31 }
32 
~runtime_impl()33 runtime_impl::~runtime_impl() {
34 }
35 
create_application(const std::string & _name)36 std::shared_ptr<application> runtime_impl::create_application(
37         const std::string &_name) {
38     static std::uint32_t postfix_id = 0;
39     std::lock_guard<std::mutex> its_lock(applications_mutex_);
40     std::string its_name_ = _name;
41     auto found_application = applications_.find(_name);
42     if( found_application != applications_.end()) {
43         its_name_ += "_" + std::to_string(postfix_id++);
44     }
45     std::shared_ptr<application> application = std::make_shared<application_impl>(its_name_);
46     applications_[its_name_] = application;
47     return application;
48 }
49 
create_message(bool _reliable) const50 std::shared_ptr<message> runtime_impl::create_message(bool _reliable) const {
51     std::shared_ptr<message_impl> its_message =
52             std::make_shared<message_impl>();
53     its_message->set_protocol_version(VSOMEIP_PROTOCOL_VERSION);
54     its_message->set_return_code(return_code_e::E_OK);
55     its_message->set_reliable(_reliable);
56     its_message->set_interface_version(DEFAULT_MAJOR);
57     return (its_message);
58 }
59 
create_request(bool _reliable) const60 std::shared_ptr<message> runtime_impl::create_request(bool _reliable) const {
61     std::shared_ptr<message_impl> its_request =
62             std::make_shared<message_impl>();
63     its_request->set_protocol_version(VSOMEIP_PROTOCOL_VERSION);
64     its_request->set_message_type(message_type_e::MT_REQUEST);
65     its_request->set_return_code(return_code_e::E_OK);
66     its_request->set_reliable(_reliable);
67     its_request->set_interface_version(DEFAULT_MAJOR);
68     return (its_request);
69 }
70 
create_response(const std::shared_ptr<message> & _request) const71 std::shared_ptr<message> runtime_impl::create_response(
72         const std::shared_ptr<message> &_request) const {
73     std::shared_ptr<message_impl> its_response =
74             std::make_shared<message_impl>();
75     its_response->set_service(_request->get_service());
76     its_response->set_instance(_request->get_instance());
77     its_response->set_method(_request->get_method());
78     its_response->set_client(_request->get_client());
79     its_response->set_session(_request->get_session());
80     its_response->set_interface_version(_request->get_interface_version());
81     its_response->set_message_type(message_type_e::MT_RESPONSE);
82     its_response->set_return_code(return_code_e::E_OK);
83     its_response->set_reliable(_request->is_reliable());
84     return (its_response);
85 }
86 
create_notification(bool _reliable) const87 std::shared_ptr<message> runtime_impl::create_notification(
88         bool _reliable) const {
89     std::shared_ptr<message_impl> its_notification = std::make_shared<
90             message_impl>();
91     its_notification->set_protocol_version(VSOMEIP_PROTOCOL_VERSION);
92     its_notification->set_message_type(message_type_e::MT_NOTIFICATION);
93     its_notification->set_return_code(return_code_e::E_OK);
94     its_notification->set_reliable(_reliable);
95     its_notification->set_interface_version(DEFAULT_MAJOR);
96     return (its_notification);
97 }
98 
create_payload() const99 std::shared_ptr<payload> runtime_impl::create_payload() const {
100     return (std::make_shared<payload_impl>());
101 }
102 
create_payload(const byte_t * _data,uint32_t _size) const103 std::shared_ptr<payload> runtime_impl::create_payload(const byte_t *_data,
104         uint32_t _size) const {
105     return (std::make_shared<payload_impl>(_data, _size));
106 }
107 
create_payload(const std::vector<byte_t> & _data) const108 std::shared_ptr<payload> runtime_impl::create_payload(
109         const std::vector<byte_t> &_data) const {
110     return (std::make_shared<payload_impl>(_data));
111 }
112 
get_application(const std::string & _name) const113 std::shared_ptr<application> runtime_impl::get_application(
114         const std::string &_name) const {
115     std::lock_guard<std::mutex> its_lock(applications_mutex_);
116     auto found_application = applications_.find(_name);
117     if(found_application != applications_.end())
118         return found_application->second.lock();
119     return nullptr;
120 }
121 
remove_application(const std::string & _name)122 void runtime_impl::remove_application(
123         const std::string &_name) {
124     std::lock_guard<std::mutex> its_lock(applications_mutex_);
125     auto found_application = applications_.find(_name);
126     if(found_application != applications_.end()) {
127         applications_.erase(_name);
128     }
129 }
130 
131 } // namespace vsomeip_v3
132