autosar_data_abstraction/communication/physical_channel/ethernet/
socketaddress.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
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
use crate::communication::{
    AbstractPhysicalChannel, ConsumedServiceInstanceV1, EthernetPhysicalChannel, NetworkEndpoint,
    ProvidedServiceInstanceV1, StaticSocketConnection, TcpRole,
};
use crate::{abstraction_element, AbstractionElement, AutosarAbstractionError, EcuInstance};
use autosar_data::{Element, ElementName};

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

/// A socket address establishes the link between one or more ECUs and a `NetworkEndpoint`.
/// It contains all settings that are relevant for this combination.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SocketAddress(Element);
abstraction_element!(SocketAddress, SocketAddress);

impl SocketAddress {
    pub(crate) fn new(
        name: &str,
        channel: &EthernetPhysicalChannel,
        network_endpoint: &NetworkEndpoint,
        tp_config: &TpConfig,
        sa_type: SocketAddressType,
    ) -> Result<Self, AutosarAbstractionError> {
        let channel_elem = channel.element();
        let (unicast, ecu_instances) = match sa_type {
            SocketAddressType::Unicast(Some(ecu_instance)) => (true, vec![ecu_instance]),
            SocketAddressType::Unicast(None) => (true, vec![]),
            SocketAddressType::Multicast(ecu_instances) => (false, ecu_instances),
        };

        // TCP connections don't work using multicast IP addresses
        if !unicast && matches!(tp_config, TpConfig::TcpTp { .. }) {
            return Err(AutosarAbstractionError::InvalidParameter(
                "TCP is incomptible with multicasting".to_string(),
            ));
        }
        // extension: check if the address is valid for multicasting?
        // IPv4: 224.0.0.0 - 239.255.255.255
        // IPv6: FFxx:/12

        // get the connector for each ECU in advance, so that nothing needs to be cleaned up if there is a problem here
        let connectors = ecu_instances
            .iter()
            .filter_map(|ecu_instance| channel.ecu_connector(ecu_instance).map(|conn| conn.element().clone()))
            .collect::<Vec<_>>();
        if connectors.len() != ecu_instances.len() {
            return Err(AutosarAbstractionError::InvalidParameter(
                "All EcuInstances must be connected to the EthernetPhysicalChannel".to_string(),
            ));
        }

        let elem = channel_elem
            .get_or_create_sub_element(ElementName::SoAdConfig)?
            .get_or_create_sub_element(ElementName::SocketAddresss)?
            .create_named_sub_element(ElementName::SocketAddress, name)?;

        if unicast {
            if !connectors.is_empty() {
                elem.create_sub_element(ElementName::ConnectorRef)
                    .unwrap()
                    .set_reference_target(&connectors[0])
                    .unwrap();
            }
        } else {
            let mc_connectors = elem.create_sub_element(ElementName::MulticastConnectorRefs)?;
            for conn in &connectors {
                mc_connectors
                    .create_sub_element(ElementName::MulticastConnectorRef)?
                    .set_reference_target(conn)?;
            }
        }

        let ae_name = format!("{name}_AE");
        let ae = elem.create_named_sub_element(ElementName::ApplicationEndpoint, &ae_name)?;
        ae.create_sub_element(ElementName::NetworkEndpointRef)?
            .set_reference_target(network_endpoint.element())?;
        let tp_configuration = ae.create_sub_element(ElementName::TpConfiguration)?;
        match tp_config {
            TpConfig::TcpTp {
                port_number,
                port_dynamically_assigned,
            } => {
                let tcptp = tp_configuration.create_sub_element(ElementName::TcpTp)?;
                let tcptp_port = tcptp.create_sub_element(ElementName::TcpTpPort)?;
                // PortNumber and DynamicallyAssigned are mutually exclusive.
                // The attribute DynamicallyAssigned is deprecated starting in Autosar 4.5.0
                if let Some(portnum) = port_number {
                    tcptp_port
                        .create_sub_element(ElementName::PortNumber)?
                        .set_character_data(portnum.to_string())?;
                } else if let Some(dyn_assign) = port_dynamically_assigned {
                    tcptp_port
                        .create_sub_element(ElementName::DynamicallyAssigned)?
                        .set_character_data(*dyn_assign)?;
                }
            }
            TpConfig::UdpTp {
                port_number,
                port_dynamically_assigned,
            } => {
                let udptp_port = tp_configuration
                    .create_sub_element(ElementName::UdpTp)?
                    .create_sub_element(ElementName::UdpTpPort)?;
                // PortNumber and DynamicallyAssigned are mutually exclusive.
                // The attribute DynamicallyAssigned is deprecated starting in Autosar 4.5.0
                if let Some(portnum) = port_number {
                    udptp_port
                        .create_sub_element(ElementName::PortNumber)?
                        .set_character_data(portnum.to_string())?;
                } else if let Some(dyn_assign) = port_dynamically_assigned {
                    let boolstr = if *dyn_assign { "true" } else { "false" };
                    udptp_port
                        .create_sub_element(ElementName::DynamicallyAssigned)?
                        .set_character_data(boolstr)?;
                }
            }
        }

        Ok(Self(elem))
    }

