Update flag FRAME_LAYOUT_IS_BIGENDIAN to BYTE_FRAME_IS_BIG_ENDIAN
[apps/agl-service-can-low-level.git] / low-can-binding / can / can-decoder.cpp
index 7d6ae2a..160a22b 100644 (file)
@@ -16,6 +16,7 @@
  */
 
 #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;
+
+       if(signal.get_message()->get_flags() & CONTINENTAL_BIT_POSITION)
+               bit_position = converter_t::continental_bit_position_mess(message->get_length(),
+                                                             signal.get_bit_position(),
+                                                             bit_size);
+       if(signal.get_message()->get_flags() & BIT_POSITION_REVERSED)
+               bit_position = converter_t::bit_position_swap(message->get_length(),
+                                                             signal.get_bit_position(),
+                                                             bit_size);
+       if(signal.get_message()->get_flags() & BYTE_FRAME_IS_BIG_ENDIAN)
+               message->frame_swap();
 
+       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;
+       sign = handle_sign(signal, data_signal, new_end_bit, data);
 
-       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;
-       }
+       if(data_signal.size() > 65535)
+               AFB_ERROR("Too long data signal %s", signal.get_name().c_str());
 
-       uint8_t bit_offset = 0;
-       if(new_start_bit > 255)
-       {
-               AFB_ERROR("Too long signal offset %d", new_start_bit);
-       }
-       else
+       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());
+}
+
+
+/// @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_bytes(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
+{
+       int i=0;
+       openxc_DynamicField decoded_value;
+       std::vector<uint8_t> data = message->get_data_vector();
+       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 / CHAR_BIT) + 1);
+
+       int new_start_byte = 0;
+       int new_end_byte = 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)
        {
-               bit_offset = (uint8_t) new_start_bit;
+               AFB_ERROR("Error in signal's description");
+               return decoded_value;
        }
 
-       uint16_t length = 0;
+       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));
 
-       if(data_signal.size() > 65535)
+       if(new_start_byte == new_end_byte)
        {
-               AFB_ERROR("Too long data signal %s",signal.get_name().c_str());
+               data[new_start_byte] = data[new_start_byte] & (mask_first_v & mask_last_v);
        }
        else
        {
-               length = (uint16_t) data_signal.size();
+               data[new_start_byte] = data[new_start_byte] & mask_first_v;
+               data[new_end_byte] = data[new_end_byte] & mask_last_v;
        }
 
-       return bitfield_parse_float(data_signal.data(), length,
-                       bit_offset, new_bit_size, signal.get_factor(),
-                       signal.get_offset());
+       for(i=new_start_byte ; i <= new_end_byte ; i++)
+               new_data.push_back(data[i]);
+
+       decoded_value = build_DynamicField(new_data);
+
+       return decoded_value;
+}
+
+
+/// @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;
+}
+
+//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);
+
+       // 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
@@ -110,9 +269,7 @@ openxc_DynamicField decoder_t::decode_noop(signal_t& signal, std::shared_ptr<mes
 
        // 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;
@@ -139,9 +296,8 @@ openxc_DynamicField decoder_t::decode_boolean(signal_t& signal, std::shared_ptr<
 
        // 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);
 
 
@@ -202,9 +358,7 @@ openxc_DynamicField decoder_t::decode_state(signal_t& signal, std::shared_ptr<me
 
        // 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);
 
 
@@ -228,7 +382,6 @@ openxc_DynamicField decoder_t::decode_state(signal_t& signal, std::shared_ptr<me
 ///
 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,