autosar_data_abstraction/communication/transport_layer/
doip_tp.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
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
use crate::communication::{EthernetCluster, PduTriggering};
use crate::{abstraction_element, AbstractionElement, ArPackage, AutosarAbstractionError};
use autosar_data::{Element, ElementName};

/// Container for `DoIp` TP configuration
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DoIpTpConfig(Element);
abstraction_element!(DoIpTpConfig, DoIpTpConfig);

impl DoIpTpConfig {
    pub(crate) fn new(
        name: &str,
        package: &ArPackage,
        cluster: &EthernetCluster,
    ) -> Result<Self, AutosarAbstractionError> {
        let pkg_elem = package.element().get_or_create_sub_element(ElementName::Elements)?;

        let tp_config_elem = pkg_elem.create_named_sub_element(ElementName::DoIpTpConfig, name)?;
        tp_config_elem
            .create_sub_element(ElementName::CommunicationClusterRef)?
            .set_reference_target(cluster.element())?;

        Ok(Self(tp_config_elem))
    }

    /// create a new `DoIpLogicAddress`
    pub fn create_doip_logic_address(
        &self,
        name: &str,
        address: u32,
    ) -> Result<DoIpLogicAddress, AutosarAbstractionError> {
        let logic_addresses_elem = self
            .element()
            .get_or_create_sub_element(ElementName::DoIpLogicAddresss)?;
        DoIpLogicAddress::new(name, &logic_addresses_elem, address)
    }

    /// iterate over all `DoIpLogicAddresss`
    pub fn doip_logic_addresses(&self) -> impl Iterator<Item = DoIpLogicAddress> {
        self.element()
            .get_sub_element(ElementName::DoIpLogicAddresss)
            .into_iter()
            .flat_map(|elem| elem.sub_elements())
            .map(DoIpLogicAddress)
    }

    /// create a new `DoIpTpConnection`
    pub fn create_doip_tp_connection(
        &self,
        name: Option<&str>,
        source: &DoIpLogicAddress,
        target: &DoIpLogicAddress,
        tp_sdu_triggering: &PduTriggering,
    ) -> Result<DoIpTpConnection, AutosarAbstractionError> {
        let tp_connections_elem = self.element().get_or_create_sub_element(ElementName::TpConnections)?;
        DoIpTpConnection::new(name, &tp_connections_elem, source, target, tp_sdu_triggering)
    }

    /// iterate over all `DoIpTpConnections`
    pub fn doip_tp_connections(&self) -> impl Iterator<Item = DoIpTpConnection> {
        self.element()
            .get_sub_element(ElementName::TpConnections)
            .into_iter()
            .flat_map(|elem| elem.sub_elements())
            .map(DoIpTpConnection)
    }
}

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

/// This element defines the logical address of a `DoIp` connection
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DoIpLogicAddress(Element);
abstraction_element!(DoIpLogicAddress, DoIpLogicAddress);

impl DoIpLogicAddress {
    pub(crate) fn new(name: &str, parent: &Element, address: u32) -> Result<Self, AutosarAbstractionError> {
        let logic_address_elem = parent.create_named_sub_element(ElementName::DoIpLogicAddress, name)?;
        logic_address_elem
            .create_sub_element(ElementName::Address)?
            .set_character_data(u64::from(address))?;
        Ok(Self(logic_address_elem))
    }

    /// get the address of this `DoIpLogicAddress`
    #[must_use]
    pub fn address(&self) -> Option<u32> {
        self.element()
            .get_sub_element(ElementName::Address)
            .and_then(|elem| elem.character_data())
            .and_then(|data| data.parse_integer())
    }
}

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

/// The `DoIpTpConnection` defines a `DoIp` transport protocol connection
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DoIpTpConnection(Element);
abstraction_element!(DoIpTpConnection, DoIpTpConnection);

impl DoIpTpConnection {
    pub(crate) fn new(
        name: Option<&str>,
        parent: &Element,
        source: &DoIpLogicAddress,
        target: &DoIpLogicAddress,
        tp_sdu_triggering: &PduTriggering,
    ) -> Result<Self, AutosarAbstractionError> {
        let tp_connection_elem = parent.create_sub_element(ElementName::DoIpTpConnection)?;
        if let Some(name) = name {
            tp_connection_elem.create_named_sub_element(ElementName::Ident, name)?;
        }
        tp_connection_elem
            .create_sub_element(ElementName::DoIpSourceAddressRef)?
            .set_reference_target(source.element())?;
        tp_connection_elem
            .create_sub_element(ElementName::DoIpTargetAddressRef)?
            .set_reference_target(target.element())?;
        tp_connection_elem
            .create_sub_element(ElementName::TpSduRef)?
            .set_reference_target(tp_sdu_triggering.element())?;
        Ok(Self(tp_connection_elem))
    }

