From 11e909e8a7f8bd5978f0ff231ae6d3dd1dfff2e7 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Thu, 9 Mar 2017 00:43:18 +0100 Subject: Fix: method to allocate a new can_message_t object from a canfd_frame. A new constructor added ad-hoc. Change-Id: Id6b909666105da0c4885351db9291aa430d138f0 Signed-off-by: Romain Forlot --- src/can/can-message.cpp | 105 ++++++++++++++++++++++++++---------------------- src/can/can-message.hpp | 42 +++++++++---------- 2 files changed, 78 insertions(+), 69 deletions(-) (limited to 'src/can') diff --git a/src/can/can-message.cpp b/src/can/can-message.cpp index 1be0d2d..cf57876 100644 --- a/src/can/can-message.cpp +++ b/src/can/can-message.cpp @@ -32,7 +32,11 @@ * Constructor about can_message_t class. */ can_message_t::can_message_t() - : id_{0}, rtr_flag_{false}, length_{0}, flags_{0}, format_{can_message_format_t::ERROR}, maxdlen_{0} + : maxdlen_{0}, id_{0}, length_{0}, format_{can_message_format_t::ERROR}, rtr_flag_{false}, flags_{0} +{} + +can_message_t::can_message_t(uint8_t maxdlen, uint32_t id, uint8_t length, can_message_format_t format, bool rtr_flag_, uint8_t flags, std::vector data) + : maxdlen_{0}, id_{0}, length_{0}, format_{can_message_format_t::ERROR}, rtr_flag_{false}, flags_{0}, data_{data} {} /** @@ -141,86 +145,92 @@ void can_message_t::set_format(const can_message_format_t new_format) */ can_message_t can_message_t::convert_to_canfd_frame(const struct canfd_frame& frame, size_t nbytes) { - switch(nbytes) + uint8_t maxdlen, length, flags = (uint8_t)NULL; + uint32_t id; + can_message_format_t format; + bool rtr_flag; + std::vector data; + + switch(nbytes) { case CANFD_MTU: DEBUG(binder_interface, "set_max_data_length: Got an CAN FD frame"); - maxdlen_ = CANFD_MAX_DLEN; + maxdlen = CANFD_MAX_DLEN; break; case CAN_MTU: DEBUG(binder_interface, "set_max_data_length: Got a legacy CAN frame"); - maxdlen_ = CAN_MAX_DLEN; + maxdlen = CAN_MAX_DLEN; break; default: ERROR(binder_interface, "set_max_data_length: unsupported CAN frame"); break; } - if(rtr_flag_) - length_ = frame.len& 0xF; + if(rtr_flag) + length = frame.len& 0xF; else - { - length_ = (frame.len > maxdlen_) ? maxdlen_ : frame.len; - } - + length = (frame.len > maxdlen) ? maxdlen : frame.len; + if (frame.can_id & CAN_ERR_FLAG) - format_ = can_message_format_t::ERROR; + format = can_message_format_t::ERROR; else if (frame.can_id & CAN_EFF_FLAG) - format_ = can_message_format_t::EXTENDED; + format = can_message_format_t::EXTENDED; else - format_ = can_message_format_t::STANDARD; - - switch(format_) + format = can_message_format_t::STANDARD; + + switch(format) { case can_message_format_t::STANDARD: - id_ = frame.can_id & CAN_SFF_MASK; + id = frame.can_id & CAN_SFF_MASK; break; case can_message_format_t::EXTENDED: - id_ = frame.can_id & CAN_EFF_MASK; + id = frame.can_id & CAN_EFF_MASK; break; case can_message_format_t::ERROR: - id_ = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG); + id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG); break; default: ERROR(binder_interface, "ERROR: Can set id, not a compatible format or format not set prior to set id."); break; } - - /* Overwrite lenght_ if RTR flags is detected. + + /* Overwrite length_ if RTR flags is detected. * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */ if (frame.can_id & CAN_RTR_FLAG) { - rtr_flag_ = true; + rtr_flag = true; if(frame.len && frame.len <= CAN_MAX_DLC) { - if(rtr_flag_) - length_ = frame.len& 0xF; - else - { - length_ = (frame.len > maxdlen_) ? maxdlen_ : frame.len; - } + if(rtr_flag) + length = frame.len& 0xF; + else + { + length = (frame.len > maxdlen) ? maxdlen : frame.len; + } } } else { - /* Flags field only present for CAN FD frames*/ - if(maxdlen_ == CANFD_MAX_DLEN) - flags_ = frame.flags & 0xF; - - if (data_.capacity() < maxdlen_) - data_.reserve(maxdlen_); - int i; + /* Flags field only present for CAN FD frames*/ + if(maxdlen == CANFD_MAX_DLEN) + flags = frame.flags & 0xF; - data_.clear(); - /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/ - for(i=0;i data_; /*!< data_ - The message's data field with a size of 8 which is the standard about CAN bus messages.*/ +private: + uint8_t maxdlen_; /*!< maxdlen_ - Max data length deduce from number of bytes read from the socket.*/ + uint32_t id_; /*!< id_ - The ID of the message. */ + uint8_t length_; /*!< length_ - the length of the data array (max 8). */ + can_message_format_t format_; /*!< format_ - the format of the message's ID.*/ + bool rtr_flag_; /*!< rtr_flag_ - Telling if the frame has RTR flag positionned. Then frame hasn't data field*/ + uint8_t flags_; /*!< flags_ - flags of a CAN FD frame. Needed if we catch FD frames.*/ + std::vector data_; /*!< data_ - The message's data field with a size of 8 which is the standard about CAN bus messages.*/ - uint8_t maxdlen_; /*!< maxdlen_ - Max data length deduce from number of bytes read from the socket.*/ +public: + can_message_t(); + can_message_t(uint8_t maxdlen, uint32_t id, uint8_t length, can_message_format_t format, bool rtr_flag_, uint8_t flags, std::vector data); - public: - can_message_t(); - - uint32_t get_id() const; - bool get_rtr_flag_() const; - can_message_format_t get_format() const; - uint8_t get_flags() const; - const uint8_t* get_data() const; - uint8_t get_length() const; + uint32_t get_id() const; + bool get_rtr_flag_() const; + can_message_format_t get_format() const; + uint8_t get_flags() const; + const uint8_t* get_data() const; + uint8_t get_length() const; - void set_format(const can_message_format_t new_format); + void set_format(const can_message_format_t new_format); - bool is_correct_to_send(); + bool is_correct_to_send(); - static can_message_t convert_to_canfd_frame(const struct canfd_frame& frame, size_t nbytes); - canfd_frame convert_to_canfd_frame(); +static can_message_t convert_to_canfd_frame(const struct canfd_frame& frame, size_t nbytes); + canfd_frame convert_to_canfd_frame(); }; /** -- cgit 1.2.3-korg