use log::{error, info};
use powerlink_rs_linux::LinuxPnetInterface;
use powerlink_rs::{
ControlledNode,
NetworkInterface,
PowerlinkError,
frame::basic::MacAddress,
nmt::{flags::FeatureFlags, states::NmtState},
node::{ManagingNode, Node, NodeAction}, od::{AccessType, Category, Object, ObjectDictionary, ObjectEntry, ObjectValue, PdoMapping},
pdo::PdoMappingEntry,
types::C_ADR_MN_DEF_NODE_ID,
};
use std::{
env, thread,
time::{Duration, Instant},
};
const CN_NODE_ID: u8 = 42;
const CYCLE_TIME_US: u32 = 20_000;
const IDX_DIGITAL_INPUTS: u16 = 0x6000;
const IDX_ANALOG_INPUTS: u16 = 0x6001;
const IDX_DIGITAL_OUTPUTS: u16 = 0x6200;
const IDX_ANALOG_OUTPUTS: u16 = 0x6201;
fn get_cn_od(node_id: u8) -> ObjectDictionary<'static> {
let mut od = ObjectDictionary::new(None);
add_mandatory_cn_objects(&mut od, node_id);
od.insert(
IDX_DIGITAL_INPUTS,
ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned8(0)),
name: "Digital_Inputs_8bit",
category: Category::Mandatory,
access: Some(AccessType::ReadWrite), default_value: Some(ObjectValue::Unsigned8(0)),
value_range: None,
pdo_mapping: Some(PdoMapping::Optional),
},
);
od.insert(
IDX_ANALOG_INPUTS,
ObjectEntry {
object: Object::Array(vec![
ObjectValue::Unsigned16(0), ObjectValue::Unsigned16(0), ObjectValue::Unsigned16(0), ObjectValue::Unsigned16(0), ]),
name: "Analog_Inputs_4x16bit",
category: Category::Mandatory,
access: None,
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
IDX_DIGITAL_OUTPUTS,
ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned8(0)),
name: "Digital_Outputs_8bit",
category: Category::Mandatory,
access: Some(AccessType::ReadWrite), default_value: Some(ObjectValue::Unsigned8(0)),
value_range: None,
pdo_mapping: Some(PdoMapping::Optional),
},
);
od.insert(
IDX_ANALOG_OUTPUTS,
ObjectEntry {
object: Object::Array(vec![
ObjectValue::Unsigned16(0),
ObjectValue::Unsigned16(0),
ObjectValue::Unsigned16(0),
ObjectValue::Unsigned16(0),
]),
name: "Analog_Outputs_4x16bit",
category: Category::Mandatory,
access: None,
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
let tpdo1_map_di = PdoMappingEntry {
index: IDX_DIGITAL_INPUTS,
sub_index: 0,
offset_bits: 0,
length_bits: 8,
};
let tpdo1_map_ai1 = PdoMappingEntry {
index: IDX_ANALOG_INPUTS,
sub_index: 1,
offset_bits: 8,
length_bits: 16,
};
od.insert(
0x1A00, ObjectEntry {
object: Object::Array(vec![
ObjectValue::Unsigned8(2), ObjectValue::Unsigned64(tpdo1_map_di.to_u64()),
ObjectValue::Unsigned64(tpdo1_map_ai1.to_u64()),
]),
name: "PDO_TxMappParam_00h_AU64",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
let rpdo1_map_do = PdoMappingEntry {
index: IDX_DIGITAL_OUTPUTS,
sub_index: 0,
offset_bits: 0,
length_bits: 8,
};
let rpdo1_map_ao1 = PdoMappingEntry {
index: IDX_ANALOG_OUTPUTS,
sub_index: 1,
offset_bits: 8,
length_bits: 16,
};
od.insert(
0x1600, ObjectEntry {
object: Object::Array(vec![
ObjectValue::Unsigned8(2), ObjectValue::Unsigned64(rpdo1_map_do.to_u64()),
ObjectValue::Unsigned64(rpdo1_map_ao1.to_u64()),
]),
name: "PDO_RxMappParam_00h_AU64",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od
}
fn get_mn_od(cn_mac: MacAddress) -> ObjectDictionary<'static> {
let mut od = ObjectDictionary::new(None);
add_mandatory_mn_objects(&mut od);
let mut mac_map_entries = vec![ObjectValue::Unsigned8(254)]; for i in 1..=254 {
if i == CN_NODE_ID as usize {
mac_map_entries.push(ObjectValue::OctetString(cn_mac.0.to_vec()));
} else {
mac_map_entries.push(ObjectValue::OctetString(vec![0; 6])); }
}
od.insert(
0x1F84, ObjectEntry {
object: Object::Array(mac_map_entries),
name: "NMT_MNNodeCurrMACAddress_AU8",
category: Category::Mandatory,
access: Some(AccessType::ReadWrite),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
IDX_DIGITAL_INPUTS,
ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned8(0)),
name: "Digital_Inputs_CN42",
category: Category::Mandatory,
access: Some(AccessType::ReadOnly),
default_value: Some(ObjectValue::Unsigned8(0)),
value_range: None,
pdo_mapping: None,
},
);
od.insert(
IDX_ANALOG_INPUTS,
ObjectEntry {
object: Object::Array(vec![
ObjectValue::Unsigned16(0),
ObjectValue::Unsigned16(0),
ObjectValue::Unsigned16(0),
ObjectValue::Unsigned16(0),
]),
name: "Analog_Inputs_CN42",
category: Category::Mandatory,
access: None,
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
IDX_DIGITAL_OUTPUTS,
ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned8(0)),
name: "Digital_Outputs_CN42",
category: Category::Mandatory,
access: Some(AccessType::ReadWrite),
default_value: Some(ObjectValue::Unsigned8(0)),
value_range: None,
pdo_mapping: None,
},
);
od.insert(
IDX_ANALOG_OUTPUTS,
ObjectEntry {
object: Object::Array(vec![
ObjectValue::Unsigned16(0),
ObjectValue::Unsigned16(0),
ObjectValue::Unsigned16(0),
ObjectValue::Unsigned16(0),
]),
name: "Analog_Outputs_CN42",
category: Category::Mandatory,
access: None,
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1401, ObjectEntry {
object: Object::Record(vec![
ObjectValue::Unsigned8(CN_NODE_ID), ObjectValue::Unsigned8(0), ]),
name: "PDO_RxCommParam_01h_REC",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
let rpdo1_map_di = PdoMappingEntry {
index: IDX_DIGITAL_INPUTS,
sub_index: 0,
offset_bits: 0,
length_bits: 8,
};
let rpdo1_map_ai1 = PdoMappingEntry {
index: IDX_ANALOG_INPUTS,
sub_index: 1,
offset_bits: 8,
length_bits: 16,
};
od.insert(
0x1601, ObjectEntry {
object: Object::Array(vec![
ObjectValue::Unsigned8(2),
ObjectValue::Unsigned64(rpdo1_map_di.to_u64()),
ObjectValue::Unsigned64(rpdo1_map_ai1.to_u64()),
]),
name: "PDO_RxMappParam_01h_AU64",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1801, ObjectEntry {
object: Object::Record(vec![
ObjectValue::Unsigned8(CN_NODE_ID), ObjectValue::Unsigned8(0), ]),
name: "PDO_TxCommParam_01h_REC",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
let tpdo1_map_do = PdoMappingEntry {
index: IDX_DIGITAL_OUTPUTS,
sub_index: 0,
offset_bits: 0,
length_bits: 8,
};
let tpdo1_map_ao1 = PdoMappingEntry {
index: IDX_ANALOG_OUTPUTS,
sub_index: 1,
offset_bits: 8,
length_bits: 16,
};
od.insert(
0x1A01, ObjectEntry {
object: Object::Array(vec![
ObjectValue::Unsigned8(2),
ObjectValue::Unsigned64(tpdo1_map_do.to_u64()),
ObjectValue::Unsigned64(tpdo1_map_ao1.to_u64()),
]),
name: "PDO_TxMappParam_01h_AU64",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od
}
fn run_cn_logic(interface_name: &str) {
info!("[CN] Starting up as I/O Module (Node ID {}).", CN_NODE_ID);
let od = get_cn_od(CN_NODE_ID);
let (mut interface, mut node) =
setup_cn_node(interface_name, CN_NODE_ID, od).expect("Failed to setup CN");
let mut buffer = [0u8; 1518];
let start_time = Instant::now();
let mut digital_input_counter: u8 = 0;
loop {
let current_time_us = start_time.elapsed().as_micros() as u64;
digital_input_counter = digital_input_counter.wrapping_add(1);
node.od
.write(
IDX_DIGITAL_INPUTS,
0,
ObjectValue::Unsigned8(digital_input_counter),
)
.unwrap();
if let Some(do_val) = node.od.read_u8(IDX_DIGITAL_OUTPUTS, 0) {
if do_val != 0 {
info!("[CN] Digital outputs received from MN: {:#04x}", do_val);
}
}
let action;
match interface.receive_frame(&mut buffer) {
Ok(0) => {
action = node.tick(current_time_us);
}
Ok(bytes) => {
action = node.process_raw_frame(&buffer[..bytes], current_time_us);
}
Err(PowerlinkError::IoError) => {
action = node.tick(current_time_us);
}
Err(e) => {
error!("[CN] Receive error: {:?}", e);
action = NodeAction::NoAction;
}
}
if let NodeAction::SendFrame(response) = action {
if let Err(e) = interface.send_frame(&response) {
error!("[CN] Failed to send frame: {:?}", e);
}
}
thread::sleep(Duration::from_micros(100));
}
}
fn run_mn_logic(interface_name: &str, cn_mac: MacAddress) {
info!("[MN] Starting up as Managing Node.");
let od = get_mn_od(cn_mac);
let (mut interface, mut node) = setup_mn_node(interface_name, od).expect("Failed to setup MN");
let start_time = Instant::now();
let mut last_log_time = Instant::now();
loop {
let current_time_us = start_time.elapsed().as_micros() as u64;
if let Some(deadline) = node.next_action_time() {
if current_time_us < deadline {
let wait_time = (deadline - current_time_us).min(1000); thread::sleep(Duration::from_micros(wait_time));
continue;
}
}
let action = node.tick(current_time_us);
if let NodeAction::SendFrame(frame) = action {
if let Err(e) = interface.send_frame(&frame) {
error!("[MN] Failed to send frame: {:?}", e);
}
}
let mut buffer = [0u8; 1518];
interface
.set_read_timeout(Duration::from_micros(100))
.unwrap();
if let Ok(bytes) = interface.receive_frame(&mut buffer) {
if bytes > 0 {
node.process_raw_frame(&buffer[..bytes], current_time_us);
}
}
if node.nmt_state() == NmtState::NmtOperational {
if let Some(di_val) = node.od.read_u8(IDX_DIGITAL_INPUTS, 0) {
node.od
.write(IDX_DIGITAL_OUTPUTS, 0, ObjectValue::Unsigned8(di_val))
.unwrap();
if last_log_time.elapsed() > Duration::from_secs(1) {
info!(
"[MN] Received Digital Inputs from CN {}: {:#04x}",
CN_NODE_ID, di_val
);
last_log_time = Instant::now();
}
}
}
}
}
fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let role = env::var("POWERLINK_ROLE").unwrap_or_else(|_| "MN".to_string());
let cn_mac_str = env::var("CN_MAC_ADDRESS").unwrap_or_else(|_| "02:00:00:00:00:42".to_string());
let cn_mac_bytes: Vec<u8> = cn_mac_str
.split(':')
.map(|s| u8::from_str_radix(s, 16).unwrap())
.collect();
let cn_mac: [u8; 6] = cn_mac_bytes
.try_into()
.unwrap_or([0x02, 0x00, 0x00, 0x00, 0x00, 0x42]);
if role == "CN" {
run_cn_logic("eth0");
} else {
run_mn_logic("eth0", cn_mac.into());
}
}
fn add_mandatory_cn_objects(od: &mut ObjectDictionary, node_id: u8) {
od.insert(
0x1000, ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned32(0x000F0191)),
name: "NMT_DeviceType_U32",
category: Category::Mandatory,
access: Some(AccessType::Constant),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1018, ObjectEntry {
object: Object::Record(vec![
ObjectValue::Unsigned32(0x12345678), ObjectValue::Unsigned32(0x00000002), ObjectValue::Unsigned32(0x00010000), ObjectValue::Unsigned32(0x98765432), ]),
name: "NMT_IdentityObject_REC",
category: Category::Mandatory,
access: None,
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
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",
category: Category::Mandatory,
access: Some(AccessType::Constant),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1F93, ObjectEntry {
object: Object::Record(vec![
ObjectValue::Unsigned8(node_id),
ObjectValue::Boolean(0), ]),
name: "NMT_EPLNodeID_REC",
category: Category::Mandatory,
access: None,
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1F99, ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned32(5_000_000)), name: "NMT_CNBasicEthernetTimeout_U32",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: Some(ObjectValue::Unsigned32(5_000_000)),
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1006, ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned32(CYCLE_TIME_US)),
name: "NMT_CycleLen_U32",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: Some(ObjectValue::Unsigned32(CYCLE_TIME_US)),
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1C14, ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned32(1_000_000)), name: "DLL_CNLossOfSocTolerance_U32",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: Some(ObjectValue::Unsigned32(1_000_000)),
value_range: None,
pdo_mapping: None,
},
);
}
fn add_mandatory_mn_objects(od: &mut ObjectDictionary) {
od.insert(
0x1000, ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned32(0x000F0191)), name: "NMT_DeviceType_U32",
category: Category::Mandatory,
access: Some(AccessType::Constant),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1018, ObjectEntry {
object: Object::Record(vec![
ObjectValue::Unsigned32(0x12345678), ObjectValue::Unsigned32(0x00000001), ObjectValue::Unsigned32(0x00010000), ObjectValue::Unsigned32(0x12345678), ]),
name: "NMT_IdentityObject_REC",
category: Category::Mandatory,
access: None,
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
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",
category: Category::Mandatory,
access: Some(AccessType::Constant),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1006, ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned32(CYCLE_TIME_US)),
name: "NMT_CycleLen_U32",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: Some(ObjectValue::Unsigned32(CYCLE_TIME_US)),
value_range: None,
pdo_mapping: None,
},
);
let mut node_assignment = vec![ObjectValue::Unsigned8(254)]; for i in 1..=254 {
if i == CN_NODE_ID as usize {
node_assignment.push(ObjectValue::Unsigned32(1 | (1 << 3)));
} else {
node_assignment.push(ObjectValue::Unsigned32(0));
}
}
od.insert(
0x1F81, ObjectEntry {
object: Object::Array(node_assignment),
name: "NMT_NodeAssignment_AU32",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1F89, ObjectEntry {
object: Object::Record(vec![
ObjectValue::Unsigned32(1_000_000), ObjectValue::Unsigned32(500_000), ]),
name: "NMT_BootTime_REC",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1F80, ObjectEntry {
object: Object::Variable(ObjectValue::Unsigned32(0)), name: "NMT_StartUp_U32",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: Some(ObjectValue::Unsigned32(0)),
value_range: None,
pdo_mapping: None,
},
);
od.insert(
0x1F98, ObjectEntry {
object: Object::Record(vec![
ObjectValue::Unsigned16(1490), ObjectValue::Unsigned16(1490), ObjectValue::Unsigned32(10000), ObjectValue::Unsigned16(100), ObjectValue::Unsigned16(100), ObjectValue::Unsigned32(20000), ObjectValue::Unsigned8(0), ObjectValue::Unsigned16(300), ObjectValue::Unsigned16(2), ]),
name: "NMT_CycleTiming_REC",
category: Category::Mandatory,
access: Some(AccessType::ReadWriteStore),
default_value: None,
value_range: None,
pdo_mapping: None,
},
);
}
fn setup_cn_node<'a>(
interface_name: &str,
node_id: u8,
od: ObjectDictionary<'a>,
) -> Result<(LinuxPnetInterface, ControlledNode<'a>), PowerlinkError> {
let mut interface = match LinuxPnetInterface::new(interface_name, node_id) {
Ok(iface) => iface,
Err(e) => {
error!("[CN] Failed to create network interface: {:?}", e);
return Err(PowerlinkError::IoError);
}
};
interface.set_read_timeout(Duration::from_millis(10))?; let mac = interface.local_mac_address();
let node = ControlledNode::new(od, mac.into())?;
Ok((interface, node))
}
fn setup_mn_node<'a>(
interface_name: &str,
od: ObjectDictionary<'a>,
) -> Result<(LinuxPnetInterface, ManagingNode<'a>), PowerlinkError> {
let mut interface = match LinuxPnetInterface::new(interface_name, C_ADR_MN_DEF_NODE_ID) {
Ok(iface) => iface,
Err(e) => {
error!("[MN] Failed to create network interface: {:?}", e);
return Err(PowerlinkError::IoError);
}
};
interface.set_read_timeout(Duration::from_micros(100))?; let mac = interface.local_mac_address();
let node = ManagingNode::new(od, mac.into())?;
Ok((interface, node))
}