autosar_data_abstraction/communication/physical_channel/
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
use crate::communication::{AbstractCommunicationConnector, CommunicationConnector, ISignalTriggering, PduTriggering};
use crate::{AbstractionElement, AutosarAbstractionError, EcuInstance};
use autosar_data::{Element, ElementName};

mod can;
mod ethernet;
mod flexray;

pub use can::*;
pub use ethernet::*;
pub use flexray::*;

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

/// trait for physical channels
pub trait AbstractPhysicalChannel: AbstractionElement {
    /// the type of communication connector used by this physical channel
    type CommunicationConnectorType: AbstractCommunicationConnector;

    /// iterate over all PduTriggerings of this physical channel
    fn pdu_triggerings(&self) -> impl Iterator<Item = PduTriggering> {
        self.element()
            .get_sub_element(ElementName::PduTriggerings)
            .into_iter()
            .flat_map(|triggerings| triggerings.sub_elements())
            .filter_map(|triggering| PduTriggering::try_from(triggering).ok())
    }

    /// iterate over all ISignalTriggerings of this physical channel
    fn signal_triggerings(&self) -> impl Iterator<Item = ISignalTriggering> {
        self.element()
            .get_sub_element(ElementName::ISignalTriggerings)
            .into_iter()
            .flat_map(|triggerings| triggerings.sub_elements())
            .filter_map(|triggering| ISignalTriggering::try_from(triggering).ok())
    }

    /// iterate over all connectors between this physical channel and any ECU
    ///
    /// # Example
    ///
    /// ```
    /// # use autosar_data::*;
    /// # use autosar_data_abstraction::{*, communication::*};
    /// # fn main() -> Result<(), AutosarAbstractionError> {
    /// # let model = AutosarModel::new();
    /// # model.create_file("filename", AutosarVersion::LATEST)?;
    /// # let package = ArPackage::get_or_create(&model, "/pkg1")?;
    /// # let system = package.create_system("System", SystemCategory::SystemExtract)?;
    /// # let cluster = system.create_can_cluster("Cluster", &package, &CanClusterSettings::default())?;
    /// # let can_channel = cluster.create_physical_channel("Channel")?;
    /// # let ecu = system.create_ecu_instance("ECU", &package)?;
    /// # let can_controller = ecu.create_can_communication_controller("Controller")?;
    /// can_controller.connect_physical_channel("Connector", &can_channel)?;
    /// for connector in can_channel.connectors() {
    ///    println!("Connector: {:?}", connector);
    /// }
    /// # assert_eq!(can_channel.connectors().count(), 1);
    /// # Ok(())}
    fn connectors(&self) -> impl Iterator<Item = Self::CommunicationConnectorType> {
        self.element()
            .get_sub_element(ElementName::CommConnectors)
            .into_iter()
            .flat_map(|connectors| connectors.sub_elements())
            .filter_map(|ccrc| {
                ccrc.get_sub_element(ElementName::CommunicationConnectorRef)
                    .and_then(|connector| connector.get_reference_target().ok())
                    .and_then(|connector| Self::CommunicationConnectorType::try_from(connector).ok())
            })
    }

    /// get the connector element between this channel and an ecu
    #[must_use]
    fn ecu_connector(&self, ecu_instance: &EcuInstance) -> Option<Self::CommunicationConnectorType> {
        // get a connector referenced by this physical channel which is contained in the ecu_instance
        // iterate over the CommunicationConnectorRefConditionals
        for connector in self.connectors() {
            if let Ok(connector_ecu_instance) = connector.ecu_instance() {
                if connector_ecu_instance == *ecu_instance {
                    return Some(connector);
                }
            }
        }

        None
    }
}

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

/// A physical channel is a communication channel between two ECUs.
///
/// This enum wraps the different physical channel types.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PhysicalChannel {
    /// A CAN physical channel
    Can(CanPhysicalChannel),
    /// An Ethernet physical channel
    Ethernet(EthernetPhysicalChannel),
    /// A `FlexRay` physical channel
    FlexRay(FlexrayPhysicalChannel),
}

