zencan_node/node_state.rs
1//! Implements node state struct
2use crate::object_dict::ObjectFlagSync;
3
4use crate::pdo::Pdo;
5use crate::storage::StorageContext;
6
7/// A trait by which NodeState is accessed
8pub trait NodeStateAccess: Sync + Send {
9 /// Get the receive PDO objects
10 fn get_rpdos(&self) -> &[Pdo];
11 /// Get the transmit PDO objects
12 fn get_tpdos(&self) -> &[Pdo];
13 /// Get the PDO flag sync object
14 fn object_flag_sync(&self) -> &ObjectFlagSync;
15 /// Get the storage context object
16 fn storage_context(&self) -> &StorageContext;
17}
18
19/// The NodeState provides config-dependent storage to the [`Node`](crate::Node) object
20///
21/// The node state has to get instantiated (statically) by zencan-build, based on the device config
22/// via the [`NodeStateAccess`] trait.
23/// file. It is then provided to the node by the application when it is instantiated, and accessed
24#[allow(missing_debug_implementations)]
25pub struct NodeState<'a> {
26 /// Pdo control objects for receive PDOs
27 rpdos: &'a [Pdo],
28 /// Pdo control objects for transmit PDOs
29 tpdos: &'a [Pdo],
30 /// A global flag used by all objects to synchronize their event flag A/B swapping
31 object_flag_sync: ObjectFlagSync,
32 /// State shared between the [`StorageCommandObject`](crate::storage::StorageCommandObject) and
33 /// [`Node`](crate::node::Node) for indicating when a store objects command has been recieved.
34 storage_context: StorageContext,
35}
36
37impl<'a> NodeState<'a> {
38 /// Create a new NodeState object
39 pub const fn new(rpdos: &'a [Pdo], tpdos: &'a [Pdo]) -> Self {
40 let object_flag_sync = ObjectFlagSync::new();
41 let storage_context = StorageContext::new();
42
43 Self {
44 rpdos,
45 tpdos,
46 object_flag_sync,
47 storage_context,
48 }
49 }
50
51 /// Access the RPDOs as a const function
52 pub const fn rpdos(&self) -> &'a [Pdo] {
53 self.rpdos
54 }
55
56 /// Access the TPDOs as a const function
57 pub const fn tpdos(&self) -> &'a [Pdo] {
58 self.tpdos
59 }
60
61 /// Access the pdo_sync as a const function
62 ///
63 /// This is required so that it can be shared with the objects in generated code
64 pub const fn object_flag_sync(&'static self) -> &'static ObjectFlagSync {
65 &self.object_flag_sync
66 }
67
68 /// Access the storage_context as a const function
69 pub const fn storage_context(&'static self) -> &'static StorageContext {
70 &self.storage_context
71 }
72}
73
74impl NodeStateAccess for NodeState<'_> {
75 fn get_rpdos(&self) -> &[Pdo] {
76 self.rpdos
77 }
78
79 fn get_tpdos(&self) -> &[Pdo] {
80 self.tpdos
81 }
82
83 fn object_flag_sync(&self) -> &ObjectFlagSync {
84 &self.object_flag_sync
85 }
86
87 fn storage_context(&self) -> &StorageContext {
88 &self.storage_context
89 }
90}