X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Fcan%2Fcan-encoder.cpp;h=596cc7e20b6bd9f06a88da6bb751aefd76a93416;hb=24bb25c9f16ecace05b64142493dce596f5a9e37;hp=a11dedfc0d5eeaab9d80b0fafc233bf565e0bbdd;hpb=2f60d294b3fa4e243fa67a738f9b82a0b428a7fc;p=apps%2Fagl-service-can-low-level.git diff --git a/low-can-binding/can/can-encoder.cpp b/low-can-binding/can/can-encoder.cpp index a11dedfc..596cc7e2 100644 --- a/low-can-binding/can/can-encoder.cpp +++ b/low-can-binding/can/can-encoder.cpp @@ -20,39 +20,140 @@ #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, uint64_t value) +/** + * @brief Allows to encode data for a signal + * + * @param sig The signal to know its location + * @param data The data to encode + * @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 sig, std::vector &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(); + float factor_v = factor ? sig->get_factor() : 1; + float offset_v = offset ? sig->get_offset() : 0; + + int new_start_byte = 0; + int new_end_byte = 0; + uint8_t new_start_bit = 0; + uint8_t 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); + std::vector data_signal(new_end_byte - new_start_byte + 1); + + if(filter) + { + for (auto& elt: data_signal) + elt = 0xFF; + uint8_t mask_first_v = static_cast(0xFF << new_start_bit); + uint8_t mask_last_v = static_cast(0xFF >> (7 - new_end_bit)); + + if(new_start_byte == new_end_byte) + { + data_signal[0] = mask_first_v & mask_last_v; + } + else + { + data_signal[0] = mask_first_v; + data_signal[new_end_byte - new_start_byte] = mask_last_v; + } + } + else + { + bitfield_encode_float(sig->get_last_value(), + new_start_bit, + bit_size, + factor_v, + offset_v, + data_signal.data(), + bit_size); + } - cf.can_id = signal->get_message()->get_id(); - cf.len = signal->get_message()->is_fd() ? - CANFD_MAX_DLEN : CAN_MAX_DLEN; + for(size_t i = new_start_byte; i <= new_end_byte ; i++) + data[i] = data[i] | data_signal[i-new_start_byte]; +} - signal->set_last_value((float)value); +/** + * @brief Allows to build a multi 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 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_frame(const std::shared_ptr& signal, uint64_t value, message_t *message, bool factor, bool offset) +{ + signal->set_last_value(static_cast(value)); + std::vector data(message->get_length(), 0); for(const auto& sig: signal->get_message()->get_signals()) + encode_data(sig, data, false, factor, offset); + + message->set_data(data); + return message; +} + +/** + * @brief Allows to build a message_t 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 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, uint64_t value, bool factor, bool offset) +{ + message_t *message; + std::vector data; + switch(signal->get_message()->get_flags()) { - 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); + case CAN_PROTOCOL_WITH_FD_FRAME: + message = new can_message_t(CANFD_MAX_DLEN, + signal->get_message()->get_id(), + CANFD_MAX_DLEN, + false, + signal->get_message()->get_flags(), + data, + 0); + return build_frame(signal, value, message, factor, offset); +#ifdef USE_FEATURE_J1939 + case J1939_PROTOCOL: + message = new j1939_message_t(signal->get_message()->get_length(), + data, + 0, + J1939_NO_NAME, + signal->get_message()->get_id(), + J1939_NO_ADDR); + return build_frame(signal, value, message, factor, offset); +#endif + case CAN_PROTOCOL: + message = new can_message_t(CAN_MAX_DLEN, + signal->get_message()->get_id(), + CAN_MAX_DLEN, + false, + signal->get_message()->get_flags(), + data, + 0); + return build_frame(signal, value, message, factor, offset); + default: + message = new can_message_t(CAN_MAX_DLEN, + signal->get_message()->get_id(), + CAN_MAX_DLEN, + false, + signal->get_message()->get_flags(), + data, + 0); + return build_frame(signal, value, message, factor, offset); } - return cf; } /// @brief Encode a boolean into an integer, fit for a CAN signal bitfield.