Change TARGET variable name and remove CMakeTools
[apps/agl-service-can-low-level.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 <mutex>
22 #include <queue>
23 #include <vector>
24 #include <string>
25
26 #include "timer.hpp"
27 #include "openxc.pb.h"
28 #include "can-bus.hpp"
29 #include "can-message.hpp"
30
31 extern "C"
32 {
33         #include <afb/afb-binding.h>
34         #include <afb/afb-event-itf.h>
35 }
36
37 #define MESSAGE_SET_ID 0
38
39 extern std::mutex subscribed_signals_mutex;
40 std::mutex& get_subscribed_signals_mutex();
41
42 /**
43  * @brief return the subscribed_signals map.
44  * 
45  * return std::map<std::string, struct afb_event> - map of subscribed signals.
46  */
47 extern std::map<std::string, struct afb_event> subscribed_signals;
48 std::map<std::string, struct afb_event>& get_subscribed_signals();
49
50 /**
51  * @brief The type signature for a CAN signal decoder.
52  *
53  * @desc A SignalDecoder transforms a raw floating point CAN signal into a number,
54  * string or boolean.
55  *
56  * @param[in] CanSignal signal - The CAN signal that we are decoding.
57  * @param[in] CanSignal signals - The list of all signals.
58  * @param[in] int signalCount - The length of the signals array.
59  * @param[in] float value - The CAN signal parsed from the message as a raw floating point
60  *      value.
61  * @param[out] bool send - An output parameter. If the decoding failed or the CAN signal should
62  *      not send for some other reason, this should be flipped to false.
63  *
64  * @return a decoded value in an openxc_DynamicField struct.
65  */
66 typedef openxc_DynamicField (*SignalDecoder)(struct CanSignal& signal,
67                 const std::vector<CanSignal>& signals, float value, bool* send);
68
69 /**
70  * @brief: The type signature for a CAN signal encoder.
71  *
72  * @desc A SignalEncoder transforms a number, string or boolean into a raw floating
73  * point value that fits in the CAN signal.
74  *
75  * @params[signal] - The CAN signal to encode. 
76  * @params[value] - The dynamic field to encode.
77  * @params[send] - An output parameter. If the encoding failed or the CAN signal should
78  * not be encoded for some other reason, this will be flipped to false.
79  */
80 typedef uint64_t (*SignalEncoder)(struct CanSignal* signal,
81                 openxc_DynamicField* value, bool* send);
82
83 /**
84  * @struct CanSignalState
85  *
86  * @brief A state encoded (SED) signal's mapping from numerical values to
87  * OpenXC state names.
88  */
89 struct CanSignalState {
90         const int value; /*!< int value - The integer value of the state on the CAN bus.*/
91         const char* name; /*!< char* name  - The corresponding string name for the state in OpenXC. */
92 };
93 typedef struct CanSignalState CanSignalState;
94
95 /**
96  * @struct CanSignal
97  *
98  * @brief A CAN signal to decode from the bus and output over USB.
99  */
100 struct CanSignal {
101         struct CanMessageDefinition* message; /*!< message         - The message this signal is a part of. */
102         const char* genericName; /*!< genericName - The name of the signal to be output over USB.*/
103         uint8_t bitPosition; /*!< bitPosition - The starting bit of the signal in its CAN message (assuming
104                                                 *       non-inverted bit numbering, i.e. the most significant bit of
105                                                 *       each byte is 0) */
106         uint8_t bitSize; /*!< bitSize - The width of the bit field in the CAN message. */
107         float factor; /*!< factor - The final value will be multiplied by this factor. Use 1 if you
108                                 *       don't need a factor. */
109         float offset; /*!< offset          - The final value will be added to this offset. Use 0 if you
110                                 *       don't need an offset. */
111         float minValue; /*!< minValue    - The minimum value for the processed signal.*/
112         float maxValue; /*!< maxValue    - The maximum value for the processed signal. */
113         FrequencyClock frequencyClock; /*!< frequencyClock - A FrequencyClock struct to control the maximum frequency to
114                                                                 *       process and send this signal. To process every value, set the
115                                                                 *       clock's frequency to 0. */
116         bool sendSame; /*!< sendSame    - If true, will re-send even if the value hasn't changed.*/
117         bool forceSendChanged; /*!< forceSendChanged - If true, regardless of the frequency, it will send the
118                                                 *       value if it has changed. */
119         const CanSignalState* states; /*!< states          - An array of CanSignalState describing the mapping
120                                                                 *       between numerical and string values for valid states. */
121         uint8_t stateCount; /*!< stateCount  - The length of the states array. */
122         bool writable; /*!< writable    - True if the signal is allowed to be written from the USB host
123                                 *       back to CAN. Defaults to false.*/
124         SignalDecoder decoder; /*!< decoder        - An optional function to decode a signal from the bus to a human
125                                                 *       readable value. If NULL, the default numerical decoder is used. */
126         SignalEncoder encoder; /*!< encoder        - An optional function to encode a signal value to be written to
127                                                 *       CAN into a byte array. If NULL, the default numerical encoder
128                                                 *       is used. */
129         bool received; /*!< received    - True if this signal has ever been received.*/
130         float lastValue; /*!< lastValue   - The last received value of the signal. If 'received' is false,
131                                         *       this value is undefined. */
132 };
133 typedef struct CanSignal CanSignal;
134
135 /* Public: Return signals from an signals array filtered on name.
136  */
137 const std::vector<CanSignal>& getSignals();
138
139 /* Public: Return the length of the array returned by getSignals(). */
140 size_t getSignalCount();
141
142 /**
143  * @brief Find one or many signals based on its name or id
144  * passed through openxc_DynamicField.
145  *
146  * @param[in] openxc_DynamicField& - a const reference with the key to search into signal.
147  * Key is either a signal name or its CAN arbitration id.
148  *
149  * @return std::vector<std::string> return found CanSignal generic name vector.
150  */
151 std::vector<CanSignal> find_can_signals(const openxc_DynamicField &key);
152
153 /**
154  * @brief Retrieve can arbitration id of a given CanSignal
155  *
156  * @param[in] CanSignal& - a const reference to a CanSignal
157  *
158  * @return uint32_t - unsigned integer representing the arbitration id.
159  */
160 inline uint32_t get_CanSignal_id(const CanSignal& sig);