DBC Framework
DBC parsing and CAN signal decoding framework
Loading...
Searching...
No Matches
dbc_decoder.cpp
Go to the documentation of this file.
1
12#include "dbc_decoder.h"
13
15 std::uint32_t canId,
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);
20
21 if (it == database.frameIndexByKey.end())
22 return nullptr;
23
24 const std::size_t index = it->second;
25 if (index >= database.frames.size())
26 return nullptr;
27
28 return &database.frames[index];
29}
30
32 const RawCanFrame &frame) const {
33 DecodedFrameValue result;
34 const DecodeFrame *definition = FindFrame(database, frame.canId, frame.isExtended);
35
36 if (definition == nullptr)
37 return result;
38
39 result.definition = definition;
40 result.valid = true;
41
42 for (std::size_t signalIndex = 0U; signalIndex < definition->signals.size(); ++signalIndex) {
43 const DecodeSignal &signal = definition->signals[signalIndex];
44 DecodedSignalValue decoded;
45 std::uint64_t unsignedValue = 0U;
46
47 decoded.definition = &signal;
48
49 if (!ExtractUnsigned(frame.data, signal, unsignedValue)) {
50 decoded.valid = false;
51 result.signals.push_back(decoded);
52 continue;
53 }
54
55 if (signal.valueType == ValueType::Signed)
56 decoded.rawValue = SignExtend(unsignedValue, signal.length);
57 else
58 decoded.rawValue = unsignedValue;
59
60 decoded.physicalValue =
61 std::visit([&signal](auto rawValue) {
62 return (static_cast<double>(rawValue) * signal.factor) + signal.offset;
63 },
64 decoded.rawValue);
65 decoded.valid = true;
66
67 result.signals.push_back(decoded);
68 }
69
70 return result;
71}
72
73bool DbcDecoder::ExtractUnsigned(const std::vector<std::uint8_t> &data,
74 const DecodeSignal &signal,
75 std::uint64_t &value) {
76 if ((signal.length == 0U) || (signal.length > 64U))
77 return false;
78
79 if (signal.byteOrder == ByteOrder::Intel)
80 return ExtractIntel(data, signal.startBit, signal.length, value);
81
82 return ExtractMotorola(data, signal.startBit, signal.length, value);
83}
84
85bool DbcDecoder::ExtractIntel(const std::vector<std::uint8_t> &data,
86 std::uint32_t startBit,
87 std::uint32_t length,
88 std::uint64_t &value) {
89 value = 0U;
90
91 for (std::uint32_t bitIndex = 0U; bitIndex < length; ++bitIndex) {
92 const std::uint32_t absoluteBit = startBit + bitIndex;
93 const std::uint32_t byteIndex = absoluteBit / 8U;
94 const std::uint32_t bitInByte = absoluteBit % 8U;
95
96 if (byteIndex >= data.size())
97 return false;
98
99 const std::uint64_t bitValue =
100 (static_cast<std::uint64_t>((data[byteIndex] >> bitInByte) & 0x01U) << bitIndex);
101
102 value |= bitValue;
103 }
104
105 return true;
106}
107
108bool DbcDecoder::ExtractMotorola(const std::vector<std::uint8_t> &data,
109 std::uint32_t startBit,
110 std::uint32_t length,
111 std::uint64_t &value) {
112 /*
113 * DBC Motorola bit numbering:
114 * - startBit points to the most significant bit of the signal
115 * - inside a byte, bit numbering goes 7..0
116 * - crossing byte boundary moves to the next byte, bit 7
117 * Example: a signal starting at 7 follows 7..0, 15..8, 23..16.
118 */
119
120 value = 0U;
121
122 std::size_t byteIndex = startBit / 8U;
123 std::uint32_t bitInByte = startBit % 8U;
124
125 for (std::uint32_t bitIndex = 0U; bitIndex < length; ++bitIndex) {
126 if (byteIndex >= data.size())
127 return false;
128
129 value <<= 1U;
130 value |= static_cast<std::uint64_t>((data[byteIndex] >> bitInByte) & 0x01U);
131
132 if (bitInByte == 0U) {
133 ++byteIndex;
134 bitInByte = 7U;
135 } else
136 --bitInByte;
137 }
138
139 return true;
140}
141
142std::int64_t DbcDecoder::SignExtend(std::uint64_t value, std::uint32_t bitLength) {
143 if ((bitLength == 0U) || (bitLength > 64U))
144 return 0;
145
146 const std::uint64_t signMask = std::uint64_t(1U) << (bitLength - 1U);
147 const std::uint64_t valueMask = (bitLength == 64U)
148 ? ~std::uint64_t(0U)
149 : (std::uint64_t(1U) << bitLength) - 1U;
150 value &= valueMask;
151
152 if ((value & signMask) == 0U)
153 return static_cast<std::int64_t>(value);
154
155 // The complemented magnitude fits int64_t, including for INT64_MIN.
156 return -1 - static_cast<std::int64_t>((~value) & valueMask);
157}
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.
ValueType valueType
std::uint32_t startBit
ByteOrder byteOrder
std::uint32_t length
Fully decoded frame.
Definition dbc_decoder.h:51
const DecodeFrame * definition
Definition dbc_decoder.h:52
One decoded signal value.
Definition dbc_decoder.h:37
std::variant< std::int64_t, std::uint64_t > rawValue
Definition dbc_decoder.h:39
const DecodeSignal * definition
Definition dbc_decoder.h:38
Key for fast frame lookup.
Raw CAN frame used for runtime or trace decoding.
Definition dbc_decoder.h:24
std::vector< std::uint8_t > data
Definition dbc_decoder.h:27
std::uint32_t canId
Definition dbc_decoder.h:25
bool isExtended
Definition dbc_decoder.h:26