impl AbstractPhysicalChannel for PhysicalChannel {
    type CommunicationConnectorType = CommunicationConnector;
}

impl AbstractionElement for PhysicalChannel {
    fn element(&self) -> &autosar_data::Element {
        match self {
            PhysicalChannel::Can(cpc) => cpc.element(),
            PhysicalChannel::Ethernet(epc) => epc.element(),
            PhysicalChannel::FlexRay(fpc) => fpc.element(),
        }
    }
}

impl From<CanPhysicalChannel> for PhysicalChannel {
    fn from(value: CanPhysicalChannel) -> Self {
        PhysicalChannel::Can(value)
    }
}

impl From<EthernetPhysicalChannel> for PhysicalChannel {
    fn from(value: EthernetPhysicalChannel) -> Self {
        PhysicalChannel::Ethernet(value)
    }
}

impl From<FlexrayPhysicalChannel> for PhysicalChannel {
    fn from(value: FlexrayPhysicalChannel) -> Self {
        PhysicalChannel::FlexRay(value)
    }
}

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

    fn try_from(element: Element) -> Result<Self, Self::Error> {
        match element.element_name() {
            ElementName::CanPhysicalChannel => Ok(CanPhysicalChannel::try_from(element)?.into()),
            ElementName::EthernetPhysicalChannel => Ok(EthernetPhysicalChannel::try_from(element)?.into()),
            ElementName::FlexrayPhysicalChannel => Ok(FlexrayPhysicalChannel::try_from(element)?.into()),
            _ => Err(AutosarAbstractionError::ConversionError {
                element,
                dest: "PhysicalChannel".to_string(),
            }),
        }
    }
}

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

#[cfg(test)]
mod test {
    use super::*;
    use crate::{
        communication::{AbstractFrame, CanAddressingMode, CanClusterSettings, CanFrameType, TransferProperty},
        ArPackage, ByteOrder, SystemCategory,
    };

    #[test]
    fn abstract_physical_channel() {
        let model = autosar_data::AutosarModel::new();
        model
            .create_file("filename", autosar_data::AutosarVersion::Autosar_00048)
            .unwrap();
        let pkg = ArPackage::get_or_create(&model, "/test").unwrap();
        let system = pkg.create_system("System", SystemCategory::SystemDescription).unwrap();
        let settings = CanClusterSettings::default();
        let cluster = system.create_can_cluster("CanCluster", &pkg, &settings).unwrap();
        let channel = cluster.create_physical_channel("channel_name").unwrap();

        let ecu = system.create_ecu_instance("ECU", &pkg).unwrap();
        let can_controller = ecu.create_can_communication_controller("Controller").unwrap();
        let connector = can_controller.connect_physical_channel("Connector", &channel).unwrap();

        let frame = system.create_can_frame("Frame", &pkg, 8).unwrap();
        let isignal_ipdu = system.create_isignal_ipdu("ISignalIPdu", &pkg, 8).unwrap();

        let system_signal = pkg.create_system_signal("SystemSignal").unwrap();
        let signal = system.create_isignal("Signal", &pkg, 8, &system_signal, None).unwrap();
        isignal_ipdu
            .map_signal(
                &signal,
                0,
                ByteOrder::MostSignificantByteLast,
                None,
                TransferProperty::Triggered,
            )
            .unwrap();
        frame
            .map_pdu(&isignal_ipdu, 0, ByteOrder::MostSignificantByteLast, None)
            .unwrap();

        let frame_triggering = channel
            .trigger_frame(&frame, 0x100, CanAddressingMode::Standard, CanFrameType::Can20)
            .unwrap();

        assert_eq!(channel.frame_triggerings().count(), 1);
        assert_eq!(channel.frame_triggerings().next(), Some(frame_triggering));
        assert_eq!(channel.pdu_triggerings().count(), 1);
        assert_eq!(
            channel.pdu_triggerings().next().unwrap().pdu().unwrap(),
            isignal_ipdu.into()
        );
        assert_eq!(channel.signal_triggerings().count(), 1);

        assert_eq!(channel.connectors().count(), 1);
        assert_eq!(channel.ecu_connector(&ecu).unwrap(), connector);
    }
}