use crate::core::power::{PowerRail, PowerStats, TotalPower};
use crate::error::Result;
use crate::platform::common::*;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
const INA3221_PATH: &str = "/sys/bus/i2c/drivers/ina3221x";
pub fn read_power_stats() -> Result<PowerStats> {
let mut stats = PowerStats::empty();
if let Ok(rails) = read_ina3221_rails() {
stats.rails = rails;
let total_power: Option<u32> = stats
.rails
.values()
.filter(|r| r.online)
.map(|r| r.power)
.sum();
stats.total = TotalPower {
power: total_power,
average: None,
};
}
Ok(stats)
}
fn read_ina3221_rails() -> Result<HashMap<String, PowerRail>> {
let mut rails = HashMap::new();
if !Path::new(INA3221_PATH).exists() {
return Ok(rails);
}
for entry in fs::read_dir(INA3221_PATH)? {
let entry = entry?;
let path = entry.path();
if !path.is_dir() {
continue;
}
let hwmon_path = path.join("hwmon");
if !hwmon_path.exists() {
continue;
}
for hwmon_entry in fs::read_dir(&hwmon_path)? {
let hwmon_entry = hwmon_entry?;
let hwmon_device = hwmon_entry.path();
for i in 1..=3 {
if let Ok(rail) = read_ina3221_channel(&hwmon_device, i) {
let label = read_file_string(hwmon_device.join(format!("in{}_label", i)))
.unwrap_or_else(|_| format!("Rail {}", i));
rails.insert(label, rail);
}
}
}
}
Ok(rails)
}
fn read_ina3221_channel(hwmon_path: &Path, channel: u32) -> Result<PowerRail> {
let voltage_path = hwmon_path.join(format!("in{}_input", channel));
let current_path = hwmon_path.join(format!("curr{}_input", channel));
let voltage = read_file_u32(&voltage_path).ok();
let current = read_file_u32(¤t_path).ok();
let power = match (voltage, current) {
(Some(v), Some(i)) => Some((v as u64 * i as u64 / 1000) as u32), _ => None,
};
let warn = read_file_u32(hwmon_path.join(format!("curr{}_crit", channel))).ok();
let crit = read_file_u32(hwmon_path.join(format!("curr{}_max", channel))).ok();
Ok(PowerRail {
online: true,
sensor_type: "INA3221".to_string(),
voltage,
current,
power,
average: None,
warn,
crit,
})
}