forked from xR3b0rn/dbcppp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork2Human.cpp
More file actions
366 lines (359 loc) · 11.5 KB
/
Network2Human.cpp
File metadata and controls
366 lines (359 loc) · 11.5 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <boost/format.hpp>
#include "../../include/dbcppp/Network2Functions.h"
using namespace dbcppp;
bool bit_is_inbetween(const ISignal& sig, std::size_t i_bit, uint64_t switch_value = -1)
{
if (switch_value != -1 &&
sig.MultiplexerIndicator() == ISignal::EMultiplexer::MuxValue &&
sig.MultiplexerSwitchValue() != switch_value)
{
return false;
}
switch (sig.ByteOrder())
{
case ISignal::EByteOrder::LittleEndian:
{
std::size_t start = sig.StartBit();
std::size_t end = sig.StartBit() + sig.BitSize();
return start <= i_bit && end > i_bit;
}
case ISignal::EByteOrder::BigEndian:
{
std::size_t start = sig.StartBit();
std::size_t n = sig.BitSize();
while (n)
{
if (start == i_bit)
{
return true;
}
if (start % 8 == 0)
{
start += 15;
}
else
{
start--;
}
n--;
}
return true;
}
}
return false;
}
bool is_start_bit(const ISignal& sig, std::size_t i_bit)
{
switch (sig.ByteOrder())
{
case ISignal::EByteOrder::LittleEndian:
return sig.StartBit() == i_bit;
case ISignal::EByteOrder::BigEndian:
break;
}
return false;
}
bool is_end_bit(const ISignal& sig, std::size_t i_bit)
{
switch (sig.ByteOrder())
{
case ISignal::EByteOrder::LittleEndian:
return sig.StartBit() + sig.BitSize() - 1 == i_bit;
case ISignal::EByteOrder::BigEndian:
break;
}
return false;
}
const ISignal* get_mux_signal(const IMessage& msg)
{
auto beg = msg.Signals().begin();
auto end = msg.Signals().end();
auto iter = std::find_if(beg, end,
[&](const ISignal& sig)
{
return sig.MultiplexerIndicator() == ISignal::EMultiplexer::MuxSwitch;
});
return iter != end ? &*iter : nullptr;
}
std::set<uint64_t> get_mux_values(const IMessage& msg)
{
std::set<uint64_t> mux_values;
for (const ISignal& sig : msg.Signals())
{
if (sig.MultiplexerIndicator() == ISignal::EMultiplexer::MuxValue)
{
mux_values.insert(sig.MultiplexerSwitchValue());
}
}
return mux_values;
}
DBCPPP_API std::ostream& dbcppp::Network2Human::operator<<(std::ostream& os, const IMessage& msg)
{
os << boost::format(" %-12s%s\n") % "Name:" % msg.Name();
os << boost::format(" %-12s0x%X\n") % "ID:" % msg.Id();
os << boost::format(" %-12s0x%X\n") % "Length:" % msg.MessageSize();
os << boost::format(" %-12s%s\n") % "Sender:" % msg.Transmitter();
os << boost::format(" Layout:\n");
auto mux_values = get_mux_values(msg);
const ISignal* mux_sig = get_mux_signal(msg);
std::vector<const ISignal*> sigs;
for (const ISignal& sig : msg.Signals())
{
sigs.push_back(&sig);
}
std::size_t num_lines = 3 + msg.MessageSize() * 2 + sigs.size();
std::size_t i = 0;
std::size_t i_bit = 7;
int64_t i_byte_str = int64_t(num_lines) / 2 + 2 - int64_t(num_lines);
auto print_ident =
[&]()
{
os << boost::format(" ");
};
auto print_bar =
[&]()
{
print_ident();
os << boost::format(" +---+---+---+---+---+---+---+---+\n");
};
auto print_signal_header =
[&]()
{
print_ident();
os << boost::format(" Bit\n");
print_ident();
os << boost::format(" 7 6 5 4 3 2 1 0\n");
};
bool print_last_line = false;
auto print_signal =
[&](uint64_t mux_value = -1)
{
print_bar();
print_ident();
os << boost::format("%d |") % (i_bit / 8);
auto find_cur_sig =
[&]()
{
auto beg = msg.Signals().begin();
auto end = msg.Signals().end();
auto iter = std::find_if(beg, end, [&](const ISignal& sig) { return bit_is_inbetween(sig, i_bit, mux_value); });
return iter != end ? &*iter : nullptr;
};
int64_t depth = 0;
for (std::size_t i = 0; i < 8; i++)
{
const auto* sig = find_cur_sig();
if (sig)
{
if (is_start_bit(*sig, i_bit) && is_end_bit(*sig, i_bit))
{
os << boost::format("<-x|");
depth++;
}
else if (is_start_bit(*sig, i_bit))
{
os << boost::format("--x|");
depth++;
}
else if (is_end_bit(*sig, i_bit))
{
os << boost::format("<--");
if (i_bit % 8 == 0)
{
os << boost::format("|");
}
else
{
os << boost::format("-");
}
}
else
{
os << boost::format("---");
if (i_bit % 8 == 0)
{
os << boost::format("|");
}
else
{
os << boost::format("-");
}
}
}
else
{
os << boost::format(" |");
}
i_bit--;
}
os << boost::format("\n");
if (depth)
{
print_last_line = false;
print_ident();
os << boost::format(" +---+---+---+---+---+---+---+---+\n");
}
else
{
print_last_line = true;
}
i_bit += 8;
if (depth)
{
print_ident();
os << boost::format(" ");
std::size_t n_sigs = 1;
for (; 0 < depth; depth--)
{
n_sigs = 0;
for (std::size_t i = 0; i < 8; i++)
{
const auto* sig = find_cur_sig();
if (sig)
{
while (i_bit > sig->StartBit() && i < 8)
{
os << boost::format(" ");
i_bit--;
i++;
}
if (i == 8)
{
continue;
}
if (depth - 1 == n_sigs)
{
os << boost::format(" +-- %s") % sig->Name();
}
else if (n_sigs < std::size_t(depth - 1))
{
os << boost::format(" | ");
}
n_sigs++;
}
else
{
os << boost::format(" ");
}
i_bit--;
}
i_bit += 8;
os << boost::format("\n");
if (depth != 1)
{
print_ident();
os << boost::format(" ");
}
}
}
i_bit += 8;
};
auto print_mux_header =
[&](uint64_t mux_value)
{
print_ident();
os << boost::format(" %s: %d\n") % mux_sig->Name() % mux_value;
};
if (mux_values.size() == 0)
{
print_signal_header();
i_bit = 7;
while (i_bit < msg.MessageSize() * 8)
{
print_signal();
}
if (print_last_line)
{
print_ident();
os << boost::format(" +---+---+---+---+---+---+---+---+\n");
}
}
else
{
for (uint64_t mux_value : mux_values)
{
print_mux_header(mux_value);
print_signal_header();
i_bit = 7;
while (i_bit < msg.MessageSize() * 8)
{
print_signal(mux_value);
}
if (print_last_line)
{
print_ident();
os << boost::format(" +---+---+---+---+---+---+---+---+\n");
}
}
}
os << boost::format("\n");
auto print_signal_tree =
[&]()
{
os << boost::format(" Signal tree:\n\n");
os << boost::format(" -- {root}\n");
if (mux_sig)
{
os << boost::format(" +-- %s\n") % mux_sig->Name();
std::size_t i_mux = mux_values.size();
for (uint64_t mux_value : mux_values)
{
os << boost::format(" | +--%d\n") % mux_value;
for (const ISignal& sig : msg.Signals())
{
if (sig.MultiplexerIndicator() == ISignal::EMultiplexer::MuxValue &&
sig.MultiplexerSwitchValue() == mux_value)
{
if (i_mux > 1)
{
os << boost::format(" | | +-- %s\n") % sig.Name();
}
else
{
os << boost::format(" | +-- %s\n") % sig.Name();
}
}
}
i_mux--;
}
}
for (const ISignal& sig : msg.Signals())
{
if (sig.MultiplexerIndicator() == ISignal::EMultiplexer::NoMux)
{
os << boost::format(" +-- %s\n") % sig.Name();
}
}
};
print_signal_tree();
os << boost::format("\n");
auto print_value_tables =
[&]()
{
os << boost::format(" Signal choices:\n\n");
for (const ISignal& sig : msg.Signals())
{
if (sig.ValueEncodingDescriptions_Size())
{
os << boost::format(" %s\n") % sig.Name();
for (const IValueEncodingDescription& ved : sig.ValueEncodingDescriptions())
{
os << boost::format(" %d %s\n") % ved.Value() % ved.Description();
}
os << boost::format("\n");
}
}
};
print_value_tables();
return os;
}
DBCPPP_API std::ostream& dbcppp::Network2Human::operator<<(std::ostream& os, const INetwork& net)
{
os << boost::format("================================= Messages =================================\n");
for (const IMessage& msg : net.Messages())
{
os << msg;
os << boost::format(" ------------------------------------------------------------------------\n");
}
return os;
}