-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathCANDecoder.cpp
202 lines (181 loc) · 8.61 KB
/
CANDecoder.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include "CANDecoder.h"
#include "LoggingModule.h"
#include <algorithm>
#include <string>
#include <typeinfo>
#define MASK64( nbits ) ( static_cast<uint64_t>( 0xFFFFFFFFFFFFFFFFULL ) >> ( 64 - ( nbits ) ) )
namespace Aws
{
namespace IoTFleetWise
{
bool
CANDecoder::decodeCANMessage( const uint8_t *frameData,
size_t frameSize,
const CANMessageFormat &format,
const std::unordered_set<uint32_t> &signalIDsToCollect,
std::vector<CANDecodedSignal> &decodedSignals )
{
uint8_t errorCounter = 0;
uint32_t frameSizeInBits = static_cast<uint32_t>( frameSize * 8 );
uint8_t multiplexorValue = UINT8_MAX;
// Check if the message is multiplexed
if ( format.isMultiplexed() )
{
// Lookup the multiplexor signal
// complexity. Try to fix in the scheme not in the code.
auto it = std::find_if( format.mSignals.begin(), format.mSignals.end(), []( CANSignalFormat signal ) -> bool {
return signal.isMultiplexor();
} );
if ( it == format.mSignals.end() )
{
FWE_LOG_ERROR( "Message ID" + std::to_string( format.mMessageID ) +
" is multiplexed but no Multiplexor signal has been found" );
return false;
}
if ( signalIDsToCollect.find( it->mSignalID ) != signalIDsToCollect.end() )
{
// Decode the multiplexor Value
int64_t rawValue = extractSignalFromFrame( frameData, *it );
multiplexorValue = static_cast<uint8_t>( static_cast<uint8_t>( rawValue ) * it->mFactor + it->mOffset );
const auto CANsignalType = it->mSignalType;
switch ( CANsignalType )
{
case ( SignalType::UINT64 ): {
if ( typeid( it->mFactor ) == typeid( double ) )
{
FWE_LOG_WARN( "Scaling Factor is double for signal ID " + std::to_string( it->mSignalID ) +
" and type as uint64" );
}
auto physicalRawValue = static_cast<uint64_t>( multiplexorValue );
auto physicalValue = DecodedSignalValue( physicalRawValue, CANsignalType );
decodedSignals.emplace_back( CANDecodedSignal( it->mSignalID, physicalValue, CANsignalType ) );
break;
}
case ( SignalType::INT64 ): {
auto physicalRawValue = static_cast<int64_t>( multiplexorValue );
auto physicalValue = DecodedSignalValue( physicalRawValue, CANsignalType );
decodedSignals.emplace_back( CANDecodedSignal( it->mSignalID, physicalValue, CANsignalType ) );
break;
}
default: {
auto physicalRawValue = static_cast<double>( multiplexorValue );
auto physicalValue = DecodedSignalValue( physicalRawValue, CANsignalType );
decodedSignals.emplace_back( CANDecodedSignal( it->mSignalID, physicalValue, CANsignalType ) );
break;
}
}
}
}
for ( size_t i = 0; i < format.mSignals.size(); ++i )
{
if ( signalIDsToCollect.find( format.mSignals[i].mSignalID ) != signalIDsToCollect.end() )
{
// Skip the signals that don't match the MUX value
if ( ( multiplexorValue != UINT8_MAX ) && ( format.mSignals[i].mMultiplexorValue != multiplexorValue ) )
{
continue;
}
if ( ( format.mSignals[i].mFirstBitPosition >= frameSizeInBits ) ||
( ( format.mSignals[i].mSizeInBits < 1 ) || ( format.mSignals[i].mSizeInBits > frameSizeInBits ) ) )
{
// Wrongly coded Signal, skip it
FWE_LOG_ERROR( "Signal Out of Range" );
errorCounter++;
continue;
}
if ( ( !format.mSignals[i].mIsBigEndian ) &&
( format.mSignals[i].mFirstBitPosition + format.mSignals[i].mSizeInBits > frameSizeInBits ) )
{
// Wrongly coded Signal, skip it
FWE_LOG_ERROR( "Little endian signal Out of Range" );
errorCounter++;
continue;
}
// Start decoding the signal, extract the value before scaling from the Frame.
int64_t rawValue = extractSignalFromFrame( frameData, format.mSignals[i] );
const auto CANsignalType = format.mSignals[i].mSignalType;
switch ( CANsignalType )
{
case ( SignalType::UINT64 ): {
if ( typeid( format.mSignals[i].mFactor ) == typeid( double ) )
{
FWE_LOG_WARN( "Scaling Factor is double for signal ID " +
std::to_string( format.mSignals[i].mSignalID ) + " and type as uint64" );
}
uint64_t physicalRawValue =
static_cast<uint64_t>( rawValue ) * static_cast<uint64_t>( format.mSignals[i].mFactor ) +
static_cast<uint64_t>( format.mSignals[i].mOffset );
auto physicalValue = DecodedSignalValue( physicalRawValue, CANsignalType );
decodedSignals.emplace_back(
CANDecodedSignal( format.mSignals[i].mSignalID, physicalValue, CANsignalType ) );
break;
}
case ( SignalType::INT64 ): {
auto physicalRawValue =
static_cast<int64_t>( rawValue ) * static_cast<int64_t>( format.mSignals[i].mFactor ) +
static_cast<int64_t>( format.mSignals[i].mOffset );
auto physicalValue = DecodedSignalValue( physicalRawValue, CANsignalType );
decodedSignals.emplace_back(
CANDecodedSignal( format.mSignals[i].mSignalID, physicalValue, CANsignalType ) );
break;
}
default: {
auto physicalRawValue =
static_cast<double>( rawValue ) * format.mSignals[i].mFactor + format.mSignals[i].mOffset;
auto physicalValue = DecodedSignalValue( physicalRawValue, CANsignalType );
decodedSignals.emplace_back(
CANDecodedSignal( format.mSignals[i].mSignalID, physicalValue, CANsignalType ) );
}
}
}
}
// Should not harm, callers will ignore the return code.
return errorCounter == 0;
}
int64_t
CANDecoder::extractSignalFromFrame( const uint8_t *frameData, const CANSignalFormat &signalDescription )
{
uint16_t startBit = static_cast<uint16_t>( signalDescription.mFirstBitPosition );
uint8_t startByte = static_cast<uint8_t>( startBit / BYTE_SIZE );
uint8_t startBitInByte = startBit % BYTE_SIZE;
uint8_t resultLength = static_cast<uint8_t>( BYTE_SIZE - startBitInByte );
uint8_t endByte = 0U;
// Write first bits to result
// NOTE: The start bit here is different from how it appears in a DBC file. In a DBC file, the
// start bit indicates the LSB for little endian and MSB for big endian signals.
// But AWS IoT FleetWise considers start bit to always be the LSB regardless of endianess.
uint64_t result = frameData[startByte] >> startBitInByte;
// Write residual bytes
if ( signalDescription.mIsBigEndian ) // Motorola (big endian)
{
endByte = static_cast<uint8_t>(
( startByte * BYTE_SIZE + BYTE_SIZE - startBitInByte - signalDescription.mSizeInBits ) / BYTE_SIZE );
for ( int count = startByte - 1; count >= endByte; count-- )
{
result |= static_cast<uint64_t>( frameData[count] ) << resultLength;
resultLength = static_cast<uint8_t>( resultLength + BYTE_SIZE );
}
}
else // Intel (little endian)
{
endByte = static_cast<uint8_t>( ( startBit + signalDescription.mSizeInBits - 1 ) / BYTE_SIZE );
for ( int count = startByte + 1; count <= endByte; count++ )
{
result |= static_cast<uint64_t>( frameData[count] ) << resultLength;
resultLength = static_cast<uint8_t>( resultLength + BYTE_SIZE );
}
}
// Mask value
result &= MASK64( signalDescription.mSizeInBits );
// perform sign extension
if ( signalDescription.mIsSigned )
{
uint64_t msbSignMask = static_cast<uint64_t>( 1U ) << ( signalDescription.mSizeInBits - 1 );
result = ( ( result ^ msbSignMask ) - msbSignMask );
}
return static_cast<int64_t>( result );
}
} // namespace IoTFleetWise
} // namespace Aws