aboutsummaryrefslogtreecommitdiffstats
path: root/low-can-binding/can
diff options
context:
space:
mode:
Diffstat (limited to 'low-can-binding/can')
-rw-r--r--low-can-binding/can/can-decoder.cpp73
-rw-r--r--low-can-binding/can/can-decoder.hpp1
-rwxr-xr-xlow-can-binding/can/signals.cpp15
-rwxr-xr-xlow-can-binding/can/signals.hpp17
4 files changed, 92 insertions, 14 deletions
diff --git a/low-can-binding/can/can-decoder.cpp b/low-can-binding/can/can-decoder.cpp
index e901240b..19594afa 100644
--- a/low-can-binding/can/can-decoder.cpp
+++ b/low-can-binding/can/can-decoder.cpp
@@ -23,6 +23,66 @@
#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()%8;
+ mask = static_cast<uint8_t>((1 << (end_bit + 1)) - 1);
+ data_byte = can_data[signal.get_bit_sign_position()/8] & 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.
///
@@ -49,16 +109,12 @@ float decoder_t::parse_signal_bitfield(signal_t& signal, std::shared_ptr<message
for(int i=new_start_byte;i<=new_end_byte;i++)
data_signal.push_back(data[i]);
-// if(bit_size > 255)
-// AFB_ERROR("Error signal %s to long bit size", signal.get_name().c_str());
-
-// if(new_start_bit > 255)
-// AFB_ERROR("Too long signal offset %d", new_start_bit);
+ int sign = decoder_t::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());
- return bitfield_parse_float(data_signal.data(), (uint16_t) data_signal.size(),
+ 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());
}
@@ -294,7 +350,10 @@ 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)
{
-
+ if(!signal.get_message()->frame_layout_is_little())
+ {
+ signal.set_bit_position(converter_t::bit_position_swap(signal.get_bit_position(),signal.get_bit_size()));
+ }
// 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,
diff --git a/low-can-binding/can/can-decoder.hpp b/low-can-binding/can/can-decoder.hpp
index a5e3acbe..02a2c7e1 100644
--- a/low-can-binding/can/can-decoder.hpp
+++ b/low-can-binding/can/can-decoder.hpp
@@ -24,6 +24,7 @@
class decoder_t
{
public:
+ static int handle_sign(const signal_t& signal, std::vector<uint8_t>& data_signal, uint8_t new_end_bit, const std::vector<uint8_t>& can_data);
static float parse_signal_bitfield(signal_t& signal, std::shared_ptr<message_t> message);
static openxc_DynamicField decode_state(signal_t& signal, std::shared_ptr<message_t> message, bool* send);
diff --git a/low-can-binding/can/signals.cpp b/low-can-binding/can/signals.cpp
index 2b87dc6f..da8df988 100755
--- a/low-can-binding/can/signals.cpp
+++ b/low-can-binding/can/signals.cpp
@@ -46,7 +46,8 @@ signal_t::signal_t(
bool received,
std::pair<bool, int> multiplex,
bool is_big_endian,
- bool is_signed,
+ sign_t sign,
+ int32_t bit_sign_position,
std::string unit)
: parent_{nullptr},
generic_name_{ generic_name }
@@ -67,7 +68,8 @@ signal_t::signal_t(
, last_value_{.0f}
, multiplex_{multiplex}
, is_big_endian_{is_big_endian}
- , is_signed_{is_signed}
+ , sign_{sign}
+ , bit_sign_position_{bit_sign_position}
, unit_{unit}
{}
@@ -235,9 +237,14 @@ bool signal_t::get_is_big_endian() const
return is_big_endian_;
}
-bool signal_t::get_is_signed() const
+sign_t signal_t::get_sign() const
{
- return is_signed_;
+ return sign_;
+}
+
+int32_t signal_t::get_bit_sign_position() const
+{
+ return bit_sign_position_;
}
const std::string signal_t::get_unit() const
diff --git a/low-can-binding/can/signals.hpp b/low-can-binding/can/signals.hpp
index d9453c50..bf779981 100755
--- a/low-can-binding/can/signals.hpp
+++ b/low-can-binding/can/signals.hpp
@@ -60,6 +60,14 @@ typedef openxc_DynamicField (*signal_decoder)(signal_t& signal, std::shared_ptr<
typedef uint64_t (*signal_encoder)(signal_t& signal,
const openxc_DynamicField& field, bool* send);
+enum sign_t
+{
+ UNSIGNED = 0,
+ SIGN_BIT = 1,
+ ONES_COMPLEMENT = 2,
+ TWOS_COMPLEMENT = 3,
+ SIGN_BIT_EXTERN = 4
+};
class signal_t
{
@@ -100,7 +108,8 @@ private:
If bool is true, that indicate that is a multiplexor
If int is different of 0, that indicate the link with a multiplexor */
bool is_big_endian_; /*!< is_big_endian - True if the signal's data are meant to be read as a big_endian */
- bool is_signed_; /* !< is_signed_ - True if the data is signed */
+ sign_t sign_; /* !< sign_ - if the data is signed it indicates the encode */
+ int32_t bit_sign_position_; /*!< bit_sign_position_ - The bit that indicates the sign of the signal in its CAN message*/
std::string unit_; /* !< unit_ - The unit of the data */
public:
@@ -123,7 +132,8 @@ public:
bool received,
std::pair<bool, int> multiplex,
bool is_big_endian,
- bool is_signed,
+ sign_t sign,
+ int32_t bit_sign_position,
std::string unit);
@@ -164,7 +174,8 @@ public:
std::pair<float, uint64_t> get_last_value_with_timestamp() const;
std::pair<bool, int> get_multiplex() const;
bool get_is_big_endian() const;
- bool get_is_signed() const;
+ sign_t get_sign() const;
+ int32_t get_bit_sign_position() const;
const std::string get_unit() const;
void set_parent(std::shared_ptr<message_definition_t> parent);