Test parse card0, move checking vendor

This commit is contained in:
Adrian Woźniak 2021-07-04 00:08:15 +02:00
parent 2b3a41cb9b
commit 408b5864cd
No known key found for this signature in database
GPG Key ID: DE43476F72AD3F6C
4 changed files with 47 additions and 15 deletions

2
Cargo.lock generated
View File

@ -13,7 +13,7 @@ dependencies = [
[[package]]
name = "amdfand"
version = "1.0.3"
version = "1.0.4"
dependencies = [
"gumdrop",
"log",

View File

@ -1,6 +1,6 @@
[package]
name = "amdfand"
version = "1.0.3"
version = "1.0.4"
edition = "2018"
description = "AMDGPU fan control service"
license = "MIT OR Apache-2.0"

View File

@ -1,11 +1,11 @@
use crate::{AmdFanError, CONFIG_PATH, ROOT_DIR};
use crate::{AmdFanError, CONFIG_PATH};
use log::LevelFilter;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::Formatter;
use std::io::ErrorKind;
use std::str::FromStr;
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Card(pub u32);
impl std::fmt::Display for Card {
@ -24,19 +24,10 @@ impl FromStr for Card {
if value.len() < 5 {
return Err(AmdFanError::InputTooShort);
}
let card = value[4..]
value[4..]
.parse::<u32>()
.map_err(|e| AmdFanError::InvalidSuffix(format!("{:?}", e)))
.map(|n| Card(n))?;
std::fs::read_to_string(format!("{}/{}/device/vendor", ROOT_DIR, card))
.map_err(|_| AmdFanError::FailedReadVendor)
.and_then(|vendor| {
if vendor.trim() == "0x1002" {
Ok(card)
} else {
Err(AmdFanError::NotAmdCard)
}
})
.map(|n| Card(n))
}
}
@ -293,6 +284,32 @@ pub fn load_config() -> std::io::Result<Config> {
Ok(config)
}
#[cfg(test)]
mod parse_config {
use super::*;
use serde::Deserialize;
#[derive(Deserialize, PartialEq, Debug)]
pub struct Foo {
card: Card,
}
#[test]
fn parse_card0() {
assert_eq!("card0".parse::<Card>(), Ok(Card(0)))
}
#[test]
fn parse_card1() {
assert_eq!("card1".parse::<Card>(), Ok(Card(1)))
}
#[test]
fn toml_card0() {
assert_eq!(toml::from_str("card = 'card0'"), Ok(Foo { card: Card(0) }))
}
}
#[cfg(test)]
mod speed_for_temp {
use super::*;

View File

@ -12,6 +12,7 @@ static CONFIG_PATH: &str = "/etc/amdfand/config.toml";
static ROOT_DIR: &str = "/sys/class/drm";
static HW_MON_DIR: &str = "device/hwmon";
#[derive(Debug, PartialEq)]
pub enum AmdFanError {
InvalidPrefix,
InputTooShort,
@ -93,6 +94,19 @@ impl HwMon {
.unwrap_or_default()
}
pub fn is_amd(&self) -> bool {
std::fs::read_to_string(format!("{}/{}/device/vendor", ROOT_DIR, self.card))
.map_err(|_| AmdFanError::FailedReadVendor)
.map(|vendor| {
if vendor.trim() == "0x1002" {
true
} else {
false
}
})
.unwrap_or_default()
}
pub fn set_manual(&self) -> std::io::Result<()> {
self.write("pwm1_enable", 1)
}
@ -255,6 +269,7 @@ fn controllers(config: &Config, filter: bool) -> std::io::Result<Vec<CardControl
.is_some()
})
.map(|card| CardController::new(card).unwrap())
.filter(|controller| !filter || controller.hw_mon.is_amd())
.filter(|reader| {
!filter
|| reader