    /// get the network endpoint of this `SocketAddress`
    #[must_use]
    pub fn network_endpoint(&self) -> Option<NetworkEndpoint> {
        let ne = self
            .element()
            .get_sub_element(ElementName::ApplicationEndpoint)?
            .get_sub_element(ElementName::NetworkEndpointRef)?
            .get_reference_target()
            .ok()?;
        ne.try_into().ok()
    }

    /// get the socket address type: unicast / multicast, as well as the connected ecus
    #[must_use]
    pub fn socket_address_type(&self) -> Option<SocketAddressType> {
        if let Some(connector_ref) = self.0.get_sub_element(ElementName::ConnectorRef) {
            let ecu = EcuInstance::try_from(connector_ref.get_reference_target().ok()?.named_parent().ok()??).ok()?;
            Some(SocketAddressType::Unicast(Some(ecu)))
        } else if let Some(mcr) = self.0.get_sub_element(ElementName::MulticastConnectorRefs) {
            let ecus = mcr
                .sub_elements()
                .filter_map(|cr| {
                    cr.get_reference_target()
                        .ok()
                        .and_then(|conn| conn.named_parent().ok().flatten())
                })
                .filter_map(|ecu_elem| EcuInstance::try_from(ecu_elem).ok())
                .collect::<Vec<_>>();
            Some(SocketAddressType::Multicast(ecus))
        } else {
            None
        }
    }

    /// add an `EcuInstance` to this multicast `SocketAddress`
    pub fn add_multicast_ecu(&self, ecu: &EcuInstance) -> Result<(), AutosarAbstractionError> {
        let socket_type = self.socket_address_type();
        match socket_type {
            Some(SocketAddressType::Multicast(multicast_ecus)) => {
                // extend the list of multicast EcuInstances if needed
                if !multicast_ecus.contains(ecu) {
                    let Some(connector) = self.physical_channel()?.ecu_connector(ecu) else {
                        return Err(AutosarAbstractionError::InvalidParameter(
                            "EcuInstance is not connected to the EthernetPhysicalChannel".to_string(),
                        ));
                    };
                    let mcr = self.0.get_or_create_sub_element(ElementName::MulticastConnectorRefs)?;
                    let mc_ref = mcr.create_sub_element(ElementName::MulticastConnectorRef)?;
                    mc_ref.set_reference_target(connector.element())?;
                }
            }
            None => {
                // add the first EcuInstance to this multicast SocketAddress
                let Some(connector) = self.physical_channel()?.ecu_connector(ecu) else {
                    return Err(AutosarAbstractionError::InvalidParameter(
                        "EcuInstance is not connected to the EthernetPhysicalChannel".to_string(),
                    ));
                };
                let mcr = self.0.get_or_create_sub_element(ElementName::MulticastConnectorRefs)?;
                let mc_ref = mcr.create_sub_element(ElementName::MulticastConnectorRef)?;
                mc_ref.set_reference_target(connector.element())?;
            }
            Some(SocketAddressType::Unicast(_)) => {
                return Err(AutosarAbstractionError::InvalidParameter(
                    "This SocketAddress is not a multicast socket".to_string(),
                ));
            }
        }

        Ok(())
    }

