From 937fe8e40aba5eb37c69fd5ff6bf4d800541a009 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Wed, 1 Mar 2017 17:42:52 +0100 Subject: New way to return canfd_frame with now number of read bytes. This way, we can detect correctly the max data length and set the data correctly. Change-Id: I346df154df76dbd4a463d1fabfd9edd56faca506 Signed-off-by: Romain Forlot --- src/can-bus.cpp | 4 ++-- src/can-bus.hpp | 2 +- src/can-message.cpp | 35 +++++++++++++++++------------------ src/can-message.hpp | 6 +++--- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/can-bus.cpp b/src/can-bus.cpp index 6279a2a7..631f6090 100644 --- a/src/can-bus.cpp +++ b/src/can-bus.cpp @@ -355,7 +355,7 @@ int can_bus_dev_t::close() return can_socket_; } -canfd_frame can_bus_dev_t::read() +std::pair can_bus_dev_t::read() { ssize_t nbytes; //int maxdlen; @@ -379,7 +379,7 @@ canfd_frame can_bus_dev_t::read() ::memset(&cfd, 0, sizeof(cfd)); } - return cfd; + return std::pair(cfd, nbytes); } void can_bus_dev_t::start_reading(can_bus_t& can_bus) diff --git a/src/can-bus.hpp b/src/can-bus.hpp index 991bf62c..b80b68d9 100644 --- a/src/can-bus.hpp +++ b/src/can-bus.hpp @@ -270,7 +270,7 @@ class can_bus_dev_t { * @param const struct afb_binding_interface* interface pointer. Used to be able to log * using application framework logger. */ - canfd_frame read(); + std::pair read(); /** * @brief Send a can message from a can_message_t object. diff --git a/src/can-message.cpp b/src/can-message.cpp index e2732487..5a3e2c27 100644 --- a/src/can-message.cpp +++ b/src/can-message.cpp @@ -63,18 +63,18 @@ uint8_t can_message_t::get_length() const return length_; } -void can_message_t::set_max_data_length(const struct canfd_frame& frame) +void can_message_t::set_max_data_length(size_t nbytes) { maxdlen_ = 0; - switch(sizeof(frame)) + switch(nbytes) { case CANFD_MTU: - DEBUG(binder_interface, "convert_from_canfd_frame: Got an CAN FD frame with length %d and flags %d", frame.len, frame.flags); + DEBUG(binder_interface, "convert_from_canfd_frame: Got an CAN FD frame"); maxdlen_ = CANFD_MAX_DLEN; break; case CAN_MTU: - DEBUG(binder_interface, "convert_from_canfd_frame: Got a legacy CAN frame with length %d", frame.len); + DEBUG(binder_interface, "convert_from_canfd_frame: Got a legacy CAN frame"); maxdlen_ = CAN_MAX_DLEN; break; default: @@ -148,28 +148,28 @@ void can_message_t::set_length(const uint8_t new_length) } } -void can_message_t::set_data(const __u8 new_data[], size_t dlen) +void can_message_t::set_data(const __u8* new_data) { - if (dlen > maxdlen_) - ERROR(binder_interface, "Can set data, too big ! It is a CAN frame ?"); - else - { int i; - /* Limiting to 8 bytes message for now, even on 64 bytes from fd frames*/ - for(i=0;iargs) { - set_max_data_length(frame); + // May be it's overkill to assign member of the pair... May be it will change... + struct canfd_frame frame = args.first; + size_t nbytes = args.second; + set_max_data_length(nbytes); set_length(frame.len); set_id_and_format(frame.can_id); - /* standard CAN frames may have RTR enabled. There are no ERR frames with RTR */ + /* Overwrite lenght_ 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; @@ -182,9 +182,8 @@ void can_message_t::convert_from_canfd_frame(const struct canfd_frame& frame) if(maxdlen_ == CANFD_MAX_DLEN) set_flags(frame.flags); - size_t dlen = sizeof(frame.data)/sizeof(__u8); - data_.reserve(dlen); - set_data(frame.data, dlen); + data_.reserve(maxdlen_); + set_data(frame.data); DEBUG(binder_interface, "convert_from_canfd_frame: Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", id_, format_, length_, data_[0], data_[1], data_[2], data_[3], data_[4], data_[5], data_[6], data_[7]); diff --git a/src/can-message.hpp b/src/can-message.hpp index 82e2e019..efae17ff 100644 --- a/src/can-message.hpp +++ b/src/can-message.hpp @@ -117,7 +117,7 @@ class can_message_t { */ uint8_t get_length() const; - void set_max_data_length(const struct canfd_frame& frame); + void set_max_data_length(size_t nbytes); /** * @brief Control whether the object is correctly initialized @@ -176,7 +176,7 @@ class can_message_t { * * @param uint8_t data_ array with a max size of 8 elements. */ - void set_data(const __u8 new_data[], size_t dlen); + void set_data(const __u8* new_data); /** * @brief Set length_ member value. @@ -195,7 +195,7 @@ class can_message_t { * * @param canfd_frame struct read from can bus device. */ - void convert_from_canfd_frame(const struct canfd_frame& frame); + void convert_from_canfd_frame(const std::pairargs); /** * @brief Take all initialized class's members and build an -- cgit 1.2.3-korg