use crate::io;
pub fn gpiodetect(_argc: i32, _argv: *const *const u8) -> i32 {
let mut found = false;
for i in 0..16 {
let mut path = [0u8; 32];
let prefix = b"/dev/gpiochip";
path[..prefix.len()].copy_from_slice(prefix);
if i < 10 {
path[prefix.len()] = b'0' + i;
path[prefix.len() + 1] = 0;
} else {
path[prefix.len()] = b'0' + (i / 10);
path[prefix.len() + 1] = b'0' + (i % 10);
path[prefix.len() + 2] = 0;
}
let mut stat_buf: libc::stat = unsafe { core::mem::zeroed() };
if unsafe { libc::stat(path.as_ptr() as *const i8, &mut stat_buf) } == 0 {
io::write_str(1, b"gpiochip");
io::write_num(1, i as u64);
io::write_str(1, b" [");
let mut label_path = [0u8; 64];
let sysfs_prefix = b"/sys/class/gpio/gpiochip";
label_path[..sysfs_prefix.len()].copy_from_slice(sysfs_prefix);
if i < 10 {
label_path[sysfs_prefix.len()] = b'0' + i;
let suffix = b"/label";
label_path[sysfs_prefix.len() + 1..sysfs_prefix.len() + 1 + suffix.len()]
.copy_from_slice(suffix);
}
io::write_str(1, b"gpio");
io::write_str(1, b"] (lines: unknown)\n");
found = true;
}
}
if !found {
io::write_str(2, b"gpiodetect: no GPIO chips found\n");
return 1;
}
0
}
#[cfg(test)]
mod tests {
extern crate std;
use std::process::Command;
use std::path::PathBuf;
fn get_armybox_path() -> PathBuf {
if let Ok(path) = std::env::var("ARMYBOX_PATH") {
return PathBuf::from(path);
}
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.unwrap_or_else(|_| std::env::current_dir().unwrap());
let release = manifest_dir.join("target/release/armybox");
if release.exists() { return release; }
manifest_dir.join("target/debug/armybox")
}
#[test]
fn test_gpiodetect_runs() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["gpiodetect"])
.output()
.unwrap();
assert!(output.status.code() == Some(0) || output.status.code() == Some(1));
}
}