DBC Framework
DBC parsing and CAN signal decoding framework
Loading...
Searching...
No Matches
examples/main.cpp
Go to the documentation of this file.
1
12#include <cstddef>
13#include <iomanip>
14#include <iostream>
15#include <memory>
16#include <vector>
17
18#include "dbc_decode_builder.h"
19#include "dbc_decoder.h"
20#include "dbc_parser.h"
21#include "dbc_tree_builder.h"
22
23static void PrintTree(const TreeNode *node, int indent) {
24 if (node == nullptr)
25 return;
26
27 for (int i = 0; i < indent; ++i)
28 std::cout << " ";
29
30 switch (node->GetType()) {
31 case NodeType::Root:
32 std::cout << "[root] " << node->GetName() << "\n";
33 break;
34
35 case NodeType::Frame: {
36 const FrameInfo *frame = node->GetFrame();
37 std::cout << "[frame] " << node->GetName();
38
39 if (frame != nullptr) {
40 std::cout << " id=0x" << std::hex << frame->canId << std::dec
41 << " ext=" << (frame->isExtended ? "yes" : "no")
42 << " dlc=" << static_cast<unsigned int>(frame->dlc);
43
44 if (frame->hasPgn)
45 std::cout << " pgn=" << frame->pgn;
46 }
47
48 std::cout << "\n";
49 break;
50 }
51
52 case NodeType::Signal: {
53 const SignalInfo *signal = node->GetSignal();
54 std::cout << "[signal] " << node->GetName();
55
56 if (signal != nullptr) {
57 std::cout << " start=" << signal->startBit
58 << " len=" << signal->length
59 << " unit=" << signal->unit;
60 }
61
62 std::cout << "\n";
63 break;
64 }
65
66 default:
67 std::cout << "[unknown]\n";
68 break;
69 }
70
71 for (std::size_t i = 0U; i < node->GetChildCount(); ++i)
72 PrintTree(node->GetChild(i), indent + 1);
73}
74
75static void PrintDecodedFrame(const DecodedFrameValue &decoded) {
76 if (!decoded.valid || (decoded.definition == nullptr)) {
77 std::cout << "No frame definition found.\n";
78 return;
79 }
80
81 std::cout << "Decoded frame: " << decoded.definition->name << "\n";
82
83 for (const DecodedSignalValue &signal : decoded.signals) {
84
85 if ((signal.definition == nullptr) || !signal.valid)
86 continue;
87
88 std::cout << " " << signal.definition->name
89 << " raw=";
90 if (const std::int64_t *signedValue = std::get_if<std::int64_t>(&signal.rawValue))
91 std::cout << *signedValue;
92 else
93 std::cout << std::get<std::uint64_t>(signal.rawValue);
94 std::cout << " physical=" << signal.physicalValue
95 << " " << signal.definition->unit
96 << "\n";
97 }
98}
99
100int main(int argc, char *argv[]) {
101 if (argc < 2) {
102 std::cerr << "Usage: dbc_demo <file.dbc>\n";
103 return 1;
104 }
105
106 try {
107 DbcParser parser;
108 DbcDatabase database = parser.ParseFile(argv[1]);
109
110 DbcTreeBuilder treeBuilder;
111 std::unique_ptr<TreeNode> root = treeBuilder.Build(database);
112
113 std::cout << "=== Parsed tree ===\n";
114 PrintTree(root.get(), 0);
115
116 DbcDecodeBuilder decodeBuilder;
117 DecodeDatabase decodeDatabase = decodeBuilder.Build(database);
118
119 /*
120 * Example raw frame.
121 * Replace with live CAN frame or trace record later.
122 */
123 RawCanFrame rawFrame;
124 rawFrame.canId = decodeDatabase.frames.empty() ? 0U : decodeDatabase.frames[0].canId;
125 rawFrame.isExtended = decodeDatabase.frames.empty() ? false : decodeDatabase.frames[0].isExtended;
126 rawFrame.data.resize(8U, 0U);
127
128 DbcDecoder decoder;
129 DecodedFrameValue decoded = decoder.Decode(decodeDatabase, rawFrame);
130
131 std::cout << "\n=== Decoded frame ===\n";
132 PrintDecodedFrame(decoded);
133 } catch (const std::exception &ex) {
134 std::cerr << "Error: " << ex.what() << "\n";
135 return 2;
136 }
137
138 return 0;
139}
Converts parsed DBC data into runtime decode database.
DecodeDatabase Build(const DbcDatabase &source) const
Build runtime decode database.
Runtime CAN decoder using prebuilt decode database.
Definition dbc_decoder.h:72
DecodedFrameValue Decode(const DecodeDatabase &database, const RawCanFrame &frame) const
Decode one raw CAN frame.
Minimal DBC parser.
Definition dbc_parser.h:34
DbcDatabase ParseFile(const std::string &filePath) const
Parse DBC file.
Builds a simple tree from parsed DBC database.
std::unique_ptr< TreeNode > Build(const DbcDatabase &database) const
Build tree representation of parsed DBC data.
Tree node for later use in model/view or other hierarchy consumers.
Definition tree_node.h:35
const SignalInfo * GetSignal() const
Get signal payload if node is signal.
Definition tree_node.cpp:64
const TreeNode * GetChild(std::size_t index) const
Get child by index.
Definition tree_node.cpp:38
const std::string & GetName() const
Get display name.
Definition tree_node.cpp:56
std::size_t GetChildCount() const
Get child count.
Definition tree_node.cpp:34
NodeType GetType() const
Get node type.
Definition tree_node.cpp:52
const FrameInfo * GetFrame() const
Get frame payload if node is frame.
Definition tree_node.cpp:60
Created: 2026-03-13 Author: Deeaitch (Dim. Himro)
Created: 2026-03-13 Author: Deeaitch (Dim. Himro)
Created: 2026-03-13 Author: Deeaitch (Dim. Himro)
Created: 2026-03-13 Author: Deeaitch (Dim. Himro)
Parsed DBC content stored in a simple internal form.
Runtime decode database with fast lookup by CAN ID.
std::string name
Fully decoded frame.
Definition dbc_decoder.h:61
const DecodeFrame * definition
Definition dbc_decoder.h:62
One decoded signal value.
Definition dbc_decoder.h:43
Describes one CAN frame from a DBC file.
Definition frame_info.h:24
bool isExtended
Definition frame_info.h:27
bool hasPgn
Definition frame_info.h:29
std::uint8_t dlc
Definition frame_info.h:30
std::uint32_t pgn
Definition frame_info.h:28
std::uint32_t canId
Definition frame_info.h:26
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
Describes one signal inside a DBC frame.
Definition signal_info.h:24
std::uint32_t length
Definition signal_info.h:28
std::string unit
Definition signal_info.h:35
std::string name
Definition signal_info.h:26
std::uint32_t startBit
Definition signal_info.h:27