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 /src/can-message.cpp | |
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>
Diffstat (limited to 'src/can-message.cpp')
-rw-r--r-- | src/can-message.cpp | 35 |
1 files changed, 17 insertions, 18 deletions
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;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]); |