DBC Framework
DBC parsing and CAN signal decoding framework
Loading...
Searching...
No Matches
dbc_decoder_test.cpp
1#include "dbc_decoder.h"
2
3#include <QtTest>
4#include <limits>
5#include <sstream>
6
7struct DecodeCase {
8 const char *name;
9 ByteOrder byteOrder;
10 std::uint32_t startBit;
11 std::uint32_t length;
12 ValueType valueType;
13 std::vector<std::uint8_t> data;
14 bool valid;
15 std::int64_t rawValue;
16 double factor;
17 double offset;
18 double physicalValue;
19};
20
21Q_DECLARE_METATYPE(DecodeCase)
22
23class DbcDecoderTest : public QObject {
24 Q_OBJECT
25private Q_SLOTS:
26 void decode_data();
27 void decode();
28 void boundaries();
29};
30
31void DbcDecoderTest::decode_data() {
32 QTest::addColumn<DecodeCase>("test");
33 const DecodeCase cases[] = {
34 {"Motorola reproduction", ByteOrder::Motorola, 7U, 16U, ValueType::Unsigned, {0x12, 0x34}, true, 0x1234, 1.0, 0.0, 4660.0},
35 {"Motorola within byte", ByteOrder::Motorola, 5U, 4U, ValueType::Unsigned, {0xAC}, true, 11, 1.0, 0.0, 11.0},
36 {"Motorola unaligned crossing", ByteOrder::Motorola, 3U, 12U, ValueType::Unsigned, {0xAB, 0xCD}, true, 0xBCD, 1.0, 0.0, 3021.0},
37 {"Motorola starts at bit zero", ByteOrder::Motorola, 0U, 2U, ValueType::Unsigned, {0x01, 0x80}, true, 3, 1.0, 0.0, 3.0},
38 {"Motorola second byte", ByteOrder::Motorola, 15U, 16U, ValueType::Unsigned, {0xFF, 0x12, 0x34}, true, 0x1234, 1.0, 0.0, 4660.0},
39 {"Motorola signed scaled", ByteOrder::Motorola, 7U, 16U, ValueType::Signed, {0xFF, 0xFE}, true, -2, 0.5, 10.0, 9.0},
40 {"Motorola 64 bits", ByteOrder::Motorola, 7U, 64U, ValueType::Unsigned, {0, 0, 0, 0, 0, 0, 0x12, 0x34}, true, 0x1234, 1.0, 0.0, 4660.0},
41 {"Motorola 64 bits high bit", ByteOrder::Motorola, 7U, 64U, ValueType::Unsigned, {0x40, 0, 0, 0, 0, 0, 0, 0}, true, 0x4000000000000000LL, 1.0, 0.0, 4611686018427387904.0},
42 {"Motorola short payload", ByteOrder::Motorola, 7U, 16U, ValueType::Unsigned, {0x12}, false, 0, 1.0, 0.0, 0.0},
43 {"Motorola empty payload", ByteOrder::Motorola, 7U, 8U, ValueType::Unsigned, {}, false, 0, 1.0, 0.0, 0.0},
44 {"Motorola large start", ByteOrder::Motorola, std::numeric_limits<std::uint32_t>::max(), 8U, ValueType::Unsigned, {0xFF}, false, 0, 1.0, 0.0, 0.0},
45 {"Motorola zero length", ByteOrder::Motorola, 7U, 0U, ValueType::Unsigned, {0xFF}, false, 0, 1.0, 0.0, 0.0},
46 {"Motorola excessive length", ByteOrder::Motorola, 7U, 65U, ValueType::Unsigned,
47 std::vector<std::uint8_t>(9U, 0xFF), false, 0, 1.0, 0.0, 0.0},
48 {"Intel regression", ByteOrder::Intel, 0U, 16U, ValueType::Unsigned, {0x12, 0x34}, true, 0x3412, 1.0, 0.0, 13330.0},
49 {"Intel signed regression", ByteOrder::Intel, 0U, 16U, ValueType::Signed, {0xFE, 0xFF}, true, -2, 0.5, 10.0, 9.0},
50 {"Intel short payload", ByteOrder::Intel, 0U, 16U, ValueType::Unsigned, {0x12}, false, 0, 1.0, 0.0, 0.0}};
51
52 for (const DecodeCase &test: cases)
53 QTest::newRow(test.name) << test;
54}
55
56void DbcDecoderTest::decode() {
57 QFETCH(DecodeCase, test);
58 DecodeSignal signal;
59 signal.name = test.name;
60 signal.byteOrder = test.byteOrder;
61 signal.startBit = test.startBit;
62 signal.length = test.length;
63 signal.valueType = test.valueType;
64 signal.factor = test.factor;
65 signal.offset = test.offset;
66
67 DecodeFrame definition;
68 definition.canId = 0x123U;
69 definition.dlc = static_cast<std::uint8_t>(test.data.size());
70 definition.signals.push_back(signal);
71 const DecodeDatabase database({definition});
72
73 RawCanFrame frame;
74 frame.canId = definition.canId;
75 frame.data = test.data;
76 const DecodedFrameValue result = DbcDecoder().Decode(database, frame);
77 QVERIFY2(result.valid, test.name);
78 QCOMPARE(result.definition.get(), &database.GetFrames()[0]);
79 QCOMPARE(result.signals.size(), std::size_t(1U));
80 const DecodedSignalValue &decoded = result.signals[0];
81 QCOMPARE(decoded.definition.get(), &database.GetFrames()[0].signals[0]);
82 QCOMPARE(decoded.valid, test.valid);
83 if (!test.valid)
84 return;
85
86 if (test.valueType == ValueType::Signed) {
87 const std::int64_t *raw = std::get_if<std::int64_t>(&decoded.rawValue);
88 QVERIFY(raw != nullptr);
89 QCOMPARE(*raw, test.rawValue);
90 } else {
91 const std::uint64_t *raw = std::get_if<std::uint64_t>(&decoded.rawValue);
92 QVERIFY(raw != nullptr);
93 QCOMPARE(*raw, static_cast<std::uint64_t>(test.rawValue));
94 }
95 QCOMPARE(decoded.physicalValue, test.physicalValue);
96}
97
98void DbcDecoderTest::boundaries() {
99 for (ByteOrder order: {ByteOrder::Intel, ByteOrder::Motorola}) {
100 for (ValueType type: {ValueType::Unsigned, ValueType::Signed}) {
101 for (std::uint64_t bits: {std::uint64_t(0U), std::uint64_t(0x7FFFFFFFFFFFFFFFULL),
102 std::uint64_t(0x8000000000000000ULL),
103 std::uint64_t(0x8000000000000001ULL),
104 std::uint64_t(0xFFFFFFFFFFFFFFFFULL)}) {
105 DecodeFrame definition;
106 DecodeSignal signal;
107 signal.startBit = order == ByteOrder::Intel ? 0U : 7U;
108 signal.length = 64U;
109 signal.byteOrder = order;
110 signal.valueType = type;
111 signal.factor = 0.5;
112 signal.offset = 10.0;
113 definition.signals.push_back(signal);
114 const DecodeDatabase database({definition});
115 RawCanFrame frame;
116 for (unsigned byte = 0U; byte < 8U; ++byte) {
117 const unsigned shift = (order == ByteOrder::Intel ? byte : 7U - byte) * 8U;
118 frame.data.push_back(static_cast<std::uint8_t>(bits >> shift));
119 }
120 const DecodedFrameValue result = DbcDecoder().Decode(database, frame);
121 const std::int64_t signedExpected = bits <= 0x7FFFFFFFFFFFFFFFULL
122 ? static_cast<std::int64_t>(bits)
123 : std::numeric_limits<std::int64_t>::min() +
124 static_cast<std::int64_t>(bits - 0x8000000000000000ULL);
125 const DecodedSignalValue &decoded = result.signals.at(0U);
126 const RawSignalValue expected = type == ValueType::Unsigned
127 ? std::variant<std::int64_t, std::uint64_t>(bits)
128 : std::variant<std::int64_t, std::uint64_t>(signedExpected);
129 const double physical = (type == ValueType::Unsigned
130 ? static_cast<double>(bits)
131 : static_cast<double>(signedExpected)) *
132 0.5 +
133 10.0;
134 std::ostringstream actualText;
135 std::ostringstream expectedText;
136 if (const std::int64_t *signedValue = std::get_if<std::int64_t>(&decoded.rawValue))
137 actualText << *signedValue;
138 else
139 actualText << std::get<std::uint64_t>(decoded.rawValue);
140 if (type == ValueType::Unsigned)
141 expectedText << bits;
142 else
143 expectedText << signedExpected;
144 QVERIFY(decoded.valid);
145 QVERIFY(decoded.rawValue == expected);
146 QCOMPARE(decoded.physicalValue, physical);
147 QCOMPARE(actualText.str(), expectedText.str());
148 }
149 }
150 }
151}
152
153int RunDecoderTests(int argc, char **argv) {
154 DbcDecoderTest tests;
155 return QTest::qExec(&tests, argc, argv);
156}
157
158#include "dbc_decoder_test.moc"
Runtime CAN decoder using prebuilt decode database.
Definition dbc_decoder.h:74
DecodedFrameValue Decode(const DecodeDatabase &database, const RawCanFrame &frame) const
Decode one raw CAN frame.
Immutable runtime snapshot. Copies share storage; reload replaces a snapshot. Owning lookup handles a...
Created: 2026-03-13 Author: Deeaitch (Dim. Himro)
ByteOrder
Signal byte order used for runtime decoding.
ValueType
Signal numeric type.
Runtime-ready frame definition.
std::uint8_t dlc
std::uint32_t canId
Runtime-ready signal definition.
std::string name
ValueType valueType
std::uint32_t startBit
ByteOrder byteOrder
std::uint32_t length
Fully decoded frame.
Definition dbc_decoder.h:62
std::shared_ptr< const DecodeFrame > definition
Definition dbc_decoder.h:63
One decoded signal value.
Definition dbc_decoder.h:43
RawSignalValue rawValue
Definition dbc_decoder.h:46
std::shared_ptr< const DecodeSignal > definition
Definition dbc_decoder.h:44
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