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