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"
21 #include "canutil/read.h"
22 #include "../utils/openxc-utils.hpp"
23 #include "message-definition.hpp"
24 #include "../binding/low-can-hat.hpp"
25 #include "../utils/converter.hpp"
27 /// @brief Handle sign of the signal according to several decoding methods
29 /// @param[in] signal - The signal
30 /// @param[in] data_signal - The data of the signal
31 /// @param[in] new_end_bit - The last bit of in the last byte of the data (data_signal[0])
32 /// @param[in] can_data - The whole can data (needed for SIGN BIT EXTERN)
34 /// @return Returns the sign of the data
36 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)
38 uint8_t data_byte = 0;
42 if(signal.get_sign() == sign_t::UNSIGNED)
44 else if(signal.get_sign() == sign_t::SIGN_BIT_EXTERN) {
45 end_bit = signal.get_bit_sign_position() % CHAR_BIT;
46 mask = static_cast<uint8_t>((1 << (end_bit + 1)) - 1);
47 data_byte = can_data[signal.get_bit_sign_position() / CHAR_BIT] & mask;
50 end_bit = new_end_bit;
51 mask = static_cast<uint8_t>((1 << (end_bit + 1)) - 1);
52 data_byte = data_signal[0] & mask;
55 //if negative: decode with right method
56 if(data_byte >> end_bit) {
57 switch(signal.get_sign())
59 //remove the sign bit to get the absolute value
60 case sign_t::SIGN_BIT:
61 data_signal[0] = static_cast<uint8_t>(data_signal[0] & (mask >> 1));
63 //same method twos complement = ones complement + 1
64 case sign_t::ONES_COMPLEMENT:
65 case sign_t::TWOS_COMPLEMENT:
66 //complement only until end_bit
67 data_signal[0] = ((data_signal[0] ^ mask) & mask);
68 if(data_signal.size() > 1)
69 for(int i=1; i < data_signal.size(); i++)
70 data_signal[i] = data_signal[i] ^ 0xFF;
71 if(signal.get_sign() == sign_t::TWOS_COMPLEMENT)
72 data_signal[data_signal.size() - 1] = static_cast<uint8_t>(data_signal[data_signal.size() - 1] + 1);
74 case sign_t::SIGN_BIT_EXTERN:
77 AFB_ERROR("Not a valid sign entry %d, considering the value as unsigned", signal.get_sign());
85 /// @brief Parses the signal's bitfield from the given data and returns the raw
88 /// @param[in] signal - The signal to be parsed from the data.
89 /// @param[in] message - message_t to parse
91 /// @return Returns the raw value of the signal parsed as a bitfield from the given byte
94 float decoder_t::parse_signal_bitfield(signal_t& signal, std::shared_ptr<message_t> message)
97 std::vector<uint8_t> data;
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 data = message->get_data_vector();
108 converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
110 for(int i=new_start_byte;i<=new_end_byte;i++)
111 data_signal.push_back(data[i]);
113 sign = handle_sign(signal, data_signal, new_end_bit, data);
115 if(data_signal.size() > 65535)
116 AFB_ERROR("Too long data signal %s", signal.get_name().c_str());
118 return static_cast<float>(sign) * bitfield_parse_float(data_signal.data(), (uint16_t) data_signal.size(),
119 new_start_bit, bit_size, signal.get_factor(),
120 signal.get_offset());
124 /// @brief Decode and return string bytes (hex) for a CAN signal's.
126 /// This is an implementation of the Signal type signature, and can be
127 /// used directly in the signal_t.decoder field.
129 /// @param[in] signal - The details of the signal.
130 /// @param[in] message - The message with data to decode.
131 /// @param[out] send - An output argument that will be set to false if the value should
132 /// not be sent for any reason.
134 /// @return Returns a DynamicField with a string value of bytes (hex)
136 openxc_DynamicField decoder_t::decode_bytes(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
139 openxc_DynamicField decoded_value;
140 std::vector<uint8_t> data = message->get_data_vector();
141 uint32_t length = message->get_length();
142 uint32_t bit_position = signal.get_bit_position();
143 uint32_t bit_size = signal.get_bit_size();
145 std::vector<uint8_t> new_data = std::vector<uint8_t>();
146 new_data.reserve((bit_size / CHAR_BIT) + 1);
148 int new_start_byte = 0;
149 int new_end_byte = 0;
150 uint8_t new_start_bit = 0;
151 uint8_t new_end_bit = 0;
153 converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
155 if(new_end_byte >= length)
156 new_end_byte = length-1;
158 if(new_start_byte >= length)
160 AFB_ERROR("Error in signal's description");
161 return decoded_value;
164 uint8_t mask_first_v = static_cast<uint8_t>(0xFF << new_start_bit);
165 uint8_t mask_last_v = static_cast<uint8_t>(0xFF >> (7 - new_end_bit));
167 if(new_start_byte == new_end_byte)
169 data[new_start_byte] = data[new_start_byte] & (mask_first_v & mask_last_v);
173 data[new_start_byte] = data[new_start_byte] & mask_first_v;
174 data[new_end_byte] = data[new_end_byte] & mask_last_v;
177 for(i=new_start_byte ; i <= new_end_byte ; i++)
178 new_data.push_back(data[i]);
180 decoded_value = build_DynamicField(new_data);
182 return decoded_value;
186 /// @brief Decode and return string bytes (hex) for a CAN signal's.
188 /// This is an implementation of the Signal type signature, and can be
189 /// used directly in the signal_t.decoder field.
191 /// @param[in] signal - The details of the signal.
192 /// @param[in] message - The message with data to decode.
193 /// @param[out] send - An output argument that will be set to false if the value should
194 /// not be sent for any reason.
196 /// @return Returns a DynamicField with a string value of bytes (hex)
198 openxc_DynamicField decoder_t::decode_ascii(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
200 std::string ret_s = "";
201 openxc_DynamicField openxc_bytes = decode_bytes(signal,message,send);
202 if(!openxc_bytes.has_bytes_value)
203 AFB_ERROR("Error no bytes value to translate to ascii");
204 ret_s = converter_t::to_ascii(openxc_bytes.bytes_value,openxc_bytes.length_array);
205 openxc_DynamicField ret = build_DynamicField(ret_s);
210 openxc_DynamicField decoder_t::decode_date(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
212 float value = decoder_t::parse_signal_bitfield(signal, message);
213 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
214 openxc_DynamicField decoded_value = build_DynamicField(value);
216 // Don't send if they is no changes
217 if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
219 signal.set_last_value(value);
221 return decoded_value;
225 openxc_DynamicField decoder_t::decode_time(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
227 float value = decoder_t::parse_signal_bitfield(signal, message);
228 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
229 openxc_DynamicField decoded_value = build_DynamicField(value);
231 // Don't send if they is no changes
232 *send = (signal.get_last_value() == value && !signal.get_send_same()) || !*send ? false : true;
233 signal.set_last_value(value);
235 return decoded_value;
239 /// @brief Wraps a raw CAN signal value in a DynamicField without modification.
241 /// This is an implementation of the Signal type signature, and can be
242 /// used directly in the signal_t.decoder field.
244 /// @param[in] signal - The details of the signal that contains the state mapping.
245 /// @param[in] message - The message with data to decode.
246 /// @param[out] send - An output argument that will be set to false if the value should
247 /// not be sent for any reason.
249 /// @return Returns a DynamicField with the original, unmodified raw CAN signal value as
250 /// its numeric value. The 'send' argument will not be modified as this decoder
253 openxc_DynamicField decoder_t::decode_noop(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
255 float value = decoder_t::parse_signal_bitfield(signal, message);
256 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
257 openxc_DynamicField decoded_value = build_DynamicField(value);
259 // Don't send if they is no changes
260 if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
262 signal.set_last_value(value);
264 return decoded_value;
266 /// @brief Coerces a numerical value to a boolean.
268 /// This is an implementation of the Signal type signature, and can be
269 /// used directly in the signal_t.decoder field.
271 /// @param[in] signal - The details of the signal that contains the state mapping.
272 /// @param[in] message - The message with data to decode.
273 /// @param[out] send - An output argument that will be set to false if the value should
274 /// not be sent for any reason.
276 /// @return Returns a DynamicField with a boolean value of false if the raw signal value
277 /// is 0.0, otherwise true. The 'send' argument will not be modified as this
278 /// decoder always succeeds.
280 openxc_DynamicField decoder_t::decode_boolean(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
282 float value = decoder_t::parse_signal_bitfield(signal, message);
283 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
284 openxc_DynamicField decoded_value = build_DynamicField(value == 0.0 ? false : true);
286 // Don't send if they is no changes
287 if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
290 signal.set_last_value(value);
293 return decoded_value;
295 /// @brief Update the metadata for a signal and the newly received value.
297 /// This is an implementation of the Signal type signature, and can be
298 /// used directly in the signal_t.decoder field.
300 /// This function always flips 'send' to false.
302 /// @param[in] signal - The details of the signal that contains the state mapping.
303 /// @param[in] message - The message with data to decode.
304 /// @param[out] send - This output argument will always be set to false, so the caller will
305 /// know not to publish this value to the pipeline.
307 /// @return Return value is undefined.
309 openxc_DynamicField decoder_t::decode_ignore(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
311 float value = decoder_t::parse_signal_bitfield(signal, message);
315 signal.set_last_value(value);
316 openxc_DynamicField decoded_value;
318 return decoded_value;
321 /// @brief Find and return the corresponding string state for a CAN signal's
322 /// raw integer value.
324 /// This is an implementation of the Signal type signature, and can be
325 /// used directly in the signal_t.decoder field.
327 /// @param[in] signal - The details of the signal that contains the state mapping.
328 /// @param[in] message - The message with data to decode.
329 /// @param[out] send - An output argument that will be set to false if the value should
330 /// not be sent for any reason.
332 /// @return Returns a DynamicField with a string value if a matching state is found in
333 /// the signal. If an equivalent isn't found, send is sent to false and the
334 /// return value is undefined.
336 openxc_DynamicField decoder_t::decode_state(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
338 float value = decoder_t::parse_signal_bitfield(signal, message);
339 AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
340 const std::string signal_state = signal.get_states((uint8_t)value);
341 openxc_DynamicField decoded_value = build_DynamicField(signal_state);
342 if(signal_state.size() <= 0)
345 AFB_ERROR("No state found with index: %d", (int)value);
348 // Don't send if they is no changes
349 if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
351 signal.set_last_value(value);
354 return decoded_value;
358 /// @brief Parse a signal from a CAN message, apply any required transforations
359 /// to get a human readable value and public the result to the pipeline.
361 /// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
362 /// will be passed to the decoder before publishing.
364 /// @param[in] signal - The details of the signal to decode and forward.
365 /// @param[in] message - The message with data to decode.
366 /// @param[out] send - An output parameter that will be flipped to false if the value could
369 /// The decoder returns an openxc_DynamicField, which may contain a number,
370 /// string or boolean.
372 openxc_DynamicField decoder_t::translate_signal(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
374 // Must call the decoders every time, regardless of if we are going to
375 // decide to send the signal or not.
376 openxc_DynamicField decoded_value = decoder_t::decode_signal(signal,
379 signal.set_received(true);
380 signal.set_timestamp(message->get_timestamp());
381 signal.get_message()->set_last_value(message);
382 return decoded_value;
385 /// @brief Parse a signal from a CAN message and apply any required
386 /// transforations to get a human readable value.
388 /// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
389 /// will be passed to the decoder before returning.
391 /// @param[in] signal - The details of the signal to decode and forward.
392 /// @param[in] message - The message with data to decode.
393 /// @param[out] send - An output parameter that will be flipped to false if the value could
396 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
397 /// string or boolean. If 'send' is false, the return value is undefined.
399 openxc_DynamicField decoder_t::decode_signal( signal_t& signal, std::shared_ptr<message_t> message, bool* send)
403 if(signal.get_message()->get_flags() & CONTINENTAL_BIT_POSITION)
404 signal.set_bit_position(converter_t::continental_bit_position_mess(message->get_length(),
405 signal.get_bit_position(),
406 signal.get_bit_size()));
407 if(signal.get_message()->get_flags() & BIT_POSITION_REVERSED)
408 signal.set_bit_position(converter_t::bit_position_swap(message->get_length(),
409 signal.get_bit_position(),
410 signal.get_bit_size()));
412 signal_decoder decoder = signal.get_decoder() == nullptr ?
413 decode_noop : signal.get_decoder();
415 openxc_DynamicField decoded_value = decoder(signal,
417 return decoded_value;
421 /// @brief Decode the payload of an OBD-II PID.
423 /// This function matches the type signature for a DiagnosticResponse, so
424 /// it can be used as the decoder for a DiagnosticRequest. It returns the decoded
425 /// value of the PID, using the standard formulas (see
426 /// http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
428 /// @param[in] response - the received DiagnosticResponse (the data is in response.payload,
429 /// a byte array). This is most often used when the byte order is
430 /// signiticant, i.e. with many OBD-II PID formulas.
431 /// @param[in] parsed_payload - the entire payload of the response parsed as an int.
433 /// @return Float decoded value.
435 float decoder_t::decode_obd2_response(const DiagnosticResponse* response, float parsed_payload)
437 return diagnostic_decode_obd2_pid(response);