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 Handle sign of the signal according to several decoding methods
28 /// @param[in] signal - The signal
29 /// @param[in] data_signal - The data of the signal
30 /// @param[in] new_end_bit - The last bit of in the last byte of the data (data_signal[0])
31 /// @param[in] can_data - The whole can data (needed for SIGN BIT EXTERN)
33 /// @return Returns the sign of the data
35 int decoder_t::handle_sign(const signal_t& signal, std::vector<uint8_t>& data_signal, uint8_t new_end_bit, const std::vector<uint8_t>& can_data)
37 uint8_t data_byte = 0;
41 if(signal.get_sign() == sign_t::UNSIGNED)
43 else if(signal.get_sign() == sign_t::SIGN_BIT_EXTERN) {
44 end_bit = signal.get_bit_sign_position()%8;
45 mask = static_cast<uint8_t>((1 << (end_bit + 1)) - 1);
46 data_byte = can_data[signal.get_bit_sign_position()/8] & mask;
49 end_bit = new_end_bit;
50 mask = static_cast<uint8_t>((1 << (end_bit + 1)) - 1);
51 data_byte = data_signal[0] & mask;
54 //if negative: decode with right method
55 if(data_byte >> end_bit) {
56 switch(signal.get_sign())
58 //remove the sign bit to get the absolute value
59 case sign_t::SIGN_BIT:
60 data_signal[0] = static_cast<uint8_t>(data_signal[0] & (mask >> 1));
62 //same method twos complement = ones complement + 1
63 case sign_t::ONES_COMPLEMENT:
64 case sign_t::TWOS_COMPLEMENT:
65 //complement only until end_bit
66 data_signal[0] = ((data_signal[0] ^ mask) & mask);
67 if(data_signal.size() > 1) {
68 for(int i=1; i < data_signal.size(); i++) {
69 data_signal[i] = data_signal[i] ^ 0xFF;
72 if(signal.get_sign() == sign_t::TWOS_COMPLEMENT)
73 data_signal[data_signal.size() - 1] = static_cast<uint8_t>(data_signal[data_signal.size() - 1] + 1);
75 case sign_t::SIGN_BIT_EXTERN:
78 AFB_ERROR("Not a valid sign entry %d, considering the value as unsigned", signal.get_sign());
86 /// @brief Parses the signal's bitfield from the given data and returns the raw
89 /// @param[in] signal - The signal to be parsed from the data.
90 /// @param[in] message - message_t to parse
92 /// @return Returns the raw value of the signal parsed as a bitfield from the given byte
95 float decoder_t::parse_signal_bitfield(signal_t& signal, std::shared_ptr<message_t> message)
97 const std::vector<uint8_t> data = message->get_data_vector();
98 std::vector<uint8_t> data_signal;
99 uint8_t bit_size = (uint8_t) signal.get_bit_size();
100 uint32_t bit_position = signal.get_bit_position();
102 int new_start_byte = 0;
103 int new_end_byte = 0;
104 uint8_t new_start_bit = 0;
105 uint8_t new_end_bit = 0;
107 converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
109 for(int i=new_start_byte;i<=new_end_byte;i++)
110 data_signal.push_back(data[i]);
112 int sign = decoder_t::handle_sign(signal, data_signal, new_end_bit, data);
114 if(data_signal.size() > 65535)
115 AFB_ERROR("Too long data signal %s", signal.get_name().c_str());
117 return static_cast<float>(sign) * bitfield_parse_float(data_signal.data(), (uint16_t) data_signal.size(),
118 new_start_bit, bit_size, signal.get_factor(),
119 signal.get_offset());
123 /// @brief Decode and return string bytes (hex) for a CAN signal's.
125 /// This is an implementation of the Signal type signature, and can be
126 /// used directly in the signal_t.decoder field.
128 /// @param[in] signal - The details of the signal.
129 /// @param[in] message - The message with data to decode.
130 /// @param[out] send - An output argument that will be set to false if the value should
131 /// not be sent for any reason.
133 /// @return Returns a DynamicField with a string value of bytes (hex)
135 openxc_DynamicField decoder_t::decode_bytes(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
138 openxc_DynamicField decoded_value;
139 std::vector<uint8_t> data = message->get_data_vector();
140 uint32_t length = message->get_length();
141 uint32_t bit_position = signal.get_bit_position();
142 uint32_t bit_size = signal.get_bit_size();
143 std::vector<uint8_t> new_data = std::vector<uint8_t>();
144 new_data.reserve(bit_size << 3);
146 int new_start_byte = 0;
147 int new_end_byte = 0;
148 uint8_t new_start_bit = 0;
149 uint8_t new_end_bit = 0;
151 converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
153 if(new_end_byte >= length)
155 new_end_byte = length-1;
158 if(new_start_byte >= length)
160 AFB_ERROR("Error in description of signals");
161 return decoded_value;
164 uint8_t first = data[new_start_byte];
166 for(i=new_start_bit;i<8;i++)
168 mask_first = mask_first | (1 << i);
171 uint8_t mask_first_v = 0;
174 AFB_ERROR("Error mask decode bytes");
178 mask_first_v = (uint8_t)mask_first;
181 data[new_start_byte]=first&mask_first_v;
183 uint8_t last = data[new_end_byte];
185 for(i=0;i<=new_end_bit;i++)
187 mask_last = mask_last | (1 << (7-i));
190 uint8_t mask_last_v = 0;
193 AFB_ERROR("Error mask decode bytes");
197 mask_last_v = (uint8_t)mask_last;
200 data[new_end_byte]=last&mask_last_v;
203 for(i=new_start_byte;i<=new_end_byte;i++)
205 new_data.push_back(data[i]);
208 decoded_value = build_DynamicField(new_data);
210 return decoded_value;
213 /// @brief Wraps a raw CAN signal value in a DynamicField without modification.
215 /// This is an implementation of the Signal type signature, and can be
216 /// used directly in the signal_t.decoder field.
218 /// @param[in] signal - The details of the signal that contains the state mapping.
219 /// @param[in] message - The message with data to decode.
220 /// @param[out] send - An output argument that will be set to false if the value should
221 /// not be sent for any reason.
223 /// @return Returns a DynamicField with the original, unmodified raw CAN signal value as
224 /// its numeric value. The 'send' argument will not be modified as this decoder
227 openxc_DynamicField decoder_t::decode_noop(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
229 float value = decoder_t::parse_signal_bitfield(signal, message);
230 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
231 openxc_DynamicField decoded_value = build_DynamicField(value);
233 // Don't send if they is no changes
234 if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
238 signal.set_last_value(value);
240 return decoded_value;
242 /// @brief Coerces a numerical value to a boolean.
244 /// This is an implementation of the Signal type signature, and can be
245 /// used directly in the signal_t.decoder field.
247 /// @param[in] signal - The details of the signal that contains the state mapping.
248 /// @param[in] message - The message with data to decode.
249 /// @param[out] send - An output argument that will be set to false if the value should
250 /// not be sent for any reason.
252 /// @return Returns a DynamicField with a boolean value of false if the raw signal value
253 /// is 0.0, otherwise true. The 'send' argument will not be modified as this
254 /// decoder always succeeds.
256 openxc_DynamicField decoder_t::decode_boolean(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
258 float value = decoder_t::parse_signal_bitfield(signal, message);
259 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
260 openxc_DynamicField decoded_value = build_DynamicField(value == 0.0 ? false : true);
262 // Don't send if they is no changes
263 if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
267 signal.set_last_value(value);
270 return decoded_value;
272 /// @brief Update the metadata for a signal and the newly received value.
274 /// This is an implementation of the Signal type signature, and can be
275 /// used directly in the signal_t.decoder field.
277 /// This function always flips 'send' to false.
279 /// @param[in] signal - The details of the signal that contains the state mapping.
280 /// @param[in] message - The message with data to decode.
281 /// @param[out] send - This output argument will always be set to false, so the caller will
282 /// know not to publish this value to the pipeline.
284 /// @return Return value is undefined.
286 openxc_DynamicField decoder_t::decode_ignore(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
288 float value = decoder_t::parse_signal_bitfield(signal, message);
292 signal.set_last_value(value);
293 openxc_DynamicField decoded_value;
295 return decoded_value;
298 /// @brief Find and return the corresponding string state for a CAN signal's
299 /// raw integer value.
301 /// This is an implementation of the Signal type signature, and can be
302 /// used directly in the signal_t.decoder field.
304 /// @param[in] signal - The details of the signal that contains the state mapping.
305 /// @param[in] message - The message with data to decode.
306 /// @param[out] send - An output argument that will be set to false if the value should
307 /// not be sent for any reason.
309 /// @return Returns a DynamicField with a string value if a matching state is found in
310 /// the signal. If an equivalent isn't found, send is sent to false and the
311 /// return value is undefined.
313 openxc_DynamicField decoder_t::decode_state(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
315 float value = decoder_t::parse_signal_bitfield(signal, message);
316 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
317 const std::string signal_state = signal.get_states((uint8_t)value);
318 openxc_DynamicField decoded_value = build_DynamicField(signal_state);
319 if(signal_state.size() <= 0)
322 AFB_ERROR("No state found with index: %d", (int)value);
325 // Don't send if they is no changes
326 if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
330 signal.set_last_value(value);
333 return decoded_value;
337 /// @brief Parse a signal from a CAN message, apply any required transforations
338 /// to get a human readable value and public the result to the pipeline.
340 /// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
341 /// will be passed to the decoder before publishing.
343 /// @param[in] signal - The details of the signal to decode and forward.
344 /// @param[in] message - The message with data to decode.
345 /// @param[out] send - An output parameter that will be flipped to false if the value could
348 /// The decoder returns an openxc_DynamicField, which may contain a number,
349 /// string or boolean.
351 openxc_DynamicField decoder_t::translate_signal(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
353 if(!signal.get_message()->frame_layout_is_little() && !signal.bit_position_is_swapped())
355 signal.set_bit_position(converter_t::bit_position_swap(signal.get_bit_position(),signal.get_bit_size()));
356 signal.bit_position_is_swapped_reverse();
358 // Must call the decoders every time, regardless of if we are going to
359 // decide to send the signal or not.
360 openxc_DynamicField decoded_value = decoder_t::decode_signal(signal,
363 signal.set_received(true);
364 signal.set_timestamp(message->get_timestamp());
365 signal.get_message()->set_last_value(message);
366 return decoded_value;
369 /// @brief Parse a signal from a CAN message and apply any required
370 /// transforations to get a human readable value.
372 /// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
373 /// will be passed to the decoder before returning.
375 /// @param[in] signal - The details of the signal to decode and forward.
376 /// @param[in] message - The message with data to decode.
377 /// @param[out] send - An output parameter that will be flipped to false if the value could
380 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
381 /// string or boolean. If 'send' is false, the return value is undefined.
383 openxc_DynamicField decoder_t::decode_signal( signal_t& signal, std::shared_ptr<message_t> message, bool* send)
385 signal_decoder decoder = signal.get_decoder() == nullptr ?
386 decode_noop : signal.get_decoder();
388 openxc_DynamicField decoded_value = decoder(signal,
390 return decoded_value;
394 /// @brief Decode the payload of an OBD-II PID.
396 /// This function matches the type signature for a DiagnosticResponse, so
397 /// it can be used as the decoder for a DiagnosticRequest. It returns the decoded
398 /// value of the PID, using the standard formulas (see
399 /// http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
401 /// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
402 /// a byte array). This is most often used when the byte order is
403 /// signiticant, i.e. with many OBD-II PID formulas.
404 /// @param[in] parsed_payload - the entire payload of the response parsed as an int.
406 /// @return Float decoded value.
408 float decoder_t::decode_obd2_response(const DiagnosticResponse* response, float parsed_payload)
410 return diagnostic_decode_obd2_pid(response);