Cleaning CMAke
[apps/agl-service-can-low-level.git] / low-can-binding / can / can-decoder.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 "can-decoder.hpp"
19
20 #include "canutil/read.h"
21 #include "../utils/openxc-utils.hpp"
22
23 /// @brief Parse the signal's bitfield from the given data and return the raw
24 /// value.
25 ///
26 /// @param[in] signal - The signal to parse from the data.
27 /// @param[in] message - can_message_t to parse
28 ///
29 /// @return Returns the raw value of the signal parsed as a bitfield from the given byte
30 /// array.
31 ///
32 float decoder_t::parseSignalBitfield(can_signal_t& signal, const can_message_t& message)
33 {
34          return bitfield_parse_float(message.get_data(), CAN_MESSAGE_SIZE,
35                         signal.get_bit_position(), signal.get_bit_size(), signal.get_factor(),
36                         signal.get_offset());
37 }
38
39 /// @brief Wrap a raw CAN signal value in a DynamicField without modification.
40 ///
41 /// This is an implementation of the SignalDecoder type signature, and can be
42 /// used directly in the can_signal_t.decoder field.
43 ///
44 /// @param[in] signal - The details of the signal that contains the state mapping.
45 /// @param[in] signals - The list of all signals
46 /// @param[in] value - The numerical value that will be wrapped in a DynamicField.
47 /// @param[out] send - An output argument that will be set to false if the value should
48 ///     not be sent for any reason.
49 ///
50 /// @return Returns a DynamicField with the original, unmodified raw CAN signal value as
51 /// its numeric value. The 'send' argument will not be modified as this decoder
52 /// always succeeds.
53 ///
54 openxc_DynamicField decoder_t::noopDecoder(can_signal_t& signal,
55                 const std::vector<can_signal_t>& signals, float value, bool* send)
56 {
57         openxc_DynamicField decoded_value = build_DynamicField(value);
58
59         return decoded_value;
60 }
61 /// @brief Coerces a numerical value to a boolean.
62 ///
63 /// This is an implementation of the SignalDecoder type signature, and can be
64 /// used directly in the can_signal_t.decoder field.
65 ///
66 /// @param[in] signal  - The details of the signal that contains the state mapping.
67 /// @param[in] signals - The list of all signals
68 /// @param[in] value - The numerical value that will be converted to a boolean.
69 /// @param[out] send - An output argument that will be set to false if the value should
70 ///     not be sent for any reason.
71 ///
72 /// @return Returns a DynamicField with a boolean value of false if the raw signal value
73 /// is 0.0, otherwise true. The 'send' argument will not be modified as this
74 /// decoder always succeeds.
75 ///
76 openxc_DynamicField decoder_t::booleanDecoder(can_signal_t& signal,
77                 const std::vector<can_signal_t>& signals, float value, bool* send)
78 {
79         openxc_DynamicField decoded_value = build_DynamicField(value == 0.0 ? false : true);
80
81         return decoded_value;
82 }
83 /// @brief Update the metadata for a signal and the newly received value.
84 ///
85 /// This is an implementation of the SignalDecoder type signature, and can be
86 /// used directly in the can_signal_t.decoder field.
87 ///
88 /// This function always flips 'send' to false.
89 ///
90 /// @param[in] signal  - The details of the signal that contains the state mapping.
91 /// @param[in] signals - The list of all signals.
92 /// @param[in] value - The numerical value that will be converted to a boolean.
93 /// @param[out] send - This output argument will always be set to false, so the caller will
94 ///      know not to publish this value to the pipeline.
95 ///
96 /// @return Return value is undefined.
97 ///
98 openxc_DynamicField decoder_t::ignoreDecoder(can_signal_t& signal,
99                 const std::vector<can_signal_t>& signals, float value, bool* send)
100 {
101         if(send)
102           *send = false;
103
104         openxc_DynamicField decoded_value;
105
106         return decoded_value;
107 }
108
109 /// @brief Find and return the corresponding string state for a CAN signal's
110 /// raw integer value.
111 ///
112 /// This is an implementation of the SignalDecoder type signature, and can be
113 /// used directly in the can_signal_t.decoder field.
114 ///
115 /// @param[in] signal  - The details of the signal that contains the state mapping.
116 /// @param[in] signals - The list of all signals.
117 /// @param[in] value - The numerical value that should map to a state.
118 /// @param[out] send - An output argument that will be set to false if the value should
119 ///     not be sent for any reason.
120 ///
121 /// @return Returns a DynamicField with a string value if a matching state is found in
122 /// the signal. If an equivalent isn't found, send is sent to false and the
123 /// return value is undefined.
124 ///
125 openxc_DynamicField decoder_t::stateDecoder(can_signal_t& signal,
126                 const std::vector<can_signal_t>& signals, float value, bool* send)
127 {
128         const std::string signal_state = signal.get_states((uint8_t)value);
129         openxc_DynamicField decoded_value = build_DynamicField(signal_state);
130         if(signal_state.size() <= 0)
131         {
132                 *send = false;
133                 ERROR(binder_interface, "stateDecoder: No state found with index: %d", (int)value);
134         }
135         return decoded_value;
136 }
137
138
139 /// @brief Parse a signal from a CAN message, apply any required transforations
140 ///      to get a human readable value and public the result to the pipeline.
141 ///
142 /// If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
143 /// will be passed to the decoder before publishing.
144 ///
145 /// @param[in] signal - The details of the signal to decode and forward.
146 /// @param[in] message - The received CAN message that should contain this signal.
147 /// @param[in] signals - an array of all active signals.
148 ///
149 /// The decoder returns an openxc_DynamicField, which may contain a number,
150 /// string or boolean.
151 ///
152 openxc_DynamicField decoder_t::translateSignal(can_signal_t& signal, can_message_t& message,
153         const std::vector<can_signal_t>& signals)
154 {
155         float value = decoder_t::parseSignalBitfield(signal, message);
156         DEBUG(binder_interface, "translateSignal: Decoded message from parseSignalBitfield: %f", value);
157
158         bool send = true;
159         // Must call the decoders every time, regardless of if we are going to
160         // decide to send the signal or not.
161         openxc_DynamicField decoded_value = decoder_t::decodeSignal(signal,
162                         value, signals, &send);
163
164         signal.set_received(true);
165         signal.set_last_value(value);
166         return decoded_value;
167 }
168
169 /// @brief Parse a signal from a CAN message and apply any required
170 /// transforations to get a human readable value.
171 ///
172 /// If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
173 /// will be passed to the decoder before returning.
174 ///
175 /// @param[in] signal - The details of the signal to decode and forward.
176 /// @param[in] value - The numerical value that will be converted to a boolean.
177 /// @param[in] signals - an array of all active signals.
178 /// @param[out] send - An output parameter that will be flipped to false if the value could
179 ///      not be decoded.
180 ///
181 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
182 /// string or boolean. If 'send' is false, the return value is undefined.
183 ///
184 openxc_DynamicField decoder_t::decodeSignal( can_signal_t& signal,
185                 float value, const std::vector<can_signal_t>& signals, bool* send)
186 {
187         SignalDecoder decoder = signal.get_decoder() == nullptr ?
188                                                         noopDecoder : signal.get_decoder();
189         openxc_DynamicField decoded_value = decoder(signal, signals,
190                         value, send);
191         return decoded_value;
192 }
193
194 /// @brief Decode a transformed, human readable value from an raw CAN signal
195 /// already parsed from a CAN message.
196 ///
197 /// This is the same as decodeSignal but you must parse the bitfield value of the signal from the CAN
198 /// message yourself. This is useful if you need that raw value for something
199 /// else.
200 ///
201 /// @param[in] signal - The details of the signal to decode and forward.
202 /// @param[in] message - Raw CAN message to decode
203 /// @param[in] signals - an array of all active signals.
204 /// @param[out] send - An output parameter that will be flipped to false if the value could
205 ///      not be decoded.
206 ///
207 openxc_DynamicField decoder_t::decodeSignal( can_signal_t& signal,
208                 const can_message_t& message, const std::vector<can_signal_t>& signals, bool* send)
209 {
210         float value = parseSignalBitfield(signal, message);
211         return decodeSignal(signal, value, signals, send);
212 }
213
214
215 ///
216 /// @brief Decode the payload of an OBD-II PID.
217 ///
218 /// This function matches the type signature for a DiagnosticResponseDecoder, so
219 /// it can be used as the decoder for a DiagnosticRequest. It returns the decoded
220 /// value of the PID, using the standard formulas (see
221 /// http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
222 ///
223 /// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
224 ///  a byte array). This is most often used when the byte order is
225 ///  signiticant, i.e. with many OBD-II PID formulas.
226 /// @param[in] parsed_payload - the entire payload of the response parsed as an int.
227 ///
228 /// @return Float decoded value.
229 ///
230 float decoder_t::decode_obd2_response(const DiagnosticResponse* response, float parsed_payload)
231 {
232         return diagnostic_decode_obd2_pid(response);
233 }