DBC framework
main.cpp
Go to the documentation of this file.
1/**
2 * @file main.cpp
3 * @brief
4 *
5 * Created: 2026-03-13
6 * Author: Deeaitch (Dim. Himro)
7 *
8 * Licensed under the MIT License.
9 * See LICENSE file in the project root for full license text.
10 */
11
12#include <iostream>
13#include <iomanip>
14#include <memory>
15#include <vector>
16#include <cstddef>
17
18#include "dbc_parser.h"
19#include "dbc_tree_builder.h"
20#include "dbc_decode_builder.h"
21#include "dbc_decoder.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 (std::size_t index = 0U; index < decoded.signals.size(); ++index) {
84 const DecodedSignalValue &signal = decoded.signals[index];
85
86 if ((signal.definition == nullptr) || !signal.valid)
87 continue;
88
89 std::cout << " " << signal.definition->name
90 << " raw=" << signal.rawValue
91 << " physical=" << signal.physicalValue
92 << " " << signal.definition->unit
93 << "\n";
94 }
95}
96
97int main (int argc, char *argv[]) {
98 if (argc < 2) {
99 std::cerr << "Usage: dbc_demo <file.dbc>\n";
100 return 1;
101 }
102
103 try {
104 DbcParser parser;
105 DbcDatabase database = parser.ParseFile (argv[1]);
106
107 DbcTreeBuilder treeBuilder;
108 std::unique_ptr<TreeNode> root = treeBuilder.Build (database);
109
110 std::cout << "=== Parsed tree ===\n";
111 PrintTree (root.get(), 0);
112
113 DbcDecodeBuilder decodeBuilder;
114 DecodeDatabase decodeDatabase = decodeBuilder.Build (database);
115
116 /*
117 * Example raw frame.
118 * Replace with live CAN frame or trace record later.
119 */
120 RawCanFrame rawFrame;
121 rawFrame.canId = decodeDatabase.frames.empty() ? 0U : decodeDatabase.frames[0].canId;
122 rawFrame.isExtended = decodeDatabase.frames.empty() ? false : decodeDatabase.frames[0].isExtended;
123 rawFrame.data.resize (8U, 0U);
124
125 DbcDecoder decoder;
126 DecodedFrameValue decoded = decoder.Decode (decodeDatabase, rawFrame);
127
128 std::cout << "\n=== Decoded frame ===\n";
129 PrintDecodedFrame (decoded);
130 } catch (const std::exception &ex) {
131 std::cerr << "Error: " << ex.what() << "\n";
132 return 2;
133 }
134
135 return 0;
136}
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:70
DecodedFrameValue Decode(const DecodeDatabase &database, const RawCanFrame &frame) const
Decode one raw CAN frame.
Definition: dbc_decoder.cpp:31
Minimal DBC parser.
Definition: dbc_parser.h:35
DbcDatabase ParseFile(const std::string &filePath) const
Parse DBC file.
Definition: dbc_parser.cpp:40
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:73
const TreeNode * GetChild(std::size_t index) const
Get child by index.
Definition: tree_node.cpp:47
const std::string & GetName() const
Get display name.
Definition: tree_node.cpp:65
std::size_t GetChildCount() const
Get child count.
Definition: tree_node.cpp:43
NodeType GetType() const
Get node type.
Definition: tree_node.cpp:61
const FrameInfo * GetFrame() const
Get frame payload if node is frame.
Definition: tree_node.cpp:69
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)
int main(int argc, char *argv[])
Definition: main.cpp:97
static void PrintDecodedFrame(const DecodedFrameValue &decoded)
Definition: main.cpp:75
static void PrintTree(const TreeNode *node, int indent)
Definition: main.cpp:23
Parsed DBC content stored in a simple internal form.
Definition: dbc_database.h:22
Runtime decode database with fast lookup by CAN ID.
std::vector< DecodeFrame > frames
std::string name
std::string unit
std::string name
Fully decoded frame.
Definition: dbc_decoder.h:55
const DecodeFrame * definition
Definition: dbc_decoder.h:56
std::vector< DecodedSignalValue > signals
Definition: dbc_decoder.h:57
One decoded signal value.
Definition: dbc_decoder.h:38
std::int64_t rawValue
Definition: dbc_decoder.h:40
const DecodeSignal * definition
Definition: dbc_decoder.h:39
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:23
std::vector< std::uint8_t > data
Definition: dbc_decoder.h:26
std::uint32_t canId
Definition: dbc_decoder.h:24
bool isExtended
Definition: dbc_decoder.h:25
Describes one signal inside a DBC frame.
Definition: signal_info.h:22
std::uint32_t length
Definition: signal_info.h:25
std::string unit
Definition: signal_info.h:32
std::uint32_t startBit
Definition: signal_info.h:24