    /// set the `EcuInstance` for this unicast `SocketAddress`
    pub fn set_unicast_ecu(&self, ecu: &EcuInstance) -> Result<(), AutosarAbstractionError> {
        let socket_type = self.socket_address_type();
        match socket_type {
            None | Some(SocketAddressType::Unicast(_)) => {
                let channel = self.physical_channel()?;
                let Some(connector) = channel.ecu_connector(ecu) else {
                    return Err(AutosarAbstractionError::InvalidParameter(
                        "EcuInstance is not connected to the EthernetPhysicalChannel".to_string(),
                    ));
                };
                self.0
                    .get_or_create_sub_element(ElementName::ConnectorRef)?
                    .set_reference_target(connector.element())?;
            }
            Some(SocketAddressType::Multicast(_)) => {
                return Err(AutosarAbstractionError::InvalidParameter(
                    "This SocketAddress is not a unicast socket".to_string(),
                ));
            }
        }

        Ok(())
    }

    /// get the transport protocol settings for this `SocketAddress`
    #[must_use]
    pub fn tp_config(&self) -> Option<TpConfig> {
        let tp = self
            .0
            .get_sub_element(ElementName::ApplicationEndpoint)?
            .get_sub_element(ElementName::TpConfiguration)?;

        if let Some(tcp_tp) = tp.get_sub_element(ElementName::TcpTp) {
            let port = tcp_tp.get_sub_element(ElementName::TcpTpPort)?;
            let (port_number, port_dynamically_assigned) = Self::port_config(&port);
            Some(TpConfig::TcpTp {
                port_number,
                port_dynamically_assigned,
            })
        } else if let Some(udp_tp) = tp.get_sub_element(ElementName::UdpTp) {
            let port = udp_tp.get_sub_element(ElementName::UdpTpPort)?;
            let (port_number, port_dynamically_assigned) = Self::port_config(&port);
            Some(TpConfig::UdpTp {
                port_number,
                port_dynamically_assigned,
            })
        } else {
            None
        }
    }

    // get the port number and dynamic assignment setting from a port element
    fn port_config(port_element: &Element) -> (Option<u16>, Option<bool>) {
        let port_number = port_element
            .get_sub_element(ElementName::PortNumber)
            .and_then(|pn| pn.character_data())
            .and_then(|cdata| cdata.parse_integer());
        let port_dynamically_assigned = port_element
            .get_sub_element(ElementName::DynamicallyAssigned)
            .and_then(|da| da.character_data())
            .and_then(|cdata| cdata.string_value())
            .map(|val| val == "true" || val == "1");
        (port_number, port_dynamically_assigned)
    }

    /// create a new `StaticSocketConnection` from this `SocketAddress` to a remote `SocketAddress`
    pub fn create_static_socket_connection(
        &self,
        name: &str,
        remote_address: &SocketAddress,
        tcp_role: Option<TcpRole>,
        tcp_connect_timeout: Option<f64>,
    ) -> Result<StaticSocketConnection, AutosarAbstractionError> {
        let own_tp_config = self.tp_config();
        let remote_tp_config = remote_address.tp_config();
        match (own_tp_config, remote_tp_config) {
            (Some(TpConfig::TcpTp { .. }), Some(TpConfig::TcpTp { .. })) => {
                StaticSocketConnection::new(name, self.element(), remote_address, tcp_role, tcp_connect_timeout)
            }
            (Some(TpConfig::UdpTp { .. }), Some(TpConfig::UdpTp { .. })) | (None, None) => {
                StaticSocketConnection::new(name, self.element(), remote_address, None, None)
            }
            _ => Err(AutosarAbstractionError::InvalidParameter(
                "Both SocketAddresses must use the same transport protocol".to_string(),
            )),
        }
    }

