|
10 | 10 |
|
11 | 11 | A CAN-dbc format parser written with Rust's [nom](https://github.com/Geal/nom) parser combinator library. CAN databases are used to exchange details about a CAN network, e.g. what messages are being send over the CAN bus and what data do they contain. |
12 | 12 |
|
13 | | -# 1. Example |
| 13 | +## Usage example |
14 | 14 |
|
15 | 15 | Read dbc file and generate Rust structs based on the messages/signals defined in the dbc. |
16 | 16 |
|
17 | 17 | ```rust,no_run |
18 | | -use can_dbc::DBC; |
| 18 | +use std::fs; |
| 19 | +use can_dbc::Dbc; |
19 | 20 | use codegen::Scope; |
20 | 21 |
|
21 | | -use std::fs::File; |
22 | | -use std::io; |
23 | | -use std::io::prelude::*; |
24 | | -
|
25 | | -fn main() -> io::Result<()> { |
26 | | - let mut f = File::open("./examples/sample.dbc")?; |
27 | | - let mut buffer = Vec::new(); |
28 | | - f.read_to_end(&mut buffer)?; |
29 | | -
|
30 | | - let dbc = can_dbc::DBC::from_slice(&buffer).expect("Failed to parse dbc file"); |
| 22 | +fn main() { |
| 23 | + let data = fs::read_to_string("./examples/sample.dbc").expect("Unable to read input file"); |
| 24 | + let dbc = Dbc::try_from(data.as_str()).expect("Failed to parse dbc file"); |
31 | 25 |
|
32 | 26 | let mut scope = Scope::new(); |
33 | | - for message in dbc.messages() { |
34 | | - for signal in message.signals() { |
35 | | -
|
36 | | - let mut scope = Scope::new(); |
37 | | - let message_struct = scope.new_struct(message.name()); |
38 | | - for signal in message.signals() { |
39 | | - message_struct.field(signal.name().to_lowercase().as_str(), "f64"); |
40 | | - } |
| 27 | + for msg in dbc.messages() { |
| 28 | + let msg_struct = scope.new_struct(msg.name()); |
| 29 | + for signal in msg.signals() { |
| 30 | + msg_struct.field(signal.name().to_lowercase().as_str(), "f64"); |
41 | 31 | } |
42 | 32 | } |
43 | 33 |
|
44 | 34 | println!("{}", scope.to_string()); |
45 | | - Ok(()) |
46 | 35 | } |
47 | 36 | ``` |
48 | 37 |
|
49 | 38 | For a proper implementation for reading or writing CAN frames according to the DBC, I recommend you take a look at [dbc-codegen](https://github.com/technocreatives/dbc-codegen). |
50 | 39 |
|
51 | | -# 2. Example |
| 40 | +## Running Parser Example |
52 | 41 |
|
53 | 42 | The file parser simply parses a dbc input file and prints the parsed content. |
54 | 43 |
|
55 | 44 | ```bash |
56 | | -cargo test && ./target/debug/examples/file_parser -i examples/sample.dbc |
57 | | -``` |
58 | | - |
59 | | -# Installation |
60 | | -can-dbc is available on crates.io and can be included in your Cargo enabled project like this: |
61 | | - |
62 | | -```toml |
63 | | -[dependencies] |
64 | | -can-dbc = "3.0" |
| 45 | +cargo run --example file_parser -- --input examples/sample.dbc |
65 | 46 | ``` |
66 | 47 |
|
67 | 48 | # Structure |
68 | 49 |
|
69 | 50 |  |
70 | 51 |
|
71 | | - |
72 | | -# Implemented DBC parts |
73 | | - |
74 | | -- [x] version |
75 | | -- [x] new_symbols |
76 | | -- [x] bit_timing *(deprecated but mandatory)* |
77 | | -- [x] nodes |
78 | | -- [x] value_tables |
79 | | -- [x] messages |
80 | | -- [x] message_transmitters |
81 | | -- [x] environment_variables |
82 | | -- [x] environment_variables_data |
83 | | -- [x] signal_types |
84 | | -- [x] comments |
85 | | -- [x] attribute_definitions |
86 | | -- [ ] sigtype_attr_list *(format missing documentation)* |
87 | | -- [x] attribute_defaults |
88 | | -- [x] attribute_values |
89 | | -- [x] value_descriptions |
90 | | -- [ ] category_definitions *(deprecated)* |
91 | | -- [ ] categories *(deprecated)* |
92 | | -- [ ] filter *(deprecated)* |
93 | | -- [x] signal_type_refs |
94 | | -- [x] signal_groups |
95 | | -- [x] signal_extended_value_type_list |
96 | | - |
97 | | -# Deviating from standard |
| 52 | +## Implemented DBC parts |
| 53 | + |
| 54 | +- [x] `version` |
| 55 | +- [x] `new_symbols` |
| 56 | +- [x] `bit_timing` *(deprecated but mandatory)* |
| 57 | +- [x] `nodes` |
| 58 | +- [x] `value_tables` |
| 59 | +- [x] `messages` |
| 60 | +- [x] `message_transmitters` |
| 61 | +- [x] `environment_variables` |
| 62 | +- [x] `environment_variables_data` |
| 63 | +- [x] `signal_types` |
| 64 | +- [x] `comments` |
| 65 | +- [x] `attribute_definitions` |
| 66 | +- [ ] `sigtype_attr_list` *(format missing documentation)* |
| 67 | +- [x] `attribute_defaults` |
| 68 | +- [x] `attribute_values` |
| 69 | +- [x] `value_descriptions` |
| 70 | +- [ ] `category_definitions` *(deprecated)* |
| 71 | +- [ ] `categories` *(deprecated)* |
| 72 | +- [ ] `filter` *(deprecated)* |
| 73 | +- [x] `signal_type_refs` |
| 74 | +- [x] `signal_groups` |
| 75 | +- [x] `signal_extended_value_type_list` |
| 76 | + |
| 77 | +### Deviating from standard |
98 | 78 | - multispace between parsers instead of single space allowing e.g. (two spaces) `SIG_GROUP 13`. |
99 | 79 | - `VAL_` suffix may be ` ;` or `;` |
100 | 80 |
|
101 | | -# Alternatives |
| 81 | +## Alternatives |
102 | 82 | - [canparse](https://github.com/jmagnuson/canparse) |
103 | 83 |
|
104 | | -# Credits |
| 84 | +## Credits |
105 | 85 | Test dbcs files were copied from the [cantools](https://github.com/eerimoq/cantools) project. |
106 | 86 |
|
107 | | -# License Checks |
108 | | - |
109 | | -This project uses [cargo-deny](https://github.com/EmbarkStudios/cargo-deny) for checking the licenses of dependencies. To run the check locally run the following: |
110 | | - |
111 | | -```bash |
112 | | -cargo install cargo-deny |
113 | | -cargo deny check |
114 | | -``` |
115 | | - |
116 | | -# Development |
| 87 | +## Development |
117 | 88 | * This project is easier to develop with [just](https://just.systems/man/en/), a modern alternative to `make`. |
118 | 89 | * To get a list of available commands, run `just`. |
119 | 90 | * To run tests, use `just test`. |
120 | 91 | * This project uses [insta](https://insta.rs) for snapshot testing. To update the snapshots run `just bless` |
| 92 | + |
| 93 | +## License |
| 94 | + |
| 95 | +Licensed under either of |
| 96 | + |
| 97 | +* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <https://www.apache.org/licenses/LICENSE-2.0>) |
| 98 | +* MIT license ([LICENSE-MIT](LICENSE-MIT) or <https://opensource.org/licenses/MIT>) |
| 99 | + at your option. |
| 100 | + |
| 101 | +### Contribution |
| 102 | + |
| 103 | +Unless you explicitly state otherwise, any contribution intentionally |
| 104 | +submitted for inclusion in the work by you, as defined in the |
| 105 | +Apache-2.0 license, shall be dual-licensed as above, without any |
| 106 | +additional terms or conditions. |
0 commit comments