1 // Copyright (C) 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 #include <compat/vsomeip/defines.hpp>
7 #include <vsomeip/runtime.hpp>
8 
9 #include "../include/application_impl.hpp"
10 #include "../include/runtime_impl.hpp"
11 #include "../../message/include/message_impl.hpp"
12 #include "../../message/include/payload_impl.hpp"
13 
14 namespace vsomeip {
15 
16 std::shared_ptr<runtime> runtime_impl::the_runtime_
17     = std::make_shared<runtime_impl>();
18 
19 std::string
get_property(const std::string & _name)20 runtime_impl::get_property(const std::string &_name) {
21 
22     return vsomeip_v3::runtime::get_property(_name);
23 }
24 
25 void
set_property(const std::string & _name,const std::string & _value)26 runtime_impl::set_property(const std::string &_name, const std::string &_value) {
27 
28     vsomeip_v3::runtime::set_property(_name, _value);
29 }
30 
31 std::shared_ptr<runtime>
get()32 runtime_impl::get() {
33 
34     return the_runtime_;
35 }
36 
~runtime_impl()37 runtime_impl::~runtime_impl() {
38 }
39 
40 std::shared_ptr<application>
create_application(const std::string & _name)41 runtime_impl::create_application(const std::string &_name) {
42 
43     auto its_application = std::make_shared<application_impl>(_name);
44 
45     {
46         std::lock_guard<std::mutex> its_lock(applications_mutex_);
47         applications_[its_application->get_name()] = its_application;
48     }
49 
50     return (its_application);
51 }
52 
53 std::shared_ptr<message>
create_message(bool _reliable) const54 runtime_impl::create_message(bool _reliable) const {
55 
56     auto its_impl = vsomeip_v3::runtime::get()->create_message(_reliable);
57     return (std::make_shared<message_impl>(its_impl));
58 }
59 
60 std::shared_ptr<message>
create_request(bool _reliable) const61 runtime_impl::create_request(bool _reliable) const {
62 
63     auto its_impl = vsomeip_v3::runtime::get()->create_request(_reliable);
64     return (std::make_shared<message_impl>(its_impl));
65 }
66 
67 std::shared_ptr<message>
create_response(const std::shared_ptr<message> & _request) const68 runtime_impl::create_response(const std::shared_ptr<message> &_request) const {
69 
70     auto its_request = std::dynamic_pointer_cast<message_impl>(_request);
71     auto its_impl = vsomeip_v3::runtime::get()->create_response(
72             its_request->get_impl());
73     return (std::make_shared<message_impl>(its_impl));
74 }
75 
76 std::shared_ptr<message>
create_notification(bool _reliable) const77 runtime_impl::create_notification(bool _reliable) const {
78 
79     auto its_impl = vsomeip_v3::runtime::get()->create_notification(_reliable);
80     return (std::make_shared<message_impl>(its_impl));
81 }
82 
83 std::shared_ptr<payload>
create_payload() const84 runtime_impl::create_payload() const {
85 
86     auto its_impl = vsomeip_v3::runtime::get()->create_payload();
87     return (std::make_shared<payload_impl>(its_impl));
88 }
89 
90 std::shared_ptr<payload>
create_payload(const byte_t * _data,uint32_t _size) const91 runtime_impl::create_payload(const byte_t *_data, uint32_t _size) const {
92 
93     auto its_impl = vsomeip_v3::runtime::get()->create_payload(_data, _size);
94     return (std::make_shared<payload_impl>(its_impl));
95 }
96 
97 std::shared_ptr<payload>
create_payload(const std::vector<byte_t> & _data) const98 runtime_impl::create_payload(const std::vector<byte_t> &_data) const {
99 
100     auto its_impl = vsomeip_v3::runtime::get()->create_payload(_data);
101     return (std::make_shared<payload_impl>(its_impl));
102 }
103 
104 std::shared_ptr<application>
get_application(const std::string & _name) const105 runtime_impl::get_application(const std::string &_name) const {
106 
107     std::lock_guard<std::mutex> its_lock(applications_mutex_);
108     auto found_application = applications_.find(_name);
109     if (found_application != applications_.end())
110         return found_application->second.lock();
111 
112     return (nullptr);
113 }
114 
115 void
remove_application(const std::string & _name)116 runtime_impl::remove_application(const std::string &_name) {
117 
118     std::lock_guard<std::mutex> its_lock(applications_mutex_);
119     auto found_application = applications_.find(_name);
120     if(found_application != applications_.end()) {
121         applications_.erase(_name);
122     }
123 }
124 
125 } // namespace vsomeip
126