|
1 | | -# dbc_gen_cpp |
2 | | -DBC CAN Message Type Generator for cpp |
| 1 | +# DBC CAN Message Type Generator for C++ |
| 2 | + |
| 3 | +Extends the code generation capability of `cantools`, which outputs C sources from DBC frame definitions, to create idiomatic C++ interfaces for using CAN message types. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +In `package.xml`, you'll need this as a `build_depend`. |
| 8 | + |
| 9 | +In your `CMakeLists.txt`: |
| 10 | + |
| 11 | +```cmake |
| 12 | +find_package(dbc_gen_cpp REQUIRED) |
| 13 | +... |
| 14 | +
|
| 15 | +generate_dbc_cpp(my_can_library_name |
| 16 | + DBC ${CMAKE_CURRENT_SOURCE_DIR}/database.dbc |
| 17 | +) |
| 18 | +
|
| 19 | +... |
| 20 | +
|
| 21 | +target_link_libraries(my_library PUBLIC my_can_library_name) |
| 22 | +``` |
| 23 | + |
| 24 | +### CAN Messages |
| 25 | + |
| 26 | +Message structs are defined in the library name's namespace. |
| 27 | + |
| 28 | +They can be implicitly converted to `can_frame` from `<linux/can.h>` |
| 29 | + |
| 30 | +```c++ |
| 31 | +#include "my_can_library_name/my_can_library_name.hpp" |
| 32 | + |
| 33 | +... |
| 34 | + |
| 35 | +my_can_library_name::MessageName message{value}; |
| 36 | +my_can_socket->send(message); |
| 37 | +can_frame frame = message; |
| 38 | +my_can_socket->send(frame); |
| 39 | + |
| 40 | +``` |
| 41 | +
|
| 42 | +### CAN Handler - Receive/Subscribe to CAN Messages |
| 43 | +
|
| 44 | +A helper class `dbc_gen_cpp::CANHandler` is provided. |
| 45 | +
|
| 46 | +Simply register a handler function for a type via `set_handler`, then forward all `can_frame`s received to the `handle()` method to trigger the registered handler functions with the typed structs. |
| 47 | +
|
| 48 | +```c++ |
| 49 | +dbc_gen_cpp::CANHandler handler; |
| 50 | +handler.set_handler<my_can_library_name::MessageName>( |
| 51 | + [](const my_can_library_name::MessageName & message) { |
| 52 | + printf('Received MessageName (value %f)\n', message.value); |
| 53 | + }); |
| 54 | +
|
| 55 | +my_can_socket.on_receive( |
| 56 | + [&](can_frame frame) { |
| 57 | + handler.handle(frame); |
| 58 | + }); |
| 59 | +``` |
| 60 | + |
| 61 | +### J1939-Specific Handling |
| 62 | + |
| 63 | +#### **How is J1939 Defined in DBCs** |
| 64 | +DBC files indicate whether a message uses **Standard CAN** or **J1939** via the two lines below. |
| 65 | +The first line defines the attribute itself. The second line is applied **per message** and |
| 66 | +should appear once for each message that is intended to be treated as J1939, using that |
| 67 | +message’s specific CAN ID. |
| 68 | + |
| 69 | +``` |
| 70 | +BA_DEF_ BO_ "VFrameFormat" ENUM "StandardCAN","ExtendedCAN","reserved","J1939PG"; |
| 71 | +BA_ "VFrameFormat" BO_ 2364539904 3; |
| 72 | +``` |
| 73 | + |
| 74 | +#### **J1939 Specific Constants** |
| 75 | +The following will only be defined if a message is labeled as J1939 in the DBC: |
| 76 | +- static constexpr uint32_t Pgn |
| 77 | +- static constexpr uint8_t DefaultPriority = 3; |
| 78 | +- static constexpr uint8_t SourceAddress = 37; |
| 79 | +- static constexpr bool IsPduBroadcast = true; |
| 80 | + |
| 81 | +The following will only be defined if the message is J1939 and it's of type PDU1 (destination specific): |
| 82 | +- static constexpr uint8_t DefaultDestinationAddress |
| 83 | + |
| 84 | +> Note: If you want to safely test if a message is of the J1939 Standard, use the variable `IsJ1939`. |
| 85 | +
|
| 86 | +#### **J1939 Logic Changes** |
| 87 | +1. When passing a can frame into the explicit constructor, it will only check if the PGN of the incoming frame matches, instead of the whole CAN ID. |
| 88 | + |
| 89 | +# Tests for dbc_gen_cpp |
| 90 | + |
| 91 | +Since [`dbc_gen_cpp`](dbc_gen_cpp/) provides mostly functionality via the `install/` space with CMake functions and a Python package with importlib-registered Jinja templates, it's not possible to test the full usage of that package internally. |
| 92 | + |
| 93 | +`test_dbc_gen_cpp` is fully dedicated to providing tests, it is not meant to be used as a dependency by any package. |
0 commit comments