Misc: Cleanup, typo, formating and comments update.
[apps/low-level-can-service.git] / CAN-binder / 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 #include "../low-can-binding.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<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<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<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<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                 ERROR(binder_interface, "%s: No state found with index: %d", __FUNCTION__, (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 ///
151 /// The decoder returns an openxc_DynamicField, which may contain a number,
152 /// string or boolean.
153 ///
154 openxc_DynamicField decoder_t::translateSignal(can_signal_t& signal, can_message_t& message,
155         const std::vector<can_signal_t>& signals)
156 {
157         float value = decoder_t::parseSignalBitfield(signal, message);
158         DEBUG(binder_interface, "%s: Decoded message from parseSignalBitfield: %f", __FUNCTION__, value);
159
160         bool send = true;
161         // Must call the decoders every time, regardless of if we are going to
162         // decide to send the signal or not.
163         openxc_DynamicField decoded_value = decoder_t::decodeSignal(signal,
164                         value, signals, &send);
165
166         signal.set_received(true);
167         signal.set_last_value(value);
168         return decoded_value;
169 }
170
171 /// @brief Parse a signal from a CAN message and apply any required
172 /// transforations to get a human readable value.
173 ///
174 /// If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
175 /// will be passed to the decoder before returning.
176 ///
177 /// @param[in] signal - The details of the signal to decode and forward.
178 /// @param[in] value - The numerical value that will be converted to a boolean.
179 /// @param[in] signals - an array of all active signals.
180 /// @param[out] send - An output parameter that will be flipped to false if the value could
181 ///      not be decoded.
182 ///
183 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
184 /// string or boolean. If 'send' is false, the return value is undefined.
185 ///
186 openxc_DynamicField decoder_t::decodeSignal( can_signal_t& signal,
187                 float value, const std::vector<can_signal_t>& signals, bool* send)
188 {
189         SignalDecoder decoder = signal.get_decoder() == nullptr ?
190                                                         noopDecoder : signal.get_decoder();
191         openxc_DynamicField decoded_value = decoder(signal, signals,
192                         value, send);
193         return decoded_value;
194 }
195
196 /// @brief Decode a transformed, human readable value from an raw CAN signal
197 /// already parsed from a CAN message.
198 ///
199 /// This is the same as decodeSignal but you must parse the bitfield value of the signal from the CAN
200 /// message yourself. This is useful if you need that raw value for something
201 /// else.
202 ///
203 /// @param[in] signal - The details of the signal to decode and forward.
204 /// @param[in] message - Raw CAN message to decode
205 /// @param[in] signals - an array of all active signals.
206 /// @param[out] send - An output parameter that will be flipped to false if the value could
207 ///      not be decoded.
208 ///
209 openxc_DynamicField decoder_t::decodeSignal( can_signal_t& signal,
210                 const can_message_t& message, const std::vector<can_signal_t>& signals, bool* send)
211 {
212         float value = parseSignalBitfield(signal, message);
213         return decodeSignal(signal, value, signals, send);
214 }
215
216
217 ///
218 /// @brief Decode the payload of an OBD-II PID.
219 ///
220 /// This function matches the type signature for a DiagnosticResponseDecoder, so
221 /// it can be used as the decoder for a DiagnosticRequest. It returns the decoded
222 /// value of the PID, using the standard formulas (see
223 /// http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
224 ///
225 /// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
226 ///  a byte array). This is most often used when the byte order is
227 ///  signiticant, i.e. with many OBD-II PID formulas.
228 /// @param[in] parsed_payload - the entire payload of the response parsed as an int.
229 ///
230 /// @return Float decoded value.
231 ///
232 float decoder_t::decode_obd2_response(const DiagnosticResponse* response, float parsed_payload)
233 {
234         return diagnostic_decode_obd2_pid(response);
235 }