// Module mc_support
// McObjectType - Copy,Clone
// McObjectQualifier - Copy,Clone
// McSupportData - Clone with warning
use serde::{Deserialize, Serialize};
use super::McIdentifier;
use super::McText;
use super::McValueType;
//----------------------------------------------------------------------------------------------
// McObjectType
/// Object type Measurement, Characteristic or Axis instances or typedefs
#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum McObjectType {
/// Object type not specified
#[default]
Unspecified = 0,
/// Measurement signal
Measurement = 1,
/// Calibration parameter
Characteristic = 2,
/// Axis of a calibration curve or map
Axis = 3,
}
impl McObjectType {
/// Measurement object
/// Could be a explicit measurement object or a typedef
pub fn is_measurement_object(self) -> bool {
self == McObjectType::Measurement
}
/// Calibration object with calibration parameter semantic
/// Could be a explicit calibration or axis object or a typedef instance with calibration semantics
/// Is constant in target software, so it is never modified by the target ECU
/// # Panics
/// If the object type is `Unspecified`
pub fn is_calibration_object(self) -> bool {
// @@@@ TODO: McObjectType::Unspecified defaults to calibration object
assert!(self != McObjectType::Unspecified);
self == McObjectType::Characteristic || self == McObjectType::Axis || self == McObjectType::Unspecified
}
/// Check if this is an axis
pub fn is_axis(self) -> bool {
self == McObjectType::Axis
}
/// Check if this is a characteristic
pub fn is_characteristic(self) -> bool {
self == McObjectType::Characteristic
}
}
impl std::fmt::Display for McObjectType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)?;
Ok(())
}
}
//----------------------------------------------------------------------------------------------
// McObjectQualifier
// Object qualifier
// Independent from McObjectType (Measurement, Characteristic or Axis), an object may be volatile or constant
// This is often associated to the terms characteristic and measurement
// To avoid confusion, the terms volatile and non-volatile are used
//
// Volatile means, that objects may be continuously modified in memory by the target
// * Measurement objects are typically volatile, but characteristics and axis objects may be volatile as well
//
// Constant means objects are never modified by the target ECU
// * Constant object needs interior mutable to be adjustable by a calibration tool
// * How to achieve interior mutability for constant objects is called the calibration concept
// * Constant objects are typically characteristics or axis objects
//
// @@@@ TODO: How to specify ReadWrite non volatile objects? DefuThis is a special case for characteristics and axis objects, which are constant in the target ECU, but may be modified by a calibration tool. This is a special case of interior mutability, which is not supported by Rust. The calibration concept needs to be specified in the XCP protocol and the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file. The calibration concept is not part of the XCP protocol, but is part of the A2L file.
/// Object qualifier for Measurement, Characteristic or Axis object type instances or typedefs
#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum McObjectQualifier {
/// No qualifier
#[default]
Default = 0,
/// For read-only parameter objects
ReadOnly = 1,
/// For writable measurement objects
ReadWrite = 2,
/// For objects that can not be accessed asynchronously
NoAsyncAccess = 3,
}
impl McObjectQualifier {
// used for serde, need &self
/// Check if this is `Default`
pub fn is_default(&self) -> bool {
*self == McObjectQualifier::Default
}
}
impl std::fmt::Display for McObjectQualifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)?;
Ok(())
}
}
//----------------------------------------------------------------------------------------------
// McSupportData
/// Metadata for measurement and calibration (characteristic or axis)
/// Instances of type Characteristic may have references to Axis
/// Not copy to inhibit that users unnecessary copy the data
#[derive(Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct McSupportData {
/// Measurement, Characteristic or Axis
pub object_type: McObjectType,
/// Access qualifier
#[serde(skip_serializing_if = "McObjectQualifier::is_default")]
#[serde(default)]
pub qualifier: McObjectQualifier,
/// Factor of the linear conversion `physical = value * factor + offset`, `None` for 1
#[serde(skip_serializing_if = "Option::is_none")]
pub factor: Option<f64>,
/// Offset of the linear conversion `physical = value * factor + offset`, `None` for 0
#[serde(skip_serializing_if = "Option::is_none")]
pub offset: Option<f64>,
/// Physical unit, or the description of an enumeration in the format `0 "OFF" 1 "ON"`
#[serde(default)]
#[serde(skip_serializing_if = "McText::is_empty")]
pub unit: McText,
/// Description text
#[serde(default)]
#[serde(skip_serializing_if = "McText::is_empty")]
pub comment: McText,
/// Lower limit, `None` for the limit of the value type
#[serde(skip_serializing_if = "Option::is_none")]
pub min: Option<f64>,
/// Upper limit, `None` for the limit of the value type
#[serde(skip_serializing_if = "Option::is_none")]
pub max: Option<f64>,
/// Step size for calibration
#[serde(skip_serializing_if = "Option::is_none")]
pub step: Option<f64>,
/// Name of the x axis referenced by a curve or map
#[serde(skip_serializing_if = "Option::is_none")]
pub x_axis_ref: Option<McIdentifier>,
/// Name of the y axis referenced by a map
#[serde(skip_serializing_if = "Option::is_none")]
pub y_axis_ref: Option<McIdentifier>,
/// Name of the input quantity of the x axis
#[serde(skip_serializing_if = "Option::is_none")]
pub x_axis_input_quantity: Option<McIdentifier>,
/// Name of the input quantity of the y axis
#[serde(skip_serializing_if = "Option::is_none")]
pub y_axis_input_quantity: Option<McIdentifier>,
/// Name of the conversion of a fixed x axis
#[serde(skip_serializing_if = "Option::is_none")]
pub x_axis_conv: Option<McIdentifier>,
/// Name of the conversion of a fixed y axis
#[serde(skip_serializing_if = "Option::is_none")]
pub y_axis_conv: Option<McIdentifier>,
}
impl Default for McSupportData {
fn default() -> Self {
McSupportData {
object_type: McObjectType::Unspecified,
qualifier: McObjectQualifier::Default,
factor: None,
offset: None,
unit: McText::default(),
comment: McText::default(),
min: None,
max: None,
step: None,
x_axis_ref: None,
y_axis_ref: None,
x_axis_input_quantity: None,
y_axis_input_quantity: None,
x_axis_conv: None,
y_axis_conv: None,
}
}
}
impl Clone for McSupportData {
fn clone(&self) -> Self {
log::trace!("Cloning McSupportData");
McSupportData {
object_type: self.object_type,
qualifier: self.qualifier,
factor: self.factor,
offset: self.offset,
unit: self.unit,
comment: self.comment,
min: self.min,
max: self.max,
step: self.step,
x_axis_ref: self.x_axis_ref,
y_axis_ref: self.y_axis_ref,
x_axis_input_quantity: self.x_axis_input_quantity,
y_axis_input_quantity: self.y_axis_input_quantity,
x_axis_conv: self.x_axis_conv,
y_axis_conv: self.y_axis_conv,
}
}
}
impl std::fmt::Display for McSupportData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "McSupportData:")?;
write!(f, " {:?}", self.object_type)?;
if self.qualifier != McObjectQualifier::Default {
write!(f, " {:?}", self.qualifier)?;
}
if let Some(v) = self.factor {
write!(f, " factor={}", v)?;
}
if let Some(v) = self.offset {
write!(f, " offset={}", v)?;
}
if !self.unit.is_empty() {
write!(f, " unit={}", self.unit)?;
}
if let Some(v) = self.min {
write!(f, " min={}", v)?;
}
if let Some(v) = self.max {
write!(f, " max={}", v)?;
}
if let Some(v) = self.step {
write!(f, " step={}", v)?;
}
if let Some(v) = self.x_axis_ref.as_ref() {
write!(f, " x_axis_ref={}", v)?;
}
if let Some(v) = self.y_axis_ref.as_ref() {
write!(f, " y_axis_ref={}", v)?;
}
if let Some(v) = self.x_axis_input_quantity.as_ref() {
write!(f, " x_axis_input_quantity={}", v)?;
}
if let Some(v) = self.y_axis_input_quantity.as_ref() {
write!(f, " y_axis_input_quantity={}", v)?;
}
if let Some(v) = self.x_axis_conv.as_ref() {
write!(f, " x_axis_conv={}", v)?;
}
if let Some(v) = self.y_axis_conv.as_ref() {
write!(f, " y_axis_conv={}", v)?;
}
if !self.comment.is_empty() {
write!(f, " {}", self.comment)?;
}
Ok(())
}
}
impl McSupportData {
/// Create metadata for an object of the given type, all other properties are unset
pub fn new(object_type: McObjectType) -> Self {
McSupportData {
object_type,
qualifier: McObjectQualifier::Default,
factor: None,
offset: None,
unit: McText::default(),
comment: McText::default(),
min: None,
max: None,
step: None,
x_axis_ref: None,
y_axis_ref: None,
x_axis_input_quantity: None,
y_axis_input_quantity: None,
x_axis_conv: None,
y_axis_conv: None,
}
}
/// Returns true if any descriptive metadata field (qualifier, unit, min, max, factor, offset, step, comment) has been explicitly set.
/// Does not consider object_type.
pub fn has_metadata(&self) -> bool {
self.qualifier != McObjectQualifier::Default
|| !self.unit.is_empty()
|| self.min.is_some()
|| self.max.is_some()
|| self.factor.is_some()
|| self.offset.is_some()
|| self.step.is_some()
|| !self.comment.is_empty()
}
/// Merge only the fields that are explicitly set in `other` into `self`.
/// Fields that are None / empty in `other` are left unchanged in `self`.
/// This allows multiple partial annotations (e.g. XCP_LIMITS + XCP_UNIT)
/// to each contribute their own slice of metadata without clobbering each other.
pub fn merge_metadata(&mut self, other: McSupportData) {
if !other.unit.is_empty() {
self.unit = other.unit;
}
if !other.comment.is_empty() {
self.comment = other.comment;
}
if other.min.is_some() {
self.min = other.min;
}
if other.max.is_some() {
self.max = other.max;
}
if other.factor.is_some() {
self.factor = other.factor;
}
if other.offset.is_some() {
self.offset = other.offset;
}
if other.step.is_some() {
self.step = other.step;
}
if other.object_type != McObjectType::Unspecified {
self.object_type = other.object_type;
}
if other.qualifier != McObjectQualifier::Default {
self.qualifier = other.qualifier;
}
}
// Read and write json string
/// Serialize to a JSON string
pub fn to_json_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
/// Deserialize from a JSON string, `None` if the string is not valid
pub fn from_json_string(s: &str) -> Option<Self> {
match serde_json::from_str(s) {
Ok(m) => Some(m),
Err(e) => {
log::error!("McSupportData from json failed: {}", e);
None
}
}
}
/// Convert a raw value to the physical value `value * factor + offset`
pub fn convert(&self, value: f64) -> f64 {
let mut result = value;
// physical_value = value * factor + offset !!
if let Some(factor) = self.factor {
result *= factor;
}
if let Some(offset) = self.offset {
result += offset;
}
result
}
//-----------------------------------------
// Setters for builder syntax (consume self, return Self — for use at construction time)
/// Set the object type
/// # Panics
/// If `object_type` is `Unspecified`
pub fn set_object_type(mut self, object_type: McObjectType) -> Self {
assert!(object_type != McObjectType::Unspecified);
self.object_type = object_type;
self
}
/// Set the access qualifier
pub fn set_qualifier(mut self, qualifier: McObjectQualifier) -> Self {
self.qualifier = qualifier;
self
}
/// Set the linear conversion `physical = value * factor + offset` and the physical unit
pub fn set_linear<T: Into<McText>>(mut self, factor: f64, offset: f64, unit: T) -> Self {
self.unit = unit.into();
self.factor = if (factor - 1.0).abs() > f64::EPSILON { Some(factor) } else { None };
self.offset = if offset.abs() > f64::EPSILON { Some(offset) } else { None };
self
}
/// Set the conversion factor, a factor of 1 is stored as `None`
pub fn set_factor(mut self, factor: Option<f64>) -> Self {
if let Some(factor) = factor {
self.factor = if (factor - 1.0).abs() > f64::EPSILON { Some(factor) } else { None };
return self;
}
self.factor = None;
self
}
/// Set the conversion offset, an offset of 0 is stored as `None`
pub fn set_offset(mut self, offset: Option<f64>) -> Self {
if let Some(offset) = offset {
self.offset = if offset.abs() > f64::EPSILON { Some(offset) } else { None };
return self;
}
self.offset = None;
self
}
/// Set the access qualifier to `ReadWrite`
pub fn set_read_write(mut self) -> Self {
self.qualifier = McObjectQualifier::ReadWrite;
self
}
/// Set the physical unit
pub fn set_unit<T: Into<McText>>(mut self, unit: T) -> Self {
self.unit = unit.into();
self
}
/// Set the description text
pub fn set_comment<T: Into<McText>>(mut self, comment: T) -> Self {
self.comment = comment.into();
self
}
/// Set the lower limit
pub fn set_min(mut self, min: Option<f64>) -> Self {
self.min = min;
self
}
/// Set the upper limit
pub fn set_max(mut self, max: Option<f64>) -> Self {
self.max = max;
self
}
/// Set the step size for calibration
pub fn set_step(mut self, step: Option<f64>) -> Self {
self.step = step;
self
}
/// Set the name of the x axis
pub fn set_x_axis_ref<T: Into<McIdentifier>>(mut self, x_axis_ref: Option<T>) -> Self {
self.x_axis_ref = x_axis_ref.map(|s| s.into());
self
}
/// Set the name of the y axis
pub fn set_y_axis_ref<T: Into<McIdentifier>>(mut self, y_axis_ref: Option<T>) -> Self {
self.y_axis_ref = y_axis_ref.map(|s| s.into());
self
}
/// Set the name of the input quantity of the x axis
pub fn set_x_axis_input_quantity<T: Into<McIdentifier>>(mut self, x_axis_input_quantity: Option<T>) -> Self {
self.x_axis_input_quantity = x_axis_input_quantity.map(|s| s.into());
self
}
/// Set the name of the input quantity of the y axis
pub fn set_y_axis_input_quantity<T: Into<McIdentifier>>(mut self, y_axis_input_quantity: Option<T>) -> Self {
self.y_axis_input_quantity = y_axis_input_quantity.map(|s| s.into());
self
}
/// Set the name of the conversion of a fixed x axis
pub fn set_x_axis_conv<T: Into<McIdentifier>>(mut self, x_axis_conv: Option<T>) -> Self {
self.x_axis_conv = x_axis_conv.map(|s| s.into());
self
}
/// Set the name of the conversion of a fixed y axis
pub fn set_y_axis_conv<T: Into<McIdentifier>>(mut self, y_axis_conv: Option<T>) -> Self {
self.y_axis_conv = y_axis_conv.map(|s| s.into());
self
}
//-----------------------------------------
// Mutation setters (&mut self — for updating an already-registered instance)
/// Set the access qualifier (mutable, for post-registration updates)
pub fn update_qualifier(&mut self, qualifier: McObjectQualifier) -> &mut Self {
self.qualifier = qualifier;
self
}
/// Set the physical unit string (mutable, for post-registration updates)
pub fn update_unit<T: Into<McText>>(&mut self, unit: T) -> &mut Self {
self.unit = unit.into();
self
}
/// Set a linear conversion rule and unit string (mutable, for post-registration updates)
/// physical_value = raw_value * factor + offset
pub fn update_linear<T: Into<McText>>(&mut self, factor: f64, offset: f64, unit: T) -> &mut Self {
self.unit = unit.into();
self.factor = if (factor - 1.0).abs() > f64::EPSILON { Some(factor) } else { None };
self.offset = if offset.abs() > f64::EPSILON { Some(offset) } else { None };
self
}
/// Set the lower display/calibration limit (mutable, for post-registration updates)
pub fn update_min(&mut self, min: Option<f64>) -> &mut Self {
self.min = min;
self
}
/// Set the upper display/calibration limit (mutable, for post-registration updates)
pub fn update_max(&mut self, max: Option<f64>) -> &mut Self {
self.max = max;
self
}
/// Set the calibration step width (mutable, for post-registration updates)
pub fn update_step(&mut self, step: Option<f64>) -> &mut Self {
self.step = step;
self
}
/// Set the comment string (mutable, for post-registration updates)
pub fn update_comment<T: Into<McText>>(&mut self, comment: T) -> &mut Self {
self.comment = comment.into();
self
}
//-----------------------------------------
/// Get the object type
/// If there is no MC semantic description (mc_support_data), return McObjectType::Unspecified
/// May be Measurement, Characteristic, Axis or Unspecified
pub fn get_object_type(&self) -> McObjectType {
self.object_type
}
/// This is a adjustable shared axis (subset of calibration object)
pub fn is_axis(&self) -> bool {
self.object_type.is_axis()
}
/// This is a characteristic object (subset of calibration object)
pub fn is_characteristic(&self) -> bool {
self.object_type.is_characteristic()
}
/// This describes an instance with calibration semantics
/// It is never modified by the target and may be modified by the calibration tool
pub fn is_calibration_object(&self) -> bool {
self.object_type.is_calibration_object()
}
/// This describes a measurement object instance
/// It is continously or sporadically modified by the target
pub fn is_measurement_object(&self) -> bool {
self.object_type.is_measurement_object()
}
/// Get the x-axis reference as McIdentifier
pub fn get_x_axis_ref(&self) -> Option<McIdentifier> {
self.x_axis_ref
}
/// Get the y-axis reference as McIdentifier
pub fn get_y_axis_ref(&self) -> Option<McIdentifier> {
self.y_axis_ref
}
/// Get the x-axis reference as McIdentifier
pub fn get_x_axis_input_quantity(&self) -> Option<McIdentifier> {
self.x_axis_input_quantity
}
/// Get the y-axis reference as McIdentifier
pub fn get_y_axis_input_quantity(&self) -> Option<McIdentifier> {
self.y_axis_input_quantity
}
/// Get the x-axis conversion as McIdentifier
pub fn get_x_axis_conv(&self) -> Option<McIdentifier> {
self.x_axis_conv
}
/// Get the y-axis conversion as McIdentifier
pub fn get_y_axis_conv(&self) -> Option<McIdentifier> {
self.y_axis_conv
}
/// Get the description (LongIdentifier, Description, Comment, ...) as &'static str
pub fn get_comment(&self) -> &'static str {
self.comment.as_str()
}
/// Get the minimum value for the type in physical units as f64
/// When the value can not be represented, it is rounded down
pub fn get_min(&self, value_type: McValueType) -> Option<f64> {
if self.min.is_some() {
return self.min;
}
if let Some(min) = value_type.get_min() {
return Some(self.convert(min));
}
value_type.get_min()
}
/// Get the maximum value for the type in physical units as f64
/// When the value can not be represented, it is rounded up
pub fn get_max(&self, value_type: McValueType) -> Option<f64> {
if self.max.is_some() {
return self.max;
}
if let Some(max) = value_type.get_max() {
return Some(self.convert(max));
}
value_type.get_max()
}
/// Get the physical conversion factor
pub fn get_factor(&self) -> Option<f64> {
if let Some(factor) = self.factor {
if factor != 1.0 {
return Some(factor);
}
}
None
}
/// Get the physical conversion offset
pub fn get_offset(&self) -> Option<f64> {
if let Some(offset) = self.offset {
if offset != 0.0 {
return Some(offset);
}
}
None
}
/// Get the physical unit as &'static str
pub fn get_unit(&self) -> &'static str {
self.unit.as_str()
}
/// Get the physical step size as f64
pub fn get_step(&self) -> Option<f64> {
if let Some(step) = self.step {
if step != 0.0 {
return Some(step);
}
}
None
}
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// Test module
#[cfg(test)]
mod mc_support_data_tests {
use super::*;
#[test]
fn test_mc_support_data() {
let m1 = McSupportData::new(McObjectType::Characteristic)
.set_min(Some(0.0))
.set_max(Some(100.0))
.set_step(Some(1.0))
.set_unit("Volts")
.set_comment("Voltage in Volts");
let m2 = m1.clone();
assert_eq!(m2.object_type, McObjectType::Characteristic);
assert_eq!(m2.min, Some(0.0));
assert_eq!(m2.max, Some(100.0));
assert_eq!(m2.step, Some(1.0));
assert_eq!(m2.unit.as_str(), "Volts");
assert_eq!(m2.comment.as_str(), "Voltage in Volts");
// Serialize, Deserialize
let m1 = McSupportData::new(McObjectType::Characteristic)
.set_min(Some(0.0))
.set_max(Some(100.0))
.set_step(Some(1.0))
.set_unit("Json-Unit")
.set_comment("Json-Comment");
let s = m1.to_json_string();
let m2 = McSupportData::from_json_string(&s).unwrap();
assert_eq!(m2.object_type, McObjectType::Characteristic);
assert_eq!(m2.min, Some(0.0));
assert_eq!(m2.max, Some(100.0));
assert_eq!(m2.unit.as_str(), "Json-Unit");
assert_eq!(m2.comment.as_str(), "Json-Comment");
let s1 = r#"{
"object_type":"Characteristic",
"unit":"Json-Unit",
"comment":"Json-Comment",
"min":0.0,
"max":50.0,
"step":1.0
}"#;
let m3 = McSupportData::from_json_string(s1).unwrap();
log::debug!("m3: {m2}");
assert_eq!(m3.object_type, McObjectType::Characteristic);
assert_eq!(m3.min, Some(0.0));
assert_eq!(m3.max, Some(50.0));
assert_eq!(m3.step, Some(1.0));
assert_eq!(m3.factor, None);
assert_eq!(m3.unit.as_str(), "Json-Unit");
assert_eq!(m3.comment.as_str(), "Json-Comment");
}
}