xcp_registry
Pure-Rust measurement and calibration (MC) registry for XCP.
No C dependency — the crate holds the complete data model and drives A2L file generation.
The registry is the source of truth for all objects that are later described in an A2L file and accessed by a calibration tool (CANape, etc.) over XCP.
Contents
- Overview
- Creating a Registry
- Application metadata
- Type definitions –
McTypeDef - Instances –
McInstance - Support data –
McSupportData - Addressing –
McAddress - Writing A2L
- Reading A2L (optional feature)
- JSON serialization
- Registry modes
- Error handling
- Derive macro (
McRegisterType) - License
Overview
The main types exported by this crate are:
| Type | Role |
|---|---|
Registry |
Root container – holds all MC objects |
McApplication |
Application name, description, EPK version string |
McTypeDef / McTypeDefField |
Struct-like type definition (maps to A2L TYPEDEF_STRUCTURE) |
McInstance |
Concrete measurement or calibration instance (maps to A2L INSTANCE, MEASUREMENT, CHARACTERISTIC, AXIS_PTS) |
McSupportData |
Rich metadata attached to a field or instance (unit, factor/offset, min/max, …) |
McValueType |
Scalar type enum or McTypeDef reference (Ubyte, Sword, Float64Ieee, TypeDef(name), …) |
McDimType |
Type + optional array dimensions [x][y] |
McAddress |
XCP addressing information (cal-seg relative, event-relative, absolute) |
McCalibrationSegment |
A named contiguous memory region for calibration data |
McEvent |
DAQ event |
Creating a Registry
use Registry;
let mut registry = new;
Set optional rendering modes before populating (see Registry modes):
use Registry;
let mut registry = new;
registry.set_flatten_typedefs_mode; // default: false → use TYPEDEF_STRUCTURE
registry.set_prefix_names_mode; // default: false → no app-name prefix
Set the XCP Ethernet transport parameters so the A2L gets an IF_DATA XCP section:
use Ipv4Addr;
use Registry;
let mut registry = new;
registry.set_xcp_eth_params;
Application metadata
Registry::application is an McApplication value.
Set name, description, application id and version (EPK and EPK address) before writing the A2L:
use Registry;
let mut registry = new;
registry.application.set_info;
registry.application.set_version;
Type definitions – McTypeDef
A McTypeDef describes a struct type.
Add one typedef per struct type, then add each struct field individually.
Adding a typedef
use Registry;
let mut registry = new;
// Register type "ControlParams" with its total size in bytes
let typedef = registry.add_typedef.unwrap;
add_typedef returns Err(RegistryError::Duplicate) when the same name already exists
(idempotent: the existing definition is kept).
Adding fields
Each field needs:
- a field name (string)
- a
McDimTypedescribing its element type and optional array dimensions - a
McSupportDatawith metadata (at minimum:McObjectType) - the byte offset of the field within the struct
use ;
let mut registry = new;
registry.add_typedef.unwrap;
registry.add_typedef.unwrap;
// Scalar f64 field "gain" at byte offset 0, calibration characteristic
registry.add_typedef_field.unwrap;
// u16 array [8] field "lut" at byte offset 8
registry.add_typedef_field.unwrap;
// Nested typedef: field "inner" of type "InnerStruct" at byte offset 24
registry.add_typedef_field.unwrap;
add_typedef_field returns Err(RegistryError::Duplicate) when a field with the same name
already exists (idempotent if the layout is identical; emits a warning on ABI mismatch).
Full example – building a typedef manually
use ;
let mut registry = new;
// 1. Declare the type
registry.add_typedef.unwrap;
// 2. Add fields
registry.add_typedef_field.unwrap;
registry.add_typedef_field.unwrap;
registry.add_typedef_field.unwrap;
Instances – McInstance
An McInstance binds a type (possibly a typedef) to a name and an address.
Instances are added directly to the registry's instance_list.
use ;
let mut registry = new;
let event_id: u16 = 1; // id of the event which measures "speed"
let offset_of_speed: i32 = 16; // byte offset of "speed" relative to the base address of the event
// Calibration instance of typedef "PidParams" relative to calibration segment "calseg"
registry.instance_list.add_instance.unwrap;
// Scalar measurement instance of type f32, event-relative (dynamic DAQ)
registry.instance_list.add_instance.unwrap;
Addressing constructors
| Constructor | Use case |
|---|---|
McAddress::new_calseg_rel(seg_name, offset) |
Calibration segment–relative (characteristic / axis) |
McAddress::new_event_dyn(index, event_id, offset) |
Event-relative dynamic DAQ (measurement) |
McAddress::new_event_abs(event_id, offset) |
Event-relative absolute addressing |
McAddress::new_a2l(addr, addr_ext) |
Raw XCP address from a third-party A2L |
McAddress::new_a2l_with_event(event_id, addr, addr_ext) |
Raw XCP address with event association |
Updating metadata on a registered instance
Use get_instance_mut to obtain a mutable reference, then call the update_* methods
on mc_support_data (which take &mut self and return &mut Self for chaining):
use Registry;
let mut registry = new;
// ... the instance "speed" is added as shown above
if let Some = registry.instance_list.get_instance_mut
The second argument to get_instance_mut is an optional event_id filter
(None matches any event).
Updating metadata on a typedef field (simple — when you know the typedef name)
Use find_typedef_mut on the typedef list, then find_field_mut on the typedef:
use Registry;
let mut registry = new;
// ... the typedef "PidParams" is added as shown above
if let Some = registry.typedef_list
.find_typedef_mut
.and_then
Updating metadata via instance + field path (preferred for complex/nested types)
When an instance has a complex type and the target field may be arbitrarily deep inside
nested typedefs, use Registry::set_instance_field_support_data. The field is addressed
by a dot-separated path relative to the instance's typedef — field names live in their
typedef's namespace, not a flat global namespace.
use ;
let mut registry = new;
// ... the typedefs and the instances "pid" and "speed_sensor" are added as shown above
// Instance "pid" has typedef "PidController"
// PidController::gains → typedef "GainStruct"
// GainStruct::kp → f64
registry.set_instance_field_support_data.unwrap;
// Direct field (no nesting):
registry.set_instance_field_support_data.unwrap;
Key behaviours:
- If the caller sets
object_typetoUnspecified, the existingobject_typeof the field is preserved automatically (safe to passMcSupportData::default()with only specific fields set viaupdate_*). - Returns
Err(RegistryError::MetadataAlreadySet)if the field already has descriptive metadata (unit / min / max / factor / offset / step / comment non-empty). This enforces the current data-model limitation: typedef fields are shared across all instances of the same type, so conflicting assignments are rejected rather than silently overwriting. - Returns
Err(RegistryError::NotFound)if the instance, its typedef, or any path component does not exist.
Limitation: because typedef fields are shared, setting metadata here affects every instance that uses the same typedef — there is no per-instance override yet.
Support data – McSupportData
McSupportData carries the rich metadata that ends up in the A2L object description.
Two distinct setter families are available:
| Family | Signature | When to use |
|---|---|---|
Builder (set_*) |
self -> Self |
At construction time — chain on McSupportData::new(...) |
Mutation (update_*) |
&mut self -> &mut Self |
Post-construction — update an already-registered instance |
Required field
use ;
let support_data = new; // Measurement | Characteristic | Axis
Builder chain (construction time)
use ;
let support_data = new
.set_unit // physical unit string
.set_linear // factor=0.1, offset=-40.0, implicit set_unit
.set_min // lower display limit
.set_max // upper display limit
.set_step // calibration step width
.set_comment // free-text description
.set_qualifier; // read-only parameter
Mutation chain (post-registration)
use Registry;
let mut registry = new;
// ... the instance "temp" is added as shown above
// Update metadata on an instance that was already added to the registry
if let Some = registry.instance_list.get_instance_mut
update_* methods available: update_unit, update_linear, update_min, update_max,
update_step, update_comment.
Object qualifier
McObjectQualifier refines how an object can be accessed, independently of its type:
| Variant | Meaning |
|---|---|
Default |
No qualifier |
ReadOnly |
For read-only parameter objects |
ReadWrite |
For writable measurement objects |
NoAsyncAccess |
For objects that can not be accessed asynchronously |
Axis references
For maps/curves whose axes are separate objects:
use ;
let support_data = new
.set_x_axis_ref
.set_y_axis_ref;
Writing A2L
use Registry;
let registry = new;
// ... events, typedefs and instances are added as shown above
registry.write_a2l.unwrap;
The writer emits TYPEDEF_STRUCTURE / INSTANCE blocks by default.
With set_flatten_typedefs_mode(true) the registry is internally flattened before
writing, producing plain MEASUREMENT / CHARACTERISTIC / AXIS_PTS objects with mangled names instead.
By default the A2L file includes the XCP A2ML description with /include "XCP_104.aml", which keeps the file small.
Tools like CANape then need XCP_104.aml next to the A2L file, its content is available as xcp_registry::XCP_104_AML.
With set_a2ml_mode(McA2mlMode::Embed) the A2ML description is embedded and the A2L file is self-contained,
McA2mlMode::None omits it.
Reading A2L (optional feature)
Enable the a2l_reader Cargo feature to load or validate A2L files:
[]
= { = "0.1", = ["a2l_reader"] }
Load an A2L file into a registry
use Registry;
let mut registry = new;
let warnings = registry.load_a2l.unwrap;
println!;
IF_DATA XCP is parsed with the XCP A2ML specification built into the crate, the A2L file does not need to contain
an A2ML description. An A2L file with /include "XCP_104.aml" still requires XCP_104.aml next to it.
Validate an A2L file (syntax + consistency check)
use Registry;
let registry = new;
let warnings = registry.check_a2l.unwrap;
JSON serialization
The registry can be round-tripped through JSON (non standard, faster and less memory than A2L):
use Registry;
let registry = new;
// ... events, typedefs and instances are added as shown above
// Serialize to JSON
registry.write_json.unwrap;
// Deserialize from JSON
let mut registry = new;
registry.load_json.unwrap;
McSupportData also has dedicated string helpers for embedding in other JSON structures:
use ;
let mc_support_data = new.set_unit;
let s: String = mc_support_data.to_json_string;
let m: = from_json_string;
Registry modes
| Method | Default | Effect |
|---|---|---|
set_flatten_typedefs_mode(bool) |
false |
When true, nested typedefs are flattened to flat objects with mangled names at A2L export time |
set_prefix_names_mode(bool) |
false |
When true, every A2L object name is prefixed with the application name |
set_a2ml_mode(McA2mlMode) |
Include |
A2ML description in the A2L file: Include writes /include "XCP_104.aml", Embed embeds it, None omits it |
Flattening only affects the export; the internal registry structure (typedef tree) is preserved regardless.
Error handling
All mutating registry calls return Result<_, RegistryError>:
use ;
| Variant | Meaning |
|---|---|
RegistryError::Duplicate(name) |
Object with that name already registered |
RegistryError::NotFound(name) |
Referenced object (e.g. typedef for a field) does not exist |
RegistryError::UnknownEventChannel(id) |
Event id not defined |
RegistryError::Io(e) |
File I/O error |
Derive macro (McRegisterType)
In practice you rarely call the low-level API above directly.
The #[derive(McRegisterType)] proc-macro (crate xcp_registry_derive) generates
the necessary add_typedef / add_typedef_field / add_instance calls for a Rust struct.
Note: the generated code refers to this crate by its name,
::xcp_registry. A crate that uses#[derive(McRegisterType)]or#[derive(McRegisterEnum)]must therefore listxcp_registryas a direct dependency in itsCargo.toml, also when it gets the macros through a re-export of another crate such asxcp_lite. Both must use compatible versions ofxcp_registry.
Minimal example
use McRegisterType;
This generates code equivalent to the manual example in the Type definitions section.
Field attributes
Override the default classification or add metadata per field:
use McRegisterType;
Supported attribute names: #[characteristic(...)], #[measurement(...)], #[axis(...)].
Supported keys inside an attribute: unit, comment, min, max, step, factor, offset.
Using the derive with CalSeg (xcp-lite)
When working through the higher-level xcp-lite crate the derive is invoked automatically
via CalSeg::register() and the daq_register_struct! macro; you do not call the registry
methods directly.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.