autosar_data_abstraction/communication/network_management/
flexray_nm.rsuse crate::communication::{
AbstractNmCluster, AbstractNmClusterCoupling, AbstractNmNode, FlexrayCluster, FlexrayCommunicationController, NmEcu,
};
use crate::{abstraction_element, AbstractionElement, AutosarAbstractionError};
use autosar_data::{Element, ElementName, EnumItem};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FlexrayNmCluster(Element);
abstraction_element!(FlexrayNmCluster, FlexrayNmCluster);
impl FlexrayNmCluster {
pub(crate) fn new(
name: &str,
parent: &Element,
settings: &FlexrayNmClusterSettings,
flexray_cluster: &FlexrayCluster,
) -> Result<Self, AutosarAbstractionError> {
let fr_nm_cluster_elem = parent.create_named_sub_element(ElementName::FlexrayNmCluster, name)?;
let fr_nm_cluster = Self(fr_nm_cluster_elem);
fr_nm_cluster.set_communication_cluster(flexray_cluster)?;
fr_nm_cluster.set_nm_data_cycle(settings.nm_data_cycle)?;
fr_nm_cluster.set_nm_remote_sleep_indication_time(settings.nm_remote_sleep_indication_time)?;
fr_nm_cluster.set_nm_repeat_message_time(settings.nm_repeat_message_time)?;
fr_nm_cluster.set_nm_repetition_cycle(settings.nm_repetition_cycle)?;
fr_nm_cluster.set_nm_voting_cycle(settings.nm_voting_cycle)?;
Ok(fr_nm_cluster)
}
pub fn set_nm_data_cycle(&self, nm_data_cycle: u32) -> Result<(), AutosarAbstractionError> {
self.element()
.get_or_create_sub_element(ElementName::NmDataCycle)?
.set_character_data(u64::from(nm_data_cycle))?;
Ok(())
}
#[must_use]
pub fn nm_data_cycle(&self) -> Option<u32> {
self.element()
.get_sub_element(ElementName::NmDataCycle)
.and_then(|elem| elem.character_data())
.and_then(|cdata| cdata.parse_integer())
}
pub fn set_nm_remote_sleep_indication_time(
&self,
nm_remote_sleep_indication_time: f64,
) -> Result<(), AutosarAbstractionError> {
self.element()
.get_or_create_sub_element(ElementName::NmRemoteSleepIndicationTime)?
.set_character_data(nm_remote_sleep_indication_time)?;
Ok(())
}
#[must_use]
pub fn nm_remote_sleep_indication_time(&self) -> Option<f64> {
self.element()
.get_sub_element(ElementName::NmRemoteSleepIndicationTime)
.and_then(|elem| elem.character_data())
.and_then(|cdata| cdata.parse_float())
}
pub fn set_nm_repeat_message_time(&self, nm_repeat_message_time: f64) -> Result<(), AutosarAbstractionError> {
self.element()
.get_or_create_sub_element(ElementName::NmRepeatMessageTime)?
.set_character_data(nm_repeat_message_time)?;
Ok(())
}
#[must_use]
pub fn nm_repeat_message_time(&self) -> Option<f64> {
self.element()
.get_sub_element(ElementName::NmRepeatMessageTime)
.and_then(|elem| elem.character_data())
.and_then(|cdata| cdata.parse_float())
}
pub fn set_nm_repetition_cycle(&self, nm_repetition_cycle: u32) -> Result<(), AutosarAbstractionError> {
self.element()
.get_or_create_sub_element(ElementName::NmRepetitionCycle)?
.set_character_data(u64::from(nm_repetition_cycle))?;
Ok(())
}
#[must_use]
pub fn nm_repetition_cycle(&self) -> Option<u32> {
self.element()
.get_sub_element(ElementName::NmRepetitionCycle)
.and_then(|elem| elem.character_data())
.and_then(|cdata| cdata.parse_integer())
}
pub fn set_nm_voting_cycle(&self, nm_voting_cycle: u32) -> Result<(), AutosarAbstractionError> {
self.element()
.get_or_create_sub_element(ElementName::NmVotingCycle)?
.set_character_data(u64::from(nm_voting_cycle))?;
Ok(())
}
#[must_use]
pub fn nm_voting_cycle(&self) -> Option<u32> {
self.element()
.get_sub_element(ElementName::NmVotingCycle)
.and_then(|elem| elem.character_data())
.and_then(|cdata| cdata.parse_integer())
}
pub fn create_flexray_nm_node(
&self,
name: &str,
controller: &FlexrayCommunicationController,
nm_ecu: &NmEcu,
) -> Result<FlexrayNmNode, AutosarAbstractionError> {
let nm_nodes = self.element().get_or_create_sub_element(ElementName::NmNodes)?;
FlexrayNmNode::new(name, &nm_nodes, controller, nm_ecu)
}
}
impl AbstractNmCluster for FlexrayNmCluster {
type CommunicationClusterType = FlexrayCluster;
type NmNodeType = FlexrayNmNode;
}
#[derive(Debug, Clone, PartialEq)]
pub struct FlexrayNmClusterSettings {
pub nm_data_cycle: u32,
pub nm_remote_sleep_indication_time: f64,
pub nm_repeat_message_time: f64,
pub nm_repetition_cycle: u32,
pub nm_voting_cycle: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FlexrayNmClusterCoupling(Element);
abstraction_element!(FlexrayNmClusterCoupling, FlexrayNmClusterCoupling);
impl FlexrayNmClusterCoupling {
pub(crate) fn new(
parent: &Element,
nm_schedule_variant: FlexrayNmScheduleVariant,
) -> Result<Self, AutosarAbstractionError> {
let nm_cluster_coupling_elem = parent.create_sub_element(ElementName::FlexrayNmClusterCoupling)?;
let nm_cluster_coupling = Self(nm_cluster_coupling_elem);
nm_cluster_coupling.set_nm_schedule_variant(nm_schedule_variant)?;
Ok(nm_cluster_coupling)
}
pub fn set_nm_schedule_variant(
&self,
nm_schedule_variant: FlexrayNmScheduleVariant,
) -> Result<(), AutosarAbstractionError> {
self.element()
.get_or_create_sub_element(ElementName::NmScheduleVariant)?
.set_character_data::<EnumItem>(nm_schedule_variant.into())?;
Ok(())
}
#[must_use]
pub fn nm_schedule_variant(&self) -> Option<FlexrayNmScheduleVariant> {
self.element()
.get_sub_element(ElementName::NmScheduleVariant)
.and_then(|elem| elem.character_data())
.and_then(|cdata| cdata.enum_value())
.and_then(|enum_item| FlexrayNmScheduleVariant::try_from(enum_item).ok())
}
}
impl AbstractNmClusterCoupling for FlexrayNmClusterCoupling {
type NmClusterType = FlexrayNmCluster;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FlexrayNmScheduleVariant {
ScheduleVariant1,
ScheduleVariant2,
ScheduleVariant3,
ScheduleVariant4,
ScheduleVariant5,
ScheduleVariant6,
ScheduleVariant7,
}
impl From<FlexrayNmScheduleVariant> for EnumItem {
fn from(value: FlexrayNmScheduleVariant) -> Self {
match value {
FlexrayNmScheduleVariant::ScheduleVariant1 => EnumItem::ScheduleVariant1,
FlexrayNmScheduleVariant::ScheduleVariant2 => EnumItem::ScheduleVariant2,
FlexrayNmScheduleVariant::ScheduleVariant3 => EnumItem::ScheduleVariant3,
FlexrayNmScheduleVariant::ScheduleVariant4 => EnumItem::ScheduleVariant4,
FlexrayNmScheduleVariant::ScheduleVariant5 => EnumItem::ScheduleVariant5,
FlexrayNmScheduleVariant::ScheduleVariant6 => EnumItem::ScheduleVariant6,
FlexrayNmScheduleVariant::ScheduleVariant7 => EnumItem::ScheduleVariant7,
}
}
}
impl TryFrom<EnumItem> for FlexrayNmScheduleVariant {
type Error = AutosarAbstractionError;
fn try_from(value: EnumItem) -> Result<Self, Self::Error> {
match value {
EnumItem::ScheduleVariant1 => Ok(FlexrayNmScheduleVariant::ScheduleVariant1),
EnumItem::ScheduleVariant2 => Ok(FlexrayNmScheduleVariant::ScheduleVariant2),
EnumItem::ScheduleVariant3 => Ok(FlexrayNmScheduleVariant::ScheduleVariant3),
EnumItem::ScheduleVariant4 => Ok(FlexrayNmScheduleVariant::ScheduleVariant4),
EnumItem::ScheduleVariant5 => Ok(FlexrayNmScheduleVariant::ScheduleVariant5),
EnumItem::ScheduleVariant6 => Ok(FlexrayNmScheduleVariant::ScheduleVariant6),
EnumItem::ScheduleVariant7 => Ok(FlexrayNmScheduleVariant::ScheduleVariant7),
_ => Err(AutosarAbstractionError::ValueConversionError {
value: value.to_string(),
dest: "FlexrayNmScheduleVariant".to_string(),
}),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FlexrayNmNode(Element);
abstraction_element!(FlexrayNmNode, FlexrayNmNode);
impl FlexrayNmNode {
pub(crate) fn new(
name: &str,
parent: &Element,
controller: &FlexrayCommunicationController,
nm_ecu: &NmEcu,
) -> Result<Self, AutosarAbstractionError> {
let fr_nm_node_elem = parent.create_named_sub_element(ElementName::FlexrayNmNode, name)?;
let fr_nm_node = Self(fr_nm_node_elem);
fr_nm_node.set_communication_controller(controller)?;
fr_nm_node.set_nm_ecu(nm_ecu)?;
Ok(fr_nm_node)
}
}
impl AbstractNmNode for FlexrayNmNode {
type CommunicationControllerType = FlexrayCommunicationController;
}