    /// get the `PhysicalChannel` containing this `SocketAddress`
    pub fn physical_channel(&self) -> Result<EthernetPhysicalChannel, AutosarAbstractionError> {
        let named_parent = self.0.named_parent()?.unwrap();
        named_parent.try_into()
    }

    /// iterate over all `StaticSocketConnection`s in this `SocketAddress`
    pub fn static_socket_connections(&self) -> impl Iterator<Item = StaticSocketConnection> {
        self.0
            .get_sub_element(ElementName::StaticSocketConnections)
            .into_iter()
            .flat_map(|ssc| ssc.sub_elements())
            .filter_map(|ssc| StaticSocketConnection::try_from(ssc).ok())
    }

    /// create a `ProvidedServiceInstanceV1` in this `SocketAddress`
    ///
    /// Creating a `ProvidedServiceInstanceV1` in a `SocketAddress` is part of the old way of defining services (<= Autosar 4.5.0).
    /// It is obsolete in newer versions of the standard.
    ///
    /// When using the new way of defining services, a `ProvidedServiceInstance` should be created in a `ServiceInstanceCollectionSet` instead.
    pub fn create_provided_service_instance(
        &self,
        name: &str,
        service_identifier: u16,
        instance_identifier: u16,
    ) -> Result<ProvidedServiceInstanceV1, AutosarAbstractionError> {
        let socket_name = self.name().unwrap_or_default();
        let ae_name = format!("{socket_name}_AE");
        let ae = self
            .element()
            .get_or_create_named_sub_element(ElementName::ApplicationEndpoint, &ae_name)?;
        let psis = ae.get_or_create_sub_element(ElementName::ProvidedServiceInstances)?;

        ProvidedServiceInstanceV1::new(name, &psis, service_identifier, instance_identifier)
    }

    /// get the `ProvidedServiceInstanceV1`s in this `SocketAddress`
    pub fn provided_service_instances(&self) -> impl Iterator<Item = ProvidedServiceInstanceV1> {
        self.element()
            .get_sub_element(ElementName::ApplicationEndpoint)
            .and_then(|ae| ae.get_sub_element(ElementName::ProvidedServiceInstances))
            .into_iter()
            .flat_map(|psis| psis.sub_elements())
            .filter_map(|psi| ProvidedServiceInstanceV1::try_from(psi).ok())
    }

    /// create a `ConsumedServiceInstanceV1` in this `SocketAddress`
    ///
    /// Creating a `ConsumedServiceInstanceV1` in a `SocketAddress` is part of the old way of defining services (<= Autosar 4.5.0).
    /// It is obsolete in newer versions of the standard.
    ///
    /// When using the new way of defining services, a `ConsumedServiceInstance` should be created in a `ServiceInstanceCollectionSet` instead.
    pub fn create_consumed_service_instance(
        &self,
        name: &str,
        provided_service_instance: &ProvidedServiceInstanceV1,
    ) -> Result<ConsumedServiceInstanceV1, AutosarAbstractionError> {
        let socket_name = self.name().unwrap_or_default();
        let ae_name = format!("{socket_name}_AE");
        let ae = self
            .element()
            .get_or_create_named_sub_element(ElementName::ApplicationEndpoint, &ae_name)?;
        let csis = ae.get_or_create_sub_element(ElementName::ConsumedServiceInstances)?;
        ConsumedServiceInstanceV1::new(name, &csis, provided_service_instance)
    }

