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