autosar_data_abstraction/communication/network_management/
flexray_nm.rs1use 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#[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 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 #[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 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 #[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 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 #[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 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 #[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 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 #[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 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#[derive(Debug, Clone, PartialEq)]
169pub struct FlexrayNmClusterSettings {
170 pub nm_data_cycle: u32,
172 pub nm_remote_sleep_indication_time: f64,
174 pub nm_repeat_message_time: f64,
176 pub nm_repetition_cycle: u32,
179 pub nm_voting_cycle: u32,
181}
182
183#[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 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 #[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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
232pub enum FlexrayNmScheduleVariant {
233 ScheduleVariant1,
235 ScheduleVariant2,
237 ScheduleVariant3,
239 ScheduleVariant4,
241 ScheduleVariant5,
243 ScheduleVariant6,
245 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#[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}