can-api-generator 1.1.0__tar.gz → 2.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/PKG-INFO +278 -20
  2. can_api_generator-2.0.0/README.md +582 -0
  3. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/__about__.py +1 -1
  4. {can_api_generator-1.1.0/tests → can_api_generator-2.0.0/src/can_api_generator}/__init__.py +1 -1
  5. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/code_generator.py +20 -1
  6. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/main.py +36 -1
  7. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/templates/declaration_macros.jinja +3 -0
  8. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/templates/header_template.jinja +6 -6
  9. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/templates/rx_macros.jinja +38 -6
  10. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/templates/source_template.jinja +5 -5
  11. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/templates/tx_macros.jinja +50 -16
  12. can_api_generator-2.0.0/tests/compat_shim.jinja +101 -0
  13. can_api_generator-2.0.0/tests/test_cli.py +680 -0
  14. can_api_generator-2.0.0/tests/test_cross_interface_compat.jinja +79 -0
  15. can_api_generator-2.0.0/tests/test_cross_interface_compat.py +130 -0
  16. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_message_parser.py +2 -0
  17. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_vcan_round_trip.py +2 -0
  18. can_api_generator-1.1.0/README.md +0 -324
  19. can_api_generator-1.1.0/tests/test_cli.py +0 -146
  20. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/.gitignore +0 -0
  21. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/LICENSE.txt +0 -0
  22. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/manual_cag_test.py +0 -0
  23. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/pyproject.toml +0 -0
  24. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/cbdb_generator.py +0 -0
  25. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/msg_processor.py +0 -0
  26. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/templates/canframe.jinja +0 -0
  27. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/templates/dyndec_header.jinja +0 -0
  28. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/templates/dyndec_source.jinja +0 -0
  29. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/templates/encoding_macros.jinja +0 -0
  30. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/types.py +0 -0
  31. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/src/can_api_generator/utils.py +0 -0
  32. {can_api_generator-1.1.0/src/can_api_generator → can_api_generator-2.0.0/tests}/__init__.py +0 -0
  33. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/cantool_test_utils.jinja +0 -0
  34. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/dyndec_test_harness.c +0 -0
  35. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/multiple_busses_test.kcd +0 -0
  36. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_1.kcd +0 -0
  37. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_basic_multiplex.kcd +0 -0
  38. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_big_endian.kcd +0 -0
  39. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_can_db_model_dep.py +0 -0
  40. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_cbdb_generator.py +0 -0
  41. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_dyndec_round_trip.py +0 -0
  42. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_float_wrong_length.kcd +0 -0
  43. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_missing_consumer.kcd +0 -0
  44. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_missing_producer.kcd +0 -0
  45. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_mux_group_count_out_of_order.kcd +0 -0
  46. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_mux_with_labeled_index.kcd +0 -0
  47. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_mux_with_partially_labled_index.kcd +0 -0
  48. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_mux_with_same_signal_names.kcd +0 -0
  49. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_mux_with_same_signal_names_labeled_mux_variants.kcd +0 -0
  50. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_mux_with_scale_and_offset.kcd +0 -0
  51. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_non_encodable_0.kcd +0 -0
  52. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_overlapping_signals.kcd +0 -0
  53. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_periodic_tx_over_can.jinja +0 -0
  54. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_procedures.jinja +0 -0
  55. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_prog_source_template.jinja +0 -0
  56. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_rx_over_can.jinja +0 -0
  57. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_direction.kcd +0 -0
  58. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/double_plain.kcd +0 -0
  59. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/double_scaled.kcd +0 -0
  60. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/double_scaled_offset.kcd +0 -0
  61. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/float_plain.kcd +0 -0
  62. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/float_scaled.kcd +0 -0
  63. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/float_scaled_offset.kcd +0 -0
  64. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/sint_matching_fractional_scaled.kcd +0 -0
  65. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/sint_matching_fractional_scaled_offset.kcd +0 -0
  66. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/sint_matching_plain.kcd +0 -0
  67. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/sint_matching_scaled.kcd +0 -0
  68. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/sint_matching_scaled_offset.kcd +0 -0
  69. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/sint_nonmatching_fractional_scaled.kcd +0 -0
  70. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/sint_nonmatching_plain.kcd +0 -0
  71. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/sint_nonmatching_scaled.kcd +0 -0
  72. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/sint_nonmatching_scaled_offset.kcd +0 -0
  73. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/test_decoding_numerical_value.jinja +0 -0
  74. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/test_encoding_numerical_value.jinja +0 -0
  75. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/test_signal_encoding_wrong_float_size.kcd +0 -0
  76. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/uint_matching_fractional_scaled.kcd +0 -0
  77. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/uint_matching_plain.kcd +0 -0
  78. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/uint_matching_scaled.kcd +0 -0
  79. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/uint_matching_scaled_offset.kcd +0 -0
  80. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/uint_nonmatching_fractional_scaled.kcd +0 -0
  81. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/uint_nonmatching_plain.kcd +0 -0
  82. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/uint_nonmatching_scaled.kcd +0 -0
  83. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_encoding/uint_nonmatching_scaled_offset.kcd +0 -0
  84. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_signal_info.kcd +0 -0
  85. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_too_large_message.kcd +0 -0
  86. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_tx_over_can.jinja +0 -0
  87. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_yaml_cbdb.py +0 -0
  88. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_yaml_input.py +0 -0
  89. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/tests/test_yaml_kcd_equivalence.py +0 -0
  90. {can_api_generator-1.1.0 → can_api_generator-2.0.0}/todo.md +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: can-api-generator
