32cb00cdc330f8179bd9c7c4f56a34cb84dc1054
[apps/low-level-can-service.git] / src / configuration.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "configuration.hpp"
19
20 #include "utils/signals.hpp"
21 #include "utils/openxc-utils.hpp"
22
23 /// @brief Return singleton instance of configuration object.
24 configuration_t& configuration_t::instance()
25 {
26         static configuration_t config;
27         return config;
28 }
29
30 can_bus_t& configuration_t::get_can_bus_manager()
31 {
32         return can_bus_manager_;
33 }
34
35 const std::map<std::string, std::shared_ptr<can_bus_dev_t>>& configuration_t::get_can_bus_devices()
36 {
37         return can_bus_manager_.get_can_devices();
38 }
39
40 diagnostic_manager_t& configuration_t::get_diagnostic_manager()
41 {
42         return diagnostic_manager_;
43 }
44
45 uint8_t configuration_t::get_active_message_set() const
46 {
47         return active_message_set_;
48 }
49
50 const std::vector<can_message_set_t>& configuration_t::get_can_message_set()
51 {
52         return can_message_set_;
53 }
54
55 std::vector<can_signal_t>& configuration_t::get_can_signals()
56 {
57         return can_signals_[active_message_set_];
58 }
59
60 std::vector<diagnostic_message_t>& configuration_t::get_diagnostic_messages()
61 {
62         return diagnostic_messages_[active_message_set_];
63 }
64
65 const std::vector<can_message_definition_t>& configuration_t::get_can_message_definition()
66 {
67         return can_message_definition_[active_message_set_];
68 }
69
70 const can_message_definition_t& configuration_t::get_can_message_definition(std::uint8_t message_set_id, std::uint8_t message_id)
71 {
72         return can_message_definition_[message_set_id][message_id];
73 }
74
75 uint32_t configuration_t::get_signal_id(diagnostic_message_t& sig) const
76 {
77         return sig.get_pid();
78 }
79
80 uint32_t configuration_t::get_signal_id(can_signal_t& sig) const
81 {
82         return sig.get_message().get_id();
83 }
84
85 void configuration_t::set_active_message_set(uint8_t id)
86 {
87         active_message_set_ = id;
88 }
89
90 /// @brief return diagnostic messages name found searching through diagnostic messages list.
91 ///
92 /// @param[in] key - can contain numeric or string value in order to search against
93 ///   can signals or obd2 signals name.
94 /// @param[out] found_signals - Vector of signals name found.
95 ///
96 void configuration_t::find_diagnostic_messages(const openxc_DynamicField &key, std::vector<diagnostic_message_t*>& found_signals)
97 {
98         switch(key.type)
99         {
100                 case openxc_DynamicField_Type::openxc_DynamicField_Type_STRING:
101                                 lookup_signals_by_name(key.string_value, diagnostic_messages_[active_message_set_], found_signals);
102                         break;
103                 case openxc_DynamicField_Type::openxc_DynamicField_Type_NUM:
104                                 lookup_signals_by_id(key.numeric_value, diagnostic_messages_[active_message_set_], found_signals);
105                         break;
106                 default:
107                         ERROR(binder_interface, "find_diagnostic_messages: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
108                         break;
109         }
110         DEBUG(binder_interface, "find_diagnostic_messages: Found %d signal(s)", (int)found_signals.size());
111 }
112
113 diagnostic_message_t* configuration_t::get_diagnostic_message(std::string message_name) const
114 {
115         std::vector<diagnostic_message_t*> found;
116         configuration_t::instance().find_diagnostic_messages(build_DynamicField(message_name), found);
117         return found.front();
118 }
119
120 DiagnosticRequest* configuration_t::get_request_from_diagnostic_message(diagnostic_message_t* diag_msg) const
121 {
122         return new DiagnosticRequest(diag_msg->build_diagnostic_request());
123 }
124
125 DiagnosticRequest* configuration_t::get_request_from_diagnostic_message(std::string message_name) const
126 {
127         return new DiagnosticRequest(get_diagnostic_message(message_name)->build_diagnostic_request());
128 }
129
130 /// @brief return signals name found searching through CAN signals list.
131 ///
132 /// @param[in] key - can contain numeric or string value in order to search against
133 ///   can signals or obd2 signals name.
134 /// @param[out] found_signals - provided vector to fill with pointer to matched signals.
135 ///
136 void configuration_t::find_can_signals(const openxc_DynamicField& key, std::vector<can_signal_t*>& found_signals)
137 {
138         switch(key.type)
139         {
140                 case openxc_DynamicField_Type::openxc_DynamicField_Type_STRING:
141                         lookup_signals_by_name(std::string(key.string_value), can_signals_[active_message_set_], found_signals);
142                         break;
143                 case openxc_DynamicField_Type::openxc_DynamicField_Type_NUM:
144                         lookup_signals_by_id(key.numeric_value, can_signals_[active_message_set_], found_signals);
145                         break;
146                 default:
147                         ERROR(binder_interface, "find_can_signals: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
148                         break;
149         }
150         DEBUG(binder_interface, "find_can_signals: Found %d signal(s)", (int)found_signals.size());
151 }