16 bool isExtended)
const {
17 const FrameKey key (canId, isExtended);
18 const std::unordered_map<FrameKey, std::size_t, FrameKeyHasher>::const_iterator it =
19 database.frameIndexByKey.find (key);
21 if (it == database.frameIndexByKey.end())
24 const std::size_t index = it->second;
25 if (index >= database.frames.size())
28 return &database.frames[index];
36 if (definition ==
nullptr)
42 for (std::size_t signalIndex = 0U; signalIndex < definition->signals.size(); ++signalIndex) {
43 const DecodeSignal &signal = definition->signals[signalIndex];
45 std::uint64_t unsignedValue = 0U;
49 if (!ExtractUnsigned (frame.
data, signal, unsignedValue)) {
50 decoded.
valid =
false;
51 result.signals.push_back (decoded);
55 if (signal.
valueType == ValueType::Signed)
58 decoded.
rawValue =
static_cast<std::int64_t
> (unsignedValue);
64 result.signals.push_back (decoded);
70bool DbcDecoder::ExtractUnsigned (
const std::vector<std::uint8_t> &data,
72 std::uint64_t &value) {
79 return ExtractMotorola (data, signal.
startBit, signal.
length, value);
82bool DbcDecoder::ExtractIntel (
const std::vector<std::uint8_t> &data,
83 std::uint32_t startBit,
85 std::uint64_t &value) {
88 for (std::uint32_t bitIndex = 0U; bitIndex < length; ++bitIndex) {
89 const std::uint32_t absoluteBit = startBit + bitIndex;
90 const std::uint32_t byteIndex = absoluteBit / 8U;
91 const std::uint32_t bitInByte = absoluteBit % 8U;
93 if (byteIndex >= data.size())
96 const std::uint64_t bitValue =
97 (
static_cast<std::uint64_t
> ((data[byteIndex] >> bitInByte) & 0x01U) << bitIndex);
105bool DbcDecoder::ExtractMotorola (
const std::vector<std::uint8_t> &data,
106 std::uint32_t startBit,
107 std::uint32_t length,
108 std::uint64_t &value) {
118 std::int32_t currentBit =
static_cast<std::int32_t
> (startBit);
120 for (std::uint32_t bitIndex = 0U; bitIndex < length; ++bitIndex) {
124 const std::uint32_t absoluteBit =
static_cast<std::uint32_t
> (currentBit);
125 const std::uint32_t byteIndex = absoluteBit / 8U;
126 const std::uint32_t bitFromMsb = absoluteBit % 8U;
127 const std::uint32_t bitInByte = 7U - bitFromMsb;
129 if (byteIndex >= data.size())
133 value |=
static_cast<std::uint64_t
> ((data[byteIndex] >> bitInByte) & 0x01U);
135 if ((absoluteBit % 8U) == 7U)
136 currentBit =
static_cast<std::int32_t
> ((byteIndex + 1U) * 8U);
144std::int64_t DbcDecoder::SignExtend (std::uint64_t value, std::uint32_t bitLength) {
145 if ((bitLength == 0U) || (bitLength >= 64U))
146 return static_cast<std::int64_t
> (value);
148 const std::uint64_t signMask = (
static_cast<std::uint64_t
> (1U) << (bitLength - 1U));
149 const std::uint64_t valueMask = (
static_cast<std::uint64_t
> (1U) << bitLength) - 1U;
153 if ((value & signMask) == 0U)
154 return static_cast<std::int64_t
> (value);
156 return static_cast<std::int64_t
> (value | (~valueMask));
DecodedFrameValue Decode(const DecodeDatabase &database, const RawCanFrame &frame) const
Decode one raw CAN frame.
const DecodeFrame * FindFrame(const DecodeDatabase &database, std::uint32_t canId, bool isExtended) const
Find frame definition by CAN ID.
Created: 2026-03-13 Author: Deeaitch (Dim. Himro)
Runtime decode database with fast lookup by CAN ID.
Runtime-ready frame definition.
Runtime-ready signal definition.
const DecodeFrame * definition
One decoded signal value.
const DecodeSignal * definition
Key for fast frame lookup.
Raw CAN frame used for runtime or trace decoding.
std::vector< std::uint8_t > data