forked from xR3b0rn/dbcppp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
281 lines (272 loc) · 10.6 KB
/
main.cpp
File metadata and controls
281 lines (272 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <regex>
#include <array>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include <filesystem>
#include <memory>
#include <cxxopts.hpp>
#include "../../include/dbcppp/Network.h"
#include "../../include/dbcppp/Network2Functions.h"
void print_help()
{
std::cout << "dbcppp v1.0.0\nFor help type: dbcppp <subprogram> --help\n"
<< "Sub programs: dbc2, decode\n";
}
int main(int argc, char** argv)
{
cxxopts::Options options("dbcppp", "");
if (argc < 2 || std::string("help") == argv[1])
{
print_help();
return 1;
}
if (std::string("dbc2") == argv[1])
{
options.add_options()
("h,help", "Produce help message")
("f,format", "Output format (C, DBC, human)", cxxopts::value<std::string>())
("dbc", "List of DBC files", cxxopts::value<std::vector<std::string>>());
for (std::size_t i = 1; i < argc - 1; i++)
{
argv[i] = argv[i + 1];
}
auto vm = options.parse(argc - 1, argv);
if (vm.count("help"))
{
std::cout << "Usage:\ndbcppp dbc2c [--help] --format=<format>... --dbc=<dbc filename>...\n";
std::cout << options.help();
return 1;
}
if (!vm.count("format"))
{
std::cout << "Argument error: Argument --format=<format> missing\n";
return 1;
}
if (!vm.count("dbc"))
{
std::cout << "Argument error: At least one --dbc=<dbc> argument required\n";
return 1;
}
const auto& format = vm["format"].as<std::string>();
auto dbcs = vm["dbc"].as<std::vector<std::string>>();
auto net = dbcppp::INetwork::Create({}, {}, dbcppp::IBitTiming::Create(0, 0, 0), {}, {}, {}, {}, {}, {}, {}, {});
for (const auto& dbc : dbcs)
{
auto nets = dbcppp::INetwork::LoadNetworkFromFile(dbc);
for (auto& other : nets)
{
net->Merge(std::move(other.second));
}
}
if (format == "C")
{
using namespace dbcppp::Network2C;
std::cout << *net;
}
else if (format == "DBC")
{
using namespace dbcppp::Network2DBC;
std::cout << *net;
}
else if (format == "human")
{
using namespace dbcppp::Network2Human;
std::cout << *net;
}
}
else if (std::string("decode") == argv[1])
{
options.add_options()
("h,help", "Produce help message")
("bus", "List of buses in format <<bus name>:<DBC filename>>", cxxopts::value<std::vector<std::string>>());
for (std::size_t i = 1; i < argc - 1; i++)
{
argv[i] = argv[i + 1];
}
auto vm = options.parse(argc, argv);
if (vm.count("help"))
{
std::cout << "Usage:\ndbcppp decode [--help] --bus=<<bus name>:<DBC filename>>...\n";
std::cout << options.help();
return 1;
}
if (!vm.count("bus"))
{
std::cout << "Argument error: At least one --bus=<<bus name>:<DBC filename>> argument required\n";
return 1;
}
const auto& opt_buses = vm["bus"].as<std::vector<std::string>>();
struct Bus
{
std::string name;
std::unique_ptr<dbcppp::INetwork> net;
};
std::unordered_map<std::string, Bus> buses;
for (const auto& opt_bus : opt_buses)
{
std::istringstream ss(opt_bus);
std::string opt;
Bus b;
if (std::getline(ss, opt, ':'))
{
b.name = opt;
}
else
{
std::cout << "error: could parse bus parameter" << std::endl;
return 1;
}
if (std::getline(ss, opt))
{
std::ifstream fdbc(opt);
b.net = dbcppp::INetwork::LoadDBCFromIs(fdbc);
if (!b.net)
{
std::cout << "error: could not load DBC '" << opt << "'" << std::endl;
return 1;
}
}
else
{
std::cout << "error: could parse bus parameter" << std::endl;
return 1;
}
buses.insert(std::make_pair(b.name, std::move(b)));
}
// example line: vcan0 123 [3] 11 22 33
std::regex regex_candump_line(
// vcan0
"^\\s*(\\S+)"
// 123
"\\s*([0-9A-F]{3})"
// 3
"\\s*\\[(\\d+)\\]"
// 11
"\\s*([0-9A-F]{2})?"
// 22
"\\s*([0-9A-F]{2})?"
// 33
"\\s*([0-9A-F]{2})?"
// ...
"\\s*([0-9A-F]{2})?"
"\\s*([0-9A-F]{2})?"
"\\s*([0-9A-F]{2})?"
"\\s*([0-9A-F]{2})?"
"\\s*([0-9A-F]{2})?");
std::string line;
while (std::getline(std::cin, line))
{
std::cmatch cm;
std::regex_match(line.c_str(), cm, regex_candump_line);
const auto& bus = buses.find(cm[1].str());
if (bus != buses.end())
{
uint64_t msg_id = std::strtol(cm[2].str().c_str(), nullptr, 16);
uint64_t msg_size = std::atoi(cm[3].str().c_str());
std::array<uint8_t, 8> data;
for (std::size_t i = 0; i < msg_size; i++)
{
data[i] = uint8_t(std::strtol(cm[4 + i].str().c_str(), nullptr, 16));
}
auto beg_msg = bus->second.net->Messages().begin();
auto end_msg = bus->second.net->Messages().end();
auto iter = std::find_if(beg_msg, end_msg, [&](const dbcppp::IMessage& msg) { return msg.Id() == msg_id; });
if (iter != end_msg)
{
const dbcppp::IMessage* msg = &*iter;
std::cout << line << " :: " << msg->Name() << "(";
bool first = true;
const auto* mux_sig = msg->MuxSignal();
auto print_signal =
[&data](const dbcppp::ISignal& sig, bool first)
{
if (!first) std::cout << ", ";
auto raw = sig.Decode(&data[0]);
auto beg_ved = sig.ValueEncodingDescriptions().begin();
auto end_ved = sig.ValueEncodingDescriptions().end();
auto iter = std::find_if(beg_ved, end_ved, [&](const dbcppp::IValueEncodingDescription& ved) { return ved.Value() == raw; });
if (iter != end_ved)
{
std::cout << sig.Name() << ": '" << iter->Description() << "' " << sig.Unit();
}
else
{
auto val = sig.RawToPhys(raw);
std::cout << sig.Name() << ": " << val;
if (sig.Unit().size())
{
std::cout << " " << sig.Unit();
}
}
};
for (const dbcppp::ISignal& sig : msg->Signals())
{
if (sig.MultiplexerIndicator() != dbcppp::ISignal::EMultiplexer::MuxValue)
{
print_signal(sig, first);
first = false;
}
else if (mux_sig && sig.SignalMultiplexerValues_Size() == 0 &&
sig.MultiplexerSwitchValue() == mux_sig->Decode(&data[0]))
{
print_signal(sig, first);
first = false;
}
else
{
std::function<bool(const dbcppp::ISignal&)> check_signal_multiplexer_values;
check_signal_multiplexer_values =
[&](const dbcppp::ISignal& sig)
-> bool
{
for (const auto& smv : sig.SignalMultiplexerValues())
{
auto sig_beg = msg->Signals().begin();
auto sig_end = msg->Signals().end();
auto sig_iter = std::find_if(sig_beg, sig_end,
[&](const auto& sig)
{
return sig.Name() == smv.SwitchName();
});
if (sig_iter != sig_end)
{
for (auto ranges : smv.ValueRanges())
{
auto raw = sig_iter->Decode(&data[0]);
if (ranges.from >= raw && ranges.to <= raw)
{
if (sig_iter->SignalMultiplexerValues_Size() != 0)
{
return check_signal_multiplexer_values(*sig_iter);
}
else
{
return true;
}
}
}
}
}
return false;
};
if (check_signal_multiplexer_values(sig))
{
print_signal(sig, first);
first = false;
}
}
}
std::cout << ")\n";
}
}
}
}
else
{
print_help();
return 1;
}
}