Line data Source code
1 : // Copyright 2024 Accenture. 2 : 3 : #include "can/canframes/CANFrame.h" 4 : 5 : #include "can/canframes/CanId.h" 6 : 7 : #if (!defined(BOOTLOADER)) && (!defined(BOOTLOADER_UPDATER)) 8 : #include "can/CanLogger.h" 9 : using ::util::logger::Logger; 10 : #endif 11 : 12 : namespace can 13 : { 14 : // define const variables for GCC 15 : // #ifdef __GNUC__ 16 : uint8_t const CANFrame::SENDER_MASK; 17 : uint8_t const CANFrame::CAN_OVERHEAD_BITS; 18 : uint8_t const CANFrame::MAX_FRAME_LENGTH; 19 : uint32_t const CANFrame::MAX_FRAME_ID; 20 : uint32_t const CANFrame::MAX_FRAME_ID_EXTENDED; 21 : 22 : // #endif 23 : 24 58 : CANFrame::CANFrame() : _id(0U), _timestamp(0U), _payloadLength(0U) 25 : { 26 58 : (void)memset(_payload, 0, sizeof(_payload)); 27 58 : } 28 : 29 2 : CANFrame::CANFrame(uint32_t const id) : _id(id), _timestamp(0U), _payloadLength(0U) 30 : { 31 2 : (void)memset(_payload, 0xFF, sizeof(_payload)); 32 2 : } 33 : 34 13 : CANFrame::CANFrame(CANFrame const& frame) 35 13 : : _id(frame._id), _timestamp(frame._timestamp), _payloadLength(frame._payloadLength) 36 : { 37 13 : (void)memcpy(_payload, frame._payload, static_cast<size_t>(frame._payloadLength)); 38 13 : } 39 : 40 15 : CANFrame::CANFrame(uint32_t const id, uint8_t const* const payload, uint8_t const length) 41 15 : : _id(id), _timestamp(0U), _payloadLength(length) 42 : { 43 15 : estd_assert(length <= MAX_FRAME_LENGTH); 44 14 : (void)memcpy(_payload, payload, static_cast<size_t>(length)); 45 14 : } 46 : 47 6 : CANFrame::CANFrame( 48 : uint32_t const rawId, 49 : uint8_t const* const payload, 50 : uint8_t const length, 51 6 : bool const isExtendedId) 52 6 : : _id(CanId::id(rawId, isExtendedId)), _timestamp(0U), _payloadLength(length) 53 : { 54 6 : estd_assert(rawId <= MAX_FRAME_ID_EXTENDED); 55 5 : estd_assert(length <= MAX_FRAME_LENGTH); 56 4 : (void)memcpy(_payload, payload, static_cast<size_t>(length)); 57 4 : } 58 : 59 13 : CANFrame& CANFrame::operator=(CANFrame const& canFrame) 60 : { 61 13 : if (&canFrame != this) 62 : { 63 12 : _id = canFrame._id; 64 12 : _timestamp = canFrame._timestamp; 65 12 : _payloadLength = canFrame._payloadLength; 66 12 : (void)memcpy(_payload, canFrame._payload, static_cast<size_t>(canFrame._payloadLength)); 67 : } 68 13 : return *this; 69 : } 70 : 71 25 : void CANFrame::setPayloadLength(uint8_t const length) 72 : { 73 : // only accept a payload length less or equal the maximum 74 25 : if (length <= MAX_FRAME_LENGTH) 75 : { 76 24 : _payloadLength = length; 77 : } 78 25 : } 79 : 80 29 : bool operator==(CANFrame const& frame1, CANFrame const& frame2) 81 : { 82 29 : if (frame1._id != frame2._id) 83 : { 84 : return false; 85 : } 86 27 : if (frame1._payloadLength != frame2._payloadLength) 87 : { 88 : return false; 89 : } 90 145 : for (uint8_t i = 0U; i < frame1._payloadLength; ++i) 91 : { 92 121 : if (frame1._payload[i] != frame2._payload[i]) 93 : { 94 : return false; 95 : } 96 : } 97 : return true; 98 : } 99 : 100 : } // namespace can