diff --git a/j1939_parser/dbc_parser/db_parser_tests.cpp b/j1939_parser/dbc_parser/db_parser_tests.cpp index 0084872..7339141 100644 --- a/j1939_parser/dbc_parser/db_parser_tests.cpp +++ b/j1939_parser/dbc_parser/db_parser_tests.cpp @@ -194,9 +194,9 @@ TEST_F(DbcParserFileTest, StoreLastMessageIgnoresExactDuplicateCanId) { EXPECT_TRUE(parser.getId("SameMessage")); } -TEST_F(DbcParserFileTest, RemoveDuplicatesLeavesNoGeneralizedPgnEntryWhenNoMessageHasStandardDlc) { - // When none of a PGN group's distinct messages have the "standard" 8-byte DLC, no - // generalized copy is stored under the bare PGN key -- only the CAN-ID-keyed copies. +TEST_F(DbcParserFileTest, RemoveDuplicatesStoresUnmarkedLayoutsBySourceAddress) { + // Distinct layouts and no J1939Default mark: one source-address row each. + // Neither DLC is 8, and DLC would not have chosen a generic row anyway. std::string content = "BO_ 2164129809 StatusMsg_11: 4 ECM\n" " SG_ Sig1 : 0|8@1+ (1,0) [0|255] \"\" Vector__XXX\n\n" @@ -208,9 +208,13 @@ TEST_F(DbcParserFileTest, RemoveDuplicatesLeavesNoGeneralizedPgnEntryWhenNoMessa ASSERT_TRUE(parser.parseFile(path)); parser.removeDuplicates(); parser.generateRunTimeFormat(); - EXPECT_EQ(parser.dbc().find(65024), parser.dbc().end()); - EXPECT_NE(parser.dbc().find(2164129792), parser.dbc().end()); - EXPECT_NE(parser.dbc().find(2164129826), parser.dbc().end()); + EXPECT_EQ(parser.dbc().size(), 2u); + EXPECT_EQ(parser.dbc().find(j1939PgnKey(0xFE00)), parser.dbc().end()); + ASSERT_NE(parser.dbc().find(j1939SaKey(0xFE00, 0x11)), parser.dbc().end()); + ASSERT_NE(parser.dbc().find(j1939SaKey(0xFE00, 0x22)), parser.dbc().end()); + EXPECT_EQ(parser.dbc().at(j1939SaKey(0xFE00, 0x11)).name_, "StatusMsg_11"); + EXPECT_EQ(parser.dbc().at(j1939SaKey(0xFE00, 0x11)).dlc_, 4); + EXPECT_EQ(parser.dbc().at(j1939SaKey(0xFE00, 0x22)).dlc_, 2); } TEST_F(DbcParserFileTest, TopLevelParserHandlesValRecordThroughRealParse) { @@ -341,9 +345,11 @@ TEST_F(DbcParserFileTest, RemoveDuplicatesGeneralizesUniqueMessagePerPgn) { ASSERT_TRUE(parser.parseFile(path)); parser.removeDuplicates(); parser.generateRunTimeFormat(); - // Stored under the bare PGN key, generalized (SA-suffix stripped from the name). - ASSERT_NE(parser.dbc().find(65024), parser.dbc().end()); - EXPECT_EQ(parser.dbc().at(65024).name_, "StatusMsg"); + // One layout: generic PGN key, SA suffix stripped. No source-address row. + EXPECT_EQ(parser.dbc().size(), 1u); + ASSERT_NE(parser.dbc().find(j1939PgnKey(0xFE00)), parser.dbc().end()); + EXPECT_EQ(parser.dbc().at(j1939PgnKey(0xFE00)).name_, "StatusMsg"); + EXPECT_EQ(parser.dbc().find(j1939SaKey(0xFE00, 0x11)), parser.dbc().end()); } TEST_F(DbcParserFileTest, RemoveDuplicatesKeepsDistinctMessagesUnderSamePgn) { @@ -359,14 +365,14 @@ TEST_F(DbcParserFileTest, RemoveDuplicatesKeepsDistinctMessagesUnderSamePgn) { ASSERT_TRUE(parser.parseFile(path)); parser.removeDuplicates(); parser.generateRunTimeFormat(); - // Both kept individually: the first message in a new PGN group is stored via its - // *generalized* (SA-reset) CAN ID (0x80fe0011 -> 0x80fe0000 = 2164129792), while - // later messages added to an existing group keep their real CAN ID as-is. - EXPECT_NE(parser.dbc().find(2164129792), parser.dbc().end()); - EXPECT_NE(parser.dbc().find(2164129826), parser.dbc().end()); - // ...and the DLC-8 one is also generalized under the bare PGN key. - ASSERT_NE(parser.dbc().find(65024), parser.dbc().end()); - EXPECT_EQ(parser.dbc().at(65024).dlc_, 8); + // Both layouts stay under their source address. The DLC-8 message is not a generic row. + EXPECT_EQ(parser.dbc().size(), 2u); + EXPECT_EQ(parser.dbc().find(j1939PgnKey(0xFE00)), parser.dbc().end()); + ASSERT_NE(parser.dbc().find(j1939SaKey(0xFE00, 0x11)), parser.dbc().end()); + ASSERT_NE(parser.dbc().find(j1939SaKey(0xFE00, 0x22)), parser.dbc().end()); + EXPECT_EQ(parser.dbc().at(j1939SaKey(0xFE00, 0x11)).dlc_, 8); + EXPECT_EQ(parser.dbc().at(j1939SaKey(0xFE00, 0x22)).dlc_, 4); + EXPECT_EQ(parser.dbc().at(j1939SaKey(0xFE00, 0x11)).name_, "StatusMsg_11"); } TEST_F(DbcParserFileTest, RemoveDuplicatesDedupsIdenticalMessagesUnderSamePgn) { @@ -382,9 +388,220 @@ TEST_F(DbcParserFileTest, RemoveDuplicatesDedupsIdenticalMessagesUnderSamePgn) { ASSERT_TRUE(parser.parseFile(path)); parser.removeDuplicates(); parser.generateRunTimeFormat(); - // Deduped to a single generalized entry under the bare PGN key. + // Deduped to a single generic row. Priority and source address are not in the key. + EXPECT_EQ(parser.dbc().size(), 1u); + ASSERT_NE(parser.dbc().find(j1939PgnKey(0xFE00)), parser.dbc().end()); + EXPECT_EQ(parser.dbc().at(j1939PgnKey(0xFE00)).name_, "StatusMsg"); +} + +// PGN 0xFE00 is PDU2, so the id is the extended flag, the priority, the PGN, and the source address. +static uint32_t pgnFe00Id(uint32_t priority, uint32_t sa) { + return CAN_EFF_FLAG | ((priority & 7u) << 26) | (0xFE00u << 8) | (sa & 0xFFu); +} + +static std::string fe00Message(uint32_t priority, uint32_t sa, const char *name, const char *signal) { + return "BO_ " + std::to_string(pgnFe00Id(priority, sa)) + " " + name + ": 8 ECM\n" + " SG_ " + std::string(signal) + "\n\n"; +} + +TEST_F(DbcParserFileTest, IdenticalLayoutIsFoundForAnyPriorityAndSource) { + const char *signal = "Sig1 : 0|8@1+ (1,0) [0|255] \"\" Vector__XXX"; + std::string content = fe00Message(6, 0x11, "Status_11", signal) + + fe00Message(3, 0x22, "Status_22", signal); + std::string path = writeTemp(content); + DbcParser parser; + parser.setVerbose(false); + ASSERT_TRUE(parser.parseFile(path)); + parser.removeDuplicates(); + parser.generateRunTimeFormat(); + EXPECT_EQ(parser.dbc().size(), 1u); - ASSERT_NE(parser.dbc().find(65024), parser.dbc().end()); + ASSERT_NE(parser.dbc().find(j1939PgnKey(0xFE00)), parser.dbc().end()); + EXPECT_EQ(parser.dbc().at(j1939PgnKey(0xFE00)).name_, "Status"); + + uint8_t payload[8] = {0x2A, 0, 0, 0, 0, 0, 0, 0}; + J1939_frame frame; + ASSERT_TRUE(frame.setFrom(pgnFe00Id(6, 0x11), payload, 8)); + J1939Parser lookup(frame, parser.dbc()); + ASSERT_NE(lookup.getPgn(), nullptr); + EXPECT_EQ(lookup.getPgn()->name_, "Status"); + auto raw = lookup.signalRawValue("Sig1"); + ASSERT_TRUE(raw); + EXPECT_EQ(*raw, 0x2Au); + + ASSERT_TRUE(frame.setFrom(pgnFe00Id(3, 0x22), payload, 8)); + EXPECT_EQ(lookup.getPgn()->name_, "Status"); + raw = lookup.signalRawValue("Sig1"); + ASSERT_TRUE(raw); + EXPECT_EQ(*raw, 0x2Au); + + ASSERT_TRUE(frame.setFrom(pgnFe00Id(7, 0x33), payload, 8)); + EXPECT_EQ(lookup.getPgn()->name_, "Status"); +} + +TEST_F(DbcParserFileTest, DistinctLayoutsStayWithTheirSourceAddress) { + // Both messages are DLC 8 and the signals differ. A third source address misses. + std::string content = + fe00Message(6, 0x11, "StatusA", "SigA : 0|8@1+ (1,0) [0|255] \"\" Vector__XXX") + + fe00Message(3, 0x22, "StatusB", "SigB : 8|8@1+ (1,0) [0|255] \"\" Vector__XXX"); + std::string path = writeTemp(content); + DbcParser parser; + parser.setVerbose(false); + ASSERT_TRUE(parser.parseFile(path)); + parser.removeDuplicates(); + parser.generateRunTimeFormat(); + + EXPECT_EQ(parser.dbc().size(), 2u); + EXPECT_EQ(parser.dbc().find(j1939PgnKey(0xFE00)), parser.dbc().end()); + EXPECT_EQ(parser.dbc().at(j1939SaKey(0xFE00, 0x11)).name_, "StatusA"); + EXPECT_EQ(parser.dbc().at(j1939SaKey(0xFE00, 0x22)).name_, "StatusB"); + + uint8_t payload[8] = {0x11, 0x22, 0, 0, 0, 0, 0, 0}; + J1939_frame frame; + ASSERT_TRUE(frame.setFrom(pgnFe00Id(6, 0x11), payload, 8)); + J1939Parser lookup(frame, parser.dbc()); + ASSERT_NE(lookup.getPgn(), nullptr); + EXPECT_EQ(lookup.getPgn()->name_, "StatusA"); + auto raw = lookup.signalRawValue("SigA"); + ASSERT_TRUE(raw); + EXPECT_EQ(*raw, 0x11u); + EXPECT_FALSE(lookup.signalRawValue("SigB")); + + ASSERT_TRUE(frame.setFrom(pgnFe00Id(3, 0x22), payload, 8)); + EXPECT_EQ(lookup.getPgn()->name_, "StatusB"); + raw = lookup.signalRawValue("SigB"); + ASSERT_TRUE(raw); + EXPECT_EQ(*raw, 0x22u); + EXPECT_FALSE(lookup.signalRawValue("SigA")); + + ASSERT_TRUE(frame.setFrom(pgnFe00Id(1, 0x33), payload, 8)); + EXPECT_EQ(lookup.getPgn(), nullptr); +} + +TEST_F(DbcParserFileTest, EndiannessAlonePreventsCollapse) { + std::string content = + fe00Message(6, 0x11, "Little_11", "Sig1 : 0|8@1+ (1,0) [0|255] \"\" Vector__XXX") + + fe00Message(0, 0x22, "Big_22", "Sig1 : 0|8@0+ (1,0) [0|255] \"\" Vector__XXX"); + std::string path = writeTemp(content); + DbcParser parser; + parser.setVerbose(false); + ASSERT_TRUE(parser.parseFile(path)); + parser.removeDuplicates(); + parser.generateRunTimeFormat(); + + EXPECT_EQ(parser.dbc().size(), 2u); + EXPECT_EQ(parser.dbc().find(j1939PgnKey(0xFE00)), parser.dbc().end()); + const PGN &little = parser.dbc().at(j1939SaKey(0xFE00, 0x11)); + const PGN &big = parser.dbc().at(j1939SaKey(0xFE00, 0x22)); + EXPECT_EQ(little.name_, "Little_11"); + EXPECT_EQ(big.name_, "Big_22"); + ASSERT_EQ(little.signals_.size(), 1u); + ASSERT_EQ(big.signals_.size(), 1u); + EXPECT_TRUE(little.signals_[0].little_endian_); + EXPECT_FALSE(big.signals_[0].little_endian_); +} + +TEST_F(DbcParserFileTest, MarkedLayoutIsTheGenericRow) { + // The marked message is not first and both DLCs are 8. The generic row follows the mark. + std::string content = + fe00Message(6, 0x11, "Wide_11", "Sig1 : 0|16@1+ (1,0) [0|65535] \"\" Vector__XXX") + + fe00Message(3, 0x22, "Narrow_22", "Sig1 : 0|8@1+ (1,0) [0|255] \"\" Vector__XXX") + + "BA_ \"J1939Default\" BO_ " + std::to_string(pgnFe00Id(3, 0x22)) + " 1;\n"; + std::string path = writeTemp(content); + DbcParser parser; + parser.setVerbose(false); + ASSERT_TRUE(parser.parseFile(path)); + parser.removeDuplicates(); + parser.generateRunTimeFormat(); + + EXPECT_EQ(parser.dbc().size(), 2u); + const PGN &generic = parser.dbc().at(j1939PgnKey(0xFE00)); + EXPECT_EQ(generic.name_, "Narrow"); + ASSERT_EQ(generic.signals_.size(), 1u); + EXPECT_EQ(generic.signals_[0].length_, 8); + EXPECT_EQ(parser.dbc().find(j1939SaKey(0xFE00, 0x22)), parser.dbc().end()); + const PGN &wide = parser.dbc().at(j1939SaKey(0xFE00, 0x11)); + EXPECT_EQ(wide.name_, "Wide_11"); + EXPECT_EQ(wide.signals_[0].length_, 16); + + uint8_t payload[8] = {0x34, 0x12, 0, 0, 0, 0, 0, 0}; + J1939_frame frame; + ASSERT_TRUE(frame.setFrom(pgnFe00Id(0, 0x11), payload, 8)); + J1939Parser lookup(frame, parser.dbc()); + ASSERT_NE(lookup.getPgn(), nullptr); + EXPECT_EQ(lookup.getPgn()->name_, "Wide_11"); + auto raw = lookup.signalRawValue("Sig1"); + ASSERT_TRUE(raw); + EXPECT_EQ(*raw, 0x1234u); + + ASSERT_TRUE(frame.setFrom(pgnFe00Id(6, 0x22), payload, 8)); + ASSERT_NE(lookup.getPgn(), nullptr); + EXPECT_EQ(lookup.getPgn()->name_, "Narrow"); + raw = lookup.signalRawValue("Sig1"); + ASSERT_TRUE(raw); + EXPECT_EQ(*raw, 0x34u); + + ASSERT_TRUE(frame.setFrom(pgnFe00Id(2, 0x44), payload, 8)); + ASSERT_NE(lookup.getPgn(), nullptr); + EXPECT_EQ(lookup.getPgn()->name_, "Narrow"); +} + +TEST_F(DbcParserFileTest, DisagreeingDefaultsLeaveNoGenericRow) { + std::string content = + fe00Message(6, 0x11, "Wide_11", "Sig1 : 0|16@1+ (1,0) [0|65535] \"\" Vector__XXX") + + fe00Message(3, 0x22, "Narrow_22", "Sig1 : 0|8@1+ (1,0) [0|255] \"\" Vector__XXX") + + "BA_ \"J1939Default\" BO_ " + std::to_string(pgnFe00Id(6, 0x11)) + " 1;\n" + "BA_ \"J1939Default\" BO_ " + std::to_string(pgnFe00Id(3, 0x22)) + " 1;\n"; + std::string path = writeTemp(content); + DbcParser parser; + parser.setVerbose(false); + ASSERT_TRUE(parser.parseFile(path)); + parser.removeDuplicates(); + parser.generateRunTimeFormat(); + + EXPECT_EQ(parser.dbc().size(), 2u); + EXPECT_EQ(parser.dbc().find(j1939PgnKey(0xFE00)), parser.dbc().end()); + EXPECT_EQ(parser.dbc().at(j1939SaKey(0xFE00, 0x11)).signals_[0].length_, 16); + EXPECT_EQ(parser.dbc().at(j1939SaKey(0xFE00, 0x22)).signals_[0].length_, 8); + + J1939_frame frame; + ASSERT_TRUE(frame.setFrom(pgnFe00Id(4, 0x33))); + J1939Parser lookup(frame, parser.dbc()); + EXPECT_EQ(lookup.getPgn(), nullptr); +} + +TEST_F(DbcParserFileTest, AgreeingDefaultsShareOneGenericRow) { + // Two marks on the same layout, plus a third layout. The marked layout is generic. + const char *wide = "Sig1 : 0|16@1+ (1,0) [0|65535] \"\" Vector__XXX"; + std::string content = + fe00Message(1, 0x33, "Narrow_33", "Sig1 : 0|8@1+ (1,0) [0|255] \"\" Vector__XXX") + + fe00Message(6, 0x11, "Wide_11", wide) + + fe00Message(3, 0x22, "Wide_22", wide) + + "BA_ \"J1939Default\" BO_ " + std::to_string(pgnFe00Id(6, 0x11)) + " 1;\n" + "BA_ \"J1939Default\" BO_ " + std::to_string(pgnFe00Id(3, 0x22)) + " 1;\n"; + std::string path = writeTemp(content); + DbcParser parser; + parser.setVerbose(false); + ASSERT_TRUE(parser.parseFile(path)); + parser.removeDuplicates(); + parser.generateRunTimeFormat(); + + EXPECT_EQ(parser.dbc().size(), 2u); + EXPECT_EQ(parser.dbc().at(j1939PgnKey(0xFE00)).name_, "Wide"); + EXPECT_EQ(parser.dbc().at(j1939PgnKey(0xFE00)).signals_[0].length_, 16); + EXPECT_EQ(parser.dbc().find(j1939SaKey(0xFE00, 0x11)), parser.dbc().end()); + EXPECT_EQ(parser.dbc().find(j1939SaKey(0xFE00, 0x22)), parser.dbc().end()); + EXPECT_EQ(parser.dbc().at(j1939SaKey(0xFE00, 0x33)).name_, "Narrow_33"); + + uint8_t payload[8] = {0x34, 0x12, 0, 0, 0, 0, 0, 0}; + J1939_frame frame; + ASSERT_TRUE(frame.setFrom(pgnFe00Id(7, 0x44), payload, 8)); + J1939Parser lookup(frame, parser.dbc()); + ASSERT_NE(lookup.getPgn(), nullptr); + EXPECT_EQ(lookup.getPgn()->name_, "Wide"); + auto raw = lookup.signalRawValue("Sig1"); + ASSERT_TRUE(raw); + EXPECT_EQ(*raw, 0x1234u); } // --- Message-level tests --- @@ -522,6 +739,7 @@ TEST(SignalTest, EqualityDetectsEachFieldMismatchIndividually) { EXPECT_FALSE(base == Signal(" SG_ Name : 0|8@1+ (2,0) [0|255] \"\" Vector__XXX\n")); // scalar_ EXPECT_FALSE(base == Signal(" SG_ Name : 0|8@1+ (1,0) [5|255] \"\" Vector__XXX\n")); // min_ EXPECT_FALSE(base == Signal(" SG_ Name : 0|8@1+ (1,0) [0|100] \"\" Vector__XXX\n")); // max_ + EXPECT_FALSE(base == Signal(" SG_ Name : 0|8@0+ (1,0) [0|255] \"\" Vector__XXX\n")); // little_endian_ } TEST(SignalTest, AddValueSkipsExactDuplicateEntry) { diff --git a/j1939_parser/dbc_parser/dbc_parser.cpp b/j1939_parser/dbc_parser/dbc_parser.cpp index 3c59808..137508e 100644 --- a/j1939_parser/dbc_parser/dbc_parser.cpp +++ b/j1939_parser/dbc_parser/dbc_parser.cpp @@ -92,7 +92,10 @@ void DbcParser::storeLastMessage() { } } void DbcParser::removeDuplicates() { - // Group messages by their PGN. + // Extended identity is the PGN, plus the source address only when that PGN has + // more than one layout. Priority is ignored. One layout becomes the generic row. + // Several layouts stay source-address specific, and a generic row is added only + // when the BA_ "J1939Default" messages share a single layout. DLC does not choose it. std::map> pgn_groups; for (const auto & id : extended_ids_) { auto msg = raw_messages_.find(id); @@ -101,50 +104,62 @@ void DbcParser::removeDuplicates() { raw_messages_.erase(msg); continue; } - uint32_t pgn = msg->second.pgn(); - auto it = pgn_groups.find(pgn); - if (it == pgn_groups.end()) { - // Add new item. - std::vector group; - group.push_back(msg->second.generalized()); - pgn_groups.emplace(pgn, group); - } else { - // Compare the new message against all previously stored under the same PGN. - bool found = false; - for (const auto & other: it->second) { - if (other == msg->second) { - found = true; + pgn_groups[msg->second.pgn()].push_back(msg->second); + raw_messages_.erase(msg); + } + for (const auto & group : pgn_groups) { + std::vector> clusters; + for (const Message &message : group.second) { + bool placed = false; + for (auto &cluster : clusters) { + if (cluster.front() == message) { + cluster.push_back(message); + placed = true; break; } } - if (!found) { - // Add message only it was different from any already accumulated ones. - it->second.push_back(msg->second); + if (!placed) { + clusters.push_back(std::vector(1, message)); } } - raw_messages_.erase(msg); - } - // Second run: replace the raw_messages_. - for (auto it : pgn_groups) { - if (it.second.size() == 1) { - assertm(raw_messages_.count(it.first) == 0, "Duplicated PGN record with a different content"); - raw_messages_.emplace(it.first, it.second[0].generalized()); + const uint32_t pgn = group.first; + // clusters.size() means there is no generic row. + size_t generic_index = clusters.size(); + if (clusters.size() == 1) { + generic_index = 0; } else { - // Several messages have the same PGN but different content, for example: DM1_0F and DM01_SC. - // Each such message will be stored using CAN ID as a key but we should also pick one to be stored by PGN. - // Let's use the first message with the standard number of bytes for that. - bool msg_by_pgn_stored = false; - for (const auto & msg : it.second) { - raw_messages_.emplace(msg.canId(), msg); - if (!msg_by_pgn_stored && (msg.dlc() == 8)) { - // Insert a generalized message copy (i.e. without the suffix) - raw_messages_.emplace(msg.pgn(), msg.generalized()); - msg_by_pgn_stored = true; + bool conflict = false; + for (size_t i = 0; i < clusters.size(); ++i) { + for (const auto &message : clusters[i]) { + if (!message.defaultMark()) { + continue; + } + if (generic_index == clusters.size()) { + generic_index = i; + } else if (!(clusters[generic_index].front() == clusters[i].front())) { + conflict = true; + } } } + if (conflict) { + generic_index = clusters.size(); + } + } + if (generic_index != clusters.size()) { + raw_messages_.emplace(j1939PgnKey(pgn), clusters[generic_index].front().generalized()); + } + if (clusters.size() == 1) { + continue; + } + for (size_t i = 0; i < clusters.size(); ++i) { + if (i == generic_index) { + continue; + } + for (const auto &message : clusters[i]) { + raw_messages_.emplace(j1939SaKey(pgn, message.sa()), message); + } } } - pgn_groups.clear(); } void DbcParser::generatePgnOutput(std::ofstream *out) const{ diff --git a/j1939_parser/dbc_parser/dbc_parser.h b/j1939_parser/dbc_parser/dbc_parser.h index d2c1a96..79347da 100644 --- a/j1939_parser/dbc_parser/dbc_parser.h +++ b/j1939_parser/dbc_parser/dbc_parser.h @@ -58,7 +58,8 @@ class DbcParser { // Parses the provided DBC file and stores its content in the raw_messages_ storage bool parseFile(const std::string &dbc_path); - // Removes duplicated messages and updates the raw_messages_ container. + // Collapses extended messages that share a PGN and a layout. The generic row is + // keyed by j1939PgnKey. A layout that is not generic is keyed by j1939SaKey. void removeDuplicates(); void setName(const std::string &name) { name_ = name; } diff --git a/j1939_parser/dbc_parser/dbc_signal.h b/j1939_parser/dbc_parser/dbc_signal.h index 5fcea4d..bc3d18c 100644 --- a/j1939_parser/dbc_parser/dbc_signal.h +++ b/j1939_parser/dbc_parser/dbc_signal.h @@ -115,6 +115,7 @@ class Signal { bool result = start_bit_ == other.start_bit_ && length_ == other.length_ && offset_ == other.offset_ && signed_ == other.signed_ && scalar_ == other.scalar_ && min_ == other.min_ && max_ == other.max_ && + little_endian_ == other.little_endian_ && enum_map_.size() == other.enum_map_.size(); if (result) { // Make sure that both enums have the same values diff --git a/j1939_parser/j1939_frame.h b/j1939_parser/j1939_frame.h index 396ecae..3458c9a 100644 --- a/j1939_parser/j1939_frame.h +++ b/j1939_parser/j1939_frame.h @@ -19,6 +19,17 @@ #define CAN_EFF_FLAG 0x80000000U #endif +// Extended-message keys. Priority and CAN_EFF_FLAG are not identity. +// Bit 31 is the generic row for a PGN. Bit 30 is the row for one source address. +// An 11-bit id stays in 0..0x7FF and is not stored as a PGN. +inline uint32_t j1939PgnKey(uint32_t pgn) { + return CAN_EFF_FLAG | (pgn & 0x3FFFFu); +} + +inline uint32_t j1939SaKey(uint32_t pgn, uint8_t sa) { + return 0x40000000u | ((pgn & 0x3FFFFu) << 8) | sa; +} + // Set rightmost "length" bits to 1 and return given mask. inline uint64_t make_mask(size_t length) { return length == 64 ? 0xFFFFFFFFFFFFFFFF : (1ul << length) - 1ul; diff --git a/j1939_parser/j1939_parser.cpp b/j1939_parser/j1939_parser.cpp index ccfef27..807cf3b 100644 --- a/j1939_parser/j1939_parser.cpp +++ b/j1939_parser/j1939_parser.cpp @@ -69,16 +69,35 @@ bool J1939Parser::toString(std::basic_ostream &out, bool show_unset_bits) } const PGN* J1939Parser::getPgn() const { - uint32_t can_id = frame_.canID(); - - auto it = dbc_.find(can_id); - - if (it != dbc_.end()) { - return & it->second; + // An 11-bit frame is looked up by that id. An extended frame uses the DBC CAN id + // when the map still has it, then the source-address row, then the generic PGN + // row. Priority is not part of those last two keys. + if (!frame_.extended_) { + auto it = dbc_.find(frame_.canID() & 0x7FFu); + if (it != dbc_.end()) { + return &it->second; + } + // A hand-built map may store a non-extended id above 11 bits under that raw id. + // The sliced PGN is not a key. + it = dbc_.find(frame_.canID()); + if (it == dbc_.end()) { + return nullptr; + } + return &it->second; + } + // An undeduplicated map is still keyed by the DBC CAN id. removeDuplicates + // replaces those ids with the PGN and source-address keys below. + auto exact = dbc_.find(frame_.canID()); + if (exact != dbc_.end()) { + return &exact->second; + } + auto specific = dbc_.find(j1939SaKey(frame_.pgn_, frame_.src_)); + if (specific != dbc_.end()) { + return &specific->second; } - it = dbc_.find(frame_.pgn_ ); - if (it != dbc_.end() ) { - return & it->second; + auto generic = dbc_.find(j1939PgnKey(frame_.pgn_)); + if (generic != dbc_.end()) { + return &generic->second; } return nullptr; } diff --git a/j1939_parser/j1939_parser_test.cpp b/j1939_parser/j1939_parser_test.cpp index 54449a6..6d1e2bc 100644 --- a/j1939_parser/j1939_parser_test.cpp +++ b/j1939_parser/j1939_parser_test.cpp @@ -377,17 +377,53 @@ TEST_F(J1939ParserTest, SetValuesRadarBigEndianTest) { EXPECT_EQ(memcmp(expected, frame_.buffer_, frame_.dlc_), 0); } -TEST_F(J1939ParserTest, GetPgnFallsBackToPgnKeyLookup) { - frame_.reset(); - frame_.pgn_ = 1234; // matches a TEST_DBC key directly - frame_.pri_ = 7; - frame_.src_ = 0x55; - frame_.dst_ = 0xFF; - // canID() bakes in pri_/src_/dst_, so it no longer equals the raw PGN value -- - // the first (canID()-keyed) lookup must miss for the PGN-keyed fallback to be - // what actually finds this entry. - ASSERT_NE(frame_.canID(), 1234u); - EXPECT_NE(parser_->getPgn(), nullptr); +TEST(J1939ParserLookupTest, SpecificSourceAddressBeatsGenericRow) { + const std::map dbc{ + {j1939PgnKey(0xFE00), {"Generic", 8, {{"G", 0, 8, false, true, 1, 0, 0, 255, {}}}}}, + {j1939SaKey(0xFE00, 0x11), {"Specific", 8, {{"S", 0, 8, false, true, 1, 0, 0, 255, {}}}}}, + }; + J1939_frame frame; + frame.reset(); + // Priority 6, SA 0x11: the source-address row wins over the generic PGN row. + ASSERT_TRUE(frame.setFrom(0x98FE0011u)); + J1939Parser parser(frame, dbc); + ASSERT_NE(parser.getPgn(), nullptr); + EXPECT_EQ(parser.getPgn()->name_, "Specific"); + + // Same SA at priority 0 still hits that row. + ASSERT_TRUE(frame.setFrom(0x80FE0011u)); + EXPECT_EQ(parser.getPgn()->name_, "Specific"); + + // Priority 3, SA 0x22 has no row of its own, so the generic PGN row is used. + ASSERT_TRUE(frame.setFrom(0x8CFE0022u)); + ASSERT_NE(parser.getPgn(), nullptr); + EXPECT_EQ(parser.getPgn()->name_, "Generic"); +} + +TEST(J1939ParserLookupTest, StandardIdIsNotLookedUpAsPgn) { + const std::map dbc{ + {0x100u, {"Std", 1, {{"A", 0, 8, false, true, 1, 0, 0, 255, {}}}}}, + {j1939PgnKey(0), {"TSC1", 8, {{"B", 0, 8, false, true, 1, 0, 0, 255, {}}}}}, + }; + J1939_frame frame; + frame.reset(); + // 0x100 slices to PGN 0. Lookup must return the 11-bit message. + ASSERT_TRUE(frame.setFrom(0x100u)); + EXPECT_EQ(frame.pgn_, 0u); + J1939Parser parser(frame, dbc); + ASSERT_NE(parser.getPgn(), nullptr); + EXPECT_EQ(parser.getPgn()->name_, "Std"); + + // A frame whose sliced PGN equals the 11-bit id is not that message. + ASSERT_TRUE(frame.setFrom(0x00010000u)); + EXPECT_EQ(frame.pgn_, 0x100u); + EXPECT_FALSE(frame.extended_); + EXPECT_EQ(parser.getPgn(), nullptr); + + // Extended PGN 0, SA 0x2A, finds TSC1 through the generic key. + ASSERT_TRUE(frame.setFrom(0x8000002Au)); + ASSERT_NE(parser.getPgn(), nullptr); + EXPECT_EQ(parser.getPgn()->name_, "TSC1"); } TEST_F(J1939ParserTest, ScanAllSignalsSkipsUnsetSignalsByDefault) { @@ -451,19 +487,19 @@ TEST(J1939ParserPgnForTest, ReturnsEmptySetForUnknownSignal) { EXPECT_TRUE(pgns.empty()); } -TEST_F(J1939ParserTest, ToStringOnExtendedFrameUsesExtendedFormat) { - frame_.reset(); - // Set pgn_ directly rather than via setFrom(): canID() bakes in extended_, so - // setting it after setFrom() would change what canID() computes and break the - // PGN lookup -- setting pgn_ directly makes the PGN-key fallback find it - // regardless of extended_/canID(). - frame_.pgn_ = 1234; - frame_.extended_ = true; - frame_.dlc_ = TEST_DBC.at(1234).dlc_; +TEST(J1939ParserLookupTest, ToStringOnExtendedFrameUsesExtendedFormat) { + const std::map dbc{ + {j1939PgnKey(0xFE00), {"StatusMsg", 8, {}}}, + }; + J1939_frame frame; + frame.reset(); + // Priority 6 and SA 0x22 are not stored in the generic key. + ASSERT_TRUE(frame.setFrom(0x98FE0022u)); + J1939Parser parser(frame, dbc); std::stringstream stream; - parser_->toString(stream); - // The extended-frame branch prints "SA:...DST:...Prio:" instead of "CAN ID:". + EXPECT_TRUE(parser.toString(stream)); + EXPECT_NE(stream.str().find("StatusMsg"), std::string::npos); EXPECT_NE(stream.str().find("SA:"), std::string::npos); EXPECT_NE(stream.str().find("DST:"), std::string::npos); }