From 6cc152447e74b1ce7fab6c7aa1f9b4e5e8232444 Mon Sep 17 00:00:00 2001 From: Clément Bénier Date: Fri, 15 Nov 2019 11:20:34 +0100 Subject: sign: handle decode methods sign from can value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add enumeration that represents the way of encoding: enum sign_t { UNSIGNED = 0, SIGN_BIT = 1, ONES_COMPLEMENT = 2, TWOS_COMPLEMENT = 3, SIGN_BIT_EXTERN = 4 }; - the function handle_sign allows to transform value if negative and return the sign of the value in order to be multiplied at the end ot the parcing Bug-AGL: SPEC-3021 Change-Id: I5766a286488170d930422474b4c4f3a8578ca726 Signed-off-by: Clément Bénier Signed-off-by: Romain Forlot --- low-can-binding/can/can-decoder.cpp | 73 +++++++++++++++++++++++++++++++++---- low-can-binding/can/can-decoder.hpp | 1 + low-can-binding/can/signals.cpp | 15 ++++++-- low-can-binding/can/signals.hpp | 17 +++++++-- plugins/agl-signals.cpp | 61 ++++++++++++++++++++----------- 5 files changed, 132 insertions(+), 35 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& data_signal, uint8_t new_end_bit, const std::vector& 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((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((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(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(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 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(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 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& data_signal, uint8_t new_end_bit, const std::vector& can_data); static float parse_signal_bitfield(signal_t& signal, std::shared_ptr message); static openxc_DynamicField decode_state(signal_t& signal, std::shared_ptr 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 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 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 get_last_value_with_timestamp() const; std::pair 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 parent); diff --git a/plugins/agl-signals.cpp b/plugins/agl-signals.cpp index e02b61ff..5f00e290 100644 --- a/plugins/agl-signals.cpp +++ b/plugins/agl-signals.cpp @@ -12,7 +12,7 @@ CTLP_LUA_REGISTER("TO-EDIT"); std::shared_ptr message_set = std::make_shared(message_set_t{0,"AGL Virtual Car", { // beginning message_definition_ vector - {std::make_shared(message_definition_t{"ls", 0x30,"", 0, 0 ,false, frequency_clock_t(5.00000f), true, + {std::make_shared(message_definition_t{"ls", 0x30,"", 0, 0, false, frequency_clock_t(5.00000f), true, { // beginning signals vector {std::make_shared (signal_t{ "hvac.fan.speed",// generic_name @@ -33,7 +33,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -55,7 +56,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -77,7 +79,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -99,7 +102,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })} } // end signals vector @@ -125,7 +129,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -147,7 +152,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -169,12 +175,13 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })} } // end signals vector })} // end message_definition entry -, {std::make_shared(message_definition_t{"hs", 0x3E9,"", 0, 0,false, frequency_clock_t(5.00000f), true, +, {std::make_shared(message_definition_t{"hs", 0x3E9,"", 0, 0, false, frequency_clock_t(5.00000f), true, { // beginning signals vector {std::make_shared (signal_t{ "vehicle.average.speed",// generic_name @@ -195,7 +202,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })} } // end signals vector @@ -221,7 +229,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -243,7 +252,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })} } // end signals vector @@ -269,7 +279,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -291,7 +302,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -313,7 +325,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -335,7 +348,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -357,7 +371,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })} } // end signals vector @@ -383,7 +398,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -405,7 +421,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -427,7 +444,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })}, {std::make_shared (signal_t{ @@ -449,7 +467,8 @@ std::shared_ptr message_set = false,// received std::make_pair(false, 0),// multiplex 0,// is_big_endian - 0,// is_signed + static_cast(0),// signed + -1,// bit_sign_position ""// unit })} } // end signals vector -- cgit 1.2.3-korg