Filter out non-AMD cards.

This commit is contained in:
Aurorans Solis 2021-07-02 10:26:32 -07:00
parent b6a4c2e627
commit 04b828ab6c
2 changed files with 22 additions and 3 deletions

View File

@ -1,4 +1,4 @@
use crate::{AmdFanError, CONFIG_PATH}; use crate::{AmdFanError, CONFIG_PATH, ROOT_DIR};
use log::LevelFilter; use log::LevelFilter;
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::Formatter; use std::fmt::Formatter;
@ -24,10 +24,20 @@ impl FromStr for Card {
if value.len() < 5 { if value.len() < 5 {
return Err(AmdFanError::InputTooShort); return Err(AmdFanError::InputTooShort);
} }
value[4..] let card = value[4..]
.parse::<u32>() .parse::<u32>()
.map_err(|e| AmdFanError::InvalidSuffix(format!("{:?}", e))) .map_err(|e| AmdFanError::InvalidSuffix(format!("{:?}", e)))
.map(|n| Card(n)) .map(|n| Card(n))?;
match std::fs::read_to_string(format!("{}/{}/device/vendor", ROOT_DIR, card)) {
Ok(vendor) => {
if vendor.trim() == "0x1002" {
Ok(card)
} else {
Err(AmdFanError::NotAmdCard)
}
}
Err(_) => Err(AmdFanError::FailedReadVendor),
}
} }
} }
@ -61,6 +71,13 @@ impl<'de> Deserialize<'de> for Card {
"{:?} must have at least 5 characters", "{:?} must have at least 5 characters",
value value
))), ))),
Err(AmdFanError::NotAmdCard) => {
Err(E::custom(format!("{} is not an AMD GPU", value)))
}
Err(AmdFanError::FailedReadVendor) => Err(E::custom(format!(
"Failed to read vendor file for {}",
value
))),
} }
} }
} }

View File

@ -16,6 +16,8 @@ pub enum AmdFanError {
InvalidPrefix, InvalidPrefix,
InputTooShort, InputTooShort,
InvalidSuffix(String), InvalidSuffix(String),
NotAmdCard,
FailedReadVendor,
} }
#[derive(Debug)] #[derive(Debug)]