Skip to content

Commit

Permalink
cooling_device: added fuzzy matching system_fan
Browse files Browse the repository at this point in the history
Added code that will update the name of the cooling_device to 'system_fan'
if there is one device loaded using the pwm-fan driver
  • Loading branch information
svenrademakers committed Jul 22, 2024
1 parent 5ba12b5 commit e8c872e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/app/bmc_application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl BmcApplication {
}

pub async fn set_node1_usb_route(&self, alternative_port: bool) -> anyhow::Result<()> {
info!("changed node1 usb route. port= {}", alternative_port);
self.pin_controller.set_node1_usb_route(alternative_port)?;
self.app_db.set(NODE1_USB_MODE, alternative_port).await;
Ok(())
Expand Down
39 changes: 37 additions & 2 deletions src/app/cooling_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{ffi::c_ulong, path::Path};
use std::{ffi::c_ulong, fs, io, path::Path};

use serde::Serialize;
use tracing::{instrument, warn};

#[derive(Debug, Serialize)]
pub struct CoolingDevice {
Expand All @@ -28,12 +29,15 @@ pub async fn get_cooling_state() -> Vec<CoolingDevice> {

if let Ok(mut dir) = tokio::fs::read_dir("/sys/class/thermal").await {
while let Some(device) = dir.next_entry().await.unwrap_or(None) {
let device_name = device.file_name().to_string_lossy().into_owned();
let mut device_name = device.file_name().to_string_lossy().into_owned();
if !device_name.starts_with("cooling_device") {
continue;
}

let device_path = device.path();
if let Some(name) = is_system_fan(&device_path).map(|n| n.replace("_", " ")) {

Check failure on line 38 in src/app/cooling_device.rs

View workflow job for this annotation

GitHub Actions / clippy

single-character string constant used as pattern

error: single-character string constant used as pattern --> src/app/cooling_device.rs:38:79 | 38 | if let Some(name) = is_system_fan(&device_path).map(|n| n.replace("_", " ")) { | ^^^ help: consider using a `char`: `'_'` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern = note: `-D clippy::single-char-pattern` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::single_char_pattern)]`
device_name = name;
}

let cur_state_path = device_path.join("cur_state");
let max_state_path = device_path.join("max_state");
Expand Down Expand Up @@ -65,6 +69,37 @@ pub async fn get_cooling_state() -> Vec<CoolingDevice> {
result
}

#[instrument(ret)]
fn is_system_fan(dev_path: &Path) -> Option<String> {
let typ = std::fs::read_to_string(dev_path.join("type")).ok()?;
if typ.trim() == "pwm-fan" {
let pwm_fan_nodes = get_pwm_fan_nodes().ok()?;
if pwm_fan_nodes.len() > 1 {
warn!("more as one pwm-fan device detected, selecting first for system_fan");
}
return pwm_fan_nodes.first().cloned();
}
None
}

#[instrument(ret)]
fn get_pwm_fan_nodes() -> io::Result<Vec<String>> {
let mut nodes = Vec::new();

for entry in fs::read_dir("/sys/bus/platform/drivers/pwm-fan")? {
let entry = entry?;
let path = entry.path();

if path.is_dir() {
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
nodes.push(name.to_string());
}
}
}

Ok(nodes)
}

pub async fn set_cooling_state(device: &str, speed: &c_ulong) -> anyhow::Result<()> {
let device_path = Path::new("/sys/class/thermal")
.join(device)
Expand Down

0 comments on commit e8c872e

Please sign in to comment.