xcp_registry 0.1.0

Measurement and Calibration registry for XCP - pure Rust, no C dependency
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
// 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");
    }
}