forked from xR3b0rn/dbcppp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKCD2Network.cpp
More file actions
414 lines (404 loc) · 13.8 KB
/
KCD2Network.cpp
File metadata and controls
414 lines (404 loc) · 13.8 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <map>
#include <memory>
#include <vector>
#include <optional>
#include <sstream>
#include <cstring>
#include <libxmlmm.h>
#include "../../include/dbcppp/Network.h"
using namespace dbcppp;
class KCDParserError
: public std::runtime_error
{
public:
using base1_t = std::runtime_error;
KCDParserError(const std::string& msg)
: base1_t("libdbcppp: " + msg)
{}
};
class KCD
{
public:
struct Value
{
ISignal::EValueType value_type{ISignal::EValueType::Unsigned};
ISignal::EExtendedValueType extended_value_type{ISignal::EExtendedValueType::Integer};
double factor{1.};
double offset{0.};
std::string unit{""};
double min{0.};
double max{1.};
};
std::map<std::string, std::unique_ptr<INetwork>> _networks;
KCD(std::istream& is)
{
xml::Document doc;
doc.read_from_stream(is);
parseNetworkDefinition(doc.get_root_element());
auto nodes = parseNodes(doc.get_root_element());
_networks = std::move(parseNetworks(doc.get_root_element(), nodes));
}
void
parseNetworkDefinition(const xml::Element* net_def)
{
if (net_def->get_name() != "NetworkDefinition")
{
throw KCDParserError("could not find root node \"NetworkDefinition\"");
}
}
std::vector<std::unique_ptr<INode>>
parseNodes(const xml::Element* net_def)
{
std::vector<std::unique_ptr<INode>> result;
const auto nodes = net_def->find_elements("/*[local-name() = 'NetworkDefinition']/*[local-name() = 'Node']");
for (const auto* node : nodes)
{
auto n = parseNode(node);
result.push_back(std::move(n));
}
return std::move(result);
}
std::unique_ptr<INode>
parseNode(const xml::Element* node)
{
auto name = std::string();
if (node->has_attribute("name"))
{
name = node->get_attribute("name");
}
if (!node->has_attribute("id"))
{
throw KCDParserError("");
}
auto id = node->get_attribute<std::size_t>("id");
_node_id_to_node_name[id] = name;
return INode::Create(std::move(name), "", {});
}
std::unique_ptr<IAttribute>
parseAttribute(const xml::Element* var, IAttributeDefinition::EObjectType object_type)
{
std::string name = var->get_attribute("name");
IAttribute::value_t value = var->get_attribute("Value");
std::stringstream ss;
std::visit([&](auto v) { ss << v; }, value);
{
int64_t i64value;
ss >> i64value;
if (!ss.eof())
{
double dvalue;
ss >> dvalue;
if (ss.eof())
{
value = dvalue;
}
}
else
{
value = i64value;
}
}
return IAttribute::Create(std::move(name), object_type, value);
}
std::map<std::string, std::unique_ptr<INetwork>>
parseNetworks(const xml::Element* net_def, const std::vector<std::unique_ptr<INode>>& nodes)
{
std::map<std::string, std::unique_ptr<INetwork>> networks;
const auto buses = net_def->find_elements("/*[local-name() = 'NetworkDefinition']/*[local-name() = 'Bus']");
for (const auto* bus : buses)
{
networks.insert(parseNetwork(bus, nodes));
}
return std::move(networks);
}
std::pair<std::string, std::unique_ptr<INetwork>>
parseNetwork(const xml::Element* bus, const std::vector<std::unique_ptr<INode>>& nodes)
{
auto name = bus->get_attribute("name");
auto bit_timing = parseBitTiming(bus);
auto msgs = std::vector<std::unique_ptr<IMessage>>();
const auto messages = bus->find_elements("./*[local-name() = 'Message']");
for (const auto* message : messages)
{
auto msg = parseMessage(message);
msgs.push_back(std::move(msg));
}
auto ns = std::vector<std::unique_ptr<INode>>();
for (const auto& node : nodes)
{
ns.push_back(node->Clone());
}
return std::make_pair(
name
, INetwork::Create(
""
, {}
, std::move(bit_timing)
, std::move(ns)
, {}
, std::move(msgs)
, {}, {}, {}, {}, ""));
}
std::unique_ptr<IBitTiming>
parseBitTiming(const xml::Element* bus)
{
uint64_t baudrate = 500000;
if (bus->has_attribute("baudrate"))
{
baudrate = bus->get_attribute<uint64_t>("baudrate");
}
return IBitTiming::Create(baudrate, 0, 0);
}
uint64_t
getMessageSize(const xml::Element* message)
{
uint64_t max = 0;
const auto signals = message->find_elements("./*[local-name() = 'Signal']");
for (const auto* signal : signals)
{
uint64_t offset = signal->get_attribute<uint64_t>("offset");
uint64_t length = 1;
if (signal->has_attribute("length"))
{
length = signal->get_attribute<uint64_t>("length");
}
if (max < offset + length)
{
max = offset + length;
}
}
return (max + 7) / 8;
}
std::unique_ptr<IMessage>
parseMessage(const xml::Element* message)
{
auto str_id = message->get_attribute("id");
auto id = uint64_t();
if (str_id[1] == 'x' || str_id[1] == 'X')
{
std::stringstream ss;
ss << std::hex << str_id;
ss >> id;
}
else
{
std::stringstream ss;
ss << str_id;
ss >> id;
}
auto name = message->get_attribute("name");
auto comment = parseComment(message);
const auto* node_ref = message->find_element("./*[local-name() = 'Producer']/*[local-name() = 'NodeRef']");
auto producer = _node_id_to_node_name.find(node_ref->get_attribute<uint64_t>("id"))->second;
auto message_size = uint64_t(0);
if (!message->has_attribute("length") ||
message->get_attribute("length") == "auto")
{
message_size = getMessageSize(message);
}
else
{
message_size = message->get_attribute<uint64_t>("length");
}
const auto* note = message->find_element("./*[local-name() = 'Notes']");
auto nt = parseComment(note);
auto transmitter = parseTransmitter(message);
auto signals = parseSignals(message, message_size);
return IMessage::Create(
id
, std::move(name)
, message_size
, std::move(producer)
, std::move(transmitter)
, std::move(signals)
, {}
, std::move(comment)
, {});
}
std::string
parseComment(const xml::Element* note)
{
std::string result;
if (note != nullptr)
{
result = note->get_text();
}
return result;
}
std::vector<std::string>
parseTransmitter(const xml::Element* message)
{
std::vector<std::string> result;
const auto node_refs = message->find_elements("./*[local-name() = 'Producer']/*[local-name() = 'NodeRef']");
for (const auto* node_ref : node_refs)
{
auto id = node_ref->get_attribute<std::size_t>("id");
const auto node_name = _node_id_to_node_name.find(id);
if (node_name == _node_id_to_node_name.end())
{
throw KCDParserError("could not find node with ID \"" + std::to_string(id) + "\"");
}
result.push_back(node_name->second);
}
return result;
}
std::vector<std::unique_ptr<ISignal>>
parseSignals(const xml::Element* message, uint64_t message_size)
{
std::vector<std::unique_ptr<ISignal>> sigs;
const auto* multiplex = message->find_element("./*[local-name() = 'Multiplex']");
if (multiplex != nullptr)
{
auto mux_signal = parseSignal(multiplex, message_size, ISignal::EMultiplexer::MuxSwitch, 0);
sigs.push_back(std::move(mux_signal));
const auto mux_groups = multiplex->find_elements("./*[local-name() = 'MuxGroup']");
for (const auto* mux_group : mux_groups)
{
uint64_t multiplex_indicator = mux_group->get_attribute<uint64_t>("count");
const auto signals = mux_group->find_elements("./*[local-name() = 'Signal']");
for (const auto* signal : signals)
{
auto sig = parseSignal(signal, message_size, ISignal::EMultiplexer::MuxValue, multiplex_indicator);
sigs.push_back(std::move(sig));
}
}
}
const auto signals = message->find_elements("./*[local-name() = 'Signal']");
for (const auto* signal : signals)
{
auto sig = parseSignal(signal, message_size, ISignal::EMultiplexer::NoMux, 0);
sigs.push_back(std::move(sig));
}
return std::move(sigs);
}
std::unique_ptr<ISignal>
parseSignal(const xml::Element* signal, uint64_t message_size, ISignal::EMultiplexer mux, uint64_t multiplex_indicator)
{
auto comment = parseComment(signal->find_element("Notes"));
auto receivers = parseReceivers(signal);
auto name = signal->get_attribute("name");
auto start_bit = signal->get_attribute<uint64_t>("offset");
auto bit_size = uint64_t(1);
if (signal->has_attribute("length"))
{
bit_size = signal->get_attribute<uint64_t>("length");
}
auto byte_order = ISignal::EByteOrder::LittleEndian;
auto value_type = ISignal::EValueType::Unsigned;
auto extended_value_type = ISignal::EExtendedValueType::Integer;
auto factor = 1.;
auto offset = 0.;
auto unit = std::string("");
auto min = 0.;
auto max = 1.;
auto value_encoding_descriptions = std::vector<std::unique_ptr<IValueEncodingDescription>>();
if (signal->has_attribute("endianess") &&
signal->get_attribute("endianess") == "big")
{
byte_order = ISignal::EByteOrder::BigEndian;
}
const auto* value = signal->find_element("./*[local-name() = 'Value']");
if (value != nullptr)
{
auto v = parseValue(value);
value_type = v.value_type;
extended_value_type = v.extended_value_type;
factor = v.factor;
offset = v.offset;
unit = v.unit;
min = v.min;
max = v.max;
}
// we are ignoring LabelGroup, it is not supported by the library
const auto label_set = signal->find_elements("./*[local-name() = 'LabelSet']/*[local-name() = 'Label']");
for (const auto* label : label_set)
{
auto value = label->get_attribute<int64_t>("value");
auto name = label->get_attribute("name");
auto ved = IValueEncodingDescription::Create(value, std::move(name));
value_encoding_descriptions.push_back(std::move(ved));
}
return ISignal::Create(
message_size
, std::move(name)
, mux
, multiplex_indicator
, start_bit
, bit_size
, byte_order
, value_type
, factor
, offset
, min
, max
, std::move(unit)
, std::move(receivers)
, {}
, std::move(value_encoding_descriptions)
, std::move(comment)
, extended_value_type
, {});
}
std::vector<std::string>
parseReceivers(const xml::Element* signal)
{
std::vector<std::string> result;
const auto node_refs = signal->find_elements("./*[local-name() = 'Consumer']/*[local-name() = 'NodeRef']");
for (const auto* node_ref : node_refs)
{
auto id = node_ref->get_attribute<std::size_t>("id");
const auto node_name = _node_id_to_node_name.find(id);
result.push_back(node_name->second);
}
return result;
}
Value
parseValue(const xml::Element* value)
{
Value result;
if (value->has_attribute("type"))
{
auto vt = value->get_attribute("type");
if (vt == "signed")
{
result.value_type = ISignal::EValueType::Signed;
}
else if (vt == "single")
{
result.extended_value_type = ISignal::EExtendedValueType::Float;
}
else if (vt == "double")
{
result.extended_value_type = ISignal::EExtendedValueType::Double;
}
}
if (value->has_attribute("slope"))
{
result.factor = value->get_attribute<double>("slope");
}
if (value->has_attribute("intercept"))
{
result.offset = value->get_attribute<double>("intercept");
}
if (value->has_attribute("unit"))
{
result.unit = value->get_attribute("unit");
}
if (value->has_attribute("min"))
{
result.min = value->get_attribute<double>("min");
}
if (value->has_attribute("max"))
{
result.min = value->get_attribute<double>("max");
}
return result;
}
private:
std::map<std::size_t, std::string> _node_id_to_node_name;
};
std::map<std::string, std::unique_ptr<INetwork>> INetwork::LoadKCDFromIs(std::istream& is)
{
KCD kcd(is);
return std::move(kcd._networks);
}