zencan_node/
node_state.rs1use zencan_common::nmt::NmtState;
3use zencan_common::AtomicCell;
4
5use crate::object_dict::ObjectFlagSync;
6
7use crate::pdo::Pdo;
8use crate::storage::StorageContext;
9
10pub trait NmtStateAccess: Send + Sync {
11 fn nmt_state(&self) -> NmtState;
12}
13
14impl NmtStateAccess for AtomicCell<NmtState> {
15 fn nmt_state(&self) -> NmtState {
16 self.load()
17 }
18}
19
20#[allow(missing_debug_implementations)]
26pub struct NodeState<'a> {
27 rpdos: &'a [Pdo<'a>],
29 tpdos: &'a [Pdo<'a>],
31 object_flag_sync: ObjectFlagSync,
33 storage_context: StorageContext,
36 nmt_state: AtomicCell<NmtState>,
38}
39
40impl NmtStateAccess for NodeState<'_> {
41 fn nmt_state(&self) -> NmtState {
42 self.nmt_state.load()
43 }
44}
45
46impl<'a> NodeState<'a> {
47 pub const fn new(rpdos: &'a [Pdo<'a>], tpdos: &'a [Pdo<'a>]) -> Self {
49 let object_flag_sync = ObjectFlagSync::new();
50 let storage_context = StorageContext::new();
51
52 Self {
53 rpdos,
54 tpdos,
55 object_flag_sync,
56 storage_context,
57 nmt_state: AtomicCell::new(NmtState::Bootup),
58 }
59 }
60
61 pub const fn rpdos(&self) -> &'a [Pdo<'a>] {
63 self.rpdos
64 }
65
66 pub const fn tpdos(&self) -> &'a [Pdo<'a>] {
68 self.tpdos
69 }
70
71 pub const fn object_flag_sync(&'a self) -> &'a ObjectFlagSync {
75 &self.object_flag_sync
76 }
77
78 pub const fn storage_context(&'a self) -> &'a StorageContext {
80 &self.storage_context
81 }
82
83 pub(crate) fn set_nmt_state(&self, nmt_state: NmtState) {
87 self.nmt_state.store(nmt_state);
88 }
89}