Skip to content

Commit 1876566

Browse files
committed
Update: moved include/ -> include/cpp-can-parser + updated README to include up-to-date content and a section on database analysis
1 parent 449a2d4 commit 1876566

File tree

8 files changed

+72
-29
lines changed

8 files changed

+72
-29
lines changed

README.md

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ This project provides a C++ library that reads and parses [CAN](https://en.wikip
44

55
Currently the library only understand a subset of the DBC file format.
66

7+
**How robust and efficient is this library ?**
8+
9+
C++ CAN Parser is an open-source project based on my work. The library is currently used in a robust
10+
industrial-grade pipeline. The few test files that I gathered in the tests/dbc-files/ directory are
11+
inspired by in-use CAN databases of several automotive industrial companies. So you can safely include
12+
this library into your own pipeline or CAN-based software.
13+
14+
C++ CAN Parser can be useful for building:
15+
* CAN sniffers
16+
* Static file generation (such as the [Auto-Generated Code on the SJ-One Board](http://socialledge.com/sjsu/index.php/DBC_Format#Auto-Generated_Code))
17+
* TCP-to-CAN and CAN-to-TCP middlewares
18+
* ...
19+
20+
`can-parse` is a tool that analyzes a CAN database and proposes different operations. You can use it to inspect all or part of the database. `can-parse printframe CAN_ID` notably give detailed and structured information about the layout of a frame. Check out [the dedicated section](#can-parse) for more info.
21+
722
Table of Contents
823
=================
924

@@ -17,23 +32,13 @@ Table of Contents
1732
- [`CANSignal`](#cansignal)
1833
- [`CANFrame`](#canframe)
1934
- [`CANDatabase`](#candatabase)
35+
- [Database analysis](#database-analysis)
2036
- [can-parse](#can-parse)
2137
- [Supported standards](#supported-standards)
2238

2339
Compile and include the library
2440
===============================
2541

26-
## The public headers
27-
28-
You need to put the public headers in an appropriate place:
29-
30-
```
31-
your_project/
32-
|_ src/
33-
|_ include/
34-
|_ cpp-can-parser/ # <-- Put here the public headers
35-
```
36-
3742
## Compilation
3843

3944
cpp-can-parser is not a header-only library. I recommand CMake to include the library into your project:
@@ -69,15 +74,15 @@ The main feature of the library is the possibility of parsing a file representin
6974

7075
```c++
7176
#include <iostream>
72-
#include "cpp-can-parser/CANDatabase.h"
77+
#include <cpp-can-parser/CANDatabase.h>
7378

7479
int main(int argc, char** argv) {
7580
try {
76-
CANDatabase db = CANDatabase::fromFile(argv[1]);
81+
CppCAN::CANDatabase db = CppCAN::CANDatabase::fromFile(argv[1]);
7782

7883
// ...
7984
}
80-
catch(const CANDatabaseException& e) {
85+
catch(const CppCAN::CANDatabaseException& e) {
8186
std::cerr << "Error: " << e.what();
8287
return 1;
8388
}
@@ -86,10 +91,11 @@ int main(int argc, char** argv) {
8691
}
8792
```
8893
89-
One can see that `CANDatabase::fromFile()` can throw a CANDatabaseException. This happens when the parsing goes wrong (basically when the given file does not exist or when there is a syntax error). `CANDatabaseException::what()` will give you details on the error.
94+
One can see that `CppCAN::CANDatabase::fromFile()` can throw a CANDatabaseException. This happens when the parsing goes wrong (basically when the given file does not exist or when there is a syntax error). `CppCAN::CANDatabaseException::what()` will give you details on the error.
9095
91-
If the data that you are using does not come from a file, it is also possible to use `CANDatabase::fromString("...")` which behaves just like its counterpart.
96+
If the data that you are using does not come from a file, it is also possible to use `CppCAN::CANDatabase::fromString("...")` which behaves just like its counterpart.
9297
98+
*Note that one can construct its own database without parsing a file by diretly manipulating the reevant objects. See the next section for mmore info.*
9399
94100
How to use the database
95101
=======================
@@ -128,17 +134,21 @@ Here are the most important properties of a `CANFrame` instance:
128134
* `comment()` : gives the registered comment (if any)
129135
* more properties to behave like a "standard container"
130136
131-
Use `begin()`/`end()` and!/or a ranged-based for loop to iterate through the signals of the frame.
137+
Use `begin()`/`end()` and/or a ranged-based for loop to iterate through the signals of the frame.
132138
133139
```c++
134-
const CANFrame& frame = ...;
140+
const CppCAN::CANFrame& frame = ...;
135141
136142
// Print the name of all the frames by increasing order
137143
for(const auto& sig : frame) {
138144
std::cout << "Signal: " << sig.second.name() << std::endl;
139145
}
140146
```
141147

148+
You can use `CppCAN::CANFrame::operator[](std::string)` and `CppCAN::CANFrame::at(std::string)` to
149+
access the signals individually. Be aware that they both throw an `std::out_of_range` exception if
150+
the signal does not exist.
151+
142152
## `CANDatabase`
143153

144154
Here are the most important properties of a `CANDatabase` instance:
@@ -149,7 +159,7 @@ Here are the most important properties of a `CANDatabase` instance:
149159
* more properties to behave like a "standard container"
150160

151161
```c++
152-
CANDatabase db = ...;
162+
CppCAN::CANDatabase db = ...;
153163

154164
// Print the name of all the frames by increasing order
155165
size_t i = 0;
@@ -158,6 +168,41 @@ for(const auto& frame : db) {
158168
}
159169
```
160170

171+
Database analysis
172+
================
173+
174+
C++ CAN Parser provides functions to analyze the coherence of a CAN Database. All the functtions are regrouped into the namespace `CppCAN::analysis`. They are notably used by `can-parse` and its *CHECKFRAME* operations.
175+
176+
* `bool CppCAN::analysis::is_frame_layout_ok(const CppCAN::CANFrame&)`: returns true if the layout of the CANFrame is correct. An overload exists which gives a list of all the problematic signals. Check `CANDatabaseAnalysis.h` for more details.
177+
* `void CppCAN::analysis::assert_frame_layout(const CppCAN::CANFrame&)`: same as `is_frame_layout_ok()` but throws a `CANDatabaseException` is the layout is invalid.
178+
179+
You must include `cpp-can-parser/CANDatabaseAnalysis.h` to access these functions.
180+
181+
**Example:**
182+
183+
```c++
184+
#include <cpp-can-parser/CANDatabaseAnalysis.h>
185+
#include <iostream>
186+
187+
CppCAN::CANDatabase db = ...;
188+
189+
// Boolean-based version ...
190+
for(const auto& frame : db) {
191+
if(!CppCAN::analysis::is_frame_layout_ok(frame))
192+
std::cerr << "Frame " << frame.name() << " contains a layout error" << std::endl;
193+
}
194+
195+
// Assert-based version ...
196+
for(const auto& frame : db) {
197+
try {
198+
CppCAN::analysis::assert_frame_layout(frame);
199+
} catch(const CppCAN::CANDatabaseException& e) {
200+
std::cerr << "Frame " << frame.name() << " contains a layout error."
201+
<< "Error details: " << e.what() << std::endl;
202+
}
203+
}
204+
205+
```
161206
can-parse
162207
=========
163208

include/CANDatabase.h renamed to include/cpp-can-parser/CANDatabase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class CPP_CAN_PARSER_EXPORT CANSignal {
114114
* to empty the signals' list.
115115
*
116116
* One can access the CANSignal with at() or operator[]. **Be careful as both will throw
117-
* a CANDatabaseException if the given key does noy match any signal in the frame.** To check
117+
* a std::out_of_range if the given key does noy match any signal in the frame.** To check
118118
* if a signal is present, use contains(). Signals can be found both by their start bit
119119
* or their name.
120120
*

include/CANDatabaseAnalysis.h renamed to include/cpp-can-parser/CANDatabaseAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef CANDATABASE_ANALYSIS_H
22
#define CANDATABASE_ANALYSIS_H
33

4-
#include "CANDatabase.h"
54
#include <vector>
5+
#include "CANDatabase.h"
66
#include "cpp_can_parser_export.h"
77

88
namespace CppCAN {

utils/can-parse/can-parse.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#include "CANDatabase.h"
2-
#include "CANDatabaseAnalysis.h"
1+
#include "cpp-can-parser/CANDatabase.h"
32
#include <iostream>
43
#include <cstring>
54
#include <algorithm>

utils/can-parse/check-frame.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "operations.h"
2-
#include "CANDatabaseAnalysis.h"
3-
#include "CANDatabase.h"
2+
#include "cpp-can-parser/CANDatabaseAnalysis.h"
3+
#include "cpp-can-parser/CANDatabase.h"
44
#include <algorithm>
55
#include <iostream>
66

utils/can-parse/operations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <string>
66
#include <stdexcept>
77
#include <vector>
8-
#include "CANDatabase.h"
8+
#include "cpp-can-parser/CANDatabase.h"
99

1010
namespace CppCAN {
1111
namespace can_parse {

utils/can-parse/print-frame.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "operations.h"
2-
#include "CANDatabaseAnalysis.h"
3-
#include "CANDatabase.h"
2+
#include "cpp-can-parser/CANDatabase.h"
43
#include <algorithm>
54
#include <iomanip>
65
#include <iostream>

utils/can-parse/print-single-frame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "operations.h"
2-
#include "CANDatabase.h"
2+
#include "cpp-can-parser/CANDatabase.h"
33
#include <iostream>
44
#include <iomanip>
55
#include <algorithm>

0 commit comments

Comments
 (0)