autosar_data_abstraction/
lib.rs

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
//! # Crate autosar-data-abstraction
//!
//! This crate provides an abstraction layer for the AUTOSAR data model.
//! It is built on top of the crate `autosar-data` and provides complex interactions with
//! the model on top of the elementary operations of `autosar-data`.
//!
//! Since the AUTOSAR data model is very complex and has many different types of elements,
//! this crate does not aim to provide full coverage of all classes.
//! Instead the focus is on the most common classes and their interactions.
//!
//! Any other data can still be accessed through the basic operations of `autosar-data`, because the
//! calls to `autosar-data` and `autosar-data-abstraction` can be mixed freely.
//!
//! ## Example
//!
//! ```rust
//! # use autosar_data::*;
//! # use autosar_data_abstraction::*;
//! # use autosar_data_abstraction::communication::*;
//! let model = AutosarModel::new();
//! let _file = model.create_file("file.arxml", AutosarVersion::Autosar_00049).unwrap();
//! let package_1 = ArPackage::get_or_create(&model, "/System").unwrap();
//! let system = package_1.create_system("System", SystemCategory::SystemExtract).unwrap();
//! let package_2 = ArPackage::get_or_create(&model, "/Clusters").unwrap();
//!
//! // create an Ethernet cluster and a physical channel for VLAN 33
//! let eth_cluster = system.create_ethernet_cluster("EthCluster", &package_2).unwrap();
//! let vlan_info = EthernetVlanInfo {
//!     vlan_id: 33,
//!     vlan_name: "VLAN_33".to_string(),
//! };
//! let eth_channel = eth_cluster
//!     .create_physical_channel("EthChannel", Some(vlan_info))
//!     .unwrap();
//! let vlan_info_2 = eth_channel.vlan_info().unwrap();
//!
//! // create an ECU instance and connect it to the Ethernet channel
//! let package_3 = ArPackage::get_or_create(&model, "/Ecus").unwrap();
//! let ecu_instance_a = system.create_ecu_instance("Ecu_A", &package_3).unwrap();
//! let ethctrl = ecu_instance_a
//!     .create_ethernet_communication_controller(
//!         "EthernetController",
//!         Some("ab:cd:ef:01:02:03".to_string())
//!     )
//!     .unwrap();
//! let channels_iter = ethctrl.connected_channels();
//! ethctrl
//!     .connect_physical_channel("Ecu_A_connector", &eth_channel)
//!     .unwrap();
//! let channels_iter = ethctrl.connected_channels();
//!
//! // ...
//! ```

#![warn(missing_docs)]

use autosar_data::{AutosarDataError, AutosarModel, Element, EnumItem};
use thiserror::Error;

// modules that are visible in the API
pub mod communication;
pub mod datatype;
pub mod ecu_configuration;
pub mod software_component;

// internal modules that only serve to split up the code
mod arpackage;
mod ecuinstance;
mod system;

// export the content of the internal modules
pub use arpackage::ArPackage;
pub use ecuinstance::*;
pub use system::*;

/// The error type `AutosarAbstractionError` wraps all errors from the crate
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum AutosarAbstractionError {
    /// converting an autosar-data element to a class in the abstract model failed
    #[error("conversion error: could not convert {} to {}", .element.element_name(), dest)]
    ConversionError {
        /// the element that could not be converted
        element: Element,
        /// the name of the destination type
        dest: String,
    },

    /// converting an autosar-data element to a class in the abstract model failed
    #[error("value conversion error: could not convert {} to {}", .value, .dest)]
    ValueConversionError {
        /// the value that could not be converted
        value: String,
        /// the name of the destination type
        dest: String,
    },

    /// `ModelError` wraps [`AutosarDataError`] errors from autosar-data operations, e.g.
    /// [`AutosarDataError::ItemDeleted`], [`AutosarDataError::IncorrectContentType`], ...
    #[error("model error: {}", .0)]
    ModelError(AutosarDataError),

    /// an invalid Autosar path was passed as a parameter
    #[error("invalid path: {}", .0)]
    InvalidPath(String),

    /// an item could not be created because another item already fulfills its role in the model
    #[error("the item already exists")]
    ItemAlreadyExists,

    /// the function parameter has an invalid value
    #[error("invalid parameter: {}", .0)]
    InvalidParameter(String),
}

impl From<AutosarDataError> for AutosarAbstractionError {
    fn from(err: AutosarDataError) -> Self {
        AutosarAbstractionError::ModelError(err)
    }
}

//#########################################################

/// The `AbstractionElement` trait is implemented by all classes that represent elements in the AUTOSAR model.
pub trait AbstractionElement: Clone + PartialEq + TryFrom<autosar_data::Element> {
    /// Get the underlying `Element` from the abstraction element
    #[must_use]
    fn element(&self) -> &Element;

