autosar_data_abstraction/communication/transport_layer/
can_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
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
use crate::communication::{AbstractIpdu, CanCluster, CanCommunicationConnector, NPdu, Pdu};
use crate::{abstraction_element, AbstractionElement, ArPackage, AutosarAbstractionError, EcuInstance};
use autosar_data::{Element, ElementName, EnumItem};

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

/// Container for `CanTp` configuration
///
/// There should be one `CanTpConfig` for each CAN network in the system
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CanTpConfig(Element);
abstraction_element!(CanTpConfig, CanTpConfig);

impl CanTpConfig {
    pub(crate) fn new(name: &str, package: &ArPackage, cluster: &CanCluster) -> 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::CanTpConfig, name)?;
        tp_config_elem
            .create_sub_element(ElementName::CommunicationClusterRef)?
            .set_reference_target(cluster.element())?;

        Ok(Self(tp_config_elem))
    }

    /// add a `CanTp` ECU to the configuration
    pub fn add_ecu(&self, tp_ecu: &CanTpEcu) -> Result<(), AutosarAbstractionError> {
        let ecu_collection = self.element().get_or_create_sub_element(ElementName::TpEcus)?;
        let ecu_elem = ecu_collection.create_sub_element(ElementName::CanTpEcu)?;
        tp_ecu.set(&ecu_elem)?;

        Ok(())
    }

    /// get all of the ECUs in the configuration
    pub fn ecus(&self) -> impl Iterator<Item = CanTpEcu> {
        self.element()
            .get_sub_element(ElementName::TpEcus)
            .into_iter()
            .flat_map(|ecu_collection| {
                ecu_collection
                    .sub_elements()
                    .filter_map(|tp_ecu_elem| CanTpEcu::get(&tp_ecu_elem))
            })
    }

    /// create a new `CanTpAddress` in the configuration
    pub fn create_address(&self, name: &str, address: u32) -> Result<CanTpAddress, AutosarAbstractionError> {
        let addresses_container = self.element().get_or_create_sub_element(ElementName::TpAddresss)?;
        CanTpAddress::new(name, &addresses_container, address)
    }

    /// get all of the Can Tp addresses in the configuration
    pub fn addresses(&self) -> impl Iterator<Item = CanTpAddress> {
        self.element()
            .get_sub_element(ElementName::TpAddresss)
            .into_iter()
            .flat_map(|addresses_container| {
                addresses_container
                    .sub_elements()
                    .filter_map(|address_elem| CanTpAddress::try_from(address_elem).ok())
            })
    }

    /// create a new `CanTpChannel` in the configuration
    pub fn create_can_tp_channel(
        &self,
        name: &str,
        channel_id: u32,
        mode: CanTpChannelMode,
    ) -> Result<CanTpChannel, AutosarAbstractionError> {
        let channels_container = self.element().get_or_create_sub_element(ElementName::TpChannels)?;
        CanTpChannel::new(name, &channels_container, channel_id, mode)
    }

    /// create a new `CanTpConnection` in the configuration
    pub fn create_can_tp_connection(
        &self,
        name: Option<&str>,
        addressing_format: CanTpAddressingFormat,
        can_tp_channel: &CanTpChannel,
        data_pdu: &NPdu,
        tp_sdu: &Pdu,
        padding_activation: bool,
    ) -> Result<CanTpConnection, AutosarAbstractionError> {
        let connections_container = self.element().get_or_create_sub_element(ElementName::TpConnections)?;
        CanTpConnection::new(
            name,
            &connections_container,
            addressing_format,
            can_tp_channel,
            data_pdu,
            tp_sdu,
            padding_activation,
        )
    }

    /// get all of the `CanTpConnections` in the configuration
    pub fn connections(&self) -> impl Iterator<Item = CanTpConnection> {
        self.element()
            .get_sub_element(ElementName::TpConnections)
            .into_iter()
            .flat_map(|connections_container| {
                connections_container
                    .sub_elements()
                    .filter_map(|connection_elem| CanTpConnection::try_from(connection_elem).ok())
            })
    }

    /// create a new `CanTpNode` in the configuration
    pub fn create_can_tp_node(&self, name: &str) -> Result<CanTpNode, AutosarAbstractionError> {
        let nodes_container = self.element().get_or_create_sub_element(ElementName::TpNodes)?;
        CanTpNode::new(name, &nodes_container)
    }

    /// get all of the `CanTpNodes` in the configuration
    pub fn nodes(&self) -> impl Iterator<Item = CanTpNode> {
        self.element()
            .get_sub_element(ElementName::TpNodes)
            .into_iter()
            .flat_map(|nodes_container| {
                nodes_container
                    .sub_elements()
                    .filter_map(|node_elem| CanTpNode::try_from(node_elem).ok())
            })
    }
}

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