3
- Version: 1.1.0
3
+ Version: 2.0.0
4
4
  Summary: Program to generate a 'signal' style CAN API in C
5
5
  Project-URL: Documentation, https://sr.ht/~laplace/can-api-generator#readme
6
6
  Project-URL: Issues, https://sr.ht/~laplace/can-api-generator/issues
@@ -35,6 +35,7 @@ Description-Content-Type: text/markdown
35
35
  - [Installation](#installation)
36
36
  - [License](#license)
37
37
  - [Overview](#overview)
38
+ - [Two ways to decode a bus](#two-ways-to-decode-a-bus)
38
39
  - [Features of the library](#features-of-the-library)
39
40
  - [Additional features](#additional-features)
40
41
  - [Global RX](#global-rx)
@@ -42,8 +43,17 @@ Description-Content-Type: text/markdown
42
43
  - [Encode All Signals](#encode-all-signals)
43
44
  - [TI C2000 compatibility](#ti-c2000-compatibility)
44
45
  - [Double-buffered signal access](#double-buffered-signal-access)
46
+ - [RX/TX callback context](#rxtx-callback-context)
47
+ - [Message signals as RX callback argument](#message-signals-as-rx-callback-argument)
45
48
  - [Example Use](#example-use)
49
+ - [Dynamic decoding](#dynamic-decoding)
50
+ - [Preparing a database (`cbdb`)](#preparing-a-database-cbdb)
51
+ - [The dynamic-decoder module (`dyndec`)](#the-dynamic-decoder-module-dyndec)
52
+ - [cbdb binary format](#cbdb-binary-format)
46
53
  - [Command-line reference](#command-line-reference)
54
+ - [`api`](#api)
55
+ - [`cbdb`](#cbdb)
56
+ - [`dyndec`](#dyndec)
47
57
 
48
58
  ## Installation
49
59
 
@@ -53,11 +63,12 @@ pip install can-api-generator
53
63
 
54
64
  ## License
55
65
 
56
- `can-api-generator` is distributed under the terms of the [GPL-3.0-only](https://spdx.org/licenses/GPL-3.0-only.html) license.
66
+ `can-api-generator` is distributed under the terms of the [LGPL-3.0-only](https://spdx.org/licenses/LGPL-3.0-only.html) license.
57
67
 
58
68
  ## Overview
59
- The `can-api-generator` is a program to generate a C implementation for a 'signal' style CAN API from a KCD file describing the signals and their arrangement
60
- in to messages sent over the can bus.
69
+ The `can-api-generator` is a program to generate a C implementation for a 'signal' style CAN API from a database describing the signals and their arrangement
70
+ in to messages sent over the can bus. The database may be a [KCD](http://kayak.2codeornot2code.org/) (XML) file or a [`can-db-model`](https://pypi.org/project/can-db-model/)
71
+ YAML file; the two are interchangeable everywhere a `DB_FILE` is accepted.
61
72
  A signal represents a value that is shared among all nodes of the bus. Due to the fact that CAN is based on packets, which are referred
62
73
  to as frames, multiple signals may to be grouped into a common message which is then transmitted as a can frame. Different messages are distinguished via the arbitration id
63
74
  that is part of the can frame.
@@ -77,6 +88,17 @@ arise when multiple nodes modify the same signal.
77
88
 
78
89
  Can frames generated by this library are compatible to the DBC and KCD definitions and can thus be decoded by industry standard tools like busmaster/canoe/PeakCan etc.
79
90
 
91
+ ## Two ways to decode a bus
92
+
93
+ The tool is a group of subcommands (run `can-api-gen --help` to list them). They cover two different decoding strategies:
94
+
95
+ * **Static, per-node API** (`can-api-gen api`) — bakes one node's message set into C at generation time. Every message becomes a struct with `encode`/`decode` functions,
96
+ and the message set is fixed at compile time. This is the smallest and fastest option and is what most firmware wants; it is the subject of the [Features](#features-of-the-library)
97
+ and [Example Use](#example-use) sections below.
98
+ * **Dynamic, database-driven decoder** (`can-api-gen cbdb` + `can-api-gen dyndec`) — generates a *fixed* C module once per firmware that decodes frames at runtime against a
99
+ compiled descriptor blob (a `.cbdb`). Because the message set lives in the blob rather than in the code, a device can be re-provisioned for a different database without
100
+ recompiling. One primary use case for this is bus observers (loggers, gateways, etc.). See [Dynamic decoding](#dynamic-decoding).
101
+
80
102
  # Features of the library
81
103
  ---
82
104
  * One struct per message: The struct contains the signals that are explicitly defined in the can frame (including multiplexes). This allows a single message struct
@@ -99,17 +121,17 @@ This generator may generate additional code that was found useful in many common
99
121
  regardless of schedule or enable state. Useful for publishing critical signal changes without waiting out the rest of the period.
100
122
  * **TI C2000 compatible** codegen (enabled with `--ti-compatible`): hoists local variable declarations to the top of encode/decode functions so the generated C
101
123
  compiles under the TI C2000 compiler's strict C90 rules. See [TI C2000 compatibility](#ti-c2000-compatibility).
102
- * **double-buffered signal access** (enabled with `--double-buffered`): emits two storage slots and front/back pointers per message so that an ISR-side decode
103
- never exposes a partially-written struct to readers, and a periodic tx encoder never reads a struct that user code is mid-update. See
124
+ * **double-buffered signal access** (enabled with `--double-buffered`): emits two storage slots and front/back pointers per message so that an code inside an ISR or
125
+ second thread is never exposed to a partially-decoded struct. A periodic tx encoder also never reads a struct that user code is mid-update. See
104
126
  [Double-buffered signal access](#double-buffered-signal-access).
105
127
 
106
128
  ### Global RX
107
- Global RX generates the code needed to take any received can frame and decode it in to the API if the message matches a definition in the API. This allows the application to
129
+ Global RX generates the code needed to take any received can frame and decode it in to the API if the message matches a message defined in the API. This allows the application to
108
130
  call a single function once per received frame and have access to all signals shared via the CAN bus system by simply reading the signals in the RX struct.
109
131
 
110
132
  The global rx funciton needs to be called with the 'current time'. which for MCUs is most often represented by a count of the milliseconds passed since power up.
111
133
 
112
- The global RX struct also contains one `MsgRxContext` struct per message. This allows to configure parameters and read back metadata about the message.
134
+ The global RX struct contains a `MsgRxContext` struct per message. This struct stores configure parameters and read back metadata about the message.
113
135
  The `MsgRxContext` has the following form:
114
136
  ```c
115
137
  struct MsgRxContext {
@@ -124,6 +146,17 @@ struct MsgRxContext {
124
146
  to simply check if the information in the message has been written to. This is useful when a critical section is to be entered that requires other programs
125
147
  to have set configuration parameters that need to be valid before the critical section is entered.
126
148
  * `last_rx` field stores the value of the 'clock' the last time this message was received. this may be used for diagnostics and statistics
149
+ * `rx_callback` is a function that is called after the corresponding message has been successfully decoded. This function optionally takes a `void *context` that allows
150
+ to parametrize the function with data or point back to a struct containing the global rx struct'. The optional context is set by ``--rx-callback-context`
151
+
152
+ By default (with `-r` alone) global RX is **callback-only**: `RxSignals` holds only a `MsgRxContext` per message (no `_signals` storage), and decoded values are
153
+ delivered solely through the per-message RX callback, which receives a `void *` to a temporary holding the just-decoded signal struct. That temporary is zero-initialized
154
+ before decoding, so a multiplexed message's inactive-group fields read as 0 rather than stack garbage, and the bytes the callback sees match what a store-based API would
155
+ hold for the same frame. A matched frame with no registered callback updates `last_rx` and returns success without decoding. Pass `--rx-signal-store` to generate the
156
+ permanent per-message store instead (with the `valid` field and dot/pointer signal access).
157
+
158
+ In callback-only mode `MsgRxContext` omits `valid` (there is no store to validate). Pass `--with-rx-valid` to re-add and set `valid` on each matched frame, so
159
+ application code can share one `struct MsgRxContext` layout across a store-based API and a callback-only API.
127
160
 
128
161
  ### Periodic TX
129
162
  The periodic TX is the counterpart to the 'global RX'. It takes care to schedule the transmission of periodic frames at the proper time. Periodic frames are pretty
@@ -141,8 +174,8 @@ struct MsgTxContext {
141
174
  uint32_t period;
142
175
  uint32_t offset;
143
176
  uint32_t offset_seed;
144
- void (*tx_callback)(void);
145
- uint16_t send_now; // only present when compiled with --send-now
177
+ void *(*tx_callback)(void); // returns the struct to encode in callback-only mode; +void* context with --tx-callback-context
178
+ uint16_t send_now; // only present when compiled with --send-now
146
179
  };
147
180
  ```
148
181
  * `arb_id` is the arbitration id of the message the context controls. It is seeded by `<api>_periodic_tx_init` so the application can look up a context by id via
@@ -159,12 +192,33 @@ struct MsgTxContext {
159
192
  load. This offset spreads out the transmission of messages through the entire transmission period, reducing pileup of messages.
160
193
  * `offset_seed` is a per-message constant seeded at generation time so that different messages with the same period end up at different phases inside that period without
161
194
  the application having to pick offsets by hand.
162
- * `tx_callback` is a function that is called after the tx frame has been encoded. It allows sending one message after the other (as used for data transfers). Using this
163
- technique it is possible to effectively set data transfer rates. This works well in combination with the enable flag.
195
+ * `tx_callback` is called for a scheduled message **before** the frame is encoded, and its return type is `void *`. In callback-only mode (the default) the pointer it
196
+ returns is the signal struct to encode: return a `struct <Message> *` and that struct is encoded into the outgoing frame. In store mode (`--tx-signal-store`) the
197
+ return value is ignored and the callback instead does a last-minute in-place update of the store before it is encoded.
164
198
  * `send_now` is only emitted when the generator is invoked with `--send-now`. Setting this flag to 1 makes the next `<api>_periodic_tx` call emit the corresponding frame
165
199
  immediately, regardless of the periodic schedule or the `enable` bit. The function auto-clears the flag after firing, so each set fires exactly once. Useful when a
166
200
  critical signal has just changed and you want it on the bus on the very next tick instead of waiting out the remainder of the period.
167
201
 
202
+ By default (with `-p` alone) periodic TX is **callback-only**: `TxSignals` holds only a `MsgTxContext` per message (no `_signals` storage), and each scheduled message's
203
+ `tx_callback` produces the frame. When a message's schedule fires, `<api>_periodic_tx` clears `send_now` and updates `last_tx` (so the slot is consumed and does not
204
+ busy-retry), then calls the callback only if it is registered; it encodes and reports the frame only if the callback returns a non-`NULL` struct pointer. A missing
205
+ callback or a `NULL` return sends nothing for that message and falls through, so another scheduled message whose callback returns data can still be sent on the same call.
206
+ Multiplex variant scheduling is the application's responsibility in this mode (the callback returns whichever variant it wants to send).
207
+
208
+ In plain callback-only mode (no `--msg-as-tx-callback-arg`) the callback returns a pointer to the signal struct to be encoded, so **the application must provide the
209
+ storage that pointer refers to**, and it must outlive the callback return so `<api>_periodic_tx` can encode from it — a `static`/global struct, not a stack local that goes
210
+ out of scope. A single buffer shared by all TX callbacks, cast to the appropriate `struct <Message>` per message, works well and keeps the footprint small, since encode
211
+ calls are serialized (one `<api>_periodic_tx` runs at a time).
212
+
213
+ Passing `--msg-as-tx-callback-arg` removes that burden in callback-only mode: `<api>_periodic_tx` stack-allocates the message struct itself and passes a `void *` to it as
214
+ the callback's first argument (context second, if `--tx-callback-context`). The callback fills that struct and returns the same pointer (or `NULL` to skip). Because the
215
+ generator owns the storage, no application-provided buffer is needed.
216
+
217
+ Pass `--tx-signal-store` to generate the permanent per-message store instead — the pre-callback-only behavior, where the callback edits the store in place (its return
218
+ value ignored), the generator handles multiplex rotation, and (with `--double-buffered`) the per-message `<api>_<msg>_commit` helper is emitted. With the store,
219
+ `--msg-as-tx-callback-arg` passes a `void *` to that message's store struct as the callback's first argument (context second, if `--tx-callback-context`), so the callback
220
+ need not look the struct up before editing it in place.
221
+
168
222
  ### Encode All Signals
169
223
  Some messages use multiplexes combining many signals in to a single message. This may be particularly true for configuration messages that have many related parameters and use
170
224
  multiplexes to fit all parameters in to a single message using many mux groups. If the entire config should be read out and the read back happens periodically, the supervisory controller
@@ -183,9 +237,12 @@ The resulting source compiles cleanly under both `-std=c99` and `-std=c90 -Wdecl
183
237
  C2000 compiler will accept.
184
238
 
185
239
  ### Double-buffered signal access
240
+ Double buffering applies to the **stored** signal interfaces, so it takes effect on the RX side together with `--rx-signal-store` and on the TX side together with
241
+ `--tx-signal-store`; in the callback-only default there is no store to double-buffer and `--double-buffered` has no effect on that side.
242
+
186
243
  When an RX frame is decoded from an ISR while the main loop reads signals (or the converse for TX, where the user updates the struct while `periodic_tx` encodes
187
- it), a reader can observe a half-written struct and act on inconsistent data. Passing `--double-buffered` changes the generated global RX and periodic TX
188
- structs so every message carries two storage slots and two pointers, and the dispatcher / commit helper publishes new values via an atomic pointer swap
244
+ it), a reader can observe a half-written struct and act on inconsistent data. Passing `--double-buffered` (with the relevant store flag) changes the generated global RX
245
+ and periodic TX structs so every message carries two storage slots and two pointers, and the dispatcher / commit helper publishes new values via an atomic pointer swap
189
246
  instead of an in-place update:
190
247
 
191
248
  ```c
@@ -226,6 +283,34 @@ translation unit. This is sufficient for a single-core MCU where the dispatcher
226
283
  case. On weakly-ordered multicore hosts, user code is responsible for inserting the appropriate memory barrier between the decode writes and the pointer
227
284
  publish, and between reading the pointer and dereferencing it — the generator does not emit any barriers itself.
228
285
 
286
+ ### RX/TX callback context
287
+
288
+ Both the [Global RX](#global-rx) and [Periodic TX](#periodic-tx) paths let you register a per-message callback. Without a context flag those callbacks take no arguments
289
+ (`void (*rx_callback)(void)` / `void *(*tx_callback)(void)`), which means a callback that needs to reach application state has to find it through a global.
290
+
291
+ Passing `--rx-callback-context` (and/or `--tx-callback-context`) adds a `void *` context parameter:
292
+
293
+ * `--rx-callback-context` adds a `void *rx_callback_context` field to each `MsgRxContext` and appends a `void *` context parameter to the callback. After a message
294
+ is decoded, its callback is invoked with `rx_callback_context`.
295
+ * `--tx-callback-context` adds a `void *tx_callback_context` field to each `MsgTxContext` and appends a `void *` context parameter to the callback. Before a message's
296
+ frame is encoded, its callback is invoked with `tx_callback_context`.
297
+
298
+ Set the context pointer once (typically to a struct the callback needs, or back to the RX/TX signals struct itself) and each firing threads it through without a global.
299
+ The flags are independent: enable only the side you need. Without the corresponding flag the callback keeps its zero-argument signature.
300
+
301
+ ### Message signals as RX callback argument
302
+
303
+ By default the RX callback receives no reference to the frame that fired it — a callback wanting the decoded values reads them out of the global RX struct by name.
304
+ Passing `--msg-as-rx-callback-arg` instead hands the callback a `void *` pointing at that message's just-decoded signal struct:
305
+
306
+ * `--msg-as-rx-callback-arg` widens the RX callback to `void (*rx_callback)(void *)` and, after a message is decoded, invokes it with a pointer to the message's signal
307
+ struct. The user casts it back, e.g. `struct Message1 *m = arg;`.
308
+ * It composes with `--rx-callback-context`: with both flags the callback becomes `void (*rx_callback)(void *, void *)`, called as
309
+ `rx_callback(<signals>, rx_callback_context)` — signals first, context second.
310
+ * Under `--double-buffered` the pointer is the live front buffer after the pointer swap, so the callback always sees a fully-decoded struct.
311
+
312
+ `struct MsgRxContext` remains a single shared type, which is why the signal struct is passed as `void *` rather than a typed pointer.
313
+
229
314
  ## Example Use
230
315
 
231
316
  As an example the following KCD definition of all messages on a CAN bus is converted:
@@ -268,9 +353,10 @@ The result of the conversion are the decode/encode functions along with the stru
268
353
  To convert the KCD file into C code the following command is used:
269
354
 
270
355
  ```bash
271
- can-api-gen example.kcd my_api DUT
356
+ can-api-gen api example.kcd my_api DUT -a
272
357
  ```
273
- The first argument is the path to the kcd file. The second argument is the name of the API. This name determins the name of the C and H file headers as well as parts of the name of the struct and functions.
358
+ `api` selects the static per-node generator (see [Two ways to decode a bus](#two-ways-to-decode-a-bus)). The first argument is the path to the database file (`.kcd` or `.yaml`).
359
+ The second argument is the name of the API. This name determins the name of the C and H file headers as well as parts of the name of the struct and functions.
274
360
  This is done so that multiple APIs may be used within the same project without interfering with each other. The last entry is the name of the Device for which the can api is to be generated.
275
361
  This is needed as multiple devices may be described on a single can Bus. The KCD file then needs to describe what signal is sent by a device, as well as which signal a device is listening for.
276
362
  The interface generator uses this info to only generate messages that are relevant to the particular device. The following is the resulting header:
@@ -314,13 +400,133 @@ To enable the additional features, pass the corresponding flags at generation ti
314
400
  ```bash
315
401
  # Generate a full-featured API with global rx, periodic tx, encode-every-signal,
316
402
  # send-now support, and TI C2000 compatible codegen.
317
- can-api-gen example.kcd my_api DUT -r -p -a --send-now --ti-compatible
403
+ can-api-gen api example.kcd my_api DUT -r -p -a --send-now --ti-compatible
404
+ ```
405
+
406
+ ## Dynamic decoding
407
+
408
+ The [static `api`](#example-use) generator bakes one node's messages into C at build time. That is ideal for a node deployed into a 'fixed' environment like a car or industrial
409
+ system. Development and Diagnostic Tools however need to be adaptable to many environments on the fly. Dynamic decode allows for the firmware to be permanently on device while the
410
+ a runtime loadable database allows for adaptation of the tool to the environment it is supposed to be used in. As the tool is likely used in conjuntion with a HMI or other equipment
411
+ that equipment can then provision the right database on the device. The database generation step also generates a sidecar json file that can be consumed by upstream tools to enrich the
412
+ decoded data. Adaptability however, trades runtime overhead for this flexibility. Dynamic decoding currently simply decodes everything it receives over the bus. Modern MCUs are plenty fast
413
+ enough for this. In the case of a logger/debugger/monitor it is also more of a feature.
414
+
415
+ There are two subcommands, used together:
416
+
417
+ 1. **`cbdb`** — the *database preparation* step. Compiles a `.kcd`/`.yaml` database into a compact binary descriptor table (a `.cbdb`) plus a JSON sidecar for host-side clients.
418
+ 2. **`dyndec`** — the *runtime module*. Generates a fixed, database-independent C module that decodes frames against a `.cbdb` at runtime. Generated once per firmware;
419
+ a new database is just a new `.cbdb`, no recompile.
420
+
421
+ ### Preparing a database (`cbdb`)
422
+
423
+ ```bash
424
+ can-api-gen cbdb example.kcd my_bus
425
+ ```
426
+
427
+ This reads the database and writes two files:
428
+
429
+ ```console
430
+ Wrote my_bus.cbdb (124 bytes, 3 signals) and my_bus_ids.json
431
+ ```
432
+
433
+ * **`my_bus.cbdb`** — the binary descriptor table (see [cbdb binary format](#cbdb-binary-format)) that the on-device `dyndec` runtime consumes. This is the artifact you
434
+ ship to the device: flash it, store it on a filesystem, or push it over the air. Provisioning a device for a new bus is nothing more than replacing this file.
435
+ * **`my_bus_ids.json`** — a sidecar for host-side / client code that never parses the binary blob. It maps each resolved `message.signal` name to the numeric `signal_id`
436
+ the firmware reports, and additionally records per-message metadata (arbitration id, extended-id flag, DLC, signal membership) and any presentation info the database
437
+ defines (unit, physical min/max, enum value labels). A dashboard app or logger uses it to label raw bus traffic symbolically without embedding the database itself.
438
+
439
+ ```json
440
+ {
441
+ "format_version": 1,
442
+ "kcd_sha256": "f5e78b31d5a666ed68256cd43f341fbd42ba34d8518bea11f289111535be458a",
443
+ "signals": {
444
+ "message_1.signal_1": 3003935338,
445
+ "message_1.signal_2": 2987157719,
446
+ "message_2.signal_3": 1415019715
447
+ },
448
+ "messages": {
449
+ "message_1": { "arb_id": 0, "extended": true, "dlc": 1,
450
+ "signals": ["message_1.signal_1", "message_1.signal_2"] },
451
+ "message_2": { "arb_id": 1, "extended": true, "dlc": 2,
452
+ "signals": ["message_2.signal_3"] }
453
+ },
454
+ "signal_info": {}
455
+ }
456
+ ```
457
+
458
+ A `signal_id` is the 32-bit FNV-1a hash of the resolved `"<message>.<signal>"` name (both names converted to C identifiers). The `cbdb` step aborts if two signals in a
459
+ database hash to the same id. The `kcd_sha256` field is the SHA-256 of the source database file; the same hash is embedded in the `.cbdb` header and is retrievable on
460
+ the device via `dyndec_kcd_hash()`, so a client can confirm the device's blob matches the sidecar it is holding.
461
+
462
+ ### The dynamic-decoder module (`dyndec`)
463
+
464
+ ```bash
465
+ can-api-gen dyndec dyndec
318
466
  ```
319
467
 
468
+ Writes `dyndec.c`, `dyndec.h`, and a sibling `canframe.h`. The module contains no database — it is generated once and reused for every `.cbdb`. As with the static API, the
469
+ `-im`/`-lm`/`-pm` flags rename the `CanFrame` members so the module drops into an existing project's frame struct.
470
+
471
+ The runtime is driven through a `DyndecContext` and a handful of functions:
472
+
473
+ ```c
474
+ int dyndec_init(DyndecContext *ctx, const uint8_t *cbdb, uint32_t cbdb_size,
475
+ DyndecMsgRef *msg_index, uint16_t max_msgs,
476
+ float *values, uint16_t max_signals);
477
+ int dyndec_process_frame(DyndecContext *ctx, const CanFrame *frame);
478
+ int32_t dyndec_lookup(const DyndecContext *ctx, uint32_t signal_id);
479
+ float dyndec_get_value(const DyndecContext *ctx, int32_t value_idx);
480
+ uint16_t dyndec_signal_count(const DyndecContext *ctx);
481
+ uint32_t dyndec_signal_id_at(const DyndecContext *ctx, uint16_t value_idx);
482
+ const uint8_t *dyndec_kcd_hash(const DyndecContext *ctx);
483
+ ```
484
+
485
+ Usage:
486
+
487
+ * **Provide storage and initialise.** The decoder does not allocate. The caller supplies a `DyndecMsgRef` array (one entry per message) and a `float` value store (one
488
+ slot per signal); `dyndec_init` validates the blob's magic, format version, and size, then builds the message index and zeroes the value store. It fails with a negative
489
+ error code (e.g. `DYNDEC_ERR_MAGIC`, `DYNDEC_ERR_VERSION`, `DYNDEC_ERR_CAPACITY`) if the blob is malformed or your arrays are too small for its message/signal counts.
490
+ The `.cbdb` memory must stay valid and unchanged for the lifetime of the context.
491
+ * **Feed frames.** Call `dyndec_process_frame(&ctx, &frame)` once per received frame. It binary-searches the message table by arbitration id and returns the matched
492
+ message's index, or `-1` if the id is not in the database. Every non-muxed signal of the matched message is stored as its **physical (scaled) value** — a `float` after
493
+ applying scale and offset. A muxed signal is updated only when the frame's multiplexer selector matches that signal's mux group.
494
+ * **Read signals.** Resolve a `signal_id` (from the sidecar) to a store index with `dyndec_lookup`, then read the latest physical value with `dyndec_get_value`. `-1` from
495
+ `dyndec_lookup` means the id is not in this database. To enumerate everything the blob carries, iterate `0 .. dyndec_signal_count()` and pair `dyndec_signal_id_at(i)`
496
+ with `dyndec_get_value(&ctx, i)`.
497
+ * **Verify provisioning.** `dyndec_kcd_hash` returns the 32-byte SHA-256 stored in the blob header, matching the sidecar's `kcd_sha256`.
498
+
499
+ Because both `dyndec_lookup` and the value store are indexed by the signal's position in the cbdb, decoding is a bounded, allocation-free pass suitable for an ISR or a
500
+ tight bridge loop.
501
+
502
+ ### cbdb binary format
503
+
504
+ The `.cbdb` is a flat, little-endian table. Messages are sorted ascending by arbitration id so the decoder can binary-search; within a message, mux selector signals come
505
+ first, then plain signals, then muxed signals, so a single decode pass has every selector's raw value in hand before it evaluates mux membership. The current format
506
+ version is `1` (`DYNDEC_CBDB_FORMAT_VERSION`).
507
+
508
+ | Section | Size | Fields |
509
+ | --- | --- | --- |
510
+ | Header | 48 B | `magic` `4s` = `"CDB1"`; `format_version` `u16` = 1; `msg_count` `u16`; `signal_count` `u16`; reserved `u16` = 0; `cbdb_size` `u32` (total bytes incl. header); `kcd_sha256` `32s` |
511
+ | Per message | 8 B | `arb_id` `u32` (bit 31 = extended-id flag); `dlc` `u8`; `n_signals` `u8`; reserved `u16` = 0 |
512
+ | Per signal | 20 B | `signal_id` `u32` (FNV-1a of `"<msg>.<sig>"`); `start_bit` `u8`; `bit_length` `u8`; `flags` `u8`; `mux_sel_idx` `u8` (index into this message's signal list, `0xFF` = not muxed); `mux_value` `u32`; `scale` `f32`; `offset` `f32` |
513
+
514
+ `flags` is a bitfield: bit 0 (`FLAG_SIGNED`) the raw value is signed, bit 1 (`FLAG_WIRE_FLOAT`) the signal is a float on the wire, bit 2 (`FLAG_MUXED`) the signal belongs
515
+ to a mux group. The message list is followed immediately by that message's signals, so message *n*'s signals precede message *n+1*'s record.
516
+
320
517
  ## Command-line reference
321
518
 
519
+ All generation is done through subcommands of `can-api-gen`. Run `can-api-gen COMMAND --help` for the authoritative option list.
520
+
521
+ ### `api`
522
+
523
+ ```
524
+ can-api-gen api [OPTIONS] DB_FILE LIBRARY_NAME NODE_NAME
525
+ ```
526
+ Generate the static, per-node C API (see [Example Use](#example-use)).
527
+
322
528
  Positional arguments:
323
- * `KCD_FILE` — path to the KCD (XML) description of the bus.
529
+ * `DB_FILE` — path to the bus description, either a KCD (XML) or a `can-db-model` YAML file.
324
530
  * `LIBRARY_NAME` — stem of the generated library. The output files are `<LIBRARY_NAME>.c`, `<LIBRARY_NAME>.h`, and a sibling `canframe.h`. The stem is also used as
325
531
  the prefix for every generated function so multiple APIs can coexist in the same project.
326
532
  * `NODE_NAME` — name of the node the API is being generated for. Only messages that this node produces or consumes are emitted, so the binary size stays small for
@@ -339,11 +545,63 @@ Options:
339
545
  * `--ti-compatible` — hoist all per-signal temporaries to the top of every encode/decode function so the generated C compiles under strict C90
340
546
  ("declarations before statements"), which is what the TI C2000 compiler requires. See [TI C2000 compatibility](#ti-c2000-compatibility).
341
547
  * `--double-buffered` — emit two signal-struct storage slots plus front/back pointers per message, an `<api>_global_rx_init` helper that wires the RX
342
- pointers, and a per-message `<api>_<msg>_commit` helper that swaps the TX staging and committed pointers. The RX dispatcher publishes new decodes via
343
- pointer swap, and `<api>_periodic_tx` encodes from the committed slot. See [Double-buffered signal access](#double-buffered-signal-access).
548
+ pointers, and a per-message `<api>_<msg>_commit` helper that swaps the TX staging and committed pointers. Applies to the stored interfaces only: the RX side needs
549
+ `--rx-signal-store` and the TX side needs `--tx-signal-store` (in the callback-only default there is no store to double-buffer). See
550
+ [Double-buffered signal access](#double-buffered-signal-access).
551
+ * `--rx-callback-context` — add a `void *rx_callback_context` field to each `MsgRxContext` and widen the RX callback to `void (*rx_callback)(void *)`, passing the
552
+ context pointer on each firing. See [RX/TX callback context](#rxtx-callback-context). Only meaningful together with `-r`.
553
+ * `--tx-callback-context` — add a `void *tx_callback_context` field to each `MsgTxContext` and append a `void *` context parameter to the TX callback, passing the
554
+ context pointer on each firing (before encode). See [RX/TX callback context](#rxtx-callback-context). Only meaningful together with `-p`.
555
+ * `--msg-as-rx-callback-arg` — pass a `void *` to the message's just-decoded signal struct as the first argument to the RX callback. Composes with
556
+ `--rx-callback-context` (signals first, then the context pointer). See [Message signals as RX callback argument](#message-signals-as-rx-callback-argument). Only
557
+ meaningful together with `-r`.
558
+ * `--tx-signal-store` — generate the permanent per-message TX signal store. Without it, periodic TX is callback-only: each scheduled message's `tx_callback` returns the
559
+ struct to encode, and a `NULL` return or missing callback sends nothing. See [Periodic TX](#periodic-tx). Only meaningful together with `-p`.
560
+ * `--msg-as-tx-callback-arg` — pass a `void *` message-struct pointer as the first argument to the TX callback (context second, with `--tx-callback-context`). With
561
+ `--tx-signal-store` the pointer is the message's store struct, which the callback edits in place before encode; in callback-only mode `<api>_periodic_tx` stack-allocates
562
+ the struct, and the callback fills it and returns it (or `NULL` to skip), so no application-provided storage is needed. See [Periodic TX](#periodic-tx). Only meaningful
563
+ together with `-p`.
564
+ * `--rx-signal-store` — generate the permanent per-message RX signal store (the pre-2.0 default). Without it, global RX is callback-only: no store is
565
+ generated and decoded signals reach the application only through the RX callback. See [Global RX](#global-rx). Only meaningful together with `-r`.
566
+ * `--with-rx-valid` — in callback-only mode, add a `valid` field to `MsgRxContext` and set it on each matched frame, for struct-layout compatibility with
567
+ store-based APIs. No effect with `--rx-signal-store`. Only meaningful together with `-r`.
344
568
  * `-im`, `--id-member-name TEXT` — override the name of the `CanFrame` member that carries the arbitration id (default `arbitration_id`). Used to adapt the API
345
569
  to an existing project's frame struct naming.
346
570
  * `-lm`, `--length-member-name TEXT` — override the name of the `CanFrame` length member (default `length`).
347
571
  * `-pm`, `--payload-member-name TEXT` — override the name of the `CanFrame` payload member (default `payload`).
348
572
  * `-v`, `--verbose` — dump the processed message dicts that get fed into the Jinja templates. Useful when debugging template or generator changes.
349
573
  * `--help` — show the CLI help and exit.
574
+
575
+ ### `cbdb`
576
+
577
+ ```
578
+ can-api-gen cbdb [OPTIONS] DB_FILE OUTPUT_BASE
579
+ ```
580
+ Compile a database into the binary descriptor blob consumed by the dynamic decoder, plus a JSON sidecar. See [Preparing a database](#preparing-a-database-cbdb).
581
+
582
+ Positional arguments:
583
+ * `DB_FILE` — path to the bus description (KCD or YAML). Unlike `api`, there is no node argument: the decoder is a bus observer, so every signal of every message is
584
+ included and no direction filtering is applied.
585
+ * `OUTPUT_BASE` — stem for the outputs. Writes `<OUTPUT_BASE>.cbdb` (the binary descriptor) and `<OUTPUT_BASE>_ids.json` (the sidecar).
586
+
587
+ Options:
588
+ * `-f`, `--force` — overwrite existing output files. Without this, the generator refuses to clobber an existing `.cbdb`/`_ids.json`.
589
+ * `--help` — show the CLI help and exit.
590
+
591
+ ### `dyndec`
592
+
593
+ ```
594
+ can-api-gen dyndec [OPTIONS] LIBRARY_NAME
595
+ ```
596
+ Generate the KCD-independent dynamic-decoder C module. See [The dynamic-decoder module](#the-dynamic-decoder-module-dyndec).
597
+
598
+ Positional arguments:
599
+ * `LIBRARY_NAME` — stem of the generated module. Writes `<LIBRARY_NAME>.c`, `<LIBRARY_NAME>.h`, and a sibling `canframe.h`. The module is database-independent, so this
600
+ is generated once per firmware and reused for every `.cbdb`.
601
+
602
+ Options:
603
+ * `-f`, `--force` — overwrite existing output files.
604
+ * `-im`, `--id-member-name TEXT` — override the name of the `CanFrame` arbitration-id member (default `arbitration_id`).
605
+ * `-lm`, `--length-member-name TEXT` — override the name of the `CanFrame` length member (default `length`).
606
+ * `-pm`, `--payload-member-name TEXT` — override the name of the `CanFrame` payload member (default `payload`).
607
+ * `--help` — show the CLI help and exit.