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 "message-definition.hpp"
23 #include "../binding/low-can-hat.hpp"
24 #include "../utils/converter.hpp"
26 /// @brief Parses the signal's bitfield from the given data and returns the raw
29 /// @param[in] signal - The signal to be parsed from the data.
30 /// @param[in] message - message_t to parse
32 /// @return Returns the raw value of the signal parsed as a bitfield from the given byte
35 float decoder_t::parse_signal_bitfield(signal_t& signal, std::shared_ptr<message_t> message)
37 const std::vector<uint8_t> data = message->get_data_vector();
38 std::vector<uint8_t> data_signal;
39 uint32_t bit_size = signal.get_bit_size();
40 uint32_t bit_position = signal.get_bit_position();
42 int new_start_byte = 0;
44 int new_start_bit = 0;
47 converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
49 for(int i=new_start_byte;i<=new_end_byte;i++)
51 data_signal.push_back(data[i]);
54 uint8_t new_bit_size = 0;
58 AFB_ERROR("Error signal %s to long bit size",signal.get_name().c_str());
62 new_bit_size = (uint8_t) bit_size;
65 uint8_t bit_offset = 0;
66 if(new_start_bit > 255)
68 AFB_ERROR("Too long signal offset %d", new_start_bit);
72 bit_offset = (uint8_t) new_start_bit;
77 if(data_signal.size() > 65535)
79 AFB_ERROR("Too long data signal %s",signal.get_name().c_str());
83 length = (uint16_t) data_signal.size();
86 return bitfield_parse_float(data_signal.data(), length,
87 bit_offset, new_bit_size, signal.get_factor(),
91 /// @brief Wraps a raw CAN signal value in a DynamicField without modification.
93 /// This is an implementation of the Signal type signature, and can be
94 /// used directly in the signal_t.decoder field.
96 /// @param[in] signal - The details of the signal that contains the state mapping.
97 /// @param[in] message - The message with data to decode.
98 /// @param[out] send - An output argument that will be set to false if the value should
99 /// not be sent for any reason.
101 /// @return Returns a DynamicField with the original, unmodified raw CAN signal value as
102 /// its numeric value. The 'send' argument will not be modified as this decoder
105 openxc_DynamicField decoder_t::decode_noop(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
107 float value = decoder_t::parse_signal_bitfield(signal, message);
108 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
109 openxc_DynamicField decoded_value = build_DynamicField(value);
111 // Don't send if they is no changes
112 if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
116 signal.set_last_value(value);
118 return decoded_value;
120 /// @brief Coerces a numerical value to a boolean.
122 /// This is an implementation of the Signal type signature, and can be
123 /// used directly in the signal_t.decoder field.
125 /// @param[in] signal - The details of the signal that contains the state mapping.
126 /// @param[in] message - The message with data to decode.
127 /// @param[out] send - An output argument that will be set to false if the value should
128 /// not be sent for any reason.
130 /// @return Returns a DynamicField with a boolean value of false if the raw signal value
131 /// is 0.0, otherwise true. The 'send' argument will not be modified as this
132 /// decoder always succeeds.
134 openxc_DynamicField decoder_t::decode_boolean(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
136 float value = decoder_t::parse_signal_bitfield(signal, message);
137 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
138 openxc_DynamicField decoded_value = build_DynamicField(value == 0.0 ? false : true);
140 // Don't send if they is no changes
141 if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
145 signal.set_last_value(value);
148 return decoded_value;
150 /// @brief Update the metadata for a signal and the newly received value.
152 /// This is an implementation of the Signal type signature, and can be
153 /// used directly in the signal_t.decoder field.
155 /// This function always flips 'send' to false.
157 /// @param[in] signal - The details of the signal that contains the state mapping.
158 /// @param[in] message - The message with data to decode.
159 /// @param[out] send - This output argument will always be set to false, so the caller will
160 /// know not to publish this value to the pipeline.
162 /// @return Return value is undefined.
164 openxc_DynamicField decoder_t::decode_ignore(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
166 float value = decoder_t::parse_signal_bitfield(signal, message);
170 signal.set_last_value(value);
171 openxc_DynamicField decoded_value;
173 return decoded_value;
176 /// @brief Find and return the corresponding string state for a CAN signal's
177 /// raw integer value.
179 /// This is an implementation of the Signal type signature, and can be
180 /// used directly in the signal_t.decoder field.
182 /// @param[in] signal - The details of the signal that contains the state mapping.
183 /// @param[in] message - The message with data to decode.
184 /// @param[out] send - An output argument that will be set to false if the value should
185 /// not be sent for any reason.
187 /// @return Returns a DynamicField with a string value if a matching state is found in
188 /// the signal. If an equivalent isn't found, send is sent to false and the
189 /// return value is undefined.
191 openxc_DynamicField decoder_t::decode_state(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
193 float value = decoder_t::parse_signal_bitfield(signal, message);
194 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
195 const std::string signal_state = signal.get_states((uint8_t)value);
196 openxc_DynamicField decoded_value = build_DynamicField(signal_state);
197 if(signal_state.size() <= 0)
200 AFB_ERROR("No state found with index: %d", (int)value);
203 // Don't send if they is no changes
204 if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
208 signal.set_last_value(value);
211 return decoded_value;
215 /// @brief Parse a signal from a CAN message, apply any required transforations
216 /// to get a human readable value and public the result to the pipeline.
218 /// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
219 /// will be passed to the decoder before publishing.
221 /// @param[in] signal - The details of the signal to decode and forward.
222 /// @param[in] message - The message with data to decode.
223 /// @param[out] send - An output parameter that will be flipped to false if the value could
226 /// The decoder returns an openxc_DynamicField, which may contain a number,
227 /// string or boolean.
229 openxc_DynamicField decoder_t::translate_signal(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
232 // Must call the decoders every time, regardless of if we are going to
233 // decide to send the signal or not.
234 openxc_DynamicField decoded_value = decoder_t::decode_signal(signal,
237 signal.set_received(true);
238 signal.set_timestamp(message->get_timestamp());
239 signal.get_message()->set_last_value(message);
240 return decoded_value;
243 /// @brief Parse a signal from a CAN message and apply any required
244 /// transforations to get a human readable value.
246 /// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
247 /// will be passed to the decoder before returning.
249 /// @param[in] signal - The details of the signal to decode and forward.
250 /// @param[in] message - The message with data to decode.
251 /// @param[out] send - An output parameter that will be flipped to false if the value could
254 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
255 /// string or boolean. If 'send' is false, the return value is undefined.
257 openxc_DynamicField decoder_t::decode_signal( signal_t& signal, std::shared_ptr<message_t> message, bool* send)
259 signal_decoder decoder = signal.get_decoder() == nullptr ?
260 decode_noop : signal.get_decoder();
262 openxc_DynamicField decoded_value = decoder(signal,
264 return decoded_value;
268 /// @brief Decode the payload of an OBD-II PID.
270 /// This function matches the type signature for a DiagnosticResponse, so
271 /// it can be used as the decoder for a DiagnosticRequest. It returns the decoded
272 /// value of the PID, using the standard formulas (see
273 /// http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
275 /// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
276 /// a byte array). This is most often used when the byte order is
277 /// signiticant, i.e. with many OBD-II PID formulas.
278 /// @param[in] parsed_payload - the entire payload of the response parsed as an int.
280 /// @return Float decoded value.
282 float decoder_t::decode_obd2_response(const DiagnosticResponse* response, float parsed_payload)
284 return diagnostic_decode_obd2_pid(response);