/// A `CanTpEcu` represents an ECU that is using the `CanTp` module
#[derive(Debug, Clone, PartialEq)]
pub struct CanTpEcu {
    /// The ECU instance backing the `CanTpEcu`
    pub ecu_instance: EcuInstance,
    /// The cycle time of the `CanTp` main function of the ECU
    pub cycle_time_main_function: Option<f64>,
}

impl CanTpEcu {
    fn get(element: &Element) -> Option<Self> {
        let ecu_instance = element
            .get_sub_element(ElementName::EcuInstanceRef)?
            .get_reference_target()
            .ok()?
            .try_into()
            .ok()?;
        let cycle_time_main_function = element
            .get_sub_element(ElementName::CycleTimeMainFunction)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.parse_float());

        Some(Self {
            ecu_instance,
            cycle_time_main_function,
        })
    }

    fn set(&self, element: &Element) -> Result<(), AutosarAbstractionError> {
        element
            .get_or_create_sub_element(ElementName::EcuInstanceRef)?
            .set_reference_target(self.ecu_instance.element())?;

        if let Some(cycle_time) = self.cycle_time_main_function {
            element
                .get_or_create_sub_element(ElementName::CycleTimeMainFunction)?
                .set_character_data(cycle_time)?;
        }

        Ok(())
    }
}

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

/// A `CanTpAddress` represents a logical address in the `CanTp` module
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CanTpAddress(Element);
abstraction_element!(CanTpAddress, CanTpAddress);

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

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

/// A `CanTpChannel` represents a channel in the `CanTp` module
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CanTpChannel(Element);
abstraction_element!(CanTpChannel, CanTpChannel);

impl CanTpChannel {
    pub(crate) fn new(
        name: &str,
        parent: &Element,
        channel_id: u32,
        mode: CanTpChannelMode,
    ) -> Result<Self, AutosarAbstractionError> {
        let channel_elem = parent.create_named_sub_element(ElementName::CanTpChannel, name)?;
        channel_elem
            .create_sub_element(ElementName::ChannelId)?
            .set_character_data(u64::from(channel_id))?;
        channel_elem
            .create_sub_element(ElementName::ChannelMode)?
            .set_character_data::<EnumItem>(mode.into())?;
        Ok(Self(channel_elem))
    }

    /// get the channel id of the channel
    #[must_use]
    pub fn channel_id(&self) -> Option<u32> {
        self.element()
            .get_sub_element(ElementName::ChannelId)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.parse_integer())
    }

    /// get the channel mode of the channel
    #[must_use]
    pub fn channel_mode(&self) -> Option<CanTpChannelMode> {
        self.element()
            .get_sub_element(ElementName::ChannelMode)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.enum_value())
            .and_then(|enumitem| enumitem.try_into().ok())
    }
}

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

/// The mode of a `CanTpChannel`
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CanTpChannelMode {
    /// Full duplex mode
    FullDuplex,
    /// Half duplex mode
    HalfDuplex,
}

impl From<CanTpChannelMode> for EnumItem {
    fn from(mode: CanTpChannelMode) -> Self {
        match mode {
            CanTpChannelMode::FullDuplex => EnumItem::FullDuplexMode,
            CanTpChannelMode::HalfDuplex => EnumItem::HalfDuplexMode,
        }
    }
}

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

    fn try_from(value: EnumItem) -> Result<Self, Self::Error> {
        match value {
            EnumItem::FullDuplexMode => Ok(CanTpChannelMode::FullDuplex),
            EnumItem::HalfDuplexMode => Ok(CanTpChannelMode::HalfDuplex),
            _ => Err(AutosarAbstractionError::ValueConversionError {
                value: value.to_string(),
                dest: "CanTpChannelMode".to_string(),
            }),
        }
    }
}

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

/// A connection identifies the sender and the receiver of this particular communication.
/// The `CanTp` module routes a Pdu through this connection.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CanTpConnection(Element);
abstraction_element!(CanTpConnection, CanTpConnection);