    /// get the name of the connection
    ///
    /// In early versions of the Autosar standard, TpConnections were not identifiable.
    /// This was fixed later by adding the Ident sub-element. This method returns the name
    /// provied in the Ident element, if it exists.
    #[must_use]
    pub fn name(&self) -> Option<String> {
        self.element()
            .get_sub_element(ElementName::Ident)
            .and_then(|elem| elem.item_name())
    }

    /// set the name of the connection
    pub fn set_name(&self, name: &str) -> Result<(), AutosarAbstractionError> {
        if let Some(ident_elem) = self.element().get_sub_element(ElementName::Ident) {
            ident_elem.set_item_name(name)?;
        } else {
            self.element().create_named_sub_element(ElementName::Ident, name)?;
        }
        Ok(())
    }

    /// get the source `DoIpLogicAddress`
    #[must_use]
    pub fn source(&self) -> Option<DoIpLogicAddress> {
        self.element()
            .get_sub_element(ElementName::DoIpSourceAddressRef)
            .and_then(|elem| elem.get_reference_target().ok())
            .and_then(|elem| DoIpLogicAddress::try_from(elem).ok())
    }

    /// get the target `DoIpLogicAddress`
    #[must_use]
    pub fn target(&self) -> Option<DoIpLogicAddress> {
        self.element()
            .get_sub_element(ElementName::DoIpTargetAddressRef)
            .and_then(|elem| elem.get_reference_target().ok())
            .and_then(|elem| DoIpLogicAddress::try_from(elem).ok())
    }

    /// get the `PduTriggering` for this connection
    #[must_use]
    pub fn tp_sdu_triggering(&self) -> Option<PduTriggering> {
        self.element()
            .get_sub_element(ElementName::TpSduRef)
            .and_then(|elem| elem.get_reference_target().ok())
            .and_then(|elem| PduTriggering::try_from(elem).ok())
    }
}

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

/// This element defines the `DoIp` configuration for a specific Ecu
///
/// Only available in `Autosar_00048` and later
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DoIpConfig(Element);
abstraction_element!(DoIpConfig, DoIpConfig);

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

#[cfg(test)]
mod test {
    use super::*;
    use crate::{
        communication::{
            CommunicationDirection, IPv4AddressSource, NetworkEndpointAddress, SocketAddressType, TpConfig,
        },
        SystemCategory,
    };
    use autosar_data::{AutosarModel, AutosarVersion};

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

        let system = package.create_system("system", SystemCategory::EcuExtract).unwrap();
        let eth_cluster = system.create_ethernet_cluster("can_cluster", &package).unwrap();
        let eth_channel = eth_cluster.create_physical_channel("can_channel", None).unwrap();
        let ecu_instance = system.create_ecu_instance("ecu_instance", &package).unwrap();
        let communication_controller = ecu_instance
            .create_ethernet_communication_controller("can_ctrl", Some("ab:cd:ef:01:02:03".to_string()))
            .unwrap();
        let _connector = communication_controller
            .connect_physical_channel("name", &eth_channel)
            .unwrap();

        // create socket #1
        let network_address_1 = NetworkEndpointAddress::IPv4 {
            address: Some("192.168.0.1".to_string()),
            address_source: Some(IPv4AddressSource::Fixed),
            default_gateway: Some("192.168.0.200".to_string()),
            network_mask: Some("255.255.255.0".to_string()),
        };
        let network_endpoint_1 = eth_channel
            .create_network_endpoint("local_endpoint", network_address_1, None)
            .unwrap();
        let udp_port_1 = TpConfig::UdpTp {
            port_number: Some(1234),
            port_dynamically_assigned: None,
        };
        let socket_type_1 = SocketAddressType::Unicast(Some(ecu_instance.clone()));
        let socket_address_tcp_1 = eth_channel
            .create_socket_address("ServerSocket", &network_endpoint_1, &udp_port_1, socket_type_1)
            .unwrap();

