Comments fixes, typo and formating.
[apps/low-level-can-service.git] / src / 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 /// 
21 /// @brief Can signal event map making access to afb_event
22 /// externaly to an openxc existing structure.
23 ///
24 /// Event map is making relation between can_signal_t generic name
25 /// and the afb_event struct used by application framework to pushed
26 /// to the subscriber.
27 ///
28 std::map<std::string, struct afb_event> subscribed_signals;
29
30 ///
31 /// @brief Mutex allowing safe manipulation on subscribed_signals map.
32 /// To ensure that the map object isn't modified when we read it, you
33 ///  have to set this mutex before use subscribed_signals map object.
34 ///
35 std::mutex subscribed_signals_mutex;
36
37 std::mutex& get_subscribed_signals_mutex()
38 {
39         return subscribed_signals_mutex;
40 }
41
42 std::map<std::string, struct afb_event>& get_subscribed_signals()
43 {
44         return subscribed_signals;
45 }
46
47 ///
48 /// @fn std::vector<std::string> find_signals(const openxc_DynamicField &key)
49 /// @brief return signals name found searching through CAN_signals and OBD2 pid
50 ///
51 /// @param[in] key : can contain numeric or string value in order to search against 
52 ///   can signals or obd2 signals name.
53 ///
54 /// @return Vector of signals name found. 
55 ///
56 std::vector<std::string> find_signals(const openxc_DynamicField &key)
57 {
58         std::vector<std::string> found_signals_name;
59
60         switch(key.type)
61         {
62                 case openxc_DynamicField_Type::openxc_DynamicField_Type_STRING:
63                                 lookup_signals_by_name(key.string_value, configuration_t::instance().get_can_signals(), found_signals_name);
64                                 lookup_signals_by_name(key.string_value, configuration_t::instance().get_diagnostic_messages(), found_signals_name);
65                         break;
66                 case openxc_DynamicField_Type::openxc_DynamicField_Type_NUM:
67                                 lookup_signals_by_id(key.numeric_value, configuration_t::instance().get_can_signals(), found_signals_name);
68                                 lookup_signals_by_id(key.numeric_value, configuration_t::instance().get_diagnostic_messages(), found_signals_name);
69                         break;
70                 default:
71                         ERROR(binder_interface, "find_signals: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
72                         break;
73         }
74         DEBUG(binder_interface, "find_signals: Found %d signal(s)", (int)found_signals_name.size());
75         return found_signals_name;
76 }