diff --git a/src/dbcppp/main.cpp b/src/dbcppp/main.cpp index 4843fe2d..462af2e2 100644 --- a/src/dbcppp/main.cpp +++ b/src/dbcppp/main.cpp @@ -88,7 +88,7 @@ int main(int argc, char** argv) { options.add_options() ("h,help", "Produce help message") - ("bus", "List of buses in format ()", cxxopts::value>()); + ("bus", "List of buses in format <:>", cxxopts::value>()); for (std::size_t i = 1; i < argc - 1; i++) { argv[i] = argv[i + 1]; @@ -96,13 +96,13 @@ int main(int argc, char** argv) auto vm = options.parse(argc, argv); if (vm.count("help")) { - std::cout << "Usage:\ndbcppp decode [--help] --bus=...\n"; + std::cout << "Usage:\ndbcppp decode [--help] --bus=<:>...\n"; std::cout << options.help(); return 1; } if (!vm.count("bus")) { - std::cout << "Argument error: At least one --bus= argument required\n"; + std::cout << "Argument error: At least one --bus=<:> argument required\n"; return 1; } const auto& opt_buses = vm["bus"].as>(); @@ -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))); } @@ -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 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; } } } @@ -212,4 +278,4 @@ int main(int argc, char** argv) print_help(); return 1; } -} \ No newline at end of file +} diff --git a/tests/test_files/dbc/issue_90.dbc b/tests/test_files/dbc/issue_90.dbc new file mode 100644 index 00000000..b8376e58 --- /dev/null +++ b/tests/test_files/dbc/issue_90.dbc @@ -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; \ No newline at end of file