Skip to content

Commit 33425b2

Browse files
committed
Merge branch 'signals'
2 parents dd26f71 + 543385e commit 33425b2

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

include/dbc.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,42 @@
1010

1111
namespace libdbc {
1212

13+
struct Signal {
14+
std::string name;
15+
bool is_multiplexed;
16+
uint32_t start_bit;
17+
uint32_t size;
18+
bool is_bigendian;
19+
bool is_signed;
20+
double factor;
21+
double offset;
22+
double min;
23+
double max;
24+
std::string unit;
25+
std::vector<std::string> receivers;
26+
27+
Signal() = delete;
28+
explicit Signal(std::string name, bool is_multiplexed, uint32_t start_bit, uint32_t size, bool is_bigendian, bool is_signed, double factor, double offset, double min, double max, std::string unit, std::vector<std::string> recievers);
29+
30+
virtual bool operator==(const Signal& rhs) const;
31+
};
32+
1333
struct Message {
1434
uint32_t id;
1535
std::string name;
1636
uint8_t size;
1737
std::string node;
38+
std::vector<Signal> signals;
1839

1940
Message() = delete;
2041
explicit Message(uint32_t id, const std::string& name, uint8_t size, const std::string& node);
2142

2243
virtual bool operator==(const Message& rhs) const;
2344
};
2445

46+
2547
std::ostream& operator<< (std::ostream &out, const Message& msg);
48+
std::ostream& operator<< (std::ostream &out, const Signal& sig);
2649

2750

2851
class Parser {
@@ -58,6 +81,7 @@ namespace libdbc {
5881
const std::regex name_space_re;
5982
const std::regex node_re;
6083
const std::regex message_re;
84+
const std::regex signal_re;
6185

6286
void parse_dbc_header(std::istream& file_stream);
6387
void parse_dbc_nodes(std::istream& file_stream);

src/dbc.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,40 @@ namespace libdbc {
2323
}
2424

2525

26+
Signal::Signal(std::string name, bool is_multiplexed, uint32_t start_bit, uint32_t size, bool is_bigendian, bool is_signed, double factor, double offset, double min, double max, std::string unit, std::vector<std::string> receivers) :
27+
name(name), is_multiplexed(is_multiplexed), start_bit(start_bit), size(size), is_bigendian(is_bigendian), is_signed(is_signed), offset(offset), min(min), max(max), unit(unit), receivers(receivers) {}
28+
29+
bool Signal::operator==(const Signal& rhs) const {
30+
return (this->name == rhs.name) && (this->is_multiplexed == rhs.is_multiplexed) &&
31+
(this->start_bit == rhs.start_bit) && (this->size == rhs.size) &&
32+
(this->is_bigendian == rhs.is_bigendian) && (this->is_signed == rhs.is_signed) &&
33+
(this->offset == rhs.offset) && (this->min == rhs.min) && (this->max == rhs.max) &&
34+
(this->unit == rhs.unit) && (this->receivers == rhs.receivers);
35+
}
36+
37+
38+
std::ostream& operator<< (std::ostream &out, const Signal& sig) {
39+
out << "Signal {name: " << sig.name << ", ";
40+
out << "Multiplexed: " << (sig.is_multiplexed ? "True" : "False") << ", ";
41+
out << "Start bit: " << sig.start_bit << ", ";
42+
out << "Size: " << sig.size << ", ";
43+
out << "Endianness: " << (sig.is_bigendian ? "Big endian" : "Little endian") << ", ";
44+
out << "Value Type: " << (sig.is_signed ? "Signed" : "Unsigned") << ", ";
45+
out << "Min: " << sig.min << ", Max: " << sig.max << ", ";
46+
out << "Unit: (" << sig.unit << "), ";
47+
out << "receivers: ";
48+
for(const auto &r : sig.receivers)
49+
out << r;
50+
return out << "}";
51+
}
52+
2653

2754
DbcParser::DbcParser() : version(""), nodes(),
2855
version_re("^(VERSION)\\s\"(.*)\""), bit_timing_re("^(BS_:)"),
2956
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)") {
57+
message_re("^(BO_)\\s(\\d+)\\s(\\w+)\\:\\s(\\d+)\\s(\\w+|Vector__XXX)"),
58+
// NOTE: No multiplex support yet
59+
signal_re("\\s(SG_)\\s(\\w+)\\s\\:\\s(\\d+)\\|(\\d+)\\@(\\d+)(\\+|\\-)\\s\\((\\d+\\.?(\\d+)?)\\,(\\d+\\.?(\\d+)?)\\)\\s\\[(\\d+\\.?(\\d+)?)\\|(\\d+\\.?(\\d+)?)\\]\\s\"(\\w*)\"\\s([\\w\\,]+|Vector__XXX)*") {
3160

3261
}
3362

@@ -115,6 +144,27 @@ namespace libdbc {
115144

116145
messages.push_back(msg);
117146
}
147+
148+
if(std::regex_search(line, match, signal_re)) {
149+
std::string name = match.str(2);
150+
bool is_multiplexed = false; // No support yet
151+
uint32_t start_bit = std::stoul(match.str(3));
152+
uint32_t size = std::stoul(match.str(4));
153+
bool is_bigendian = (std::stoul(match.str(5)) == 1);
154+
bool is_signed = (match.str(6) == "-");
155+
// Alternate groups because a group is for the decimal portion
156+
double factor = std::stod(match.str(7));
157+
double offset = std::stod(match.str(9));
158+
double min = std::stod(match.str(11));
159+
double max = std::stod(match.str(13));
160+
std::string unit = match.str(15);
161+
162+
std::vector<std::string> receivers;
163+
utils::String::split(match.str(16), receivers, ',');
164+
165+
Signal sig(name, is_multiplexed, start_bit, size, is_bigendian, is_signed, factor, offset, min, max, unit, receivers);
166+
messages.back().signals.push_back(sig);
167+
}
118168
}
119169

120170
}

test/test_dbc.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ TEST_CASE("Testing dbc file loading", "[fileio]") {
3232

3333
libdbc::Message msg(500, "IO_DEBUG", 4, "IO");
3434

35+
std::vector<std::string> receivers{"DBG"};
36+
libdbc::Signal sig("IO_DEBUG_test_unsigned", false, 0, 8, true, false, 1, 0, 0, 0, "", receivers);
37+
msg.signals.push_back(sig);
38+
3539
std::vector<libdbc::Message> msgs = {msg};
3640

3741
parser->parse_file(SIMPLE_DBC_FILE);
@@ -41,6 +45,8 @@ TEST_CASE("Testing dbc file loading", "[fileio]") {
4145
REQUIRE(parser->get_nodes() == nodes);
4246

4347
REQUIRE(parser->get_messages() == msgs);
48+
49+
REQUIRE(parser->get_messages().front().signals == msg.signals);
4450
}
4551

4652
}

0 commit comments

Comments
 (0)