Move all signals search functions into new signals_manager_t object
[apps/agl-service-can-low-level.git] / CAN-binder / low-can-binding / utils / signals.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 "signals.hpp"
19
20 namespace utils
21 {
22         /// @brief Return singleton instance of configuration object.
23         signals_manager_t& signals_manager_t::instance()
24         {
25                 static signals_manager_t sm;
26                 return sm;
27         }
28
29         /// @brief Return Subscribed signals map mutex.
30         std::mutex& signals_manager_t::get_subscribed_signals_mutex()
31         {
32                 return subscribed_signals_mutex_;
33         }
34
35         ///
36         /// @brief return the subscribed_signals map.
37         /// 
38         /// @return Map of subscribed signals.
39         std::map<std::string, struct afb_event>& signals_manager_t::get_subscribed_signals()
40         {
41                 return subscribed_signals_;
42         }
43
44         ///
45         /// @fn std::vector<std::string> find_signals(const openxc_DynamicField &key)
46         /// @brief return signals name found searching through CAN_signals and OBD2 pid
47         ///
48         /// @param[in] key : can contain numeric or string value in order to search against 
49         ///   can signals or obd2 signals name.
50         ///
51         /// @return Vector of signals name found. 
52         ///
53         struct signals_found signals_manager_t::find_signals(const openxc_DynamicField &key)
54         {
55                 struct signals_found sf;
56
57                 switch(key.type)
58                 {
59                         case openxc_DynamicField_Type::openxc_DynamicField_Type_STRING:
60                                         lookup_signals_by_name(key.string_value, configuration_t::instance().get_can_signals(), sf.can_signals);
61                                         lookup_signals_by_name(key.string_value, configuration_t::instance().get_diagnostic_messages(), sf.diagnostic_messages);
62                                 break;
63                         case openxc_DynamicField_Type::openxc_DynamicField_Type_NUM:
64                                         lookup_signals_by_id(key.numeric_value, configuration_t::instance().get_can_signals(), sf.can_signals);
65                                         lookup_signals_by_id(key.numeric_value, configuration_t::instance().get_diagnostic_messages(), sf.diagnostic_messages);
66                                 break;
67                         default:
68                                 ERROR(binder_interface, "find_signals: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
69                                 break;
70                 }
71                 DEBUG(binder_interface, "find_signals: Found %d signal(s)", (int)(sf.can_signals.size() + sf.diagnostic_messages.size()));
72                 return sf;
73         }
74 }