Skip to content

Commit

Permalink
chore: change types from u8 to c_ulong
Browse files Browse the repository at this point in the history
  • Loading branch information
barrenechea authored and svenrademakers committed Apr 8, 2024
1 parent 0a490a1 commit 8daa42c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/api/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use async_compression::Level;
use humansize::{format_size, DECIMAL};
use serde_json::json;
use std::collections::HashMap;
use std::ffi::c_ulong;
use std::io;
use std::ops::Deref;
use std::path::PathBuf;
Expand Down Expand Up @@ -521,7 +522,7 @@ async fn set_cooling_info(query: Query) -> LegacyResult<()> {
.ok_or(LegacyResponse::bad_request("Device not found"))?;

// check if the speed is a valid number within the range of the device
let speed = match u8::from_str(speed_str) {
let speed = match c_ulong::from_str(speed_str) {
Ok(s) if s <= device.max_speed => s,
_ => return Err(LegacyResponse::bad_request(format!("Parameter `speed` must be a number between 0-{}", device.max_speed))),
};
Expand Down
12 changes: 6 additions & 6 deletions src/app/cooling_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

use serde::Serialize;

#[derive(Debug, Serialize)]
pub struct CoolingDevice {
pub device: String,
pub speed: u8,
pub max_speed: u8,
pub speed: c_ulong,
pub max_speed: c_ulong,
}

pub async fn get_cooling_state() -> Vec<CoolingDevice> {
Expand All @@ -39,15 +39,15 @@ pub async fn get_cooling_state() -> Vec<CoolingDevice> {
let max_state_path = device_path.join("max_state");

let cur_state = match tokio::fs::read_to_string(cur_state_path).await {
Ok(state) => state.trim().parse::<u8>().unwrap_or(0),
Ok(state) => state.trim().parse::<c_ulong>().unwrap_or(0),
Err(err) => {
eprintln!("Error reading cur_state file: {}", err);
0
}
};

let max_state = match tokio::fs::read_to_string(max_state_path).await {
Ok(max_speed) => max_speed.trim().parse::<u8>().unwrap_or(0),
Ok(max_speed) => max_speed.trim().parse::<c_ulong>().unwrap_or(0),
Err(err) => {
eprintln!("Error reading max_state file: {}", err);
0
Expand All @@ -65,7 +65,7 @@ pub async fn get_cooling_state() -> Vec<CoolingDevice> {
result
}

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

tokio::fs::write(device_path, speed.to_string()).await?;
Expand Down

0 comments on commit 8daa42c

Please sign in to comment.