autosar_data_abstraction/communication/network_management/
flexray_nm.rs

1use crate::communication::{
2    AbstractNmCluster, AbstractNmClusterCoupling, AbstractNmNode, FlexrayCluster, FlexrayCommunicationController, NmEcu,
3};
4use crate::{AbstractionElement, AutosarAbstractionError, IdentifiableAbstractionElement, abstraction_element};
5use autosar_data::{Element, ElementName, EnumItem};
6
7//##################################################################
8
9/// Flexray specific `NmCluster`
10#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11pub struct FlexrayNmCluster(Element);
12abstraction_element!(FlexrayNmCluster, FlexrayNmCluster);
13impl IdentifiableAbstractionElement for FlexrayNmCluster {}
14
15impl FlexrayNmCluster {
16    pub(crate) fn new(
17        name: &str,
18        parent: &Element,
19        settings: &FlexrayNmClusterSettings,
20        flexray_cluster: &FlexrayCluster,
21    ) -> Result<Self, AutosarAbstractionError> {
22        let fr_nm_cluster_elem = parent.create_named_sub_element(ElementName::FlexrayNmCluster, name)?;
23        let fr_nm_cluster = Self(fr_nm_cluster_elem);
24
25        fr_nm_cluster.set_communication_cluster(flexray_cluster)?;
26        fr_nm_cluster.set_nm_data_cycle(settings.nm_data_cycle)?;
27        fr_nm_cluster.set_nm_remote_sleep_indication_time(settings.nm_remote_sleep_indication_time)?;
28        fr_nm_cluster.set_nm_repeat_message_time(settings.nm_repeat_message_time)?;
29        fr_nm_cluster.set_nm_repetition_cycle(settings.nm_repetition_cycle)?;
30        fr_nm_cluster.set_nm_voting_cycle(settings.nm_voting_cycle)?;
31
32        Ok(fr_nm_cluster)
33    }
34
35    /// set the nmDataCycle
36    ///
37    /// Number of `FlexRay` Communication Cycles needed to transmit the Nm Data PDUs of all `FlexRay` Nm Ecus of this `FlexRayNmCluster`.
38    pub fn set_nm_data_cycle(&self, nm_data_cycle: u32) -> Result<(), AutosarAbstractionError> {
39        self.element()
40            .get_or_create_sub_element(ElementName::NmDataCycle)?
41            .set_character_data(u64::from(nm_data_cycle))?;
42        Ok(())
43    }
44
45    /// get the nmDataCycle
46    ///
47    /// Number of `FlexRay` Communication Cycles needed to transmit the Nm Data PDUs of all `FlexRay` Nm Ecus of this `FlexRayNmCluster`.
48    #[must_use]
49    pub fn nm_data_cycle(&self) -> Option<u32> {
50        self.element()
51            .get_sub_element(ElementName::NmDataCycle)
52            .and_then(|elem| elem.character_data())
53            .and_then(|cdata| cdata.parse_integer())
54    }
55
56    /// set the nmRemoteSleepIndicationTime
57    ///
58    /// Timeout for Remote Sleep Indication in seconds.
59    pub fn set_nm_remote_sleep_indication_time(
60        &self,
61        nm_remote_sleep_indication_time: f64,
62    ) -> Result<(), AutosarAbstractionError> {
63        self.element()
64            .get_or_create_sub_element(ElementName::NmRemoteSleepIndicationTime)?
65            .set_character_data(nm_remote_sleep_indication_time)?;
66        Ok(())
67    }
68
69    /// get the nmRemoteSleepIndicationTime
70    ///
71    /// Timeout for Remote Sleep Indication in seconds.
72    #[must_use]
73    pub fn nm_remote_sleep_indication_time(&self) -> Option<f64> {
74        self.element()
75            .get_sub_element(ElementName::NmRemoteSleepIndicationTime)
76            .and_then(|elem| elem.character_data())
77            .and_then(|cdata| cdata.parse_float())
78    }
79
80    /// set the nmRepeatMessageTime
81    ///
82    /// Timeout for Repeat Message State in seconds.
83    pub fn set_nm_repeat_message_time(&self, nm_repeat_message_time: f64) -> Result<(), AutosarAbstractionError> {
84        self.element()
85            .get_or_create_sub_element(ElementName::NmRepeatMessageTime)?
86            .set_character_data(nm_repeat_message_time)?;
87        Ok(())
88    }
89
90    /// get the nmRepeatMessageTime
91    ///
92    /// Timeout for Repeat Message State in seconds.
93    #[must_use]
94    pub fn nm_repeat_message_time(&self) -> Option<f64> {
95        self.element()
96            .get_sub_element(ElementName::NmRepeatMessageTime)
97            .and_then(|elem| elem.character_data())
98            .and_then(|cdata| cdata.parse_float())
99    }
100
101    /// set the nmRepetitionCycle
102    ///
103    /// Number of `FlexRay` Communication Cycles used to repeat the transmission of the Nm vote Pdus of all
104    /// `FlexRay` `NmEcus` of this `FlexRayNmCluster`. This value shall be an integral multiple of nmVotingCycle.
105    pub fn set_nm_repetition_cycle(&self, nm_repetition_cycle: u32) -> Result<(), AutosarAbstractionError> {
106        self.element()
107            .get_or_create_sub_element(ElementName::NmRepetitionCycle)?
108            .set_character_data(u64::from(nm_repetition_cycle))?;
109        Ok(())
110    }
111
112    /// get the nmRepetitionCycle
113    ///
114    /// Number of `FlexRay` Communication Cycles used to repeat the transmission of the Nm vote Pdus of all
115    /// `FlexRay` `NmEcus` of this `FlexRayNmCluster`. This value shall be an integral multiple of nmVotingCycle.
116    #[must_use]
117    pub fn nm_repetition_cycle(&self) -> Option<u32> {
118        self.element()
119            .get_sub_element(ElementName::NmRepetitionCycle)
120            .and_then(|elem| elem.character_data())
121            .and_then(|cdata| cdata.parse_integer())
122    }
123
124    /// set the nmVotingCycle
125    ///
126    /// The number of `FlexRay` Communication Cycles used to transmit the Nm Vote PDUs of all `FlexRay` Nm Ecus of this `FlexRayNmCluster`.
127    pub fn set_nm_voting_cycle(&self, nm_voting_cycle: u32) -> Result<(), AutosarAbstractionError> {
128        self.element()
129            .get_or_create_sub_element(ElementName::NmVotingCycle)?
130            .set_character_data(u64::from(nm_voting_cycle))?;
131        Ok(())
132    }
133
134    /// get the nmVotingCycle
135    ///
136    /// The number of `FlexRay` Communication Cycles used to transmit the Nm Vote PDUs of all `FlexRay` Nm Ecus of this `FlexRayNmCluster`.
137    #[must_use]
138    pub fn nm_voting_cycle(&self) -> Option<u32> {
139        self.element()
140            .get_sub_element(ElementName::NmVotingCycle)
141            .and_then(|elem| elem.character_data())
142            .and_then(|cdata| cdata.parse_integer())
143    }
144
145    /// add a `FlexrayNmNode` to the cluster
146    pub fn create_flexray_nm_node(
147        &self,
148        name: &str,
149        controller: &FlexrayCommunicationController,
150        nm_ecu: &NmEcu,
151    ) -> Result<FlexrayNmNode, AutosarAbstractionError> {
152        let nm_nodes = self.element().get_or_create_sub_element(ElementName::NmNodes)?;
153        FlexrayNmNode::new(name, &nm_nodes, controller, nm_ecu)
154    }
155}
156
157impl AbstractNmCluster for FlexrayNmCluster {
158    type CommunicationClusterType = FlexrayCluster;
159    type NmNodeType = FlexrayNmNode;
160}
161
162//##################################################################
163
164/// Mandatory settings for a `FlexrayNmCluster`
165///
166/// These settings must be provided when creating a new `FlexrayNmCluster`.
167/// Additional optional settings can be set using `FlexrayNmCluster` methods.
168#[derive(Debug, Clone, PartialEq)]
169pub struct FlexrayNmClusterSettings {
170    /// nmDataCycle: Number of `FlexRay` Communication Cycles needed to transmit the Nm Data PDUs of all `FlexRay` Nm Ecus of this `FlexrayNmCluster`.
171    pub nm_data_cycle: u32,
172    /// nmRemoteSleepIndicationTime: Timeout for Remote Sleep Indication in seconds.
173    pub nm_remote_sleep_indication_time: f64,
174    /// nmRepeatMessageTime: Timeout for Repeat Message State in seconds.
175    pub nm_repeat_message_time: f64,
176    /// nmRepetitionCycle: Number of `FlexRay` Communication Cycles used to repeat the transmission of the Nm vote Pdus of all
177    /// `FlexRay` `NmEcus` of this `FlexrayNmCluster`. This value shall be an integral multiple of nmVotingCycle.
178    pub nm_repetition_cycle: u32,
179    /// nmVotingCycle: The number of `FlexRay` Communication Cycles used to transmit the Nm Vote PDUs of all `FlexRay` Nm Ecus of this `FlexrayNmCluster`.
180    pub nm_voting_cycle: u32,
181}
182
183//##################################################################
184
185/// A `FlexRayNmClusterCoupling` couples multiple `FlexrayNmCluster`s.
186#[derive(Debug, Clone, PartialEq, Eq, Hash)]
187pub struct FlexrayNmClusterCoupling(Element);
188abstraction_element!(FlexrayNmClusterCoupling, FlexrayNmClusterCoupling);
189
190impl FlexrayNmClusterCoupling {
191    pub(crate) fn new(
192        parent: &Element,
193        nm_schedule_variant: FlexrayNmScheduleVariant,
194    ) -> Result<Self, AutosarAbstractionError> {
195        let nm_cluster_coupling_elem = parent.create_sub_element(ElementName::FlexrayNmClusterCoupling)?;
196        let nm_cluster_coupling = Self(nm_cluster_coupling_elem);
197        nm_cluster_coupling.set_nm_schedule_variant(nm_schedule_variant)?;
198
199        Ok(nm_cluster_coupling)
200    }
201
202    /// set the nmScheduleVariant
203    pub fn set_nm_schedule_variant(
204        &self,
205        nm_schedule_variant: FlexrayNmScheduleVariant,
206    ) -> Result<(), AutosarAbstractionError> {
207        self.element()
208            .get_or_create_sub_element(ElementName::NmScheduleVariant)?
209            .set_character_data::<EnumItem>(nm_schedule_variant.into())?;
210        Ok(())
211    }
212
213    /// get the nmScheduleVariant
214    #[must_use]
215    pub fn nm_schedule_variant(&self) -> Option<FlexrayNmScheduleVariant> {
216        self.element()
217            .get_sub_element(ElementName::NmScheduleVariant)
218            .and_then(|elem| elem.character_data())
219            .and_then(|cdata| cdata.enum_value())
220            .and_then(|enum_item| FlexrayNmScheduleVariant::try_from(enum_item).ok())
221    }
222}
223
224impl AbstractNmClusterCoupling for FlexrayNmClusterCoupling {
225    type NmClusterType = FlexrayNmCluster;
226}
227
228//##################################################################
229
230/// The `FlexrayNmScheduleVariant` defines the way the NM-Vote and NM-Data are transmitted within the `FlexRay` network.
231#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
232pub enum FlexrayNmScheduleVariant {
233    /// NM-Vote and NM Data transmitted within one PDU in static segment. The NM-Vote has to be realized as separate bit within the PDU.
234    ScheduleVariant1,
235    /// NM-Vote and NM-Data transmitted within one PDU in dynamic segment. The presence (or non-presence) of the PDU corresponds to the NM-Vote
236    ScheduleVariant2,
237    /// NM-Vote and NM-Data are transmitted in the static segment in separate PDUs. This alternative is not recommended => Alternative 1 should be used instead.
238    ScheduleVariant3,
239    /// NM-Vote transmitted in static and NM-Data transmitted in dynamic segment.
240    ScheduleVariant4,
241    /// NM-Vote is transmitted in dynamic and NM-Data is transmitted in static segment. This alternative is not recommended => Variants 2 or 6 should be used instead.
242    ScheduleVariant5,
243    /// NM-Vote and NM-Data are transmitted in the dynamic segment in separate PDUs.
244    ScheduleVariant6,
245    /// NM-Vote and a copy of the CBV are transmitted in the static segment (using the `FlexRay` NM Vector support) and NM-Data is transmitted in the dynamic segment
246    ScheduleVariant7,
247}
248
249impl From<FlexrayNmScheduleVariant> for EnumItem {
250    fn from(value: FlexrayNmScheduleVariant) -> Self {
251        match value {
252            FlexrayNmScheduleVariant::ScheduleVariant1 => EnumItem::ScheduleVariant1,
253            FlexrayNmScheduleVariant::ScheduleVariant2 => EnumItem::ScheduleVariant2,
254            FlexrayNmScheduleVariant::ScheduleVariant3 => EnumItem::ScheduleVariant3,
255            FlexrayNmScheduleVariant::ScheduleVariant4 => EnumItem::ScheduleVariant4,
256            FlexrayNmScheduleVariant::ScheduleVariant5 => EnumItem::ScheduleVariant5,
257            FlexrayNmScheduleVariant::ScheduleVariant6 => EnumItem::ScheduleVariant6,
258            FlexrayNmScheduleVariant::ScheduleVariant7 => EnumItem::ScheduleVariant7,
259        }
260    }
261}
262
263impl TryFrom<EnumItem> for FlexrayNmScheduleVariant {
264    type Error = AutosarAbstractionError;
265
266    fn try_from(value: EnumItem) -> Result<Self, Self::Error> {
267        match value {
268            EnumItem::ScheduleVariant1 => Ok(FlexrayNmScheduleVariant::ScheduleVariant1),
269            EnumItem::ScheduleVariant2 => Ok(FlexrayNmScheduleVariant::ScheduleVariant2),
270            EnumItem::ScheduleVariant3 => Ok(FlexrayNmScheduleVariant::ScheduleVariant3),
271            EnumItem::ScheduleVariant4 => Ok(FlexrayNmScheduleVariant::ScheduleVariant4),
272            EnumItem::ScheduleVariant5 => Ok(FlexrayNmScheduleVariant::ScheduleVariant5),
273            EnumItem::ScheduleVariant6 => Ok(FlexrayNmScheduleVariant::ScheduleVariant6),
274            EnumItem::ScheduleVariant7 => Ok(FlexrayNmScheduleVariant::ScheduleVariant7),
275            _ => Err(AutosarAbstractionError::ValueConversionError {
276                value: value.to_string(),
277                dest: "FlexrayNmScheduleVariant".to_string(),
278            }),
279        }
280    }
281}
282
283//##################################################################
284
285/// A `FlexrayNmNode` represents a `FlexRay` specific `NmNode`.
286///
287/// It connects a `FlexrayCommunicationController` with a `NmEcu`.
288#[derive(Debug, Clone, PartialEq, Eq, Hash)]
289pub struct FlexrayNmNode(Element);
290abstraction_element!(FlexrayNmNode, FlexrayNmNode);
291impl IdentifiableAbstractionElement for FlexrayNmNode {}
292
293impl FlexrayNmNode {
294    pub(crate) fn new(
295        name: &str,
296        parent: &Element,
297        controller: &FlexrayCommunicationController,
298        nm_ecu: &NmEcu,
299    ) -> Result<Self, AutosarAbstractionError> {
300        let fr_nm_node_elem = parent.create_named_sub_element(ElementName::FlexrayNmNode, name)?;
301        let fr_nm_node = Self(fr_nm_node_elem);
302        fr_nm_node.set_communication_controller(controller)?;
303        fr_nm_node.set_nm_ecu(nm_ecu)?;
304
305        Ok(fr_nm_node)
306    }
307}
308
309impl AbstractNmNode for FlexrayNmNode {
310    type CommunicationControllerType = FlexrayCommunicationController;
311}