    /// Get the item name of the element
    #[must_use]
    fn name(&self) -> Option<String> {
        self.element().item_name()
    }

    // fn set_timestamp(&self) {
    //     todo!()
    // }
}

macro_rules! abstraction_element {
    ($name: ident, $base_elem: ident) => {
        impl TryFrom<autosar_data::Element> for $name {
            type Error = AutosarAbstractionError;

            fn try_from(element: autosar_data::Element) -> Result<Self, Self::Error> {
                if element.element_name() == autosar_data::ElementName::$base_elem {
                    Ok($name(element))
                } else {
                    Err(AutosarAbstractionError::ConversionError {
                        element,
                        dest: stringify!($name).to_string(),
                    })
                }
            }
        }

        impl AbstractionElement for $name {
            fn element(&self) -> &autosar_data::Element {
                &self.0
            }
        }

        impl From<$name> for autosar_data::Element {
            fn from(val: $name) -> Self {
                val.0
            }
        }
    };
}

pub(crate) use abstraction_element;

//#########################################################

/// The `ByteOrder` is used to define the order of bytes in a multi-byte value
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ByteOrder {
    /// Most significant byte at the lowest address = big endian
    MostSignificantByteFirst,
    /// Most significant byte at the highest address = little endian
    MostSignificantByteLast,
    /// The byte order is not defined / not relevant
    Opaque,
}

impl TryFrom<EnumItem> for ByteOrder {
    type Error = AutosarAbstractionError;

    fn try_from(value: EnumItem) -> Result<Self, Self::Error> {
        match value {
            EnumItem::MostSignificantByteFirst => Ok(ByteOrder::MostSignificantByteFirst),
            EnumItem::MostSignificantByteLast => Ok(ByteOrder::MostSignificantByteLast),
            EnumItem::Opaque => Ok(ByteOrder::Opaque),
            _ => Err(AutosarAbstractionError::ValueConversionError {
                value: value.to_string(),
                dest: "ByteOrder".to_string(),
            }),
        }
    }
}

impl From<ByteOrder> for EnumItem {
    fn from(value: ByteOrder) -> Self {
        match value {
            ByteOrder::MostSignificantByteFirst => EnumItem::MostSignificantByteFirst,
            ByteOrder::MostSignificantByteLast => EnumItem::MostSignificantByteLast,
            ByteOrder::Opaque => EnumItem::Opaque,
        }
    }
}

//#########################################################

macro_rules! reflist_iterator {
    ($name: ident, $output: ident) => {
        #[doc(hidden)]
        pub struct $name {
            items: Vec<autosar_data::WeakElement>,
            position: usize,
        }

        impl $name {
            pub(crate) fn new(items: Vec<autosar_data::WeakElement>) -> Self {
                Self { items, position: 0 }
            }
        }

        impl Iterator for $name {
            type Item = $output;

            fn next(&mut self) -> Option<Self::Item> {
                while self.position < self.items.len() {
                    if let Some(out) = self.items[self.position]
                        .upgrade()
                        .and_then(|ref_elem| ref_elem.named_parent().ok().flatten())
                        .and_then(|elem| $output::try_from(elem).ok())
                    {
                        self.position += 1;
                        return Some(out);
                    }
                    self.position += 1;
                }

                self.position = usize::MAX;
                None
            }
        }

        impl std::iter::FusedIterator for $name {}
    };
}

pub(crate) use reflist_iterator;

//##################################################################

pub(crate) fn make_unique_name(model: &AutosarModel, base_path: &str, initial_name: &str) -> String {
    let mut full_path = format!("{base_path}/{initial_name}");
    let mut name = initial_name.to_string();
    let mut counter = 0;
    while model.get_element_by_path(&full_path).is_some() {
        counter += 1;
        name = format!("{initial_name}_{counter}");
        full_path = format!("{base_path}/{name}");
    }

    name
}

//#########################################################

#[cfg(test)]
mod test {
    use autosar_data::AutosarModel;

    use super::*;

    #[test]
    fn errors() {
        let model = AutosarModel::new();

        let err = AutosarAbstractionError::ConversionError {
            element: model.root_element(),
            dest: "TEST".to_string(),
        };
        let string = format!("{err}");
        assert!(!string.is_empty());

        let err = AutosarAbstractionError::InvalidPath("lorem ipsum".to_string());
        let string = format!("{err}");
        assert!(!string.is_empty());

        let err = AutosarAbstractionError::ItemAlreadyExists;
        let string = format!("{err}");
        assert!(!string.is_empty());
    }
}