Skip to content

Commit ca34f24

Browse files
committed
add status flag to show which error occured
1 parent 876bab9 commit ca34f24

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

include/libdbc/dbc.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ namespace libdbc {
3434
std::vector<std::string> get_nodes() const;
3535
std::vector<libdbc::Message> get_messages() const;
3636

37-
bool parseMessage(const uint32_t id, const std::vector<uint8_t>& data, std::vector<double>& out_values);
38-
bool parseMessage(const uint32_t id, const std::array<uint8_t, 8>& data, std::vector<double>& out_values);
37+
Message::ParseSignalsStatus parseMessage(const uint32_t id, const std::vector<uint8_t>& data, std::vector<double>& out_values);
38+
Message::ParseSignalsStatus parseMessage(const uint32_t id, const std::array<uint8_t, 8>& data, std::vector<double>& out_values);
3939

4040
void sortSignals();
4141

include/libdbc/message.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ namespace libdbc {
1212
Message() = delete;
1313
explicit Message(uint32_t id, const std::string& name, uint8_t size, const std::string& node);
1414

15+
enum class ParseSignalsStatus {
16+
Success = 0,
17+
ErrorMessageToLong = -1,
18+
ErrorBigEndian = -2,
19+
ErrorUnknownID = -3,
20+
};
21+
1522
/*!
1623
* \brief parseSignals
1724
* \param data
1825
* \param values
1926
* \return
2027
*/
21-
bool parseSignals(const std::vector<uint8_t>& data, std::vector<double> &values) const;
22-
bool parseSignals(const std::array<uint8_t,8>& data, std::vector<double>& values) const;
23-
bool parseSignals(const uint8_t* data, int size, std::vector<double>& values) const;
28+
ParseSignalsStatus parseSignals(const std::vector<uint8_t>& data, std::vector<double> &values) const;
29+
ParseSignalsStatus parseSignals(const std::array<uint8_t,8>& data, std::vector<double>& values) const;
30+
ParseSignalsStatus parseSignals(const uint8_t* data, int size, std::vector<double>& values) const;
2431

2532
void appendSignal(const Signal& signal);
2633
const std::vector<Signal> signals() const;

src/dbc.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,20 @@ namespace libdbc {
9595
return messages;
9696
}
9797

98-
bool DbcParser::parseMessage(const uint32_t id, const std::vector<uint8_t>& data, std::vector<double>& out_values) {
98+
Message::ParseSignalsStatus DbcParser::parseMessage(const uint32_t id, const std::vector<uint8_t>& data, std::vector<double>& out_values) {
9999
for (const auto& message: messages) {
100100
if (message.id() == id)
101101
return message.parseSignals(data, out_values);
102102
}
103-
return false;
103+
return Message::ParseSignalsStatus::ErrorUnknownID;
104104
}
105105

106-
bool DbcParser::parseMessage(const uint32_t id, const std::array<uint8_t, 8>& data, std::vector<double>& out_values) {
106+
Message::ParseSignalsStatus DbcParser::parseMessage(const uint32_t id, const std::array<uint8_t, 8>& data, std::vector<double>& out_values) {
107107
for (const auto& message: messages) {
108108
if (message.id() == id)
109109
return message.parseSignals(data, out_values);
110110
}
111-
return false;
111+
return Message::ParseSignalsStatus::ErrorUnknownID;
112112
}
113113

114114

src/message.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ namespace libdbc {
1212
(m_size == rhs.m_size) && (m_node == rhs.m_node);
1313
}
1414

15-
bool Message::parseSignals(const uint8_t* data, int size, std::vector<double>& values) const {
15+
Message::ParseSignalsStatus Message::parseSignals(const uint8_t* data, int size, std::vector<double>& values) const {
1616
// With the current approach it is not needed to prepare the message by sorting the signals
1717
// if (!m_prepared)
1818
// return false;
1919

2020
if (size > 8)
21-
return false; // not supported yet
21+
return ParseSignalsStatus::ErrorMessageToLong; // not supported yet
2222

2323
// Currently only little endian will be supported, because
2424
// The code below was not tested with bigendian!
2525
// All signals must be little endian
2626
for (const auto& signal: m_signals) {
2727
if (signal.is_bigendian)
28-
return false;
28+
return ParseSignalsStatus::ErrorBigEndian;
2929
}
3030

3131
uint64_t data_little_endian = 0;
@@ -52,14 +52,14 @@ namespace libdbc {
5252
}
5353
values.push_back(v * signal.factor + signal.offset);
5454
}
55-
return true;
55+
return ParseSignalsStatus::Success;
5656
}
5757

58-
bool Message::parseSignals(const std::array<uint8_t,8>& data, std::vector<double>& values) const {
58+
Message::ParseSignalsStatus Message::parseSignals(const std::array<uint8_t,8>& data, std::vector<double>& values) const {
5959
return parseSignals(data.data(), data.size(), values);
6060
}
6161

62-
bool Message::parseSignals(const std::vector<uint8_t> &data, std::vector<double>& values) const {
62+
Message::ParseSignalsStatus Message::parseSignals(const std::vector<uint8_t> &data, std::vector<double>& values) const {
6363
return parseSignals(data.data(), data.size(), values);
6464
}
6565

test/test_dbc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ TEST_CASE("Parse Message little endian") {
170170

171171
std::vector<uint8_t> data{0x08, 0x27, 0xa3, 0x22, 0xe5, 0x1f, 0x45, 0x14}; // little endian
172172
std::vector<double> result_values;
173-
REQUIRE(p.parseMessage(0x21d, data, result_values) == true);
173+
REQUIRE(p.parseMessage(0x21d, data, result_values) == libdbc::Message::ParseSignalsStatus::Success);
174174
REQUIRE(result_values.size() == 4);
175175
REQUIRE(Catch::Approx(result_values.at(0)) == 99.92);
176176
REQUIRE(Catch::Approx(result_values.at(1)) == 88.67);
@@ -191,7 +191,7 @@ TEST_CASE("Parse Message big endian") {
191191

192192
std::vector<uint8_t> data{0x27, 0x08, 0x22, 0xa3, 0x1f, 0xe5, 0x14, 0x45}; // big endian
193193
std::vector<double> result_values;
194-
REQUIRE(p.parseMessage(0x21d, data, result_values) == false);
194+
REQUIRE(p.parseMessage(0x21d, data, result_values) == libdbc::Message::ParseSignalsStatus::ErrorBigEndian);
195195
// Big endian not yet supported
196196
// REQUIRE(result_values.size() == 4);
197197
// REQUIRE(Catch::Approx(result_values.at(0)) == 99.92);

test/test_parseMessage.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ TEST_CASE("Parse Message 1 Big Endian") {
2323

2424
SECTION("Evaluating first message") {
2525
std::vector<double> out_values;
26-
CHECK(parser.parseMessage(234, std::vector<uint8_t>({0x01, 0x02}), out_values) == false);
26+
CHECK(parser.parseMessage(234, std::vector<uint8_t>({0x01, 0x02}), out_values) == libdbc::Message::ParseSignalsStatus::ErrorBigEndian);
2727
// Big endian not supported
2828
// CHECK(out_values.size() == 2);
2929
// CHECK(out_values.at(0) == 0x01 * 0.1 - 3);
@@ -50,7 +50,7 @@ BO_ 123 MSG2: 8 Vector__XXX
5050

5151
SECTION("Evaluating first message") {
5252
std::vector<double> out_values;
53-
CHECK(parser.parseMessage(234, std::vector<uint8_t>({0x01, 0x02}), out_values) == false);
53+
CHECK(parser.parseMessage(234, std::vector<uint8_t>({0x01, 0x02}), out_values) == libdbc::Message::ParseSignalsStatus::ErrorBigEndian);
5454
// Big endian not supported
5555
// std::vector<double> refData{0x01, 0x02};
5656
// CHECK(refData.size() == 2);
@@ -62,7 +62,7 @@ BO_ 123 MSG2: 8 Vector__XXX
6262

6363
SECTION("Evaluating unknown message id") {
6464
std::vector<double> out_values;
65-
CHECK(parser.parseMessage(578, std::vector<uint8_t>({0xFF, 0xA2}), out_values) == false);
65+
CHECK(parser.parseMessage(578, std::vector<uint8_t>({0xFF, 0xA2}), out_values) == libdbc::Message::ParseSignalsStatus::ErrorUnknownID);
6666
}
6767
}
6868

@@ -87,7 +87,7 @@ TEST_CASE("Parse Message Big Number not aligned little endian") {
8787

8888
SECTION("Evaluating first message") {
8989
std::vector<double> out_values;
90-
CHECK(parser.parseMessage(337, std::vector<uint8_t>({0, 4, 252, 19, 0, 0, 0, 0}), out_values) == true);
90+
CHECK(parser.parseMessage(337, std::vector<uint8_t>({0, 4, 252, 19, 0, 0, 0, 0}), out_values) == libdbc::Message::ParseSignalsStatus::Success);
9191
std::vector<double> refData{0, 0, 1, 0, 0, 2, 0};
9292
CHECK(refData.size() == 7);
9393
CHECK(out_values.size() == refData.size());
@@ -98,7 +98,7 @@ TEST_CASE("Parse Message Big Number not aligned little endian") {
9898

9999
SECTION("Evaluating second message") {
100100
std::vector<double> out_values;
101-
CHECK(parser.parseMessage(337, std::vector<uint8_t>({47, 4, 60, 29, 0, 0, 0, 0}), out_values) == true);
101+
CHECK(parser.parseMessage(337, std::vector<uint8_t>({47, 4, 60, 29, 0, 0, 0, 0}), out_values) == libdbc::Message::ParseSignalsStatus::Success);
102102
std::vector<double> refData{47, 0, 1, 0, 32, 3, 0};
103103
CHECK(refData.size() == 7);
104104
CHECK(out_values.size() == refData.size());
@@ -109,7 +109,7 @@ TEST_CASE("Parse Message Big Number not aligned little endian") {
109109

110110
SECTION("Evaluating third message") {
111111
std::vector<double> out_values;
112-
CHECK(parser.parseMessage(337, std::vector<uint8_t>({57, 4, 250, 29, 0, 0, 0, 0}), out_values) == true);
112+
CHECK(parser.parseMessage(337, std::vector<uint8_t>({57, 4, 250, 29, 0, 0, 0, 0}), out_values) == libdbc::Message::ParseSignalsStatus::Success);
113113
std::vector<double> refData{57, 0, 1, 0, 51, 3, 0};
114114
CHECK(refData.size() == 7);
115115
CHECK(out_values.size() == refData.size());

0 commit comments

Comments
 (0)