This commit is contained in:
deeaitch committed 2026-10-05 21:41:19 -04:00
1 parent 0067cb4ddd
commit f17ddb6aad
104 files changed
+1928 -889

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
TEMPLATE = app
TARGET = dbc_demo
CONFIG += console c++17
CONFIG -= qt app_bundle
include(../core/core.pri)
SOURCES += main.cpp
CONFIG -= debug_and_release debug_and_release_target
+140
View File
@@ -0,0 +1,140 @@
/**
* @file main.cpp
* @brief
*
* Created: 2026-03-13
* Author: Deeaitch (Dim. Himro)
*
* Licensed under the MIT License.
* See LICENSE file in the project root for full license text.
*/
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <memory>
#include <vector>
#include "dbc_decode_builder.h"
#include "dbc_decoder.h"
#include "dbc_parser.h"
#include "dbc_tree_builder.h"
static void PrintTree(const TreeNode *node, int indent) {
if (node == nullptr)
return;
for (int i = 0; i < indent; ++i)
std::cout << " ";
switch (node->GetType()) {
case NodeType::Root:
std::cout << "[root] " << node->GetName() << "\n";
break;
case NodeType::Frame: {
const FrameInfo *frame = node->GetFrame();
std::cout << "[frame] " << node->GetName();
if (frame != nullptr) {
std::cout << " id=0x" << std::hex << frame->canId << std::dec
<< " ext=" << (frame->isExtended ? "yes" : "no")
<< " dlc=" << static_cast<unsigned int>(frame->dlc);
if (frame->hasPgn)
std::cout << " pgn=" << frame->pgn;
}
std::cout << "\n";
break;
}
case NodeType::Signal: {
const SignalInfo *signal = node->GetSignal();
std::cout << "[signal] " << node->GetName();
if (signal != nullptr) {
std::cout << " start=" << signal->startBit
<< " len=" << signal->length
<< " unit=" << signal->unit;
}
std::cout << "\n";
break;
}
default:
std::cout << "[unknown]\n";
break;
}
for (std::size_t i = 0U; i < node->GetChildCount(); ++i)
PrintTree(node->GetChild(i), indent + 1);
}
static void PrintDecodedFrame(const DecodedFrameValue &decoded) {
if (!decoded.valid || (decoded.definition == nullptr)) {
std::cout << "No frame definition found.\n";
return;
}
std::cout << "Decoded frame: " << decoded.definition->name << "\n";
for (std::size_t index = 0U; index < decoded.signals.size(); ++index) {
const DecodedSignalValue &signal = decoded.signals[index];
if ((signal.definition == nullptr) || !signal.valid)
continue;
std::cout << " " << signal.definition->name
<< " raw=";
if (const auto *signedValue = std::get_if<std::int64_t>(&signal.rawValue))
std::cout << *signedValue;
else
std::cout << std::get<std::uint64_t>(signal.rawValue);
std::cout << " physical=" << signal.physicalValue
<< " " << signal.definition->unit
<< "\n";
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage: dbc_demo <file.dbc>\n";
return 1;
}
try {
DbcParser parser;
DbcDatabase database = parser.ParseFile(argv[1]);
DbcTreeBuilder treeBuilder;
std::unique_ptr<TreeNode> root = treeBuilder.Build(database);
std::cout << "=== Parsed tree ===\n";
PrintTree(root.get(), 0);
DbcDecodeBuilder decodeBuilder;
DecodeDatabase decodeDatabase = decodeBuilder.Build(database);
/*
* Example raw frame.
* Replace with live CAN frame or trace record later.
*/
RawCanFrame rawFrame;
rawFrame.canId = decodeDatabase.frames.empty() ? 0U : decodeDatabase.frames[0].canId;
rawFrame.isExtended = decodeDatabase.frames.empty() ? false : decodeDatabase.frames[0].isExtended;
rawFrame.data.resize(8U, 0U);
DbcDecoder decoder;
DecodedFrameValue decoded = decoder.Decode(decodeDatabase, rawFrame);
std::cout << "\n=== Decoded frame ===\n";
PrintDecodedFrame(decoded);
} catch (const std::exception &ex) {
std::cerr << "Error: " << ex.what() << "\n";
return 2;
}
return 0;
}