impl CanTpConnection {
    pub(crate) fn new(
        name: Option<&str>,
        parent: &Element,
        addressing_format: CanTpAddressingFormat,
        can_tp_channel: &CanTpChannel,
        data_pdu: &NPdu,
        tp_sdu: &Pdu,
        padding_activation: bool,
    ) -> Result<Self, AutosarAbstractionError> {
        let connection_elem = parent.create_sub_element(ElementName::CanTpConnection)?;
        if let Some(name) = name {
            connection_elem.create_named_sub_element(ElementName::Ident, name)?;
        }
        connection_elem
            .create_sub_element(ElementName::AddressingFormat)?
            .set_character_data::<EnumItem>(addressing_format.into())?;
        connection_elem
            .create_sub_element(ElementName::CanTpChannelRef)?
            .set_reference_target(can_tp_channel.element())?;
        connection_elem
            .create_sub_element(ElementName::DataPduRef)?
            .set_reference_target(data_pdu.element())?;
        connection_elem
            .create_sub_element(ElementName::TpSduRef)?
            .set_reference_target(tp_sdu.element())?;
        connection_elem
            .create_sub_element(ElementName::PaddingActivation)?
            .set_character_data(padding_activation)?;

        Ok(Self(connection_elem))
    }

    /// get the name of the connection
    ///
    /// In early versions of the Autosar standard, `CanTpConnection` was 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())
    }

    /// get the `CanTpChannel` associated with this connection
    #[must_use]
    pub fn channel(&self) -> Option<CanTpChannel> {
        self.element()
            .get_sub_element(ElementName::CanTpChannelRef)
            .and_then(|elem| elem.get_reference_target().ok())
            .and_then(|target| CanTpChannel::try_from(target).ok())
    }

    /// get the `NPdu` associated with this connection
    ///
    /// This is the Pdu that is sent over the CAN network
    #[must_use]
    pub fn data_pdu(&self) -> Option<NPdu> {
        self.element()
            .get_sub_element(ElementName::DataPduRef)
            .and_then(|elem| elem.get_reference_target().ok())
            .and_then(|target| NPdu::try_from(target).ok())
    }

    /// get the `IPdu` associated with this connection
    ///
    /// This is the Pdu that is sent over the transport protocol
    #[must_use]
    pub fn tp_sdu(&self) -> Option<Pdu> {
        self.element()
            .get_sub_element(ElementName::TpSduRef)
            .and_then(|elem| elem.get_reference_target().ok())
            .and_then(|target| Pdu::try_from(target).ok())
    }

    /// get the addressing format of the connection
    #[must_use]
    pub fn addressing_format(&self) -> Option<CanTpAddressingFormat> {
        self.element()
            .get_sub_element(ElementName::AddressingFormat)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.enum_value())
            .and_then(|enumitem| enumitem.try_into().ok())
    }

    /// get the padding activation of the connection
    #[must_use]
    pub fn padding_activation(&self) -> Option<bool> {
        self.element()
            .get_sub_element(ElementName::PaddingActivation)
            .and_then(|elem| elem.character_data())
            .and_then(|cdata| cdata.parse_bool())
    }

    /// 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(())
    }

    /// set the `CanTpChannel` associated with this connection
    pub fn set_channel(&self, channel: &CanTpChannel) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::CanTpChannelRef)?
            .set_reference_target(channel.element())?;
        Ok(())
    }

    /// set the `NPdu` associated with this connection
    pub fn set_data_pdu(&self, data_pdu: &NPdu) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::DataPduRef)?
            .set_reference_target(data_pdu.element())?;
        Ok(())
    }

    /// set the `IPdu` associated with this connection
    pub fn set_tp_sdu<T: AbstractIpdu>(&self, tp_sdu: &T) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::TpSduRef)?
            .set_reference_target(tp_sdu.element())?;
        Ok(())
    }

    /// set the addressing format of the connection
    pub fn set_addressing_format(
        &self,
        addressing_format: CanTpAddressingFormat,
    ) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::AddressingFormat)?
            .set_character_data::<EnumItem>(addressing_format.into())?;
        Ok(())
    }

    /// set the padding activation of the connection
    pub fn set_padding_activation(&self, padding_activation: bool) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::PaddingActivation)?
            .set_character_data(padding_activation)?;
        Ok(())
    }

    /// set the transmitter of the connection
    ///
    /// This is a `CanTpNode` representing an ECU that will send the data
    pub fn set_transmitter(&self, transmitter: &CanTpNode) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::TransmitterRef)?
            .set_reference_target(transmitter.element())?;
        Ok(())
    }

    /// get the transmitter of the connection
    #[must_use]
    pub fn transmitter(&self) -> Option<CanTpNode> {
        self.element()
            .get_sub_element(ElementName::TransmitterRef)
            .and_then(|elem| elem.get_reference_target().ok())
            .and_then(|target| CanTpNode::try_from(target).ok())
    }

    /// add a receiver to the connection
    ///
    /// This is a `CanTpNode` representing an ECU that will receive the data
    pub fn add_receiver(&self, receiver: &CanTpNode) -> Result<(), AutosarAbstractionError> {
        let receivers = self.element().get_or_create_sub_element(ElementName::ReceiverRefs)?;
        let receiver_ref_elem = receivers.create_sub_element(ElementName::ReceiverRef)?;
        receiver_ref_elem.set_reference_target(receiver.element())?;
        Ok(())
    }

    /// get all of the receivers of the connection
    pub fn receivers(&self) -> impl Iterator<Item = CanTpNode> {
        self.element()
            .get_sub_element(ElementName::ReceiverRefs)
            .into_iter()
            .flat_map(|receivers| {
                receivers.sub_elements().filter_map(|receiver_ref_elem| {
                    receiver_ref_elem
                        .get_reference_target()
                        .ok()
                        .and_then(|target| CanTpNode::try_from(target).ok())
                })
            })
    }
}

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

