use crate::utils::ffi;
pub struct SlaveDiagnosticsData {
pub link_quality_percent: Vec<u32>,
pub port_error_count: Vec<u32>,
pub lost_link_count: Vec<u32>,
pub frame_errors: u32,
pub lost_frames: u32,
pub checksum_errors: u32,
pub timeout_frames: u32,
pub primary_port_errors: u32,
pub secondary_port_errors: u32,
}
pub fn get_diagnostics(master_index: u16, slave_count: u16) -> Option<SlaveDiagnosticsData> {
if slave_count == 0 {
return None;
}
let count = slave_count as usize;
let mut link_quality = vec![0u32; count + 1];
let mut port_error = vec![0u32; count + 1];
let mut lost_link = vec![0u32; count + 1];
for i in 1..=count {
let slave_idx = i as u16;
unsafe {
let lq = ffi::GetSlaveLinkQuality(master_index, slave_idx);
link_quality[i] = if lq < 0 { 0 } else { lq as u32 };
let mut rx = [0u8; 4];
let mut inv = [0u8; 4];
let mut lost = [0u8; 4];
if ffi::ReadSlavePortErrorCounters(
master_index, slave_idx,
rx.as_mut_ptr(), inv.as_mut_ptr(), lost.as_mut_ptr(),
) != 0 {
for p in 0..4 {
port_error[i] += (rx[p] as u32) + (inv[p] as u32);
lost_link[i] += lost[p] as u32;
}
}
}
}
Some(SlaveDiagnosticsData {
link_quality_percent: link_quality,
port_error_count: port_error,
lost_link_count: lost_link,
frame_errors: 0,
lost_frames: 0,
checksum_errors: 0,
timeout_frames: 0,
primary_port_errors: 0,
secondary_port_errors: 0,
})
}
pub fn initialize_logging(
log_callback: Option<crate::utils::ffi::LogCallback>,
crash_callback: Option<crate::utils::ffi::CrashNotifyCallback>,
) {
unsafe {
if let Some(cb) = log_callback {
ffi::SetLogCallback(cb);
}
if let Some(cb) = crash_callback {
ffi::SetCrashCallback(cb);
}
ffi::SetPDOLogging(0);
ffi::SetMailboxLogging(0);
ffi::SetDebugLogging(0);
}
}
pub fn enable_pdo_logging(enable: bool) {
unsafe { ffi::SetPDOLogging(if enable { 1 } else { 0 }); }
}
pub fn enable_mailbox_logging(enable: bool) {
unsafe { ffi::SetMailboxLogging(if enable { 1 } else { 0 }); }
}
pub fn enable_debug_logging(enable: bool) {
unsafe { ffi::SetDebugLogging(if enable { 1 } else { 0 }); }
}