autosar_data_abstraction/software_component/interface/
mod.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
use crate::{abstraction_element, AbstractionElement, ArPackage, AutosarAbstractionError, Element};
use autosar_data::ElementName;

mod clientserver;
mod senderreceiver;

pub use clientserver::*;
pub use senderreceiver::*;

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

/// A `ModeSwitchInterface` defines a set of modes that can be switched
///
/// Use [`ArPackage::create_mode_switch_interface`] to create a new mode switch interface
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ModeSwitchInterface(Element);
abstraction_element!(ModeSwitchInterface, ModeSwitchInterface);

impl AbstractPortInterface for ModeSwitchInterface {}

impl ModeSwitchInterface {
    /// Create a new `ModeSwitchInterface`
    pub(crate) fn new(name: &str, package: &ArPackage) -> Result<Self, AutosarAbstractionError> {
        let elements = package.element().get_or_create_sub_element(ElementName::Elements)?;
        let mode_switch_interface = elements.create_named_sub_element(ElementName::ModeSwitchInterface, name)?;

        Ok(Self(mode_switch_interface))
    }
}

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

/// A `ParameterInterface` defines a set of parameters that can be accessed
///
/// Use [`ArPackage::create_parameter_interface`] to create a new parameter interface
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ParameterInterface(Element);
abstraction_element!(ParameterInterface, ParameterInterface);

impl AbstractPortInterface for ParameterInterface {}

impl ParameterInterface {
    /// Create a new `ParameterInterface`
    pub(crate) fn new(name: &str, package: &ArPackage) -> Result<Self, AutosarAbstractionError> {
        let elements = package.element().get_or_create_sub_element(ElementName::Elements)?;
        let parameter_interface = elements.create_named_sub_element(ElementName::ParameterInterface, name)?;

        Ok(Self(parameter_interface))
    }
}

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

/// An `NvDataInterface` defines non-volatile data that can be accessed through the interface
///
/// Use [`ArPackage::create_nv_data_interface`] to create a new non-volatile data interface
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NvDataInterface(Element);
abstraction_element!(NvDataInterface, NvDataInterface);

impl AbstractPortInterface for NvDataInterface {}

impl NvDataInterface {
    /// Create a new `NvDataInterface`
    pub(crate) fn new(name: &str, package: &ArPackage) -> Result<Self, AutosarAbstractionError> {
        let elements = package.element().get_or_create_sub_element(ElementName::Elements)?;
        let nv_data_interface = elements.create_named_sub_element(ElementName::NvDataInterface, name)?;

        Ok(Self(nv_data_interface))
    }
}

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

/// A `TriggerInterface` declares a number of triggers that can be sent by an trigger source
///
/// Use [`ArPackage::create_trigger_interface`] to create a new trigger interface
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TriggerInterface(Element);
abstraction_element!(TriggerInterface, TriggerInterface);

impl AbstractPortInterface for TriggerInterface {}

impl TriggerInterface {
    /// Create a new `TriggerInterface`
    pub(crate) fn new(name: &str, package: &ArPackage) -> Result<Self, AutosarAbstractionError> {
        let elements = package.element().get_or_create_sub_element(ElementName::Elements)?;
        let trigger_interface = elements.create_named_sub_element(ElementName::TriggerInterface, name)?;

        Ok(Self(trigger_interface))
    }
}

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

/// The `AbstractPortInterface` trait is a marker trait for all port interfaces
pub trait AbstractPortInterface: AbstractionElement {}

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

/// The `PortInterface` enum represents all possible port interfaces
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PortInterface {
    /// The interface is a sender-receiver interface
    SenderReceiverInterface(SenderReceiverInterface),
    /// The interface is a client-server interface
    ClientServerInterface(ClientServerInterface),
    /// The interface is a mode switch interface
    ModeSwitchInterface(ModeSwitchInterface),
    /// The interface is a parameter interface
    ParameterInterface(ParameterInterface),
    /// The interface is a non-volatile data interface
    NvDataInterface(NvDataInterface),
    /// The interface is a trigger interface
    TriggerInterface(TriggerInterface),
}

impl AbstractionElement for PortInterface {
    fn element(&self) -> &Element {
        match self {
            PortInterface::SenderReceiverInterface(sender_receiver_interface) => sender_receiver_interface.element(),
            PortInterface::ClientServerInterface(client_server_interface) => client_server_interface.element(),
            PortInterface::ModeSwitchInterface(mode_switch_interface) => mode_switch_interface.element(),
            PortInterface::ParameterInterface(parameter_interface) => parameter_interface.element(),
            PortInterface::NvDataInterface(nv_data_interface) => nv_data_interface.element(),
            PortInterface::TriggerInterface(trigger_interface) => trigger_interface.element(),
        }
    }
}

