Skip to content

Commit

Permalink
dbcppp now also takes signal multiplexed values into account (#91)
Browse files Browse the repository at this point in the history
* dbcppp now also takes signal multiplexed values into account
  • Loading branch information
xR3b0rn authored Mar 13, 2022
1 parent 3b63e95 commit a33c094
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 14 deletions.
94 changes: 80 additions & 14 deletions src/dbcppp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ int main(int argc, char** argv)
{
options.add_options()
("h,help", "Produce help message")
("bus", "List of buses in format (<bus name, DBC filename>)", cxxopts::value<std::vector<std::string>>());
("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 << "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";
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>>();
Expand All @@ -117,22 +117,29 @@ int main(int argc, char** argv)
std::istringstream ss(opt_bus);
std::string opt;
Bus b;
if (std::getline(ss, opt, ','))
if (std::getline(ss, opt, ':'))
{
b.name = opt;
}
else
{
// TODO error
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
{
// TODO error
std::cout << "error: could parse bus parameter" << std::endl;
return 1;
}
buses.insert(std::make_pair(b.name, std::move(b)));
}
Expand Down Expand Up @@ -181,24 +188,83 @@ int main(int argc, char** argv)
bool first = true;
const auto* mux_sig = msg->MuxSignal();

for (const dbcppp::ISignal& sig : msg->Signals())
{
if (sig.MultiplexerIndicator() != dbcppp::ISignal::EMultiplexer::MuxValue ||
mux_sig && sig.MultiplexerSwitchValue() == mux_sig->Decode(&data[0]))
auto print_signal =
[&data](const dbcppp::ISignal& sig, bool first)
{
if (first) first = false; else std::cout << ", ";
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();
std::cout << sig.Name() << ": '" << iter->Description() << "' " << sig.Unit();
}
else
{
auto val = sig.RawToPhys(raw);
std::cout << sig.Name() << ": " << val << " " << sig.Unit();
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;
}
}
}
Expand All @@ -212,4 +278,4 @@ int main(int argc, char** argv)
print_help();
return 1;
}
}
}
55 changes: 55 additions & 0 deletions tests/test_files/dbc/issue_90.dbc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
VERSION "Created by canmatrix and CSS Electronics (www.csselectronics.com)"


NS_ :
NS_DESC_
CM_
BA_DEF_
BA_
VAL_
CAT_DEF_
CAT_
FILTER
BA_DEF_DEF_
EV_DATA_
ENVVAR_DATA_
SGTYPE_
SGTYPE_VAL_
BA_DEF_SGTYPE_
BA_SGTYPE_
SIG_TYPE_REF_
VAL_TABLE_
SIG_GROUP_
SIG_VALTYPE_
SIGTYPE_VALTYPE_
BO_TX_BU_
BA_DEF_REL_
BA_REL_
BA_DEF_DEF_REL_
BU_SG_REL_
BU_EV_REL_
BU_BO_REL_
SG_MUL_VAL_

BS_:

BU_:


BO_ 0 Msg0: 8 Vector__XXX
SG_ MuxedSig0 m0 : 0|32@1+ (1,0) [0|4294967295] "" Vector__XXX
SG_ MuxedSig1 m1 : 0|32@1+ (1,0) [0|4294967295] "" Vector__XXX
SG_ MuxedSig2 m2 : 0|32@1+ (1,0) [0|65535] "" Vector__XXX
SG_ MuxedSig3 m3 : 0|32@1+ (1,0) [0|65535] "" Vector__XXX
SG_ MuxSig0 m1M : 46|1@1+ (1,0) [0|255] "" Vector__XXX
SG_ MuxSig1 m2M : 46|1@1+ (1,0) [0|255] "" Vector__XXX
SG_ Mux M : 47|1@1+ (1,0) [0|15] "" Vector__XXX
SG_ response : 48|8@1+ (1,0) [0|15] "" Vector__XXX
SG_ length : 56|8@1+ (1,0) [0|255] "" Vector__XXX

SG_MUL_VAL_ 0 MuxedSig0 MuxSig0 0-0;
SG_MUL_VAL_ 0 MuxedSig1 MuxSig0 1-1;
SG_MUL_VAL_ 0 MuxedSig2 MuxSig1 0-0;
SG_MUL_VAL_ 0 MuxedSig3 MuxSig1 1-1;
SG_MUL_VAL_ 0 MuxSig0 Mux 0-0;
SG_MUL_VAL_ 0 MuxSig1 Mux 1-1;

0 comments on commit a33c094

Please sign in to comment.