aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClément Bénier <clement.benier@iot.bzh>2019-11-15 11:20:34 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2020-01-09 14:40:41 +0100
commit6cc152447e74b1ce7fab6c7aa1f9b4e5e8232444 (patch)
tree2e4f9dd31136abd18e5c1c52c8bf2a0ec8921331
parent03a4775eefeeba913f69d331e3930700a567c4c5 (diff)
sign: handle decode methods sign from can value
- 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 <clement.benier@iot.bzh> Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-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
-rw-r--r--plugins/agl-signals.cpp61
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<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);
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_t> message_set =
std::make_shared<message_set_t>(message_set_t{0,"AGL Virtual Car",
{ // beginning message_definition_ vector
- {std::make_shared<message_definition_t>(message_definition_t{"ls", 0x30,"", 0, 0 ,false, frequency_clock_t(5.00000f), true,
+ {std::make_shared<message_definition_t>(message_definition_t{"ls", 0x30,"", 0, 0, false, frequency_clock_t(5.00000f), true,
{ // beginning signals vector
{std::make_shared<signal_t> (signal_t{
"hvac.fan.speed",// generic_name
@@ -33,7 +33,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -55,7 +56,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -77,7 +79,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -99,7 +102,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})}
} // end signals vector
@@ -125,7 +129,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -147,7 +152,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -169,12 +175,13 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})}
} // end signals vector
})} // end message_definition entry
-, {std::make_shared<message_definition_t>(message_definition_t{"hs", 0x3E9,"", 0, 0,false, frequency_clock_t(5.00000f), true,
+, {std::make_shared<message_definition_t>(message_definition_t{"hs", 0x3E9,"", 0, 0, false, frequency_clock_t(5.00000f), true,
{ // beginning signals vector
{std::make_shared<signal_t> (signal_t{
"vehicle.average.speed",// generic_name
@@ -195,7 +202,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})}
} // end signals vector
@@ -221,7 +229,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -243,7 +252,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})}
} // end signals vector
@@ -269,7 +279,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -291,7 +302,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -313,7 +325,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -335,7 +348,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -357,7 +371,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})}
} // end signals vector
@@ -383,7 +398,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -405,7 +421,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -427,7 +444,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})},
{std::make_shared<signal_t> (signal_t{
@@ -449,7 +467,8 @@ std::shared_ptr<message_set_t> message_set =
false,// received
std::make_pair<bool, int>(false, 0),// multiplex
0,// is_big_endian
- 0,// is_signed
+ static_cast<sign_t>(0),// signed
+ -1,// bit_sign_position
""// unit
})}
} // end signals vector