Skip to content

Commit dd26f71

Browse files
committed
Messages now parsed
Signals still need to be added but messages are now parsed out. i updated the dbc files a little bit to not test other features of dbc in some of the error files. Print function for message was also added to better see error messages when messages don't match. A constructor was added to initialize the message. for now i deleted default to not allow uninitialized variables in message.
1 parent 192104e commit dd26f71

File tree

5 files changed

+74
-12
lines changed

5 files changed

+74
-12
lines changed

include/dbc.hpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,25 @@
66
#include "utils/utils.hpp"
77

88
#include <regex>
9+
#include <iostream>
910

1011
namespace libdbc {
1112

13+
struct Message {
14+
uint32_t id;
15+
std::string name;
16+
uint8_t size;
17+
std::string node;
18+
19+
Message() = delete;
20+
explicit Message(uint32_t id, const std::string& name, uint8_t size, const std::string& node);
21+
22+
virtual bool operator==(const Message& rhs) const;
23+
};
24+
25+
std::ostream& operator<< (std::ostream &out, const Message& msg);
26+
27+
1228
class Parser {
1329
public:
1430
virtual ~Parser() = default;
@@ -30,23 +46,22 @@ namespace libdbc {
3046

3147
std::string get_version() const;
3248
std::vector<std::string> get_nodes() const;
49+
std::vector<libdbc::Message> get_messages() const;
3350

3451
private:
3552
std::string version;
3653
std::vector<std::string> nodes;
54+
std::vector<libdbc::Message> messages;
3755

3856
const std::regex version_re;
3957
const std::regex bit_timing_re;
4058
const std::regex name_space_re;
4159
const std::regex node_re;
60+
const std::regex message_re;
4261

4362
void parse_dbc_header(std::istream& file_stream);
4463
void parse_dbc_nodes(std::istream& file_stream);
45-
46-
};
47-
48-
49-
struct Message {
64+
void parse_dbc_messages(const std::vector<std::string>& lines);
5065

5166
};
5267

src/dbc.cpp

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,47 @@
66

77
namespace libdbc {
88

9+
Message::Message(uint32_t id, const std::string& name, uint8_t size, const std::string& node) :
10+
id(id), name(name), size(size), node(node) {}
11+
12+
bool Message::operator==(const Message& rhs) const {
13+
return (this->id == rhs.id) && (this->name == rhs.name) &&
14+
(this->size == rhs.size) && (this->node == rhs.node);
15+
}
16+
17+
std::ostream& operator<< (std::ostream &out, const Message& msg) {
18+
out << "Message: {id: " << msg.id << ", ";
19+
out << "name: " << msg.name << ", ";
20+
out << "size: " << msg.size << ", ";
21+
out << "node: " << msg.node << "}";
22+
return out;
23+
}
24+
25+
26+
927
DbcParser::DbcParser() : version(""), nodes(),
1028
version_re("^(VERSION)\\s\"(.*)\""), bit_timing_re("^(BS_:)"),
11-
name_space_re("^(NS_)\\s\\:"), node_re("^(BU_:)\\s((?:[\\w]+?\\s?)*)") {
29+
name_space_re("^(NS_)\\s\\:"), node_re("^(BU_:)\\s((?:[\\w]+?\\s?)*)"),
30+
message_re("^(BO_)\\s(\\d+)\\s(\\w+)\\:\\s(\\d+)\\s(\\w+|Vector__XXX)") {
1231

1332
}
1433

1534
void DbcParser::parse_file(const std::string& file) {
1635
std::ifstream s(file.c_str());
1736
std::string line;
37+
std::vector<std::string> lines;
1838

1939
parse_dbc_header(s);
2040

2141
parse_dbc_nodes(s);
2242

2343
while(!s.eof()) {
24-
utils::StreamHandler::get_line( s, line );
44+
utils::StreamHandler::get_next_non_blank_line( s, line );
45+
lines.push_back(line);
2546
}
2647

48+
parse_dbc_messages(lines);
49+
2750
}
2851

2952
std::string DbcParser::get_version() const {
@@ -34,6 +57,10 @@ namespace libdbc {
3457
return nodes;
3558
}
3659

60+
std::vector<libdbc::Message> DbcParser::get_messages() const {
61+
return messages;
62+
}
63+
3764

3865
void DbcParser::parse_dbc_header(std::istream& file_stream) {
3966
std::string line;
@@ -74,6 +101,23 @@ namespace libdbc {
74101

75102
}
76103

104+
void DbcParser::parse_dbc_messages(const std::vector<std::string>& lines) {
105+
std::smatch match;
106+
107+
for(const auto &line : lines) {
108+
if(std::regex_search(line, match, message_re)) {
109+
uint32_t id = std::stoul(match.str(2));
110+
std::string name = match.str(3);
111+
uint8_t size = std::stoul(match.str(4));
112+
std::string node = match.str(5);
113+
114+
Message msg(id, name, size, node);
115+
116+
messages.push_back(msg);
117+
}
118+
}
119+
120+
}
77121

78122

79123
}

test/dbcs/MissingNewSymbols.dbc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,3 @@ NS_ :
2424
BS_:
2525

2626
BU_: DBG DRIVER IO MOTOR SENSOR
27-
28-
BO_ 500 IO_DEBUG: 4 IO
29-
SG_ IO_DEBUG_test_unsigned : 0|8@1+ (1,0) [0|0] "" DBG

test/dbcs/Simple.dbc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ BS_:
3535
BU_: DBG DRIVER IO MOTOR SENSOR
3636

3737
BO_ 500 IO_DEBUG: 4 IO
38-
SG_ IO_DEBUG_test_unsigned : 0|8@1+ (1,0) [0|0] "" DBG
38+
SG_ IO_DEBUG_test_unsigned : 0|8@1+ (1,0) [0|0] "" DBG

test/test_dbc.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ TEST_CASE("Testing dbc file loading error issues", "[fileio][error]") {
1717
REQUIRE_THROWS_AS(parser->parse_file(MISSING_BIT_TIMING_DBC_FILE), libdbc::validity_error);
1818
}
1919

20-
SECTION("Loading a dbc with some missing namespace section tags (_NS :)", "[error]") {
20+
SECTION("Loading a dbc with some missing namespace section tags (NS_ :)", "[error]") {
2121
// Confusion about this type of error. it appears that the header isn't
2222
// very well standardized for now we ignore this type of error.
2323
CHECK_NOTHROW(parser->parse_file(MISSING_NEW_SYMBOLS_DBC_FILE));
@@ -30,11 +30,17 @@ TEST_CASE("Testing dbc file loading", "[fileio]") {
3030
SECTION("Loading a single simple dbc file", "[dbc]") {
3131
std::vector<std::string> nodes = {"DBG", "DRIVER", "IO", "MOTOR", "SENSOR"};
3232

33+
libdbc::Message msg(500, "IO_DEBUG", 4, "IO");
34+
35+
std::vector<libdbc::Message> msgs = {msg};
36+
3337
parser->parse_file(SIMPLE_DBC_FILE);
3438

3539
REQUIRE(parser->get_version() == "1.0.0");
3640

3741
REQUIRE(parser->get_nodes() == nodes);
42+
43+
REQUIRE(parser->get_messages() == msgs);
3844
}
3945

4046
}

0 commit comments

Comments
 (0)