Skip to content

Commit 658229d

Browse files
authored
Merge pull request #5 from xatavian/topic-checkframe-all
Added CHECKFRAME operation to can-parse and improved the CppCAN::analysis functions
2 parents af9e509 + cb63e77 commit 658229d

22 files changed

+754
-291
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
bin/*
22
!bin/.gitkeep
3-
.vs
3+
.vs
4+
build/*
5+
.vscode/

CMakeLists.txt

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ cmake_minimum_required(VERSION 3.7.0)
22

33
project(CPP-CAN-Parser)
44

5-
65
include(CTest)
6+
include(GenerateExportHeader)
7+
78

89
set(CPPPARSER_INCLUDE_DIRECTORY
910
${CMAKE_CURRENT_LIST_DIR}/include
@@ -12,7 +13,7 @@ set(CPPPARSER_INCLUDE_DIRECTORY
1213
set(CPPPARSER_SRC_FILES
1314
src/models/CANDatabase.cpp
1415
src/models/CANFrame.cpp
15-
src/models/CANSignal.cpp
16+
src/models/CANSignal.cpp
1617
src/parsing/DBCParser.cpp
1718
src/parsing/ParsingUtils.cpp
1819
src/parsing/Tokenizer.cpp
@@ -22,16 +23,51 @@ set(CPP_CAN_PARSER_COMPILATION_TYPE SHARED)
2223
if(CPP_CAN_PARSER_USE_STATIC)
2324
set(CPP_CAN_PARSER_COMPILATION_TYPE STATIC)
2425
endif()
25-
26-
add_library(cpp-can-parser
26+
27+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS True)
28+
add_library(cpp-can-parser
2729
${CPP_CAN_PARSER_COMPILATION_TYPE}
2830
${CPPPARSER_SRC_FILES})
2931
target_include_directories(cpp-can-parser
30-
PUBLIC ${CPPPARSER_INCLUDE_DIRECTORY})
32+
PUBLIC ${CPPPARSER_INCLUDE_DIRECTORY}
33+
${CMAKE_CURRENT_BINARY_DIR}/exports/)
34+
generate_export_header(cpp-can-parser
35+
BASE_NAME cpp_can_parser
36+
EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/exports/cpp_can_parser_export.h)
37+
install(FILES cpp-can-parser
38+
${CMAKE_CURRENT_BINARY_DIR}/cpp_can_parser_export.h
39+
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/include)
3140

3241
add_executable(can-parse
3342
utils/can-parse/can-parse.cpp
3443
utils/can-parse/print-frame.cpp
3544
utils/can-parse/print-single-frame.cpp
3645
utils/can-parse/check-frame.cpp)
37-
target_link_libraries(can-parse cpp-can-parser)
46+
target_link_libraries(can-parse cpp-can-parser)
47+
48+
if(BUILD_TESTING)
49+
file(COPY tests/dbc-files/
50+
DESTINATION dbc-files/)
51+
52+
add_executable(cpc-test-parsing
53+
tests/test-parsing.cpp)
54+
target_link_libraries(cpc-test-parsing PUBLIC cpp-can-parser)
55+
56+
add_test(NAME cpc-test-parsing
57+
COMMAND cpc-test-parsing)
58+
59+
add_test(NAME cpc-checkframe-1
60+
COMMAND can-parse checkframe dbc-files/single-frame-1.dbc)
61+
62+
add_test(NAME cpc-checkframe-2
63+
COMMAND can-parse checkframe dbc-files/single-frame-2.dbc)
64+
65+
add_test(NAME cpc-checkframe-big-endian-1
66+
COMMAND can-parse checkframe 294 dbc-files/big-endian-1.dbc)
67+
68+
add_test(NAME cpc-checkframe-big-endian-2
69+
COMMAND can-parse checkframe 1807 dbc-files/big-endian-1.dbc)
70+
71+
add_test(NAME cpc-checkframe-big-endian-3
72+
COMMAND can-parse checkframe 1800 dbc-files/big-endian-1.dbc)
73+
endif()

include/CANDatabase.h

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <exception>
77
#include <map>
88

9+
#include "cpp_can_parser_export.h"
910
#include "CANFrame.h"
1011
#include "CANDatabaseException.h"
1112

@@ -23,21 +24,34 @@
2324
* If the database was parsed from a file, the filename() method can be used to
2425
* retrieve the name of the source file.
2526
*/
26-
class CANDatabase {
27+
class CPP_CAN_PARSER_EXPORT CANDatabase {
28+
public:
29+
/**
30+
* @brief A parsing warning and its location
31+
*/
32+
struct parsing_warning {
33+
unsigned long long line;
34+
std::string description;
35+
};
36+
2737
public:
2838
/**
2939
* @brief Parse a CANDatabase from the given source file.
3040
* @param filename Path to the file to parse
41+
* @param warnings (Optional) Filled with all the warnings found during the parsing
3142
* @throw CANDatabaseException if the parsing failed
3243
*/
33-
static CANDatabase fromFile(const std::string& filename);
44+
static CANDatabase fromFile(
45+
const std::string& filename, std::vector<parsing_warning>* warnings = nullptr);
3446

3547
/**
3648
* @brief Construct a CANDatabase object from a database described by src_string
3749
* @param src_string Source string to parse
50+
* @param warnings (Optional) Filled with all the warnings found during the parsing
3851
* @throw CANDatabaseException if the parsing failed
3952
*/
40-
static CANDatabase fromString(const std::string& src_string);
53+
static CANDatabase fromString(
54+
const std::string& src_string, std::vector<parsing_warning>* warnings = nullptr);
4155

4256
public:
4357
struct IDKey {
@@ -60,7 +74,7 @@ class CANDatabase {
6074
/**
6175
* @brief Creates a CANDatabase object with no source file
6276
*/
63-
CANDatabase() = default;
77+
CANDatabase();
6478

6579
/**
6680
* @brief Creates a CANDatabase object that has been constructed from a file
@@ -72,23 +86,24 @@ class CANDatabase {
7286
* Creates a copy of the database: the individual frames are deep copied so there is no
7387
* shared memory betwwen the two databases.
7488
*/
75-
CANDatabase(const CANDatabase&) = default;
89+
CANDatabase(const CANDatabase&);
7690

7791
/**
7892
* @brief Makes a copy of the given database
7993
*/
80-
CANDatabase& operator=(const CANDatabase&) = default;
94+
CANDatabase& operator=(const CANDatabase&);
8195

8296
/**
8397
* @brief Moves a CANDatabase object. The CANFrame objects are NOT deep copied.
8498
*/
85-
CANDatabase(CANDatabase&&) = default;
99+
CANDatabase(CANDatabase&&);
86100

87101
/**
88-
* @see CANDatabase(const CANDatabase&&)
102+
* @see CANDatabase(CANDatabase&&)
89103
*/
90-
CANDatabase& operator=(CANDatabase&&) = default;
104+
CANDatabase& operator=(CANDatabase&&);
91105

106+
~CANDatabase();
92107
public:
93108
/**
94109
* @brief Get the frame with the given frame name
@@ -182,11 +197,8 @@ class CANDatabase {
182197
void removeFrame(const std::string& name);
183198

184199
private:
185-
std::string filename_;
186-
container_type map_; // Index by CAN ID
187-
188-
std::map<unsigned long long, IDKey> intKeyIndex_;
189-
std::map<std::string, IDKey> strKeyIndex_;
200+
class CANDatabaseImpl;
201+
CANDatabaseImpl* impl;
190202
};
191203

192204
#endif

include/CANDatabaseAnalysis.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,24 @@ namespace CppCAN {
77
namespace analysis {
88
/**
99
* @brief Analyses the frame signals to see if some signals are overlapping
10+
* @param src The CANFrame instance to inspect
1011
* @return true if no overlapping is detected, false otherwise
1112
*/
1213
bool is_frame_layout_ok(const CANFrame& src);
1314

15+
/**
16+
* @brief Overload of is_frame_layout_ok() that outputs a diagnosis of the
17+
* problematic signals if a layout error is detected.
18+
* @param src The CANFrame instance to inspect
19+
* @param diagnosis Filled with the names of all the overlapping signals
20+
* @return true if no overlapping is detected, false otherwise
21+
*/
22+
bool is_frame_layout_ok(const CANFrame& src, std::vector<std::string>& diagnosis);
23+
1424
/**
1525
* @brief Like is_frame_layout_ok() but throws a CANDatabaseException if the layout is ill-formed
1626
*/
17-
void assert_frame_layout();
27+
void assert_frame_layout(const CANFrame& src);
1828
}
1929
} // namespace CppCAN
2030

include/DBCParser.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
#include <memory>
88

99
namespace DBCParser {
10-
CANDatabase fromTokenizer(const std::string& name,
11-
Tokenizer& tokenizer);
10+
CANDatabase fromTokenizer(
11+
const std::string& name, Tokenizer& tokenizer,
12+
std::vector<CANDatabase::parsing_warning>* warnings = nullptr);
1213

13-
CANDatabase fromTokenizer(Tokenizer& tokenizer);
14+
CANDatabase fromTokenizer(
15+
Tokenizer& tokenizer,
16+
std::vector<CANDatabase::parsing_warning>* warnings = nullptr);
1417
};
1518

1619
#endif

include/ParsingUtils.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
#include "Tokenizer.h"
55
#include <string>
66
#include <iostream>
7+
#include "CANDatabase.h"
78
#include "CANDatabaseException.h"
89

9-
void throw_error(const std::string& category, const std::string& description,
10-
unsigned long long line);
10+
void throw_error(
11+
const std::string& category, const std::string& description,
12+
unsigned long long line);
1113

12-
void warning(const std::string& description, unsigned long long line) ;
14+
void warning(
15+
std::vector<CANDatabase::parsing_warning>* warnings,
16+
const std::string& description, unsigned long long line);
1317

1418
const Token&
1519
assert_token(Tokenizer& tokenizer, const std::string& token);

include/Tokenizer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class Tokenizer {
3838
bool started;
3939

4040
protected:
41-
unsigned long long charCnt;
42-
unsigned long long lineCnt;
41+
size_t charCnt;
42+
size_t lineCnt;
4343
bool addLine;
4444
};
4545

0 commit comments

Comments
 (0)