/// The addressing format of a `CanTpConnection`
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CanTpAddressingFormat {
    /// Extended addressing format
    Extended,
    /// Mixed 11-bit addressing format
    Mixed,
    /// Mixed 29-bit addressing format
    Mixed29Bit,
    /// Normal fixed addressing format
    NormalFixed,
    /// Standard addressing format
    Standard,
}

impl From<CanTpAddressingFormat> for EnumItem {
    fn from(format: CanTpAddressingFormat) -> Self {
        match format {
            CanTpAddressingFormat::Extended => EnumItem::Extended,
            CanTpAddressingFormat::Mixed => EnumItem::Mixed,
            CanTpAddressingFormat::Mixed29Bit => EnumItem::Mixed29Bit,
            CanTpAddressingFormat::NormalFixed => EnumItem::Normalfixed,
            CanTpAddressingFormat::Standard => EnumItem::Standard,
        }
    }
}

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

    fn try_from(value: EnumItem) -> Result<Self, Self::Error> {
        match value {
            EnumItem::Extended => Ok(CanTpAddressingFormat::Extended),
            EnumItem::Mixed => Ok(CanTpAddressingFormat::Mixed),
            EnumItem::Mixed29Bit => Ok(CanTpAddressingFormat::Mixed29Bit),
            EnumItem::Normalfixed => Ok(CanTpAddressingFormat::NormalFixed),
            EnumItem::Standard => Ok(CanTpAddressingFormat::Standard),
            _ => Err(AutosarAbstractionError::ValueConversionError {
                value: value.to_string(),
                dest: "CanTpAddressingFormat".to_string(),
            }),
        }
    }
}

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

/// A `CanTpNode` provides the TP address and the connection to the topology description in a `CanTpConfig`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CanTpNode(Element);
abstraction_element!(CanTpNode, CanTpNode);

impl CanTpNode {
    pub(crate) fn new(name: &str, parent: &Element) -> Result<Self, AutosarAbstractionError> {
        let node_elem = parent.create_named_sub_element(ElementName::CanTpNode, name)?;
        Ok(Self(node_elem))
    }

