-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcan_identifier.cpp
145 lines (126 loc) · 3.91 KB
/
can_identifier.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//================================================================================================
/// @file can_identifier.cpp
///
/// @brief A representation of a classical CAN identifier with utility functions for ectracting
/// values that are encoded inside, along with some helpful constants.
/// @author Adrian Del Grosso
///
/// @copyright 2022 The Open-Agriculture Developers
//================================================================================================
#include "can_identifier.hpp"
namespace isobus
{
CANIdentifier::CANIdentifier(std::uint32_t rawIdentifierData) :
m_RawIdentifier(rawIdentifierData)
{
}
CANIdentifier::CANIdentifier(Type identifierType,
std::uint32_t pgn,
CANPriority priority,
std::uint8_t destinationAddress,
std::uint8_t sourceAddress) :
m_RawIdentifier(0)
{
if (Type::Extended == identifierType)
{
m_RawIdentifier = (static_cast<std::uint32_t>(priority) << PRIORITY_DATA_BIT_OFFSET);
if (((pgn << PARAMTER_GROUP_NUMBER_OFFSET) & PDU2_FORMAT_MASK) < PDU2_FORMAT_MASK)
{
pgn = (pgn & DESTINATION_SPECIFIC_PGN_MASK);
pgn = (pgn << PARAMTER_GROUP_NUMBER_OFFSET);
m_RawIdentifier |= pgn;
m_RawIdentifier |= (static_cast<std::uint32_t>(destinationAddress) << PARAMTER_GROUP_NUMBER_OFFSET);
}
else
{
m_RawIdentifier |= ((pgn & BROADCAST_PGN_MASK) << PARAMTER_GROUP_NUMBER_OFFSET);
}
}
m_RawIdentifier |= static_cast<std::uint32_t>(sourceAddress);
}
CANIdentifier::CANPriority CANIdentifier::get_priority() const
{
const std::uint8_t EXTENDED_IDENTIFIER_MASK = 0x07;
CANPriority retVal;
if (Type::Extended == get_identifier_type())
{
retVal = static_cast<CANPriority>((m_RawIdentifier >> PRIORITY_DATA_BIT_OFFSET) & EXTENDED_IDENTIFIER_MASK);
}
else
{
retVal = CANPriority::PriorityHighest0;
}
return retVal;
}
std::uint32_t CANIdentifier::get_identifier() const
{
return (m_RawIdentifier & (~IDENTIFIER_TYPE_BIT_MASK));
}
CANIdentifier::Type CANIdentifier::get_identifier_type() const
{
const std::uint32_t STANDARD_ID_11_BIT_SIZE = 0x7FF;
Type retVal;
if (m_RawIdentifier <= STANDARD_ID_11_BIT_SIZE)
{
retVal = Type::Standard;
}
else
{
retVal = Type::Extended;
}
return retVal;
}
std::uint32_t CANIdentifier::get_parameter_group_number() const
{
std::uint32_t retVal = UNDEFINED_PARAMETER_GROUP_NUMBER;
if (Type::Extended == get_identifier_type())
{
if ((PDU2_FORMAT_MASK & m_RawIdentifier) < PDU2_FORMAT_MASK)
{
retVal = ((m_RawIdentifier >> PARAMTER_GROUP_NUMBER_OFFSET) & DESTINATION_SPECIFIC_PGN_MASK);
}
else
{
retVal = ((m_RawIdentifier >> PARAMTER_GROUP_NUMBER_OFFSET) & BROADCAST_PGN_MASK);
}
}
return retVal;
}
std::uint8_t CANIdentifier::get_destination_address() const
{
const std::uint8_t ADDRESS_BITS_SIZE = 0xFF;
const std::uint8_t ADDRESS_DATA_OFFSET = 8;
std::uint8_t retVal = GLOBAL_ADDRESS;
if ((CANIdentifier::Type::Extended == get_identifier_type()) &&
((PDU2_FORMAT_MASK & m_RawIdentifier) < PDU2_FORMAT_MASK))
{
retVal = ((m_RawIdentifier >> ADDRESS_DATA_OFFSET) & ADDRESS_BITS_SIZE);
}
return retVal;
}
std::uint8_t CANIdentifier::get_source_address() const
{
const std::uint8_t ADDRESS_BITS_SIZE = 0xFF;
std::uint8_t retVal = CANIdentifier::GLOBAL_ADDRESS;
if (CANIdentifier::Type::Extended == get_identifier_type())
{
retVal = (m_RawIdentifier & ADDRESS_BITS_SIZE);
}
return retVal;
}
bool CANIdentifier::get_is_valid() const
{
const std::uint32_t EXTENDED_ID_29_BIT_SIZE = 0x1FFFFFFF;
const std::uint32_t STANDARD_ID_11_BIT_SIZE = 0x7FF;
bool retVal;
if (Type::Extended == get_identifier_type())
{
retVal = (m_RawIdentifier <= EXTENDED_ID_29_BIT_SIZE);
}
else
{
retVal = (m_RawIdentifier <= STANDARD_ID_11_BIT_SIZE);
}
return retVal;
}
} // namespace isobus