*/
#include "can-decoder.hpp"
+#include <climits>
#include "canutil/read.h"
#include "../utils/openxc-utils.hpp"
#include "../binding/low-can-hat.hpp"
#include "../utils/converter.hpp"
+/// @brief Handle sign of the signal according to several decoding methods
+///
+/// @param[in] signal - The signal
+/// @param[in] data_signal - The data of the signal
+/// @param[in] new_end_bit - The last bit of in the last byte of the data (data_signal[0])
+/// @param[in] can_data - The whole can data (needed for SIGN BIT EXTERN)
+///
+/// @return Returns the sign of the data
+///
+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)
+{
+ uint8_t data_byte = 0;
+ uint8_t mask = 0;
+ int end_bit = 0;
+
+ if(signal.get_sign() == sign_t::UNSIGNED)
+ return 1;
+ else if(signal.get_sign() == sign_t::SIGN_BIT_EXTERN) {
+ end_bit = signal.get_bit_sign_position() % CHAR_BIT;
+ mask = static_cast<uint8_t>((1 << (end_bit + 1)) - 1);
+ data_byte = can_data[signal.get_bit_sign_position() / CHAR_BIT] & mask;
+ }
+ else {
+ end_bit = new_end_bit;
+ mask = static_cast<uint8_t>((1 << (end_bit + 1)) - 1);
+ data_byte = data_signal[0] & mask;
+ }
+
+ //if negative: decode with right method
+ if(data_byte >> end_bit) {
+ switch(signal.get_sign())
+ {
+ //remove the sign bit to get the absolute value
+ case sign_t::SIGN_BIT:
+ data_signal[0] = static_cast<uint8_t>(data_signal[0] & (mask >> 1));
+ break;
+ //same method twos complement = ones complement + 1
+ case sign_t::ONES_COMPLEMENT:
+ case sign_t::TWOS_COMPLEMENT:
+ //complement only until end_bit
+ data_signal[0] = ((data_signal[0] ^ mask) & mask);
+ if(data_signal.size() > 1)
+ for(int i=1; i < data_signal.size(); i++)
+ data_signal[i] = data_signal[i] ^ 0xFF;
+ if(signal.get_sign() == sign_t::TWOS_COMPLEMENT)
+ data_signal[data_signal.size() - 1] = static_cast<uint8_t>(data_signal[data_signal.size() - 1] + 1);
+ break;
+ case sign_t::SIGN_BIT_EXTERN:
+ break;
+ default:
+ AFB_ERROR("Not a valid sign entry %d, considering the value as unsigned", signal.get_sign());
+ break;
+ }
+ return -1;
+ }
+ return 1;
+}
+
/// @brief Parses the signal's bitfield from the given data and returns the raw
/// value.
///
///
float decoder_t::parse_signal_bitfield(signal_t& signal, std::shared_ptr<message_t> message)
{
- const std::vector<uint8_t> data = message->get_data_vector();
+ int sign;
+ std::vector<uint8_t> data;
std::vector<uint8_t> data_signal;
- uint32_t bit_size = signal.get_bit_size();
+ uint8_t bit_size = (uint8_t) signal.get_bit_size();
uint32_t bit_position = signal.get_bit_position();
int new_start_byte = 0;
int new_end_byte = 0;
- int new_start_bit = 0;
- int new_end_bit = 0;
+ uint8_t new_start_bit = 0;
+ uint8_t new_end_bit = 0;
+ data = message->get_data_vector();
converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
for(int i=new_start_byte;i<=new_end_byte;i++)
- {
data_signal.push_back(data[i]);
- }
-
- uint8_t new_bit_size = 0;
- if(bit_size > 255)
- {
- AFB_ERROR("Error signal %s to long bit size",signal.get_name().c_str());
- }
- else
- {
- new_bit_size = (uint8_t) bit_size;
- }
-
- uint8_t bit_offset = 0;
- if(new_start_bit > 255)
- {
- AFB_ERROR("Too long signal offset %d", new_start_bit);
- }
- else
- {
- bit_offset = (uint8_t) new_start_bit;
- }
-
- uint16_t length = 0;
+ sign = handle_sign(signal, data_signal, new_end_bit, data);
if(data_signal.size() > 65535)
- {
- AFB_ERROR("Too long data signal %s",signal.get_name().c_str());
- }
- else
- {
- length = (uint16_t) data_signal.size();
- }
+ AFB_ERROR("Too long data signal %s", signal.get_name().c_str());
- return bitfield_parse_float(data_signal.data(), length,
- bit_offset, new_bit_size, signal.get_factor(),
+ return static_cast<float>(sign) * bitfield_parse_float(data_signal.data(), (uint16_t) data_signal.size(),
+ new_start_bit, bit_size, signal.get_factor(),
signal.get_offset());
}
uint32_t length = message->get_length();
uint32_t bit_position = signal.get_bit_position();
uint32_t bit_size = signal.get_bit_size();
+
std::vector<uint8_t> new_data = std::vector<uint8_t>();
- new_data.reserve(bit_size << 3);
+ new_data.reserve((bit_size / CHAR_BIT) + 1);
int new_start_byte = 0;
int new_end_byte = 0;
- int new_start_bit = 0;
- int new_end_bit = 0;
+ uint8_t new_start_bit = 0;
+ uint8_t new_end_bit = 0;
converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
if(new_end_byte >= length)
- {
new_end_byte = length-1;
- }
if(new_start_byte >= length)
{
- AFB_ERROR("Error in description of signals");
+ AFB_ERROR("Error in signal's description");
return decoded_value;
}
- uint8_t first = data[new_start_byte];
- int mask_first = 0;
- for(i=new_start_bit;i<8;i++)
- {
- mask_first = mask_first | (1 << i);
- }
+ uint8_t mask_first_v = static_cast<uint8_t>(0xFF << new_start_bit);
+ uint8_t mask_last_v = static_cast<uint8_t>(0xFF >> (7 - new_end_bit));
- uint8_t mask_first_v = 0;
- if(mask_first > 255)
+ if(new_start_byte == new_end_byte)
{
- AFB_ERROR("Error mask decode bytes");
+ data[new_start_byte] = data[new_start_byte] & (mask_first_v & mask_last_v);
}
else
{
- mask_first_v = (uint8_t)mask_first;
+ data[new_start_byte] = data[new_start_byte] & mask_first_v;
+ data[new_end_byte] = data[new_end_byte] & mask_last_v;
}
- data[new_start_byte]=first&mask_first_v;
+ for(i=new_start_byte ; i <= new_end_byte ; i++)
+ new_data.push_back(data[i]);
- uint8_t last = data[new_end_byte];
- int mask_last = 0;
- for(i=0;i<=new_end_bit;i++)
- {
- mask_last = mask_last | (1 << (7-i));
- }
+ decoded_value = build_DynamicField(new_data);
- uint8_t mask_last_v = 0;
- if(mask_last > 255)
- {
- AFB_ERROR("Error mask decode bytes");
- }
- else
- {
- mask_last_v = (uint8_t)mask_last;
- }
+ return decoded_value;
+}
- data[new_end_byte]=last&mask_last_v;
+/// @brief Decode and return string bytes (hex) for a CAN signal's.
+///
+/// This is an implementation of the Signal type signature, and can be
+/// used directly in the signal_t.decoder field.
+///
+/// @param[in] signal - The details of the signal.
+/// @param[in] message - The message with data to decode.
+/// @param[out] send - An output argument that will be set to false if the value should
+/// not be sent for any reason.
+///
+/// @return Returns a DynamicField with a string value of bytes (hex)
+///
+openxc_DynamicField decoder_t::decode_ascii(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
+{
+ std::string ret_s = "";
+ openxc_DynamicField openxc_bytes = decode_bytes(signal,message,send);
+ if(!openxc_bytes.has_bytes_value)
+ AFB_ERROR("Error no bytes value to translate to ascii");
+ ret_s = converter_t::to_ascii(openxc_bytes.bytes_value,openxc_bytes.length_array);
+ openxc_DynamicField ret = build_DynamicField(ret_s);
+ return ret;
+}
- for(i=new_start_byte;i<=new_end_byte;i++)
- {
- new_data.push_back(data[i]);
- }
+//edit
+openxc_DynamicField decoder_t::decode_date(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
+{
+ float value = decoder_t::parse_signal_bitfield(signal, message);
+ AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
+ openxc_DynamicField decoded_value = build_DynamicField(value);
- decoded_value = build_DynamicField(new_data);
+ // Don't send if they is no changes
+ if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
+ *send = false;
+ signal.set_last_value(value);
return decoded_value;
}
+//edit
+openxc_DynamicField decoder_t::decode_time(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
+{
+ float value = decoder_t::parse_signal_bitfield(signal, message);
+ AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
+ openxc_DynamicField decoded_value = build_DynamicField(value);
+
+ // Don't send if they is no changes
+ *send = (signal.get_last_value() == value && !signal.get_send_same()) || !*send ? false : true;
+ signal.set_last_value(value);
+
+ return decoded_value;
+}
+
+
/// @brief Wraps a raw CAN signal value in a DynamicField without modification.
///
/// This is an implementation of the Signal type signature, and can be
// Don't send if they is no changes
if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
- {
*send = false;
- }
signal.set_last_value(value);
return decoded_value;
// Don't send if they is no changes
if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
- {
*send = false;
- }
+
signal.set_last_value(value);
// Don't send if they is no changes
if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
- {
*send = false;
- }
signal.set_last_value(value);
///
openxc_DynamicField decoder_t::translate_signal(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
{
-
// Must call the decoders every time, regardless of if we are going to
// decide to send the signal or not.
openxc_DynamicField decoded_value = decoder_t::decode_signal(signal,