#[cfg(feature = "a2l_reader")]
pub mod a2l_reader;
pub mod a2l_writer;
#[cfg(feature = "a2l_reader")]
pub mod aml_ifdata;
use super::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum McA2mlMode {
#[default]
Include,
Embed,
None,
}
#[cfg(feature = "a2l_reader")]
fn load_a2l_file(path: &std::path::Path, strict: bool) -> Result<(a2lfile::A2lFile, Vec<a2lfile::A2lError>), RegistryError> {
let a2ml_spec = Some(aml_ifdata::A2MLVECTOR_TEXT.to_string());
a2lfile::load(path, a2ml_spec, strict).map_err(|e| RegistryError::A2l(format!("a2lfile::load failed: {}", e)))
}
impl Registry {
#[cfg(feature = "a2l_reader")]
pub fn check_a2l<P: AsRef<std::path::Path>>(&self, path: &P) -> Result<u32, RegistryError> {
let res = load_a2l_file(path.as_ref(), true);
match res {
Ok((a2l_file, log_msgs)) => {
let mut warnings: u32 = 0;
for log_msg in log_msgs {
log::warn!("A2L warning: {}", log_msg);
warnings += 1;
}
let log_msgs = a2l_file.check();
for log_msg in log_msgs {
log::warn!("A2L check: {}", log_msg);
warnings += 1;
}
Ok(warnings)
}
Err(e) => Err(e),
}
}
#[cfg(feature = "a2l_reader")]
pub fn load_a2l<P: AsRef<std::path::Path>>(&mut self, path: &P, print_warnings: bool, strict: bool, check: bool, flatten_typedefs: bool) -> Result<u32, RegistryError> {
let res = load_a2l_file(path.as_ref(), strict);
match res {
Ok((a2l_file, log_msgs)) => {
let mut warnings: u32 = 0;
if print_warnings {
log::info!("{} A2L warnings:", log_msgs.len());
for log_msg in log_msgs {
log::warn!("Warning: {}", log_msg);
warnings += 1;
}
}
if check {
let log_msgs = a2l_file.check();
log::info!("{} A2L checker finding:", log_msgs.len());
for log_msg in log_msgs {
log::warn!("Check: {}", log_msg);
warnings += 1;
}
}
self.load_a2lfile(&a2l_file)?;
if flatten_typedefs {
self.flatten_typedefs();
}
Ok(warnings)
}
Err(e) => Err(e),
}
}
pub fn write_a2l<P: AsRef<std::path::Path>>(
&self,
path: &P,
title_comment: &str,
project_name: &str,
project_description: &str,
module_name: &str,
project_no: &str,
check: bool,
) -> Result<(), std::io::Error> {
log::info!("Write A2L file {:?}", path.as_ref());
let mut a2l_file = std::fs::File::create(path)?;
std::io::Write::write_all(&mut a2l_file, b"\xEF\xBB\xBF")?;
let writer: &mut dyn std::io::Write = &mut std::io::LineWriter::new(a2l_file);
let mut a2l_writer = a2l_writer::A2lWriter::new(writer, self);
a2l_writer.write_a2l(title_comment, project_name, project_description, module_name, project_no)?;
#[cfg(not(feature = "a2l_reader"))]
if check {
log::warn!("A2L file check not available, feature a2l_reader not enabled");
}
#[cfg(feature = "a2l_reader")]
if check {
log::info!("Check A2L file {:?}", path.as_ref());
match self.check_a2l(path) {
Err(e) => {
log::error!("A2l file check error: {}", e);
}
Ok(w) => {
if w > 0 {
log::warn!("A2L file check with {w} warnings !!");
} else {
log::info!("A2L file check ok");
}
}
}
}
Ok(())
}
}
#[cfg(all(test, feature = "a2l_reader"))]
mod test {
use super::*;
use a2lfile::A2lObject;
fn test_dir(name: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!("xcp_registry_test_{}_{}", std::process::id(), name));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn test_registry(a2ml_mode: McA2mlMode) -> Registry {
let mut reg = Registry::new();
reg.application.set_info("test", "test application", 0);
reg.set_xcp_eth_params("UDP", std::net::Ipv4Addr::new(127, 0, 0, 1), 5555);
reg.set_a2ml_mode(a2ml_mode);
reg.event_list.add_event(McEvent::new("event1", 0, 1, 0)).unwrap();
reg.event_list.add_event(McEvent::new("event2", 0, 2, 0)).unwrap();
reg.instance_list
.add_instance(
"counter",
McDimType::new(McValueType::Ulong, 1, 1),
McSupportData::new(McObjectType::Measurement),
McAddress::new_event_abs(2, 0x100),
)
.unwrap();
reg
}
fn write_test_a2l(dir: &std::path::Path, a2ml_mode: McA2mlMode) -> std::path::PathBuf {
let path = dir.join("test.a2l");
test_registry(a2ml_mode).write_a2l(&path, "test", "test", "", "test", "XCPLITE__ACSDD", false).unwrap();
path
}
fn check_loaded(path: &std::path::Path) {
let mut reg = Registry::new();
reg.load_a2l(&path, true, true, true, false).unwrap();
assert_eq!(reg.event_list.find_event_id(1).unwrap().get_name(), "event1");
assert_eq!(reg.event_list.find_event_id(2).unwrap().get_name(), "event2");
let counter = reg.instance_list.get_instance("counter", McObjectType::Measurement, None).unwrap();
assert_eq!(counter.get_address().get_event_id(), Some(2));
reg.check_a2l(&path).unwrap();
}
#[test]
fn test_load_a2l_enum_conversion() {
let dir = test_dir("enum_conversion");
let path = dir.join("test.a2l");
let enum_unit = r#"0 "OFF" 1 "ON" 2 "STAND BY" -1 "ERROR""#;
let mut reg = test_registry(McA2mlMode::Embed);
reg.instance_list
.add_instance(
"state",
McDimType::new(McValueType::Sbyte, 1, 1),
McSupportData::new(McObjectType::Measurement).set_unit(enum_unit),
McAddress::new_event_abs(1, 0x200),
)
.unwrap();
reg.instance_list
.add_instance(
"voltage",
McDimType::new(McValueType::Uword, 1, 1),
McSupportData::new(McObjectType::Measurement).set_linear(0.5, 1.0, "Volt"),
McAddress::new_event_abs(1, 0x204),
)
.unwrap();
reg.write_a2l(&path, "test", "test", "", "test", "XCPLITE__ACSDD", false).unwrap();
let mut reg = Registry::new();
reg.load_a2l(&path, true, true, true, false).unwrap();
let state = reg.instance_list.get_instance("state", McObjectType::Measurement, None).unwrap();
assert_eq!(state.unit(), enum_unit);
let counter = reg.instance_list.get_instance("counter", McObjectType::Measurement, None).unwrap();
assert_eq!(counter.unit(), "");
let voltage = reg.instance_list.get_instance("voltage", McObjectType::Measurement, None).unwrap();
assert_eq!(voltage.unit(), "Volt");
assert_eq!(voltage.get_mc_support_data().get_factor(), Some(0.5));
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn test_load_a2l_include_missing_aml() {
let dir = test_dir("include_missing_aml");
let path = write_test_a2l(&dir, McA2mlMode::Include);
let text = std::fs::read_to_string(&path).unwrap();
assert!(text.contains("/include \"XCP_104.aml\""));
std::fs::write(&path, text.replace("/include \"XCP_104.aml\"", "/include \"XCP_104_missing.aml\"")).unwrap();
let mut reg = Registry::new();
assert!(reg.load_a2l(&path, true, true, true, false).is_err());
assert!(reg.check_a2l(&path).is_err());
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn test_load_a2l_include_aml() {
let dir = test_dir("include_aml");
let path = write_test_a2l(&dir, McA2mlMode::Include);
std::fs::write(dir.join("XCP_104.aml"), crate::XCP_104_AML).unwrap();
check_loaded(&path);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn test_write_a2l_without_transport_layer() {
let dir = test_dir("without_transport_layer");
let path = dir.join("test.a2l");
let mut reg = test_registry(McA2mlMode::None);
reg.xcp_tl_params = None;
reg.xcp_params = None;
assert!(!reg.has_xcp_params());
reg.write_a2l(&path, "test", "test", "", "test", "XCPLITE__ACSDD", false).unwrap();
let text = std::fs::read_to_string(&path).unwrap();
assert!(text.contains("/begin IF_DATA XCP") && text.contains("/begin PROTOCOL_LAYER") && !text.contains("XCP_ON_"));
assert!(text.contains(" 248 1024 BYTE_ORDER_MSB_LAST")); check_loaded(&path);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn test_load_a2l_embedded_and_no_aml() {
let dir = test_dir("embedded_aml");
let path = write_test_a2l(&dir, McA2mlMode::Embed);
let text = std::fs::read_to_string(&path).unwrap();
assert!(text.contains("/begin A2ML") && !text.contains("/include"));
check_loaded(&path);
let path = write_test_a2l(&dir, McA2mlMode::None);
let text = std::fs::read_to_string(&path).unwrap();
assert!(!text.contains("/begin A2ML") && !text.contains("/include"));
check_loaded(&path);
std::fs::remove_dir_all(&dir).unwrap();
}
fn spec_test_a2l(protocol: &str) -> String {
let mut reg = test_registry(McA2mlMode::None);
if protocol == "SxI" {
reg.set_xcp_sxi_params(115200);
} else if protocol == "TCP" {
reg.set_xcp_eth_params("TCP", std::net::Ipv4Addr::new(127, 0, 0, 1), 5555);
}
reg.cal_seg_list.add_cal_seg("params", Some(0), 16).unwrap();
let mut buf: Vec<u8> = Vec::new();
{
let writer: &mut dyn std::io::Write = &mut buf;
a2l_writer::A2lWriter::new(writer, ®).write_a2l("test", "test", "", "test", "XCPLITE__ACSDD").unwrap();
}
String::from_utf8(buf).unwrap()
}
fn module_if_data(a2l_file: &a2lfile::A2lFile) -> Vec<&a2lfile::IfData> {
let module = &a2l_file.project.module[0];
let mut if_data: Vec<&a2lfile::IfData> = module.if_data.iter().collect();
if let Some(mod_par) = &module.mod_par {
for s in &mod_par.memory_segment {
if_data.extend(&s.if_data);
}
}
for m in &module.measurement {
if_data.extend(&m.if_data);
}
for c in &module.characteristic {
if_data.extend(&c.if_data);
}
for i in &module.instance {
if_data.extend(&i.if_data);
}
if_data
}
fn generic_if_data_lines(data: &a2lfile::GenericIfData, indent: usize, lines: &mut Vec<String>) {
use a2lfile::GenericIfData as G;
let pad = " ".repeat(indent);
let tagged = |kind: &str, items: &std::collections::HashMap<String, Vec<a2lfile::GenericIfDataTaggedItem>>, lines: &mut Vec<String>| {
lines.push(format!("{pad}{kind}"));
let mut tags: Vec<&String> = items.keys().collect();
tags.sort();
for tag in tags {
for item in &items[tag] {
lines.push(format!("{pad} {tag}{}", if item.is_block { " (block)" } else { "" }));
generic_if_data_lines(&item.data, indent + 2, lines);
}
}
};
match data {
G::Array(items) | G::Sequence(items) | G::Struct(_, _, items) | G::Block { items, .. } => {
lines.push(format!("{pad}{}", if matches!(data, G::Array(_)) { "Array" } else { "Struct" }));
for item in items {
generic_if_data_lines(item, indent + 1, lines);
}
}
G::TaggedStruct(items) => tagged("TaggedStruct", items, lines),
G::TaggedUnion(items) => tagged("TaggedUnion", items, lines),
G::EnumItem(_, value) => lines.push(format!("{pad}Enum {value}")),
G::String(_, value) => lines.push(format!("{pad}String {value:?}")),
G::Char(_, (v, _)) => lines.push(format!("{pad}Char {v}")),
G::Int(_, (v, _)) => lines.push(format!("{pad}Int {v}")),
G::Long(_, (v, _)) => lines.push(format!("{pad}Long {v}")),
G::Int64(_, (v, _)) => lines.push(format!("{pad}Int64 {v}")),
G::UChar(_, (v, _)) => lines.push(format!("{pad}UChar {v}")),
G::UInt(_, (v, _)) => lines.push(format!("{pad}UInt {v}")),
G::ULong(_, (v, _)) => lines.push(format!("{pad}ULong {v}")),
G::UInt64(_, (v, _)) => lines.push(format!("{pad}UInt64 {v}")),
G::Float(_, v) => lines.push(format!("{pad}Float {v}")),
G::Double(_, v) => lines.push(format!("{pad}Double {v}")),
G::None => lines.push(format!("{pad}None")),
}
}
fn a2ml_body(a2ml: &str) -> &str {
a2ml.trim()
.strip_prefix("/begin A2ML")
.and_then(|s| s.strip_suffix("/end A2ML"))
.expect("A2ML description is not enclosed in /begin A2ML ... /end A2ML")
}
fn compare_a2ml_specifications(a2l_texts: &[String], name: &str, a2ml: &str) {
for text in a2l_texts {
let (builtin, _) = a2lfile::load_from_string(text, Some(aml_ifdata::A2MLVECTOR_TEXT.to_string()), true).unwrap();
let (file, _) = a2lfile::load_from_string(text, Some(a2ml.to_string()), true).unwrap();
let (builtin, file) = (module_if_data(&builtin), module_if_data(&file));
assert!(builtin.len() >= 3, "IF_DATA blocks missing in test A2L file");
assert_eq!(builtin.len(), file.len());
for (b, f) in builtin.iter().zip(&file) {
assert!(
b.ifdata_valid && f.ifdata_valid,
"IF_DATA in line {} not valid, built-in {} {name} {}",
b.get_line(),
b.ifdata_valid,
f.ifdata_valid
);
let (mut b_lines, mut f_lines) = (Vec::new(), Vec::new());
generic_if_data_lines(b.ifdata_items.as_ref().unwrap(), 0, &mut b_lines);
generic_if_data_lines(f.ifdata_items.as_ref().unwrap(), 0, &mut f_lines);
if let Some(i) = (0..b_lines.len().max(f_lines.len())).find(|&i| b_lines.get(i) != f_lines.get(i)) {
let context = |lines: &[String]| lines[i.saturating_sub(8)..(i + 4).min(lines.len())].join("\n");
panic!(
"IF_DATA in line {} parsed differently\n--- built-in (aml_ifdata.rs):\n{}\n--- {name}:\n{}",
b.get_line(),
context(&b_lines),
context(&f_lines)
);
}
}
}
}
fn replace_block(a2l: &str, begin: &str, end: &str, new: &str) -> String {
let start = a2l.find(begin).unwrap();
let stop = a2l[start..].find(end).unwrap() + start + end.len();
format!("{}{}{}", &a2l[..start], new, &a2l[stop..])
}
#[test]
fn test_a2ml_specification_xcp_104() {
let mut a2l_texts: Vec<String> = ["UDP", "TCP", "SxI"].iter().map(|protocol| spec_test_a2l(protocol)).collect();
let sxi = &mut a2l_texts[2];
if !sxi.contains("/begin FRAMING") {
*sxi = sxi.replace("FRAMING 0x9A 0x9B", "/begin FRAMING 0x9A 0x9B /end FRAMING");
}
let udp = a2l_texts[0].replace(
"CONSISTENCY DAQ /end EVENT",
"CONSISTENCY DAQ /begin BUFFER_RESERVE_EVENT 10 20 /end BUFFER_RESERVE_EVENT \
/begin CPU_LOAD_CONSUMPTION_DAQ 1.0 2.0 3.0 /begin CORE_LOAD_EP 1 0.5 /end CORE_LOAD_EP /end CPU_LOAD_CONSUMPTION_DAQ \
/begin CPU_LOAD_CONSUMPTION_QUEUE_STIM 1.0 2.0 /begin CORE_LOAD_EP 2 0.5 /end CORE_LOAD_EP /end CPU_LOAD_CONSUMPTION_QUEUE_STIM /end EVENT",
);
let udp_14 = replace_block(
&udp,
"/begin XCP_ON_UDP_IP",
"/end XCP_ON_UDP_IP",
"/begin XCP_ON_UDP_IP 0x0104 5555 ADDRESS \"127.0.0.1\" MAX_BUS_LOAD 50 PACKET_ALIGNMENT PACKET_ALIGNMENT_32 \
OPTIONAL_TL_SUBCMD GET_SLAVE_ID OPTIONAL_TL_SUBCMD GET_DAQ_CLOCK_MULTICAST /end XCP_ON_UDP_IP",
);
let can = replace_block(
&udp,
"/begin XCP_ON_UDP_IP",
"/end XCP_ON_UDP_IP",
"/begin XCP_ON_CAN 0x0100 CAN_ID_MASTER 0x0301 CAN_ID_SLAVE 0x01 CAN_ID_GET_DAQ_CLOCK_MULTICAST 0x0302 BAUDRATE 0x0F4240 \
SAMPLE_POINT 0x4B SAMPLE_RATE SINGLE BTL_CYCLES 0x08 SJW 0x02 SYNC_EDGE SINGLE MEASUREMENT_SPLIT_ALLOWED \
OPTIONAL_TL_SUBCMD GET_DAQ_ID OPTIONAL_TL_SUBCMD GET_DAQ_CLOCK_MULTICAST /end XCP_ON_CAN",
);
a2l_texts.extend([udp_14, can]);
compare_a2ml_specifications(&a2l_texts, "XCP_104.aml", a2ml_body(crate::XCP_104_AML));
let dir = test_dir("xcp_14_keywords");
let path = dir.join("test.a2l");
for text in &a2l_texts[3..] {
std::fs::write(&path, text).unwrap();
check_loaded(&path);
}
std::fs::remove_dir_all(&dir).unwrap();
}
}