summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--low-can-binding/binding/low-can-cb.cpp2
-rw-r--r--low-can-binding/can/can-decoder.cpp54
-rw-r--r--low-can-binding/can/can-encoder.cpp191
-rw-r--r--low-can-binding/can/can-encoder.hpp9
4 files changed, 165 insertions, 91 deletions
diff --git a/low-can-binding/binding/low-can-cb.cpp b/low-can-binding/binding/low-can-cb.cpp
index ad9543d9..737a851a 100644
--- a/low-can-binding/binding/low-can-cb.cpp
+++ b/low-can-binding/binding/low-can-cb.cpp
@@ -669,7 +669,7 @@ static void write_signal(afb_req_t request, const std::string& name, json_object
}
// cfd = encoder_t::build_frame(sig, value);
- message_t *message = encoder_t::build_message(sig,value);
+ message_t *message = encoder_t::build_message(sig,value,false,false);
if(! send_message(message, sig->get_message()->get_bus_device_name(), type) && send)
{
diff --git a/low-can-binding/can/can-decoder.cpp b/low-can-binding/can/can-decoder.cpp
index 46976b5c..3c811be6 100644
--- a/low-can-binding/can/can-decoder.cpp
+++ b/low-can-binding/can/can-decoder.cpp
@@ -21,6 +21,7 @@
#include "../utils/openxc-utils.hpp"
#include "message-definition.hpp"
#include "../binding/low-can-hat.hpp"
+#include "../utils/converter.hpp"
/// @brief Parses the signal's bitfield from the given data and returns the raw
/// value.
@@ -33,8 +34,57 @@
///
float decoder_t::parse_signal_bitfield(signal_t& signal, std::shared_ptr<message_t> message)
{
- return bitfield_parse_float(message->get_data(), CAN_MESSAGE_SIZE,
- signal.get_bit_position(), signal.get_bit_size(), signal.get_factor(),
+ const std::vector<uint8_t> data = message->get_data_vector();
+ std::vector<uint8_t> data_signal;
+ uint32_t bit_size = signal.get_bit_size();
+ uint32_t bit_position = signal.get_bit_position();
+
+ int new_start_byte = 0;
+ int new_end_byte = 0;
+ int new_start_bit = 0;
+ int new_end_bit = 0;
+
+ converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
+
+ for(int i=new_start_byte;i<=new_end_byte;i++)
+ {
+ data_signal.push_back(data[i]);
+ }
+
+ uint8_t new_bit_size = 0;
+
+ if(bit_size > 255)
+ {
+ AFB_ERROR("Error signal %s to long bit size",signal.get_name().c_str());
+ }
+ else
+ {
+ new_bit_size = (uint8_t) bit_size;
+ }
+
+ uint8_t bit_offset = 0;
+ if(new_start_bit > 255)
+ {
+ AFB_ERROR("Too long signal offset %d", new_start_bit);
+ }
+ else
+ {
+ bit_offset = (uint8_t) new_start_bit;
+ }
+
+ uint16_t length = 0;
+
+ if(data_signal.size() > 65535)
+ {
+ AFB_ERROR("Too long data signal %s",signal.get_name().c_str());
+ }
+ else
+ {
+ length = (uint16_t) data_signal.size();
+ }
+
+ return bitfield_parse_float(data_signal.data(), length,
+ bit_offset, new_bit_size, signal.get_factor(),
signal.get_offset());
}
diff --git a/low-can-binding/can/can-encoder.cpp b/low-can-binding/can/can-encoder.cpp
index ccd49722..44b5d9b2 100644
--- a/low-can-binding/can/can-encoder.cpp
+++ b/low-can-binding/can/can-encoder.cpp
@@ -20,75 +20,116 @@
#include "canutil/write.h"
#include "../utils/openxc-utils.hpp"
#include "message-definition.hpp"
+#include "../utils/converter.hpp"
-/// @brief Write a value in a CAN signal in the destination buffer.
-///
-/// @param[in] signal - The CAN signal to write, including the bit position and bit size.
-/// @param[in] value - The encoded integer value to write in the CAN signal.
-/// @param[out] data - The destination buffer.
-/// @param[in] length - The length of the destination buffer.
-///
-/// @return Returns a canfd_frame struct initialized and ready to be send.
-const canfd_frame encoder_t::build_frame(const std::shared_ptr<signal_t>& signal, uint64_t value)
+/**
+ * @brief Allows to encode data for a signal
+ *
+ * @param sig The signal to know its location
+ * @param data The data to encod
+ * @param filter If true that will generate the filter BCM for the signal
+ * @param factor If true that will use the factor of the signal else 1
+ * @param offset If true that will use the offset of the signal else 0
+ */
+void encoder_t::encode_data(std::shared_ptr<signal_t> sig, std::vector<uint8_t> &data, bool filter, bool factor, bool offset)
{
- struct canfd_frame cf;
- ::memset(&cf, 0, sizeof(cf));
+ uint32_t bit_size = sig->get_bit_size();
+ uint32_t bit_position = sig->get_bit_position();
+ int new_start_byte = 0;
+ int new_end_byte = 0;
+ int new_start_bit_tmp = 0;
+ int new_end_bit = 0;
- cf.can_id = signal->get_message()->get_id();
- cf.len = signal->get_message()->is_fd() ?
- CANFD_MAX_DLEN : CAN_MAX_DLEN;
+ converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit_tmp, new_end_bit);
- signal->set_last_value((float)value);
+ int len_signal_bytes_tmp = new_end_byte - new_start_byte + 1;
- for(const auto& sig: signal->get_message()->get_signals())
+ uint8_t len_signal_bytes = 0;
+ if(len_signal_bytes_tmp > 255)
{
- float last_value = sig->get_last_value();
- bitfield_encode_float(last_value,
- sig->get_bit_position(),
- sig->get_bit_size(),
- sig->get_factor(),
- sig->get_offset(),
- cf.data,
- cf.len);
+ AFB_ERROR("Error signal %s too long",sig->get_name().c_str());
+ }
+ else
+ {
+ len_signal_bytes = (uint8_t) len_signal_bytes_tmp;
}
- return cf;
-}
+ uint8_t new_start_bit = 0;
+ if(new_start_bit_tmp > 255)
+ {
+ AFB_ERROR("Error signal %s too long",sig->get_name().c_str());
+ }
+ else
+ {
+ new_start_bit = (uint8_t) new_start_bit_tmp;
+ }
-/**
- * @brief Allows to build a single frame message with correct data to be send
- *
- * @param signal The CAN signal to write, including the bit position and bit size.
- * @param value The encoded integer value to write in the CAN signal.
- * @param message A single frame message to complete
- * @return message_t* The message that is generated
- */
-message_t* encoder_t::build_one_frame_message(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message)
-{
- signal->set_last_value((float)value);
- uint8_t data_tab[message->get_length()];
- ::memset(&data_tab, 0, sizeof(data_tab));
- std::vector<uint8_t> data;
+ uint8_t new_bit_size = 0;
+ if(bit_size > 255)
+ {
+ AFB_ERROR("Error signal %s to long bit size",sig->get_name().c_str());
+ }
+ else
+ {
+ new_bit_size = (uint8_t) bit_size;
+ }
- for(const auto& sig: signal->get_message()->get_signals())
+ uint8_t data_signal[len_signal_bytes] = {0};
+ float factor_v = 1;
+ if(factor)
{
- float last_value = sig->get_last_value();
- bitfield_encode_float(last_value,
- sig->get_bit_position(),
- sig->get_bit_size(),
- sig->get_factor(),
- sig->get_offset(),
- data_tab,
- (uint8_t)message->get_length());
+ factor_v = sig->get_factor();
}
- for (size_t i = 0; i < (uint8_t) message->get_length(); i++)
+ float offset_v = 0;
+ if(factor)
{
- data.push_back(data_tab[i]);
+ offset_v = sig->get_offset();
}
- message->set_data(data);
- return message;
+ if(filter)
+ {
+ uint8_t tmp = 0;
+ int j=0;
+ for(int i=0;i<new_bit_size;i++)
+ {
+ int mask = 1 << ((i%8)+new_start_bit);
+
+ uint8_t mask_v = 0;
+ if(mask > 255)
+ {
+ AFB_ERROR("Error mask too large");
+ }
+ else
+ {
+ mask_v = (uint8_t) mask;
+ }
+ tmp = tmp|mask_v;
+
+ if(i%8 == 7)
+ {
+ data_signal[j] = tmp;
+ tmp = 0;
+ j++;
+ }
+ }
+ data_signal[j]=tmp;
+ }
+ else
+ {
+ bitfield_encode_float( sig->get_last_value(),
+ new_start_bit,
+ new_bit_size,
+ factor_v,
+ offset_v,
+ data_signal,
+ len_signal_bytes);
+ }
+
+ for(size_t i = new_start_byte; i <= new_end_byte ; i++)
+ {
+ data[i] = data[i] | data_signal[i-new_start_byte];
+ }
}
/**
@@ -97,42 +138,23 @@ message_t* encoder_t::build_one_frame_message(const std::shared_ptr<signal_t>& s
* @param signal The CAN signal to write, including the bit position and bit size.
* @param value The encoded integer value to write in the CAN signal.
* @param message A multi frame message to complete
+ * @param factor If true that will use the factor of the signal else 1
+ * @param offset If true that will use the offset of the signal else 0
* @return message_t* The message that is generated
*/
-message_t* encoder_t::build_multi_frame_message(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message)
+message_t* encoder_t::build_frame(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message, bool factor, bool offset)
{
signal->set_last_value((float)value);
std::vector<uint8_t> data;
-
- uint32_t msgs_len = signal->get_message()->get_length(); // multi frame - number of bytes
- int number_of_frame = (int) msgs_len / 8;
-
- uint8_t data_tab[number_of_frame][8] = {0};
-
- for(const auto& sig: signal->get_message()->get_signals())
+ for(int i = 0; i<message->get_length();i++)
{
-
- int frame_position = (int) sig->get_bit_position() / 64;
- float last_value = sig->get_last_value();
- uint8_t bit_position = sig->get_bit_position() - ((uint8_t)(64 * frame_position));
-
- bitfield_encode_float(last_value,
- bit_position,
- sig->get_bit_size(),
- sig->get_factor(),
- sig->get_offset(),
- data_tab[frame_position],
- 8);
+ data.push_back(0);
}
- for (size_t i = 0; i < number_of_frame; i++)
+ for(const auto& sig: signal->get_message()->get_signals())
{
- for(size_t j = 0; j < 8 ; j++)
- {
- data.push_back(data_tab[i][j]);
- }
+ encode_data(sig,data,false,factor,offset);
}
-
message->set_data(data);
return message;
}
@@ -142,28 +164,31 @@ message_t* encoder_t::build_multi_frame_message(const std::shared_ptr<signal_t>&
*
* @param signal The CAN signal to write, including the bit position and bit size.
* @param value The encoded integer value to write in the CAN signal.
+ * @param factor If true that will use the factor of the signal else 1
+ * @param offset If true that will use the offset of the signal else 0
* @return message_t* The message that is generated
*/
-message_t* encoder_t::build_message(const std::shared_ptr<signal_t>& signal, uint64_t value)
+message_t* encoder_t::build_message(const std::shared_ptr<signal_t>& signal, uint64_t value, bool factor, bool offset)
{
message_t *message;
std::vector<uint8_t> data;
if(signal->get_message()->is_fd())
{
message = new can_message_t(CANFD_MAX_DLEN,signal->get_message()->get_id(),CANFD_MAX_DLEN,signal->get_message()->get_format(),false,CAN_FD_FRAME,data,0);
- return build_one_frame_message(signal,value,message);
+
+ return build_frame(signal,value,message, factor, offset);
}
#ifdef USE_FEATURE_J1939
else if(signal->get_message()->is_j1939())
{
message = new j1939_message_t(J1939_MAX_DLEN,signal->get_message()->get_length(),signal->get_message()->get_format(),data,0,J1939_NO_NAME,signal->get_message()->get_id(),J1939_NO_ADDR);
- return build_multi_frame_message(signal,value,message);
+ return build_frame(signal,value,message, factor, offset);
}
#endif
else
{
message = new can_message_t(CAN_MAX_DLEN,signal->get_message()->get_id(),CAN_MAX_DLEN,signal->get_message()->get_format(),false,0,data,0);
- return build_one_frame_message(signal,value,message);
+ return build_frame(signal,value,message, factor, offset);
}
}
diff --git a/low-can-binding/can/can-encoder.hpp b/low-can-binding/can/can-encoder.hpp
index c37521f4..d4505f04 100644
--- a/low-can-binding/can/can-encoder.hpp
+++ b/low-can-binding/can/can-encoder.hpp
@@ -28,13 +28,12 @@
class encoder_t
{
public:
- static const canfd_frame build_frame(const std::shared_ptr<signal_t>& signal, uint64_t value);
- static message_t* build_message(const std::shared_ptr<signal_t>& signal, uint64_t value);
- static message_t* build_one_frame_message(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message);
- static message_t* build_multi_frame_message(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message);
+ static message_t* build_message(const std::shared_ptr<signal_t>& signal, uint64_t value, bool factor=true, bool offset=true);
+ static message_t* build_frame(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message, bool factor=true, bool offset=true);
static uint64_t encode_state(const signal_t& signal, const std::string& value, bool* send);
static uint64_t encode_boolean(const signal_t& signal, bool value, bool* send);
static uint64_t encode_number(const signal_t& signal, float value, bool* send);
+ static void encode_data(std::shared_ptr<signal_t> sig, std::vector<uint8_t> &data, bool filter=false, bool factor=true, bool offset=true);
static uint64_t encode_DynamicField(signal_t& signal, const openxc_DynamicField& field, bool* send);
-};
+}; \ No newline at end of file