decoder: rework how to swap frame layout. 60/23260/6
authorRomain Forlot <romain.forlot@iot.bzh>
Wed, 4 Dec 2019 16:26:58 +0000 (17:26 +0100)
committerRomain Forlot <romain.forlot@iot.bzh>
Thu, 9 Jan 2020 15:25:36 +0000 (16:25 +0100)
This also change the bit_position to retrieve the bit word starting from the left
or the right of the frame depending on the endianness of the frame layout.

Change-Id: I28658e9d46bd35d8ecabeece317331832229384a
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
low-can-binding/can/can-decoder.cpp
low-can-binding/can/message/message.cpp
low-can-binding/can/message/message.hpp
low-can-binding/can/signals.cpp
low-can-binding/can/signals.hpp
low-can-binding/utils/converter.cpp
low-can-binding/utils/converter.hpp

index cf4306a..0e64ccc 100644 (file)
@@ -99,6 +99,14 @@ float decoder_t::parse_signal_bitfield(signal_t& signal, std::shared_ptr<message
        uint8_t bit_size = (uint8_t) signal.get_bit_size();
        uint32_t bit_position = signal.get_bit_position();
 
+       if(!signal.get_message()->frame_layout_is_little())
+       {
+               bit_position = converter_t::bit_position_swap(message->get_length(),
+                                                             signal.get_bit_position(),
+                                                             signal.get_bit_size());
+               message->frame_swap();
+       }
+
        int new_start_byte = 0;
        int new_end_byte = 0;
        uint8_t new_start_bit = 0;
@@ -320,11 +328,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)
 {
-       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.
        openxc_DynamicField decoded_value = decoder_t::decode_signal(signal,
index 22887c9..56e9379 100644 (file)
@@ -177,3 +177,14 @@ void message_t::set_length(uint32_t length)
 {
        length_ = length;
 }
+
+void message_t::frame_swap()
+{
+       int i;
+       uint8_t *temp = (uint8_t*)alloca(length_);
+
+       for(i = 0; i < length_; i++)
+               temp[i] = data_[length_ - i - 1];
+
+       memcpy(data_.data(), temp, length_);
+}
index 9a074e3..e5e6769 100644 (file)
@@ -44,7 +44,7 @@
 #define ISOTP_SEND 0x0020
 #define ISOTP_RECEIVE 0x0040
 #define CAN_PROTOCOL_WITH_FD_FRAME 0x0080
-#define FRAME_LAYOUT_IS_LE 0x0100
+#define FRAME_LAYOUT_IS_LE 0x0100 // LE stand for Little Endian
 
 /// @class message_t
 ///
@@ -84,4 +84,5 @@ public:
        uint32_t get_maxdlen();
        void set_maxdlen(uint32_t maxdlen);
        void set_length(uint32_t length);
+       void frame_swap();
 };
index 28246a0..da8df98 100755 (executable)
@@ -71,7 +71,6 @@ signal_t::signal_t(
        , sign_{sign}
        , bit_sign_position_{bit_sign_position}
        , unit_{unit}
-       ,bit_position_is_swapped_{false}
 {}
 
 signal_t::signal_t(
@@ -105,7 +104,6 @@ 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
@@ -253,13 +251,3 @@ 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 5f14375..bf77998 100755 (executable)
@@ -111,8 +111,6 @@ 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:
 
@@ -179,12 +177,10 @@ 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 0a09125..6392fbd 100644 (file)
@@ -65,20 +65,24 @@ void converter_t::signal_to_bits_bytes(unsigned int bit_position, unsigned int b
  * @param bit_size             Size of the data.
  * @return uint32_t    New bit position.
  */
-uint32_t converter_t::bit_position_swap(uint32_t bit_position,uint32_t bit_size)
+uint32_t converter_t::bit_position_swap(unsigned int msg_length, unsigned int bit_position, unsigned int bit_size)
 {
-       uint32_t start_byte_position = (uint32_t)(bit_position/8);
-       uint32_t bit_size_rest = bit_size;
+       return msg_length - bit_position - bit_size;
+       /*
+       unsigned int start_byte_position = (unsigned int)(bit_position/8);
+       unsigned int bit_size_rest = bit_size;
 
-       if((int)(bit_size-(8 + start_byte_position*8-bit_position%8))>0)
+       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))
+       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));
+               return (unsigned int)(start_byte_position*8 + (8-bit_size));
        }
        else
        {
@@ -88,7 +92,7 @@ uint32_t converter_t::bit_position_swap(uint32_t bit_position,uint32_t bit_size)
                        start_byte_position--;
                        bit_position = start_byte_position*8;
                } while (bit_size_rest>8);
-               return (uint32_t)(start_byte_position*8 + (8-bit_size_rest));
+               return (unsigned int)(start_byte_position*8 + (8-bit_size_rest));
        }
-
+       */
 }
index 32c898f..23f8ae7 100644 (file)
@@ -29,6 +29,7 @@ class converter_t
                                                 int &new_end_byte,
                                                 uint8_t &new_start_bit,
                                                 uint8_t &new_end_bit);
-               static uint32_t bit_position_swap(unsigned int bit_position,
+               static uint32_t bit_position_swap(unsigned int msg_length,
+                                                 unsigned int bit_position,
                                                  unsigned int bit_size);
 };