Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2024 Accenture
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : #include "can/canframes/CANFrame.h"
12 :
13 : #include "can/canframes/CanId.h"
14 :
15 : #include <etl/error_handler.h>
16 :
17 : namespace can
18 : {
19 : // define const variables for GCC
20 : // #ifdef __GNUC__
21 : uint8_t const CANFrame::SENDER_MASK;
22 : uint8_t const CANFrame::CAN_OVERHEAD_BITS;
23 : uint8_t const CANFrame::MAX_FRAME_LENGTH;
24 : uint32_t const CANFrame::MAX_FRAME_ID;
25 : uint32_t const CANFrame::MAX_FRAME_ID_EXTENDED;
26 :
27 : // #endif
28 :
29 59 : CANFrame::CANFrame() : _id(0U), _timestamp(0U), _payloadLength(0U)
30 : {
31 59 : (void)memset(_payload, 0, sizeof(_payload));
32 59 : }
33 :
34 1 : CANFrame::CANFrame(uint32_t const id) : _id(id), _timestamp(0U), _payloadLength(0U)
35 : {
36 1 : (void)memset(_payload, 0xFF, sizeof(_payload));
37 1 : }
38 :
39 15 : CANFrame::CANFrame(uint32_t const id, uint8_t const* const payload, uint8_t const length)
40 15 : : _id(id), _timestamp(0U), _payloadLength(length)
41 : {
42 15 : ETL_ASSERT(
43 : length <= MAX_FRAME_LENGTH,
44 : ETL_ERROR_GENERIC("CAN frame length must be smaller than maximum length"));
45 :
46 14 : (void)memcpy(_payload, payload, static_cast<size_t>(length));
47 14 : }
48 :
49 6 : CANFrame::CANFrame(
50 : uint32_t const rawId,
51 : uint8_t const* const payload,
52 : uint8_t const length,
53 6 : bool const isExtendedId)
54 6 : : _id(CanId::id(rawId, isExtendedId)), _timestamp(0U), _payloadLength(length)
55 : {
56 6 : ETL_ASSERT(
57 : rawId <= MAX_FRAME_ID_EXTENDED,
58 : ETL_ERROR_GENERIC("CAN frame id must be smaller than the maximum allowed one"));
59 :
60 5 : ETL_ASSERT(
61 : length <= MAX_FRAME_LENGTH,
62 : ETL_ERROR_GENERIC("CAN frame length must be smaller than maximum length"));
63 :
64 4 : (void)memcpy(_payload, payload, static_cast<size_t>(length));
65 4 : }
66 :
67 25 : void CANFrame::setPayloadLength(uint8_t const length)
68 : {
69 : // only accept a payload length less or equal the maximum
70 25 : if (length <= MAX_FRAME_LENGTH)
71 : {
72 24 : _payloadLength = length;
73 : }
74 25 : }
75 :
76 29 : bool operator==(CANFrame const& frame1, CANFrame const& frame2)
77 : {
78 29 : if (frame1._id != frame2._id)
79 : {
80 2 : return false;
81 : }
82 27 : if (frame1._payloadLength != frame2._payloadLength)
83 : {
84 2 : return false;
85 : }
86 145 : for (uint8_t i = 0U; i < frame1._payloadLength; ++i)
87 : {
88 121 : if (frame1._payload[i] != frame2._payload[i])
89 : {
90 1 : return false;
91 : }
92 : }
93 24 : return true;
94 : }
95 :
96 : } // namespace can
|