impl AbstractPortInterface for PortInterface {}

impl TryFrom<Element> for PortInterface {
    type Error = AutosarAbstractionError;

    fn try_from(element: Element) -> Result<Self, Self::Error> {
        match element.element_name() {
            ElementName::SenderReceiverInterface => {
                Ok(PortInterface::SenderReceiverInterface(SenderReceiverInterface(element)))
            }
            ElementName::ClientServerInterface => {
                Ok(PortInterface::ClientServerInterface(ClientServerInterface(element)))
            }
            ElementName::ModeSwitchInterface => Ok(PortInterface::ModeSwitchInterface(ModeSwitchInterface(element))),
            ElementName::ParameterInterface => Ok(PortInterface::ParameterInterface(ParameterInterface(element))),
            ElementName::NvDataInterface => Ok(PortInterface::NvDataInterface(NvDataInterface(element))),
            ElementName::TriggerInterface => Ok(PortInterface::TriggerInterface(TriggerInterface(element))),
            _ => Err(AutosarAbstractionError::ConversionError {
                element,
                dest: "PortInterface".to_string(),
            }),
        }
    }
}

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

#[cfg(test)]
mod test {
    use crate::software_component::AbstractSwComponentType;

    use super::*;
    use autosar_data::{AutosarModel, AutosarVersion};

    #[test]
    fn test_interfaces() {
        let model = AutosarModel::new();
        let _file = model.create_file("filename", AutosarVersion::LATEST).unwrap();
        let package = ArPackage::get_or_create(&model, "/package").unwrap();

        let sender_receiver_interface = package
            .create_sender_receiver_interface("sender_receiver_interface")
            .unwrap();
        let client_server_interface = package
            .create_client_server_interface("client_server_interface")
            .unwrap();
        let mode_switch_interface = package.create_mode_switch_interface("mode_switch_interface").unwrap();
        let parameter_interface = package.create_parameter_interface("parameter_interface").unwrap();
        let nv_data_interface = package.create_nv_data_interface("nv_data_interface").unwrap();
        let trigger_interface = package.create_trigger_interface("trigger_interface").unwrap();

        let composition = package.create_composition_sw_component_type("composition").unwrap();

        let port_1 = composition.create_p_port("port_1", &sender_receiver_interface).unwrap();
        assert!(matches!(
            port_1.port_interface(),
            Ok(PortInterface::SenderReceiverInterface(interface)) if interface == sender_receiver_interface
        ));
        assert_eq!(
            port_1.port_interface().unwrap().element(),
            sender_receiver_interface.element()
        );

        let port_2 = composition.create_p_port("port_2", &client_server_interface).unwrap();
        assert!(matches!(
            port_2.port_interface(),
            Ok(PortInterface::ClientServerInterface(interface)) if interface == client_server_interface
        ));
        assert_eq!(
            port_2.port_interface().unwrap().element(),
            client_server_interface.element()
        );

        let port_3 = composition.create_p_port("port_3", &mode_switch_interface).unwrap();
        assert!(matches!(
            port_3.port_interface(),
            Ok(PortInterface::ModeSwitchInterface(interface)) if interface == mode_switch_interface
        ));
        assert_eq!(
            port_3.port_interface().unwrap().element(),
            mode_switch_interface.element()
        );

        let port_4 = composition.create_p_port("port_4", &parameter_interface).unwrap();
        assert!(matches!(
            port_4.port_interface(),
            Ok(PortInterface::ParameterInterface(interface)) if interface == parameter_interface
        ));
        assert_eq!(
            port_4.port_interface().unwrap().element(),
            parameter_interface.element()
        );

        let port_5 = composition.create_p_port("port_5", &nv_data_interface).unwrap();
        assert!(matches!(
            port_5.port_interface(),
            Ok(PortInterface::NvDataInterface(interface)) if interface == nv_data_interface
        ));
        assert_eq!(port_5.port_interface().unwrap().element(), nv_data_interface.element());

        let port_6 = composition.create_p_port("port_6", &trigger_interface).unwrap();
        assert!(matches!(
            port_6.port_interface(),
            Ok(PortInterface::TriggerInterface(interface)) if interface == trigger_interface
        ));
        assert_eq!(port_6.port_interface().unwrap().element(), trigger_interface.element());
    }
}