use super::state_machine::NmtStateMachine;
use crate::frame::DllError;
use crate::od::{ObjectDictionary, ObjectValue};
use crate::types::NodeId;
use crate::PowerlinkError;
use super::flags::FeatureFlags;
use super::states::{NmtEvent, NmtState};
use alloc::vec::Vec;
pub struct CnNmtStateMachine {
pub current_state: NmtState,
pub node_id: NodeId,
pub feature_flags: FeatureFlags,
pub basic_ethernet_timeout: u32,
}
impl CnNmtStateMachine {
pub fn new(
node_id: NodeId,
feature_flags: FeatureFlags,
basic_ethernet_timeout: u32,
) -> Self {
Self {
current_state: NmtState::NmtGsInitialising,
node_id,
feature_flags,
basic_ethernet_timeout,
}
}
pub fn from_od(od: &ObjectDictionary) -> Result<Self, PowerlinkError> {
let node_id_val = od.read(0x1F93, 1).ok_or(PowerlinkError::ObjectNotFound)?;
let node_id = if let ObjectValue::Unsigned8(val) = &*node_id_val {
NodeId::try_from(*val)?
} else {
return Err(PowerlinkError::TypeMismatch);
};
let feature_flags_val = od.read(0x1F82, 0).ok_or(PowerlinkError::ObjectNotFound)?;
let feature_flags = if let ObjectValue::Unsigned32(val) = &*feature_flags_val {
FeatureFlags::from_bits_truncate(*val)
} else {
return Err(PowerlinkError::TypeMismatch);
};
let basic_ethernet_timeout_val = od.read(0x1F99, 0).ok_or(PowerlinkError::ObjectNotFound)?;
let basic_ethernet_timeout = if let ObjectValue::Unsigned32(val) = &*basic_ethernet_timeout_val {
*val
} else {
return Err(PowerlinkError::TypeMismatch);
};
Ok(Self::new(node_id, feature_flags, basic_ethernet_timeout))
}
}
impl NmtStateMachine for CnNmtStateMachine {
fn current_state(&self) -> NmtState {
self.current_state
}
fn reset(&mut self, event: NmtEvent) {
match event {
NmtEvent::Reset => self.current_state = NmtState::NmtGsInitialising,
NmtEvent::ResetNode => self.current_state = NmtState::NmtGsResetApplication,
NmtEvent::ResetCommunication => self.current_state = NmtState::NmtGsResetCommunication,
NmtEvent::ResetConfiguration => self.current_state = NmtState::NmtGsResetConfiguration,
_ => {}, }
}
fn run_internal_initialisation(&mut self, od: &mut ObjectDictionary) {
let mut transition = true;
while transition {
let next_state = match self.current_state {
NmtState::NmtGsInitialising => NmtState::NmtGsResetApplication,
NmtState::NmtGsResetApplication => NmtState::NmtGsResetCommunication,
NmtState::NmtGsResetCommunication => NmtState::NmtGsResetConfiguration,
NmtState::NmtGsResetConfiguration => NmtState::NmtNotActive,
_ => {
transition = false; self.current_state
}
};
if self.current_state != next_state {
self.current_state = next_state;
self.update_od_state(od);
} else {
transition = false;
}
}
}
fn process_event(&mut self, event: NmtEvent, od: &mut ObjectDictionary) -> Option<Vec<DllError>> {
let mut errors: Vec<DllError> = Vec::new();
let old_state = self.current_state;
let next_state = match (self.current_state, event) {
(_, NmtEvent::Reset) => NmtState::NmtGsInitialising,
(_, NmtEvent::ResetNode) => NmtState::NmtGsResetApplication,
(_, NmtEvent::ResetCommunication) => NmtState::NmtGsResetCommunication,
(_, NmtEvent::ResetConfiguration) => NmtState::NmtGsResetConfiguration,
(NmtState::NmtNotActive, NmtEvent::SocSoAReceived) => NmtState::NmtPreOperational1,
(NmtState::NmtNotActive, NmtEvent::Timeout) => NmtState::NmtBasicEthernet,
(NmtState::NmtPreOperational1, NmtEvent::SocReceived) => NmtState::NmtPreOperational2,
(NmtState::NmtPreOperational2, NmtEvent::EnableReadyToOperate) => NmtState::NmtPreOperational2,
(NmtState::NmtPreOperational2, NmtEvent::CnConfigurationComplete) => NmtState::NmtReadyToOperate,
(NmtState::NmtReadyToOperate, NmtEvent::StartNode) => NmtState::NmtOperational,
(NmtState::NmtPreOperational2 | NmtState::NmtReadyToOperate | NmtState::NmtOperational, NmtEvent::StopNode) => NmtState::NmtCsStopped,
(NmtState::NmtOperational, NmtEvent::EnterPreOperational2) => NmtState::NmtPreOperational2,
(NmtState::NmtCsStopped, NmtEvent::EnterPreOperational2) => NmtState::NmtPreOperational2,
(NmtState::NmtPreOperational2 | NmtState::NmtReadyToOperate | NmtState::NmtOperational | NmtState::NmtCsStopped, NmtEvent::Error) => NmtState::NmtPreOperational1,
(NmtState::NmtBasicEthernet, NmtEvent::PowerlinkFrameReceived) => NmtState::NmtPreOperational1,
(current, _) => {
errors.push(DllError::UnexpectedEventInState { state: current as u8, event: event as u8 });
current
},
};
if old_state != next_state {
self.current_state = next_state;
self.update_od_state(od);
}
if errors.is_empty() {
None
} else {
Some(errors)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::od::{Object, ObjectEntry};
use crate::od::AccessType;
use alloc::vec;
fn get_test_od() -> ObjectDictionary<'static> {
let mut od = ObjectDictionary::new(None);
od.insert(0x1F93, ObjectEntry {
object: Object::Record(vec![
ObjectValue::Unsigned8(42),
ObjectValue::Boolean(0),
]),
name: "NMT_EPLNodeID_REC",
access: Some(AccessType::ReadWrite),
default_value: None,
value_range: None,
pdo_mapping: None,
category: crate::od::Category::Optional,
});
let flags = FeatureFlags::ISOCHRONOUS | FeatureFlags::SDO_ASND | FeatureFlags::SDO_UDP;
od.insert(0x1F82, ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned32(flags.0)),
name: "NMT_FeatureFlags_U32",
access: Some(AccessType::Constant),
default_value: None,
value_range: None,
pdo_mapping: None,
category: crate::od::Category::Optional,
});
od.insert(0x1F99, ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned32(5_000_000)),
name: "NMT_CNBasicEthernetTimeout_U32",
access: Some(AccessType::ReadWrite),
default_value: None,
value_range: None,
pdo_mapping: None,
category: crate::od::Category::Optional,
});
od.insert(0x1F8C, ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned8(0)),
name: "NMT_CurrNMTState_U8",
access: Some(AccessType::ReadOnly),
default_value: None,
value_range: None,
pdo_mapping: None,
category: crate::od::Category::Optional,
});
od
}
fn get_test_nmt() -> CnNmtStateMachine {
let node_id = NodeId::try_from(42).unwrap();
let feature_flags = FeatureFlags::ISOCHRONOUS | FeatureFlags::SDO_ASND;
CnNmtStateMachine::new(node_id, feature_flags, 5_000_000)
}
#[test]
fn test_from_od_reads_parameters() {
let od = get_test_od();
let nmt = CnNmtStateMachine::from_od(&od).unwrap();
assert_eq!(nmt.node_id, NodeId(42));
assert!(nmt.feature_flags.contains(FeatureFlags::SDO_ASND));
assert_eq!(nmt.basic_ethernet_timeout, 5_000_000);
}
#[test]
fn test_from_od_fails_if_missing_objects() {
let od = ObjectDictionary::new(None);
let result = CnNmtStateMachine::from_od(&od);
assert_eq!(result.err(), Some(PowerlinkError::ObjectNotFound));
}
#[test]
fn test_internal_boot_sequence() {
let mut od = get_test_od();
let mut nmt = get_test_nmt();
assert_eq!(nmt.current_state(), NmtState::NmtGsInitialising);
nmt.run_internal_initialisation(&mut od);
assert_eq!(nmt.current_state(), NmtState::NmtNotActive);
assert_eq!(od.read_u8(0x1F8C, 0), Some(NmtState::NmtNotActive as u8));
}
#[test]
fn test_full_boot_up_happy_path() {
let mut od = get_test_od();
let mut nmt = get_test_nmt();
nmt.current_state = NmtState::NmtNotActive;
nmt.process_event(NmtEvent::SocSoAReceived, &mut od);
assert_eq!(nmt.current_state(), NmtState::NmtPreOperational1);
nmt.process_event(NmtEvent::SocReceived, &mut od);
assert_eq!(nmt.current_state(), NmtState::NmtPreOperational2);
nmt.process_event(NmtEvent::EnableReadyToOperate, &mut od);
assert_eq!(nmt.current_state(), NmtState::NmtPreOperational2);
nmt.process_event(NmtEvent::CnConfigurationComplete, &mut od);
assert_eq!(nmt.current_state(), NmtState::NmtReadyToOperate);
nmt.process_event(NmtEvent::StartNode, &mut od);
assert_eq!(nmt.current_state(), NmtState::NmtOperational);
assert_eq!(od.read_u8(0x1F8C, 0), Some(NmtState::NmtOperational as u8));
}
#[test]
fn test_error_handling_transition() {
let mut od = get_test_od();
let mut nmt = get_test_nmt();
nmt.current_state = NmtState::NmtOperational;
nmt.process_event(NmtEvent::Error, &mut od);
assert_eq!(nmt.current_state(), NmtState::NmtPreOperational1);
}
#[test]
fn test_stop_and_restart_node() {
let mut od = get_test_od();
let mut nmt = get_test_nmt();
nmt.current_state = NmtState::NmtOperational;
nmt.process_event(NmtEvent::StopNode, &mut od);
assert_eq!(nmt.current_state(), NmtState::NmtCsStopped);
nmt.process_event(NmtEvent::EnterPreOperational2, &mut od);
assert_eq!(nmt.current_state(), NmtState::NmtPreOperational2);
}
}