Change content target and use a testing
[apps/low-level-can-service.git] / src / can-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 <map>
21 #include <queue>
22 #include <vector>
23 #include <string>
24 #include <thread>
25 #include <linux/can.h>
26 #include <mutex>
27 #include <condition_variable>
28
29 #include "timer.hpp"
30 #include "openxc.pb.h"
31 #include "can-utils.hpp"
32
33 extern "C"
34 {
35         #include <afb/afb-binding.h>
36         #include <afb/afb-event-itf.h>
37 }
38
39 #define MESSAGE_SET_ID 0
40
41 /** 
42  * @brief Can signal event map making access to afb_event
43  * externaly to an openxc existing structure.
44  *
45  * @desc Event map is making relation between CanSignal generic name
46  * and the afb_event struct used by application framework to pushed
47  * to the subscriber.
48  */
49 static std::map<std::string, struct afb_event> subscribed_signals;
50
51 /**
52 * @brief Mutex allowing safe manipulation on subscribed_signals map.
53 * @desc To ensure that the map object isn't modified when we read it, you
54 *  have to set this mutex before use subscribed_signals map object.
55 */
56 extern std::mutex subscribed_signals_mutex;
57
58 /** Public: Return the currently active CAN configuration. */
59 CanMessageSet* getActiveMessageSet();
60
61 /** Public: Retrive a list of all possible CAN configurations.
62  *
63  * Returns a pointer to an array of all configurations.
64  */
65 CanMessageSet* getMessageSets();
66
67 /** Public: Return the length of the array returned by getMessageSets() */
68 int getMessageSetCount();
69
70 /* Public: Return the number of CAN buses configured in the active
71  * configuration. This is limited to 2, as the hardware controller only has 2
72  * CAN channels.
73  */
74 int getCanBusCount();
75
76 /* Public: Return an array of all CAN messages to be processed in the active
77  * configuration.
78  */
79 CanMessageDefinition* getMessages();
80
81 /* Public: Return signals from an signals array filtered on name.
82  */
83 const std::vector<CanSignal> getSignals();
84
85 /* Public: Return an array of all OpenXC CAN commands enabled in the active
86  * configuration that can write back to CAN with a custom handler.
87  *
88  * * Commands not defined here are handled using a 1-1 mapping from the signals
89  * list.
90  *              */
91 CanCommand* getCommands();
92
93 /* Public: Return the length of the array returned by getCommandCount(). */
94 int getCommandCount();
95
96 /* Public: Return the length of the array returned by getSignals(). */
97 size_t getSignalCount();
98
99 /* Public: Return the length of the array returned by getMessages(). */
100 int getMessageCount();
101
102 /**
103  * @brief Return an array of the metadata for the 2 CAN buses you want to
104  * monitor. The size of this array is fixed at 2.
105  */
106 CanBus* getCanBuses();
107
108 /**
109  * @brief Find one or many signals based on its name or id
110  * passed through openxc_DynamicField.
111  *
112  * @param[in] openxc_DynamicField& - a const reference with the key to search into signal.
113  * Key is either a signal name or its CAN arbitration id.
114  *
115  * @return std::vector<std::string> return found CanSignal generic name vector.
116  */
117 std::vector<CanSignal> find_can_signals(const openxc_DynamicField &key);
118
119 /**
120  * @brief Retrieve can arbitration id of a given CanSignal
121  *
122  * @param[in] CanSignal& - a const reference to a CanSignal
123  *
124  * @return uint32_t - unsigned integer representing the arbitration id.
125  */
126 inline uint32_t get_CanSignal_id(const CanSignal& sig);
127
128 /**
129  * @brief return the subscribed_signals map.
130  * 
131  * return std::map<std::string, struct afb_event> - map of subscribed signals.
132  */
133 const std::map<std::string, struct afb_event> get_subscribed_signals();