diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-03-01 17:42:52 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-03-01 17:42:52 +0100 |
commit | 937fe8e40aba5eb37c69fd5ff6bf4d800541a009 (patch) | |
tree | cd7b1d7919aacba6861ab63e98ed924531b00002 | |
parent | 4f06114c79f804ed404d40ffad771cbacab0fe8b (diff) |
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 <romain.forlot@iot.bzh>
-rw-r--r-- | src/can-bus.cpp | 4 | ||||
-rw-r--r-- | src/can-bus.hpp | 2 | ||||
-rw-r--r-- | src/can-message.cpp | 35 | ||||
-rw-r--r-- | 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 6279a2a..631f609 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<struct canfd_frame&, size_t> 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<struct canfd_frame&, size_t>(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 991bf62..b80b68d 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<struct canfd_frame&, size_t> 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 e273248..5a3e2c2 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;i<CAN_MESSAGE_SIZE;i++) + + /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/ + for(i=0;i<maxdlen_;i++) { data_.push_back(new_data[i]); } - } } -void can_message_t::convert_from_canfd_frame(const struct canfd_frame& frame) +void can_message_t::convert_from_canfd_frame(const std::pair<struct canfd_frame&, size_t>args) { - 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 82e2e01..efae17f 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::pair<struct canfd_frame&, size_t>args); /** * @brief Take all initialized class's members and build an |