decoder: Fix bit_position swapping 12/23212/6
authorCorentin Le Gall <corentin.legall@iot.bzh>
Mon, 18 Nov 2019 15:48:20 +0000 (16:48 +0100)
committerRomain Forlot <romain.forlot@iot.bzh>
Thu, 9 Jan 2020 13:40:41 +0000 (14:40 +0100)
-Added an attribute to signals that tells if the bit_position
has been swapped.
-Test if bit_size and bit_position gives an "out of range" data

Bug-AGL: SPEC-3022

Change-Id: I589565ca923ec807da2d4f0db7c4c92fb737b579
Signed-off-by: Corentin Le Gall <corentin.legall@iot.bzh>
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
low-can-binding/can/can-decoder.cpp
low-can-binding/can/signals.cpp
low-can-binding/can/signals.hpp
low-can-binding/utils/converter.cpp

index 19594af..76ea79e 100644 (file)
@@ -350,9 +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())
+       if(!signal.get_message()->frame_layout_is_little() && !signal.bit_position_is_swapped())
        {
                signal.set_bit_position(converter_t::bit_position_swap(signal.get_bit_position(),signal.get_bit_size()));
+               signal.bit_position_is_swapped_reverse();
        }
        // Must call the decoders every time, regardless of if we are going to
        // decide to send the signal or not.
index da8df98..28246a0 100755 (executable)
@@ -71,6 +71,7 @@ signal_t::signal_t(
        , sign_{sign}
        , bit_sign_position_{bit_sign_position}
        , unit_{unit}
+       ,bit_position_is_swapped_{false}
 {}
 
 signal_t::signal_t(
@@ -104,6 +105,7 @@ signal_t::signal_t(
        , decoder_{decoder}
        , encoder_{encoder}
        , received_{received}
+       , bit_position_is_swapped_{false}
 {}
 
 std::shared_ptr<message_definition_t> signal_t::get_message() const
@@ -251,3 +253,13 @@ const std::string signal_t::get_unit() const
 {
        return unit_;
 }
+
+bool signal_t::bit_position_is_swapped() const
+{
+       return bit_position_is_swapped_;
+}
+
+void signal_t::bit_position_is_swapped_reverse()
+{
+       bit_position_is_swapped_ = !bit_position_is_swapped_;
+}
index bf77998..5f14375 100755 (executable)
@@ -111,6 +111,8 @@ private:
        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 */
+       bool bit_position_is_swapped_; /* !<bit_position_is_swapped_- True if the signal's bit position has been
+                                                                               swapped (refer to converter_t::bit_position_swap()). Default is false.*/
 
 public:
 
@@ -177,10 +179,12 @@ public:
        sign_t get_sign() const;
        int32_t get_bit_sign_position() const;
        const std::string get_unit() const;
+       bool bit_position_is_swapped() const;
 
        void set_parent(std::shared_ptr<message_definition_t> parent);
        void set_received(bool r);
        void set_last_value(float val);
        void set_timestamp(uint64_t timestamp);
        void set_bit_position(uint32_t bit_position);
+       void bit_position_is_swapped_reverse();
 };
index f790fc0..9e70bc9 100644 (file)
@@ -69,6 +69,13 @@ uint32_t converter_t::bit_position_swap(uint32_t bit_position,uint32_t bit_size)
 {
        uint32_t start_byte_position = (uint32_t)(bit_position/8);
        uint32_t bit_size_rest = bit_size;
+
+       if((int)(bit_size-(8 + start_byte_position*8-bit_position%8))>0)
+       {
+               AFB_ERROR("Error: bit_position and bit_size getting out of range");
+               return bit_position;
+       }
+
        if(bit_size<=8 && ((bit_position+bit_size)%8==bit_size || (bit_position+bit_size)%8==0))
        {
                return (uint32_t)(start_byte_position*8 + (8-bit_size));