Add 'CAN-binder/libs/uds-c/' from commit 'ca20db3dd978871bbb9f01f3c862b510c03d1dc4'
[apps/low-level-can-service.git] / CAN-binder / low-can-binding / utils / signals.hpp
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 #pragma once
19
20 #include <vector>
21 #include <string>
22 #include <fnmatch.h>
23
24 #include "openxc.pb.h"
25 #include "../configuration.hpp"
26 #include "../can/can-signals.hpp"
27 #include "../diagnostic/diagnostic-message.hpp"
28
29 #include "../low-can-binding.hpp"
30
31 namespace utils
32 {
33         struct signals_found
34         {
35                 std::vector<can_signal_t*> can_signals;
36                 std::vector<diagnostic_message_t*> diagnostic_messages;
37         };
38
39         class signals_manager_t
40         {
41         private:
42                 std::mutex subscribed_signals_mutex_;
43                 std::map<std::string, struct afb_event> subscribed_signals_;
44
45                 signals_manager_t(); ///< Private constructor to make singleton class.
46
47         public:
48                 static signals_manager_t& instance();
49
50                 std::mutex& get_subscribed_signals_mutex();
51                 std::map<std::string, struct afb_event>& get_subscribed_signals();
52
53                 struct signals_found find_signals(const openxc_DynamicField &key);
54                 void find_diagnostic_messages(const openxc_DynamicField &key, std::vector<diagnostic_message_t*>& found_signals);
55                 void find_can_signals(const openxc_DynamicField &key, std::vector<can_signal_t*>& found_signals);
56
57                 template <typename T>
58                 void lookup_signals_by_name(const std::string& key, std::vector<T>& signals, std::vector<T*>& found_signals)
59                 {
60                         for(T& s : signals)
61                         {
62                                 if(::fnmatch(key.c_str(), s.get_generic_name().c_str(), FNM_CASEFOLD) == 0)
63                                         found_signals.push_back(&s);
64                                 else if(::fnmatch(key.c_str(), s.get_name().c_str(), FNM_CASEFOLD) == 0)
65                                         found_signals.push_back(&s);
66                         }
67                 }
68
69                 template <typename T>
70                 void lookup_signals_by_id(const double key, std::vector<T>& signals, std::vector<T*>& found_signals)
71                 {
72                         for(T& s : signals)
73                         {
74                                 if(configuration_t::instance().get_signal_id(s) == key)
75                                 {
76                                         found_signals.push_back(&s);
77                                 }
78                         }
79                 }
80         };
81 }