X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Fcan%2Fmessage%2Fcan-message.cpp;h=deca5c724305374d30a65f7ba897bc4ab73f4804;hb=aeeb786722b5cdeabff5568b40f7075dc23140b9;hp=b012bbe5d443e05d61ce7c92e8d4178a858e8b46;hpb=2f60d294b3fa4e243fa67a738f9b82a0b428a7fc;p=apps%2Fagl-service-can-low-level.git diff --git a/low-can-binding/can/message/can-message.cpp b/low-can-binding/can/message/can-message.cpp index b012bbe5..deca5c72 100644 --- a/low-can-binding/can/message/can-message.cpp +++ b/low-can-binding/can/message/can-message.cpp @@ -28,22 +28,20 @@ /// can_message_t::can_message_t() : message_t(), - maxdlen_{0}, id_{0}, rtr_flag_{false}, flags_{0} {} -can_message_t::can_message_t(uint8_t maxdlen, +can_message_t::can_message_t(uint32_t maxdlen, uint32_t id, - uint8_t length, + uint32_t length, message_format_t format, bool rtr_flag, - uint8_t flags, + uint32_t flags, std::vector& data, uint64_t timestamp) - : message_t(length, format, data, timestamp), - maxdlen_{maxdlen}, + : message_t(maxdlen, length, format, data, timestamp), id_{id}, rtr_flag_{rtr_flag}, flags_{flags} @@ -69,7 +67,7 @@ bool can_message_t::is_correct_to_send() if (id_ != 0 && length_ != 0 && format_ != message_format_t::INVALID) { int i; - for(i=0;i can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp) { - uint8_t maxdlen = 0, length = 0, flags = 0; + uint32_t maxdlen = 0, length = 0; + uint8_t flags = 0; uint32_t id; message_format_t format; bool rtr_flag; @@ -164,6 +163,109 @@ std::shared_ptr can_message_t::convert_from_frame(const struct ca return std::make_shared(can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp)); } +/// @brief Take all initialized class members and build a +/// canfd_frame struct that can be use to send a CAN message over +/// the bus. +/// +/// @return canfd_frame struct built from class members. +struct canfd_frame can_message_t::convert_to_canfd_frame() +{ + canfd_frame frame; + + if(is_correct_to_send()) + { + frame.can_id = get_id(); + frame.len = (uint8_t) get_length(); + ::memcpy(frame.data, get_data(), length_); + } + else + AFB_ERROR("can_message_t not correctly initialized to be sent"); + + return frame; +} + +/// @brief Take all initialized class members and build a +/// canfd_frame struct that can be use to send a CAN message over +/// the bus. +/// +/// @return canfd_frame struct built from class members. +struct std::vector can_message_t::convert_to_canfd_frame_vector() +{ + std::vector ret; + if(is_correct_to_send()) + { + if(flags_ & CAN_FD_FRAME) + { + int i=0; + do + { + canfd_frame frame; + frame.can_id = id_; + frame.len = 64; + std::vector data = get_data_vector((i*64),(i*64)+63); + if(data.size()<64) + { + ::memset(frame.data,0,sizeof(frame.data)); + ::memcpy(frame.data,data.data(),data.size()); + } + else + { + ::memcpy(frame.data,data.data(),64); + } + ret.push_back(frame); + i++; + } while (i<(length_ >> 6)); + } + else + { + int i=0; + do + { + canfd_frame frame; + frame.can_id = id_; + frame.len = 8; + std::vector data = get_data_vector(i*8,(i*8)+7); + if(data.size()<8) + { + ::memset(frame.data,0,sizeof(frame.data)); + ::memcpy(frame.data,data.data(),data.size()); + } + else + { + ::memset(frame.data,0,sizeof(frame.data)); + ::memcpy(frame.data,data.data(),8); + } + ret.push_back(frame); + i++; + } while (i<(length_ >> 3)); + } + } + else + AFB_ERROR("can_message_t not correctly initialized to be sent"); + + return ret; +} + +/// @brief Take all initialized class members and build a +/// can_frame struct that can be use to send a CAN message over +/// the bus. +/// +/// @return can_frame struct built from class members. +struct can_frame can_message_t::convert_to_can_frame() +{ + can_frame frame; + + if(is_correct_to_send()) + { + frame.can_id = get_id(); + frame.can_dlc = (uint8_t) get_length(); + ::memcpy(frame.data, get_data(), length_); + } + else + AFB_ERROR("can_message_t not correctly initialized to be sent"); + + return frame; +} bool can_message_t::is_set() { @@ -173,13 +275,13 @@ bool can_message_t::is_set() std::string can_message_t::get_debug_message() { std::string ret = ""; - ret = ret + "Here is the next can message : id " + std::to_string(id_) + " length " + std::to_string(length_) + ", data "; - for (size_t i = 0; i < data_.size(); i++) - { - ret = ret + std::to_string(data_[i]); - } + ret = ret + "Here is the next can message : id " + std::to_string(id_) + " length " + std::to_string(length_) + ", data "; + for (size_t i = 0; i < data_.size(); i++) + { + ret = ret + std::to_string(data_[i]); + } - return ret; + return ret; } struct bcm_msg can_message_t::get_bcm_msg() @@ -192,3 +294,7 @@ void can_message_t::set_bcm_msg(struct bcm_msg bcm_msg) bcm_msg_ = bcm_msg; } +uint32_t can_message_t::get_flags() +{ + return flags_; +} \ No newline at end of file