10 std::uint32_t startBit;
13 std::vector<std::uint8_t> data;
15 std::int64_t rawValue;
22 const DecodeCase cases[] = {
23 {
"Motorola reproduction", ByteOrder::Motorola, 7U, 16U, ValueType::Unsigned, {0x12, 0x34},
true, 0x1234, 1.0, 0.0, 4660.0},
24 {
"Motorola within byte", ByteOrder::Motorola, 5U, 4U, ValueType::Unsigned, {0xAC},
true, 11, 1.0, 0.0, 11.0},
25 {
"Motorola unaligned crossing", ByteOrder::Motorola, 3U, 12U, ValueType::Unsigned, {0xAB, 0xCD},
true, 0xBCD, 1.0, 0.0, 3021.0},
26 {
"Motorola starts at bit zero", ByteOrder::Motorola, 0U, 2U, ValueType::Unsigned, {0x01, 0x80},
true, 3, 1.0, 0.0, 3.0},
27 {
"Motorola second byte", ByteOrder::Motorola, 15U, 16U, ValueType::Unsigned, {0xFF, 0x12, 0x34},
true, 0x1234, 1.0, 0.0, 4660.0},
28 {
"Motorola signed scaled", ByteOrder::Motorola, 7U, 16U, ValueType::Signed, {0xFF, 0xFE},
true, -2, 0.5, 10.0, 9.0},
29 {
"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},
30 {
"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},
31 {
"Motorola short payload", ByteOrder::Motorola, 7U, 16U, ValueType::Unsigned, {0x12},
false, 0, 1.0, 0.0, 0.0},
32 {
"Motorola empty payload", ByteOrder::Motorola, 7U, 8U, ValueType::Unsigned, {},
false, 0, 1.0, 0.0, 0.0},
33 {
"Motorola large start", ByteOrder::Motorola, std::numeric_limits<std::uint32_t>::max(), 8U, ValueType::Unsigned, {0xFF},
false, 0, 1.0, 0.0, 0.0},
34 {
"Motorola zero length", ByteOrder::Motorola, 7U, 0U, ValueType::Unsigned, {0xFF},
false, 0, 1.0, 0.0, 0.0},
35 {
"Motorola excessive length", ByteOrder::Motorola, 7U, 65U, ValueType::Unsigned,
36 std::vector<std::uint8_t>(9U, 0xFF),
false, 0, 1.0, 0.0, 0.0},
37 {
"Intel regression", ByteOrder::Intel, 0U, 16U, ValueType::Unsigned, {0x12, 0x34},
true, 0x3412, 1.0, 0.0, 13330.0},
38 {
"Intel signed regression", ByteOrder::Intel, 0U, 16U, ValueType::Signed, {0xFE, 0xFF},
true, -2, 0.5, 10.0, 9.0},
39 {
"Intel short payload", ByteOrder::Intel, 0U, 16U, ValueType::Unsigned, {0x12},
false, 0, 1.0, 0.0, 0.0}};
42 for (
const DecodeCase &test: cases) {
44 signal.
name = test.name;
47 signal.
length = test.length;
49 signal.
factor = test.factor;
50 signal.
offset = test.offset;
53 definition.
canId = 0x123U;
54 definition.
dlc =
static_cast<std::uint8_t
>(test.data.size());
55 definition.signals.push_back(signal);
57 database.frames.push_back(definition);
58 database.frameIndexByKey.emplace(
FrameKey(definition.
canId,
false), 0U);
62 frame.
data = test.data;
64 const bool passed = result.
valid &&
66 (result.signals.size() == 1U) &&
67 (result.signals[0].
definition == &database.frames[0].signals[0]) &&
68 (result.signals[0].
valid == test.valid) &&
69 (!test.valid || (((test.valueType == ValueType::Signed)
70 ? (std::get_if<std::int64_t>(&result.signals[0].rawValue) &&
71 std::get<std::int64_t>(result.signals[0].rawValue) == test.rawValue)
72 : (std::get_if<std::uint64_t>(&result.signals[0].rawValue) &&
73 std::get<std::uint64_t>(result.signals[0].rawValue) ==
74 static_cast<std::uint64_t>(test.rawValue))) &&
75 (result.signals[0].physicalValue == test.physicalValue)));
77 std::cerr <<
"FAIL: " << test.name <<
'\n';
81 std::size_t boundaryCases = 0U;
82 for (
ByteOrder order: {ByteOrder::Intel, ByteOrder::Motorola}) {
83 for (
ValueType type: {ValueType::Unsigned, ValueType::Signed}) {
84 for (std::uint64_t bits: {std::uint64_t(0U), std::uint64_t(0x7FFFFFFFFFFFFFFFULL),
85 std::uint64_t(0x8000000000000000ULL),
86 std::uint64_t(0x8000000000000001ULL),
87 std::uint64_t(0xFFFFFFFFFFFFFFFFULL)}) {
91 signal.
startBit = order == ByteOrder::Intel ? 0U : 7U;
97 definition.signals.push_back(signal);
98 database.frames.push_back(definition);
99 database.frameIndexByKey.emplace(
FrameKey(0U,
false), 0U);
101 for (
unsigned byte = 0U;
byte < 8U; ++byte) {
102 const unsigned shift = (order == ByteOrder::Intel ? byte : 7U - byte) * 8U;
103 frame.
data.push_back(
static_cast<std::uint8_t
>(bits >> shift));
106 const std::int64_t signedExpected = bits <= 0x7FFFFFFFFFFFFFFFULL
107 ?
static_cast<std::int64_t
>(bits)
108 : std::numeric_limits<std::int64_t>::min() +
109 static_cast<std::int64_t>(bits - 0x8000000000000000ULL);
110 const auto &decoded = result.signals.at(0U);
111 const auto expected = type == ValueType::Unsigned
112 ? std::variant<std::int64_t, std::uint64_t>(bits)
113 : std::variant<std::int64_t, std::uint64_t>(signedExpected);
114 const double physical = (type == ValueType::Unsigned
115 ?
static_cast<double>(bits)
116 : static_cast<double>(signedExpected)) *
119 std::ostringstream actualText;
120 std::ostringstream expectedText;
121 std::visit([&actualText](
auto raw) { actualText << raw; }, decoded.rawValue);
122 if (type == ValueType::Unsigned)
123 expectedText << bits;
125 expectedText << signedExpected;
126 if (!decoded.valid || decoded.rawValue != expected ||
127 decoded.physicalValue != physical || actualText.str() != expectedText.str()) {
128 std::cerr <<
"FAIL: 64-bit boundary " << bits <<
'\n';
137 std::cout <<
"All " <<
sizeof(cases) /
sizeof(cases[0]) + boundaryCases <<
" decoder cases passed.\n";
Runtime CAN decoder using prebuilt decode database.
DecodedFrameValue Decode(const DecodeDatabase &database, const RawCanFrame &frame) const
Decode one raw CAN frame.
Created: 2026-03-13 Author: Deeaitch (Dim. Himro)
ByteOrder
Signal byte order used for runtime decoding.
ValueType
Signal numeric type.
Runtime decode database with fast lookup by CAN ID.
Runtime-ready frame definition.
Runtime-ready signal definition.
const DecodeFrame * definition
Key for fast frame lookup.
Raw CAN frame used for runtime or trace decoding.
std::vector< std::uint8_t > data