Skip to content

Commit

Permalink
fix(Config): add matching on CPU vendor id
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowApex committed May 28, 2024
1 parent 63635ce commit 763c4d1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ log = { version = "0.4.20", features = [
] }
nix = { version = "0.28.0", features = ["fs"] }
packed_struct = "0.10.1"
procfs = "0.16.0"
serde = { version = "1.0.192", features = ["derive"] }
serde_yaml = "0.9.27"
thiserror = "1.0.56"
Expand Down
17 changes: 14 additions & 3 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io;

use ::procfs::CpuInfo;
use glob_match::glob_match;
use hidapi::DeviceInfo;
use serde::Deserialize;
Expand Down Expand Up @@ -486,12 +487,12 @@ impl CompositeDeviceConfig {

/// Returns true if the configuration has a valid set of matches. This will
/// return true if ANY match config matches. If this list is empty, it will return true.
pub fn has_valid_matches(&self, data: DMIData) -> bool {
self.get_valid_matches(data).is_some()
pub fn has_valid_matches(&self, data: &DMIData, cpu_info: &CpuInfo) -> bool {
self.get_valid_matches(data, cpu_info).is_some()
}

/// Returns matches that matched system data.
pub fn get_valid_matches(&self, data: DMIData) -> Option<Vec<Match>> {
pub fn get_valid_matches(&self, data: &DMIData, cpu_info: &CpuInfo) -> Option<Vec<Match>> {
let mut matches: Vec<Match> = Vec::new();

// If there are no match definitions, consider it a match
Expand All @@ -505,6 +506,16 @@ impl CompositeDeviceConfig {
let mut has_matches = false;

if let Some(dmi_config) = match_config.dmi_data {
if let Some(cpu_vendor) = dmi_config.cpu_vendor {
if !glob_match(
cpu_vendor.as_str(),
cpu_info.vendor_id(0).unwrap_or_default(),
) {
continue;
}
has_matches = true;
}

if let Some(bios_release) = dmi_config.bios_release {
if !glob_match(bios_release.as_str(), data.bios_release.as_str()) {
continue;
Expand Down
7 changes: 7 additions & 0 deletions src/dmi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fs;

use procfs::{CpuInfo, Current};

use self::data::DMIData;

pub mod data;
Expand Down Expand Up @@ -45,6 +47,11 @@ pub fn get_dmi_data() -> DMIData {
}
}

/// Returns the CPU info from the system
pub fn get_cpu_info() -> Result<CpuInfo, procfs::ProcError> {
CpuInfo::current()
}

/// Read the given DMI property
fn get_dmi_property(name: &str) -> String {
let path = format!("/sys/devices/virtual/dmi/id/{name}");
Expand Down
18 changes: 17 additions & 1 deletion src/input/manager.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use core::panic;
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::time::Duration;

use ::procfs::CpuInfo;
use thiserror::Error;
use tokio::sync::mpsc;
use zbus::zvariant::ObjectPath;
Expand All @@ -18,6 +20,7 @@ use crate::dbus::interface::manager::ManagerInterface;
use crate::dbus::interface::source::evdev::SourceEventDeviceInterface;
use crate::dbus::interface::source::hidraw::SourceHIDRawInterface;
use crate::dmi::data::DMIData;
use crate::dmi::get_cpu_info;
use crate::dmi::get_dmi_data;
use crate::iio;
use crate::input::composite_device;
Expand Down Expand Up @@ -123,6 +126,8 @@ pub struct Manager {
dbus: Connection,
/// System DMI data
dmi_data: DMIData,
/// System CPU info
cpu_info: CpuInfo,
/// The transmit side of the [rx] channel used to send [Command] messages.
/// This can be cloned to allow child objects to communicate up to the
/// manager.
Expand Down Expand Up @@ -166,9 +171,20 @@ impl Manager {
let dmi_data = get_dmi_data();
log::debug!("Got DMI data: {:?}", dmi_data);

log::debug!("Loading CPU info");
let cpu_info = match get_cpu_info() {
Ok(info) => info,
Err(e) => {
log::error!("Failed to get CPU info: {e:?}");
panic!("Unable to determine CPU info!");
}
};
log::debug!("Got CPU info: {cpu_info:?}");

Manager {
dbus: conn,
dmi_data,
cpu_info,
rx,
tx,
composite_devices: HashMap::new(),
Expand Down Expand Up @@ -923,7 +939,7 @@ impl Manager {
log::debug!("Checking config {:?} for device", config.name);

// Check to see if this configuration matches the system
if !config.has_valid_matches(self.dmi_data.clone()) {
if !config.has_valid_matches(&self.dmi_data, &self.cpu_info) {
log::debug!("Configuration does not match system");
continue;
}
Expand Down

0 comments on commit 763c4d1

Please sign in to comment.