autosar_data_abstraction/ecuinstance.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
use crate::communication::{
    CanCommunicationController, CommunicationController, EthernetCommunicationController,
    FlexrayCommunicationController,
};
use crate::{abstraction_element, AbstractionElement, ArPackage, AutosarAbstractionError};
use autosar_data::{Element, ElementName};
/// The `EcuInstance` represents one ECU in a `System`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EcuInstance(Element);
abstraction_element!(EcuInstance, EcuInstance);
impl EcuInstance {
    // Create a new EcuInstance
    //
    // This new EcuInstance will not be connected to a System.
    pub(crate) fn new(name: &str, package: &ArPackage) -> Result<EcuInstance, AutosarAbstractionError> {
        let elem_pkg_elements = package.element().get_or_create_sub_element(ElementName::Elements)?;
        let elem_ecu_instance = elem_pkg_elements.create_named_sub_element(ElementName::EcuInstance, name)?;
        Ok(EcuInstance(elem_ecu_instance))
    }
    /// Create a CAN-COMMUNICATION-CONTROLLER for this ECU-INSTANCE
    ///
    /// The ECU must have one controller per bus it communicates on.
    /// For example, if it communicates on two CAN buses, then two CAN-COMMUNICATION-CONTROLLERs are needed.
    ///
    /// # Example
    ///
    /// ```
    /// # use autosar_data::*;
    /// # use autosar_data_abstraction::*;
    /// # let model = AutosarModel::new();
    /// # model.create_file("filename", AutosarVersion::Autosar_00048).unwrap();
    /// # let package = ArPackage::get_or_create(&model, "/pkg1").unwrap();
    /// # let system = package.create_system("System", SystemCategory::SystemExtract).unwrap();
    /// let ecu_instance = system.create_ecu_instance("ecu_name", &package).unwrap();
    /// let can_controller = ecu_instance.create_can_communication_controller("CanCtrl").unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// - [`AutosarAbstractionError::ModelError`] An error occurred in the Autosar model while trying to create the ECU-INSTANCE
    pub fn create_can_communication_controller(
        &self,
        name: &str,
    ) -> Result<CanCommunicationController, AutosarAbstractionError> {
        CanCommunicationController::new(name, self)
    }
    /// Create an ETHERNET-COMMUNICATION-CONTROLLER for this ECU-INSTANCE
    ///
    /// The ECU must have one controller per bus it communicates on.
    /// For example, if it communicates on two CAN buses, then two CAN-COMMUNICATION-CONTROLLERs are needed.
    ///
    /// # Example
    ///
    /// ```
    /// # use autosar_data::*;
    /// # use autosar_data_abstraction::*;
    /// # let model = AutosarModel::new();
    /// # model.create_file("filename", AutosarVersion::Autosar_00048).unwrap();
    /// # let package = ArPackage::get_or_create(&model, "/pkg1").unwrap();
    /// # let system = package.create_system("System", SystemCategory::SystemExtract).unwrap();
    /// let ecu_instance = system.create_ecu_instance("ecu_name", &package).unwrap();
    /// let ethernet_controller = ecu_instance
    ///     .create_ethernet_communication_controller("EthCtrl", Some("ab:cd:ef:01:02:03".to_string()))
    ///     .unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// - [`AutosarAbstractionError::ModelError`] An error occurred in the Autosar model while trying to create the ECU-INSTANCE
    pub fn create_ethernet_communication_controller(
        &self,
        name: &str,
        mac_address: Option<String>,
    ) -> Result<EthernetCommunicationController, AutosarAbstractionError> {
        EthernetCommunicationController::new(name, self, mac_address)
    }
    /// Create a FLEXRAY-COMMUNICATION-CONTROLLER for this ECU-INSTANCE
    ///
    /// The ECU must have one controller per bus it communicates on.
    /// For example, if it communicates on two CAN buses, then two CAN-COMMUNICATION-CONTROLLERs are needed.
    ///
    /// # Example
    ///
    /// ```
    /// # use autosar_data::*;
    /// # use autosar_data_abstraction::*;
    /// # let model = AutosarModel::new();
    /// # model.create_file("filename", AutosarVersion::Autosar_00048).unwrap();
    /// # let package = ArPackage::get_or_create(&model, "/pkg1").unwrap();
    /// # let system = package.create_system("System", SystemCategory::SystemExtract).unwrap();
    /// let ecu_instance = system.create_ecu_instance("ecu_name", &package).unwrap();
    /// let flexray_controller = ecu_instance
    ///     .create_flexray_communication_controller("FlexrayCtrl")
    ///     .unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// - [`AutosarAbstractionError::ModelError`] An error occurred in the Autosar model while trying to create the ECU-INSTANCE
    pub fn create_flexray_communication_controller(
        &self,
        name: &str,
    ) -> Result<FlexrayCommunicationController, AutosarAbstractionError> {
        FlexrayCommunicationController::new(name, self)
    }
    /// return an interator over all communication controllers in this `EcuInstance`
    ///
    /// # Example
    ///
    /// ```
    /// # use autosar_data::*;
    /// # use autosar_data_abstraction::*;
    /// # let model = AutosarModel::new();
    /// # model.create_file("filename", AutosarVersion::Autosar_00048).unwrap();
    /// # let package = ArPackage::get_or_create(&model, "/pkg1").unwrap();
    /// # let system = package.create_system("System", SystemCategory::SystemExtract).unwrap();
    /// let ecu_instance = system.create_ecu_instance("ecu_name", &package).unwrap();
    /// ecu_instance.create_flexray_communication_controller("FlexrayCtrl").unwrap();
    /// ecu_instance.create_can_communication_controller("CanCtrl").unwrap();
    /// for ctrl in ecu_instance.communication_controllers() {
    ///     // ...
    /// }
    /// # assert_eq!(ecu_instance.communication_controllers().count(), 2);
    /// ```
    pub fn communication_controllers(&self) -> impl Iterator<Item = CommunicationController> {
        self.0
            .get_sub_element(ElementName::CommControllers)
            .into_iter()
            .flat_map(|cc| cc.sub_elements())
            .filter_map(|ccelem| CommunicationController::try_from(ccelem).ok())
    }
}
//##################################################################
#[cfg(test)]
mod test {
    use crate::*;
    use autosar_data::{AutosarModel, AutosarVersion};
    #[test]
    fn ecu() {
        let model = AutosarModel::new();
        model.create_file("filename", AutosarVersion::Autosar_00048).unwrap();
        let package = ArPackage::get_or_create(&model, "/pkg1").unwrap();
        let system = package.create_system("System", SystemCategory::SystemExtract).unwrap();
        let ecu_instance = system.create_ecu_instance("ecu_name", &package).unwrap();
        ecu_instance
            .create_flexray_communication_controller("FlexrayCtrl")
            .unwrap();
        ecu_instance.create_can_communication_controller("CanCtrl").unwrap();
        ecu_instance
            .create_ethernet_communication_controller("EthCtrl", None)
            .unwrap();
        assert_eq!(ecu_instance.communication_controllers().count(), 3);
    }
}