    /// get the `ConsumedServiceInstance`s in this `SocketAddress`
    pub fn consumed_service_instances(&self) -> impl Iterator<Item = ConsumedServiceInstanceV1> {
        self.element()
            .get_sub_element(ElementName::ApplicationEndpoint)
            .and_then(|ae| ae.get_sub_element(ElementName::ConsumedServiceInstances))
            .into_iter()
            .flat_map(|csis| csis.sub_elements())
            .filter_map(|csi| ConsumedServiceInstanceV1::try_from(csi).ok())
    }
}

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

/// transport protocol settings of a [`SocketAddress`]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TpConfig {
    /// The socket uses TCP
    TcpTp {
        /// The port number used by the socket
        port_number: Option<u16>,
        /// If the port number is dynamically assigned. Obsolete; set the port number to None instead
        port_dynamically_assigned: Option<bool>,
        // additional TCP options: currently not supported
    },
    /// The socket uses UDP
    UdpTp {
        /// The port number used by the socket
        port_number: Option<u16>,
        /// If the port number is dynamically assigned. Obsolete; set the port number to None instead
        port_dynamically_assigned: Option<bool>,
    },
    // RtpTp, Ieee1722Tp, HttpTp: currently not supported
}

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

/// Describes if a [`SocketAddress`] is used for unicast or multicast
#[derive(Debug, Clone, PartialEq)]
pub enum SocketAddressType {
    /// The socket is used for unicast communication with a single ECU
    Unicast(Option<EcuInstance>),
    /// The socket is used for multicast communication with multiple ECUs
    Multicast(Vec<EcuInstance>),
}

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

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

    #[test]
    fn socket_address() {
        let model = AutosarModel::new();
        model.create_file("filename", AutosarVersion::Autosar_4_3_0).unwrap();
        let package = ArPackage::get_or_create(&model, "/pkg1").unwrap();
        let system = package.create_system("System", SystemCategory::SystemExtract).unwrap();
        let cluster = system.create_ethernet_cluster("Cluster", &package).unwrap();
        let channel = cluster.create_physical_channel("Channel", None).unwrap();

        let ecu_instance = system.create_ecu_instance("Ecu", &package).unwrap();
        let controller = ecu_instance
            .create_ethernet_communication_controller("EthCtrl", None)
            .unwrap();
        controller.connect_physical_channel("connection", &channel).unwrap();

        let ecu_instance2 = system.create_ecu_instance("Ecu2", &package).unwrap();
        let controller2 = ecu_instance2
            .create_ethernet_communication_controller("EthCtrl", None)
            .unwrap();
        controller2.connect_physical_channel("connection", &channel).unwrap();

        let ecu_instance3 = system.create_ecu_instance("Ecu3", &package).unwrap();
        let controller3 = ecu_instance3
            .create_ethernet_communication_controller("EthCtrl", None)
            .unwrap();
        controller3.connect_physical_channel("connection", &channel).unwrap();

        let endpoint_address = NetworkEndpointAddress::IPv4 {
            address: Some("192.168.0.1".to_string()),
            address_source: Some(IPv4AddressSource::Fixed),
            default_gateway: Some("192.168.0.2".to_string()),
            network_mask: Some("255.255.255.0".to_string()),
        };
        let network_endpoint = channel
            .create_network_endpoint("Address", endpoint_address, Some(&ecu_instance))
            .unwrap();
        let tcp_port = TpConfig::UdpTp {
            port_number: Some(1234),
            port_dynamically_assigned: None,
        };

        // create a unicast socket with an EcuInstance
        let socket_type: SocketAddressType = SocketAddressType::Unicast(Some(ecu_instance.clone()));
        let unicast_socket_address = channel
            .create_socket_address("Socket", &network_endpoint, &tcp_port, socket_type.clone())
            .unwrap();
        assert_eq!(channel.socket_addresses().count(), 1);
        assert_eq!(unicast_socket_address.network_endpoint().unwrap(), network_endpoint);
        assert_eq!(unicast_socket_address.socket_address_type().unwrap(), socket_type);
        // replace the EcuInstance in the socket
        unicast_socket_address.set_unicast_ecu(&ecu_instance2).unwrap();
        assert_eq!(
            unicast_socket_address.socket_address_type().unwrap(),
            SocketAddressType::Unicast(Some(ecu_instance2.clone()))
        );

        // create a unicast socket without an EcuInstance
        let socket_type: SocketAddressType = SocketAddressType::Unicast(None);
        let unicast_socket_address2 = channel
            .create_socket_address("Socket2", &network_endpoint, &tcp_port, socket_type.clone())
            .unwrap();
        // set the EcuInstance and verify that it is set
        unicast_socket_address2.set_unicast_ecu(&ecu_instance).unwrap();
        assert_eq!(
            unicast_socket_address2.socket_address_type().unwrap(),
            SocketAddressType::Unicast(Some(ecu_instance.clone()))
        );

        // create a multicast socket with multiple EcuInstances
        let socket_type: SocketAddressType =
            SocketAddressType::Multicast(vec![ecu_instance.clone(), ecu_instance2.clone()]);
        let multicast_socket_address = channel
            .create_socket_address("Socket3", &network_endpoint, &tcp_port, socket_type.clone())
            .unwrap();
        assert_eq!(multicast_socket_address.socket_address_type().unwrap(), socket_type);
        // add an EcuInstance to the multicast socket
        multicast_socket_address.add_multicast_ecu(&ecu_instance3).unwrap();
        assert_eq!(
            multicast_socket_address.socket_address_type().unwrap(),
            SocketAddressType::Multicast(vec![ecu_instance.clone(), ecu_instance2.clone(), ecu_instance3.clone()])
        );
    }

    #[test]
    fn socket_sd_config() {
        let model = AutosarModel::new();
        model.create_file("filename", AutosarVersion::Autosar_4_3_0).unwrap();
        let package = ArPackage::get_or_create(&model, "/pkg1").unwrap();
        let system = package.create_system("System", SystemCategory::SystemExtract).unwrap();
        let cluster = system.create_ethernet_cluster("Cluster", &package).unwrap();
        let channel = cluster.create_physical_channel("Channel", None).unwrap();

        // let ecu_instance = system.create_ecu_instance("Ecu", &package).unwrap();
        // let controller = ecu_instance
        //     .create_ethernet_communication_controller("EthCtrl", None)
        //     .unwrap();
        // controller.connect_physical_channel("connection", &channel).unwrap();

        let endpoint_address = NetworkEndpointAddress::IPv4 {
            address: Some("192.168.0.1".to_string()),
            address_source: Some(IPv4AddressSource::Fixed),
            default_gateway: None,
            network_mask: None,
        };
        let network_endpoint = channel
            .create_network_endpoint("Address", endpoint_address, None)
            .unwrap();
        let tcp_port = TpConfig::TcpTp {
            port_number: Some(1234),
            port_dynamically_assigned: None,
        };
        let socket_type: SocketAddressType = SocketAddressType::Unicast(None);
        let socket = channel
            .create_socket_address("Socket", &network_endpoint, &tcp_port, socket_type.clone())
            .unwrap();

        let provided_service_instance = socket.create_provided_service_instance("psi", 1, 2).unwrap();
        let consumed_service_instance = socket
            .create_consumed_service_instance("csi", &provided_service_instance)
            .unwrap();

        assert_eq!(socket.provided_service_instances().count(), 1);
        assert_eq!(
            socket.provided_service_instances().next().unwrap(),
            provided_service_instance
        );
        assert_eq!(socket.consumed_service_instances().count(), 1);
        assert_eq!(
            socket.consumed_service_instances().next().unwrap(),
            consumed_service_instance
        );
    }
}