    /// set the `CanTpAddress` of this Node
    pub fn set_address(&self, address: &CanTpAddress) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::TpAddressRef)?
            .set_reference_target(address.element())?;
        Ok(())
    }

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

    /// set the reference to a `CanCommunicationConnector` of an ECU and a `CanPhysicalChannel`
    ///
    /// The connector connects the ECU to the physical channel, so by setting this reference, the
    /// ECU is also connected to the `CanTpNode`
    pub fn set_connector(&self, connector: &CanCommunicationConnector) -> Result<(), AutosarAbstractionError> {
        self.element()
            .get_or_create_sub_element(ElementName::ConnectorRef)?
            .set_reference_target(connector.element())?;
        Ok(())
    }

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

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

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

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

        let system = package.create_system("system", SystemCategory::EcuExtract).unwrap();
        let can_cluster = system
            .create_can_cluster("can_cluster", &package, &CanClusterSettings::default())
            .unwrap();
        let can_channel = can_cluster.create_physical_channel("can_channel").unwrap();
        let ecu_instance = system.create_ecu_instance("ecu_instance", &package).unwrap();
        let communication_controller = ecu_instance.create_can_communication_controller("can_ctrl").unwrap();
        let connector = communication_controller
            .connect_physical_channel("name", &can_channel)
            .unwrap();

        let can_tp_config = system
            .create_can_tp_config("can_tp_config", &package, &can_cluster)
            .unwrap();
        let can_tp_ecu = CanTpEcu {
            ecu_instance,
            cycle_time_main_function: Some(1.0),
        };
        can_tp_config.add_ecu(&can_tp_ecu).unwrap();
        assert_eq!(can_tp_config.ecus().count(), 1);
        assert_eq!(can_tp_config.ecus().next().unwrap(), can_tp_ecu);

        let address = can_tp_config.create_address("address", 0x1234).unwrap();
        assert_eq!(can_tp_config.addresses().count(), 1);
        assert_eq!(can_tp_config.addresses().next().unwrap(), address);

        let channel = can_tp_config
            .create_can_tp_channel("channel", 1, CanTpChannelMode::FullDuplex)
            .unwrap();
        let channel2 = can_tp_config
            .create_can_tp_channel("channel2", 2, CanTpChannelMode::FullDuplex)
            .unwrap();
        assert_eq!(channel.channel_id().unwrap(), 1);
        assert_eq!(channel.channel_mode().unwrap(), CanTpChannelMode::FullDuplex);

        let data_pdu = system.create_n_pdu("data_pdu", &package, 8).unwrap();
        let data_pdu2 = system.create_n_pdu("data_pdu2", &package, 8).unwrap();
        let tp_sdu = system.create_dcm_ipdu("ipdu", &package, 4096).unwrap();
        let tp_sdu2 = system.create_dcm_ipdu("ipdu2", &package, 4096).unwrap();

        let connection = can_tp_config
            .create_can_tp_connection(
                Some("connection"),
                CanTpAddressingFormat::Standard,
                &channel,
                &data_pdu,
                &tp_sdu.clone().into(),
                false,
            )
            .unwrap();
        assert_eq!(can_tp_config.connections().count(), 1);
        assert_eq!(can_tp_config.connections().next().unwrap(), connection);

        assert_eq!(connection.name().unwrap(), "connection");
        // in a CanTpConnection, the name is provided by the optional IDENT sub-element
        connection
            .element()
            .remove_sub_element_kind(ElementName::Ident)
            .unwrap();
        assert_eq!(connection.name(), None);
        connection.set_name("new_name").unwrap();
        assert_eq!(connection.name().unwrap(), "new_name");

        assert_eq!(connection.channel().unwrap(), channel);
        connection.set_channel(&channel2).unwrap();
        assert_eq!(connection.channel().unwrap(), channel2);

        assert_eq!(connection.data_pdu().unwrap(), data_pdu);
        connection.set_data_pdu(&data_pdu2).unwrap();
        assert_eq!(connection.data_pdu().unwrap(), data_pdu2);

        assert_eq!(connection.tp_sdu().unwrap(), tp_sdu.into());
        connection.set_tp_sdu(&tp_sdu2).unwrap();
        assert_eq!(connection.tp_sdu().unwrap(), tp_sdu2.into());

        assert_eq!(connection.addressing_format().unwrap(), CanTpAddressingFormat::Standard);
        connection
            .set_addressing_format(CanTpAddressingFormat::Extended)
            .unwrap();
        assert_eq!(connection.addressing_format().unwrap(), CanTpAddressingFormat::Extended);

        assert_eq!(connection.padding_activation().unwrap(), false);
        connection.set_padding_activation(true).unwrap();
        assert_eq!(connection.padding_activation().unwrap(), true);

        let node = can_tp_config.create_can_tp_node("node").unwrap();
        assert_eq!(can_tp_config.nodes().count(), 1);
        assert_eq!(can_tp_config.nodes().next().unwrap(), node);

        node.set_address(&address).unwrap();
        assert_eq!(node.address().unwrap(), address);
        node.set_connector(&connector).unwrap();
        assert_eq!(node.connector().unwrap(), connector);
        connection.set_transmitter(&node).unwrap();
        assert_eq!(connection.transmitter().unwrap(), node);

        connection.add_receiver(&node).unwrap();
        assert_eq!(connection.receivers().count(), 1);
    }
}