        // ceate socket #2
        let network_address_2 = NetworkEndpointAddress::IPv4 {
            address: Some("192.168.0.2".to_string()),
            address_source: Some(IPv4AddressSource::Fixed),
            default_gateway: Some("192.168.0.200".to_string()),
            network_mask: Some("255.255.255.0".to_string()),
        };
        let network_endpoint_2 = eth_channel
            .create_network_endpoint("remote_endpoint", network_address_2, None)
            .unwrap();
        let udp_port_2 = TpConfig::UdpTp {
            port_number: Some(5678),
            port_dynamically_assigned: None,
        };
        let socket_type_2 = SocketAddressType::Unicast(None);
        let socket_address_tcp_2 = eth_channel
            .create_socket_address("ClientSocket", &network_endpoint_2, &udp_port_2, socket_type_2)
            .unwrap();

        // create a connection (V2)
        let (static_socket_connection_a, static_socket_connection_b) = eth_channel
            .create_static_socket_connection_pair(
                "StaticSocketConnection",
                &socket_address_tcp_1,
                &socket_address_tcp_2,
                None,
            )
            .unwrap();

        // create a DCM_I_Pdu
        let dcm_i_pdu = system.create_dcm_ipdu("Diag", &package, 1024).unwrap();

        // create an IPduIdentifier, which is used to map the PDU to both sides of the socket connection
        let ipdu_identifier_set_package = ArPackage::get_or_create(&model, "/Network/IpduIdentifierSets").unwrap();
        let socon_ipdu_identifier_set = system
            .create_socket_connection_ipdu_identifier_set("IpduIdentifierSet", &ipdu_identifier_set_package)
            .unwrap();
        let ipdu_identifier = socon_ipdu_identifier_set
            .create_socon_ipdu_identifier("IpduIdentifier", &dcm_i_pdu, &eth_channel, Some(0x1000), None, None)
            .unwrap();

        // trigger the DCM_I_Pdu on the connection
        static_socket_connection_a
            .add_ipdu_identifier(&ipdu_identifier)
            .unwrap();
        static_socket_connection_b
            .add_ipdu_identifier(&ipdu_identifier)
            .unwrap();
        let pdu_triggering = ipdu_identifier.pdu_triggering().unwrap();
        pdu_triggering
            .create_pdu_port(&ecu_instance, CommunicationDirection::Out)
            .unwrap();

        let doip_tp_config = system
            .create_doip_tp_config("doip_tp_config", &package, &eth_cluster)
            .unwrap();

        let doip_logic_address_source = doip_tp_config.create_doip_logic_address("addr_source", 1).unwrap();
        assert_eq!(doip_logic_address_source.address(), Some(1));
        let doip_logic_address_target = doip_tp_config.create_doip_logic_address("addr_target", 2).unwrap();
        assert_eq!(doip_logic_address_target.address(), Some(2));

        let doip_tp_connection = doip_tp_config
            .create_doip_tp_connection(
                Some("connection_name"),
                &doip_logic_address_source,
                &doip_logic_address_target,
                &pdu_triggering,
            )
            .unwrap();
        assert_eq!(doip_tp_connection.source(), Some(doip_logic_address_source.clone()));
        assert_eq!(doip_tp_connection.target(), Some(doip_logic_address_target.clone()));
        assert_eq!(doip_tp_connection.tp_sdu_triggering(), Some(pdu_triggering.clone()));

        assert_eq!(doip_tp_connection.name().unwrap(), "connection_name");
        doip_tp_connection.set_name("other_name").unwrap();
        assert_eq!(doip_tp_connection.name().unwrap(), "other_name");
        doip_tp_connection
            .element()
            .remove_sub_element_kind(ElementName::Ident)
            .unwrap();
        assert_eq!(doip_tp_connection.name(), None);

        let doip_tp_connections: Vec<DoIpTpConnection> = doip_tp_config.doip_tp_connections().collect();
        assert_eq!(doip_tp_connections.len(), 1);
        assert_eq!(doip_tp_connections[0], doip_tp_connection);

        let doip_logic_addresses: Vec<DoIpLogicAddress> = doip_tp_config.doip_logic_addresses().collect();
        assert_eq!(doip_logic_addresses.len(), 2);
        assert_eq!(doip_logic_addresses[0], doip_logic_address_source);
    }
}