socketcan-bcm: Fix wrongly opened socket
[apps/agl-service-can-low-level.git] / 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 <list>
21 #include <vector>
22 #include <string>
23 #include <fnmatch.h>
24
25 #include "openxc.pb.h"
26 #include "../binding/application.hpp"
27 #include "../can/signals.hpp"
28 #include "../diagnostic/diagnostic-message.hpp"
29
30 #include "../binding/low-can-subscription.hpp"
31
32 typedef std::list<std::shared_ptr<signal_t>> list_ptr_signal_t;
33 typedef std::list<std::shared_ptr<diagnostic_message_t>> list_ptr_diag_msg_t;
34
35 namespace utils
36 {
37         struct signals_found
38         {
39                 list_ptr_signal_t signals;
40                 list_ptr_diag_msg_t diagnostic_messages;
41         };
42
43         /// @brief Signal manager singleton hold subscription object with attached afb_event_t and its mutex
44         /// to read and write it safely.
45         /// It can be used to browse CAN signals and Diagnostic messages vectors and find a particular signal to
46         /// subscribe to.
47         class signals_manager_t
48         {
49         private:
50                 std::mutex subscribed_signals_mutex_;
51                 map_subscription subscribed_signals_; ///< Map containing all subscribed signals, key is the socket int value.
52
53                 signals_manager_t(); ///< Private constructor to make singleton class.
54
55         public:
56                 static signals_manager_t& instance();
57
58                 std::mutex& get_subscribed_signals_mutex();
59                 map_subscription& get_subscribed_signals();
60
61                 struct signals_found find_signals(const openxc_DynamicField &key);
62                 void find_diagnostic_messages(const openxc_DynamicField &key, vect_ptr_diag_msg_t& found_signals);
63                 void find_signals(const openxc_DynamicField &key, vect_ptr_signal_t& found_signals);
64
65                 template <typename T>
66                 void lookup_signals_by_name(const std::string& key, std::vector<std::shared_ptr<T> > signals, std::list<std::shared_ptr<T> >& found_signals)
67                 {
68                         for(std::shared_ptr<T> s : signals)
69                         {
70                                 if(::fnmatch(key.c_str(), s->get_generic_name().c_str(), FNM_CASEFOLD) == 0)
71                                         found_signals.push_back(s);
72                                 else if(::fnmatch(key.c_str(), s->get_name().c_str(), FNM_CASEFOLD) == 0)
73                                         found_signals.push_back(s);
74                         }
75                 }
76
77                 template <typename T>
78                 void lookup_signals_by_id(const double key, std::vector<std::shared_ptr<T> > signals, std::list<std::shared_ptr<T> >& found_signals)
79                 {
80                         for(std::shared_ptr<T> s : signals)
81                         {
82                                 if(application_t::instance().get_signal_id(*s) == key)
83                                 {
84                                         found_signals.push_back(s);
85                                 }
86                         }
87                 }
88         };
89 }