#![cfg(target_os = "linux")]
use pnet::datalink::{self, Channel, NetworkInterface as PnetInterface};
use powerlink_rs::{NetworkInterface, PowerlinkError};
use std::io;
use std::sync::Mutex;
use std::time::Duration;
pub struct LinuxPnetInterface {
tx: Mutex<Box<dyn datalink::DataLinkSender>>,
rx: Mutex<Box<dyn datalink::DataLinkReceiver>>,
pnet_iface: PnetInterface, node_id: u8,
mac_address: [u8; 6],
}
impl LinuxPnetInterface {
pub fn new(interface_name: &str, node_id: u8) -> Result<Self, String> {
let interface = datalink::interfaces()
.into_iter()
.find(|iface: &PnetInterface| iface.name == interface_name)
.ok_or_else(|| format!("Interface '{}' not found", interface_name))?;
let mac_address = interface.mac.ok_or("Interface has no MAC address")?.into();
let config = datalink::Config {
read_timeout: Some(Duration::from_millis(100)),
promiscuous: true,
..Default::default()
};
let (tx, rx) = match datalink::channel(&interface, config) {
Ok(Channel::Ethernet(tx, rx)) => (tx, rx),
Ok(_) => return Err("Unsupported channel type".to_string()),
Err(e) => return Err(e.to_string()),
};
Ok(Self {
tx: Mutex::new(tx),
rx: Mutex::new(rx),
pnet_iface: interface, node_id,
mac_address,
})
}
pub fn set_read_timeout(&mut self, duration: Duration) -> Result<(), PowerlinkError> {
let config = datalink::Config {
read_timeout: Some(duration),
promiscuous: true,
..Default::default()
};
match datalink::channel(&self.pnet_iface, config) {
Ok(Channel::Ethernet(tx, rx)) => {
*self.tx.lock().unwrap() = tx;
*self.rx.lock().unwrap() = rx;
Ok(())
}
Ok(_) => Err(PowerlinkError::IoError),
Err(e) => {
println!("Failed to set read timeout: {}", e);
Err(PowerlinkError::IoError)
}
}
}
}
impl NetworkInterface for LinuxPnetInterface {
fn send_frame(&mut self, frame: &[u8]) -> Result<(), PowerlinkError> {
self.tx
.lock()
.unwrap()
.send_to(frame, None)
.ok_or(PowerlinkError::IoError)? .map_err(|_| PowerlinkError::IoError)?; Ok(())
}
fn receive_frame(&mut self, buffer: &mut [u8]) -> Result<usize, PowerlinkError> {
let mut rx_guard = self.rx.lock().unwrap();
match rx_guard.next() {
Ok(frame) => {
let len = frame.len();
if buffer.len() >= len {
buffer[..len].copy_from_slice(frame);
Ok(len)
} else {
Err(PowerlinkError::BufferTooShort)
}
}
Err(e) => {
if e.kind() == io::ErrorKind::TimedOut {
Ok(0) } else {
Err(PowerlinkError::IoError)
}
}
}
}
fn local_node_id(&self) -> u8 {
self.node_id
}
fn local_mac_address(&self) -> [u8; 6] {
self.mac_address
}
}