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"
22 #include "can-message-definition.hpp"
23 #include "../binding/low-can-hat.hpp"
25 /// @brief Parses the signal's bitfield from the given data and returns the raw
28 /// @param[in] signal - The signal to be parsed 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::parse_signal_bitfield(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 Wraps a raw CAN signal value in a DynamicField without modification.
43 /// This is an implementation of the Signal 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] value - The numerical value that will be wrapped in a DynamicField.
48 /// @param[out] send - An output argument that will be set to false if the value should
49 /// not be sent for any reason.
51 /// @return Returns a DynamicField with the original, unmodified raw CAN signal value as
52 /// its numeric value. The 'send' argument will not be modified as this decoder
55 openxc_DynamicField decoder_t::decode_noop(can_signal_t& signal, float value, bool* send)
57 openxc_DynamicField decoded_value = build_DynamicField(value);
61 /// @brief Coerces a numerical value to a boolean.
63 /// This is an implementation of the Signal type signature, and can be
64 /// used directly in the can_signal_t.decoder field.
66 /// @param[in] signal - The details of the signal that contains the state mapping.
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.
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.
75 openxc_DynamicField decoder_t::decode_boolean(can_signal_t& signal, float value, bool* send)
77 openxc_DynamicField decoded_value = build_DynamicField(value == 0.0 ? false : true);
81 /// @brief Update the metadata for a signal and the newly received value.
83 /// This is an implementation of the Signal type signature, and can be
84 /// used directly in the can_signal_t.decoder field.
86 /// This function always flips 'send' to false.
88 /// @param[in] signal - The details of the signal that contains the state mapping.
89 /// @param[in] value - The numerical value that will be converted to a boolean.
90 /// @param[out] send - This output argument will always be set to false, so the caller will
91 /// know not to publish this value to the pipeline.
93 /// @return Return value is undefined.
95 openxc_DynamicField decoder_t::decode_ignore(can_signal_t& signal, float value, bool* send)
100 openxc_DynamicField decoded_value;
102 return decoded_value;
105 /// @brief Find and return the corresponding string state for a CAN signal's
106 /// raw integer value.
108 /// This is an implementation of the Signal type signature, and can be
109 /// used directly in the can_signal_t.decoder field.
111 /// @param[in] signal - The details of the signal that contains the state mapping.
112 /// @param[in] value - The numerical value that should map to a state.
113 /// @param[out] send - An output argument that will be set to false if the value should
114 /// not be sent for any reason.
116 /// @return Returns a DynamicField with a string value if a matching state is found in
117 /// the signal. If an equivalent isn't found, send is sent to false and the
118 /// return value is undefined.
120 openxc_DynamicField decoder_t::decode_state(can_signal_t& signal, float value, bool* send)
122 const std::string signal_state = signal.get_states((uint8_t)value);
123 openxc_DynamicField decoded_value = build_DynamicField(signal_state);
124 if(signal_state.size() <= 0)
127 AFB_ERROR("No state found with index: %d", (int)value);
129 return decoded_value;
133 /// @brief Parse a signal from a CAN message, apply any required transforations
134 /// to get a human readable value and public the result to the pipeline.
136 /// If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
137 /// will be passed to the decoder before publishing.
139 /// @param[in] signal - The details of the signal to decode and forward.
140 /// @param[in] message - The received CAN message that should contain this signal.
141 /// @param[out] send - An output parameter that will be flipped to false if the value could
144 /// The decoder returns an openxc_DynamicField, which may contain a number,
145 /// string or boolean.
147 openxc_DynamicField decoder_t::translate_signal(can_signal_t& signal, const can_message_t& message, bool* send)
149 float value = decoder_t::parse_signal_bitfield(signal, message);
150 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
152 // Must call the decoders every time, regardless of if we are going to
153 // decide to send the signal or not.
154 openxc_DynamicField decoded_value = decoder_t::decode_signal(signal,
157 signal.set_received(true);
159 // Don't send if they is no changes
160 if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
164 signal.set_last_value(value);
165 signal.set_timestamp(message.get_timestamp());
166 signal.get_message()->set_last_value(message);
167 return decoded_value;
170 /// @brief Parse a signal from a CAN message and apply any required
171 /// transforations to get a human readable value.
173 /// If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal value
174 /// will be passed to the decoder before returning.
176 /// @param[in] signal - The details of the signal to decode and forward.
177 /// @param[in] value - The numerical value that will be converted to a boolean.
178 /// @param[out] send - An output parameter that will be flipped to false if the value could
181 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
182 /// string or boolean. If 'send' is false, the return value is undefined.
184 openxc_DynamicField decoder_t::decode_signal( can_signal_t& signal, float value, bool* send)
186 signal_decoder decoder = signal.get_decoder() == nullptr ?
187 decode_noop : signal.get_decoder();
188 openxc_DynamicField decoded_value = decoder(signal,
190 return decoded_value;
193 /// @brief Decode a transformed, human readable value from an raw CAN signal
194 /// already parsed from a CAN message.
196 /// This is the same as decode_signal but you must parse the bitfield value of the signal from the CAN
197 /// message yourself. This is useful if you need that raw value for something
200 /// @param[in] signal - The details of the signal to decode and forward.
201 /// @param[in] message - Raw CAN message to decode
202 /// @param[out] send - An output parameter that will be flipped to false if the value could
205 openxc_DynamicField decoder_t::decode_signal( can_signal_t& signal, const can_message_t& message, bool* send)
207 float value = parse_signal_bitfield(signal, message);
208 return decode_signal(signal, value, send);
213 /// @brief Decode the payload of an OBD-II PID.
215 /// This function matches the type signature for a DiagnosticResponse, so
216 /// it can be used as the decoder for a DiagnosticRequest. It returns the decoded
217 /// value of the PID, using the standard formulas (see
218 /// http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
220 /// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
221 /// a byte array). This is most often used when the byte order is
222 /// signiticant, i.e. with many OBD-II PID formulas.
223 /// @param[in] parsed_payload - the entire payload of the response parsed as an int.
225 /// @return Float decoded value.
227 float decoder_t::decode_obd2_response(const DiagnosticResponse* response, float parsed_payload)
229 return diagnostic_decode_obd2_pid(response);