2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "can-decoder.hpp"
20 #include "canutil/read.h"
21 #include "../utils/openxc-utils.hpp"
23 #include "../binding/low-can-binding.hpp"
25 /// @brief Parse the signal's bitfield from the given data and return the raw
28 /// @param[in] signal - The signal to parse from the data.
29 /// @param[in] message - can_message_t to parse
31 /// @return Returns the raw value of the signal parsed as a bitfield from the given byte
34 float decoder_t::parseSignalBitfield(can_signal_t& signal, const can_message_t& message)
36 return bitfield_parse_float(message.get_data(), CAN_MESSAGE_SIZE,
37 signal.get_bit_position(), signal.get_bit_size(), signal.get_factor(),
41 /// @brief Wrap a raw CAN signal value in a DynamicField without modification.
43 /// This is an implementation of the SignalDecoder type signature, and can be
44 /// used directly in the can_signal_t.decoder field.
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.
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
56 openxc_DynamicField decoder_t::noopDecoder(can_signal_t& signal,
57 const std::vector<can_signal_t>& signals, float value, bool* send)
59 openxc_DynamicField decoded_value = build_DynamicField(value);
63 /// @brief Coerces a numerical value to a boolean.
65 /// This is an implementation of the SignalDecoder type signature, and can be
66 /// used directly in the can_signal_t.decoder field.
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.
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.
78 openxc_DynamicField decoder_t::booleanDecoder(can_signal_t& signal,
79 const std::vector<can_signal_t>& signals, float value, bool* send)
81 openxc_DynamicField decoded_value = build_DynamicField(value == 0.0 ? false : true);
85 /// @brief Update the metadata for a signal and the newly received value.
87 /// This is an implementation of the SignalDecoder type signature, and can be
88 /// used directly in the can_signal_t.decoder field.
90 /// This function always flips 'send' to false.
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.
98 /// @return Return value is undefined.
100 openxc_DynamicField decoder_t::ignoreDecoder(can_signal_t& signal,
101 const std::vector<can_signal_t>& signals, float value, bool* send)
106 openxc_DynamicField decoded_value;
108 return decoded_value;
111 /// @brief Find and return the corresponding string state for a CAN signal's
112 /// raw integer value.
114 /// This is an implementation of the SignalDecoder type signature, and can be
115 /// used directly in the can_signal_t.decoder field.
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.
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.
127 openxc_DynamicField decoder_t::stateDecoder(can_signal_t& signal,
128 const std::vector<can_signal_t>& signals, float value, bool* send)
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)
135 ERROR(binder_interface, "%s: No state found with index: %d", __FUNCTION__, (int)value);
137 return decoded_value;
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.
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.
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
153 /// The decoder returns an openxc_DynamicField, which may contain a number,
154 /// string or boolean.
156 openxc_DynamicField decoder_t::translateSignal(can_signal_t& signal, can_message_t& message,
157 const std::vector<can_signal_t>& signals, bool* send)
159 float value = decoder_t::parseSignalBitfield(signal, message);
160 DEBUG(binder_interface, "%s: Decoded message from parseSignalBitfield: %f", __FUNCTION__, value);
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);
167 signal.set_received(true);
169 // Don't send if they is no changes
170 if ((signal.get_last_value() == value && !signal.get_send_same()) || !send )
174 signal.set_last_value(value);
175 return decoded_value;
178 /// @brief Parse a signal from a CAN message and apply any required
179 /// transforations to get a human readable value.
181 /// If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
182 /// will be passed to the decoder before returning.
184 /// @param[in] signal - The details of the signal to decode and forward.
185 /// @param[in] value - The numerical value that will be converted to a boolean.
186 /// @param[in] signals - an array of all active signals.
187 /// @param[out] send - An output parameter that will be flipped to false if the value could
190 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
191 /// string or boolean. If 'send' is false, the return value is undefined.
193 openxc_DynamicField decoder_t::decodeSignal( can_signal_t& signal,
194 float value, const std::vector<can_signal_t>& signals, bool* send)
196 SignalDecoder decoder = signal.get_decoder() == nullptr ?
197 noopDecoder : signal.get_decoder();
198 openxc_DynamicField decoded_value = decoder(signal, signals,
200 return decoded_value;
203 /// @brief Decode a transformed, human readable value from an raw CAN signal
204 /// already parsed from a CAN message.
206 /// This is the same as decodeSignal but you must parse the bitfield value of the signal from the CAN
207 /// message yourself. This is useful if you need that raw value for something
210 /// @param[in] signal - The details of the signal to decode and forward.
211 /// @param[in] message - Raw CAN message to decode
212 /// @param[in] signals - an array of all active signals.
213 /// @param[out] send - An output parameter that will be flipped to false if the value could
216 openxc_DynamicField decoder_t::decodeSignal( can_signal_t& signal,
217 const can_message_t& message, const std::vector<can_signal_t>& signals, bool* send)
219 float value = parseSignalBitfield(signal, message);
220 return decodeSignal(signal, value, signals, send);
225 /// @brief Decode the payload of an OBD-II PID.
227 /// This function matches the type signature for a DiagnosticResponseDecoder, so
228 /// it can be used as the decoder for a DiagnosticRequest. It returns the decoded
229 /// value of the PID, using the standard formulas (see
230 /// http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
232 /// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
233 /// a byte array). This is most often used when the byte order is
234 /// signiticant, i.e. with many OBD-II PID formulas.
235 /// @param[in] parsed_payload - the entire payload of the response parsed as an int.
237 /// @return Float decoded value.
239 float decoder_t::decode_obd2_response(const DiagnosticResponse* response, float parsed_payload)
241 return diagnostic_decode_obd2_pid(response);