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