Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fanguard",
"version": "0.1.0-beta.6",
"version": "0.1.0-beta.7",
"private": true,
"description": "FanGuard — macOS fan control application built with Tauri v2 and Svelte 5",
"author": "naufaldi",
Expand All @@ -26,6 +26,7 @@
"type": "module",
"scripts": {
"dev": "vite",
"dev:native": "VITE_FANGUARD_NATIVE=1 vite",
"dev:prepare-helper": "\"$HOME/.cargo/bin/cargo\" build --manifest-path src-tauri/Cargo.toml --features helper-binary --bin fanguard-helper",
"build": "vite build",
"build:app-store": "VITE_FANGUARD_DISTRIBUTION=app-store vite build",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fanguard"
version = "0.1.0-beta.6"
version = "0.1.0-beta.7"
description = "FanGuard — macOS fan control utility: monitor temperatures and manage fan speeds via SMC"
authors = ["naufaldi"]
license = "MIT"
Expand Down
7 changes: 6 additions & 1 deletion src-tauri/src/bin/fanguard-helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::os::unix::net::{UnixListener, UnixStream};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

use fanguard_lib::smc_protocol::{HelperRequest, HelperResponse, SOCKET_PATH};
use fanguard_lib::smc_protocol::{
HelperRequest, HelperResponse, HELPER_PROTOCOL_VERSION, SOCKET_PATH,
};
use fanguard_lib::smc_writer::{SmcWriteApi, SmcWriter};

const HELPER_SOCKET_MODE: u32 = 0o660;
Expand Down Expand Up @@ -169,6 +171,9 @@ fn dispatch_request(request: HelperRequest, writer: &Mutex<SmcWriter>) -> Helper
};

match request {
HelperRequest::GetProtocolVersion => HelperResponse::ProtocolVersion {
version: HELPER_PROTOCOL_VERSION,
},
HelperRequest::Ping => HelperResponse::Pong,
HelperRequest::SetFanTargetRpm {
fan_index,
Expand Down
4 changes: 4 additions & 0 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ pub fn get_privilege_status(state: State<'_, AppState>) -> Result<PrivilegeStatu
}

let writer_guard = state.smc_writer.lock().map_err(|e| e.to_string())?;
debug_log!(
"[fanguard] privilege status: helper_write_access={}",
writer_guard.is_some()
);
Ok(PrivilegeStatus {
has_write_access: writer_guard.is_some(),
fan_control_available: true,
Expand Down
61 changes: 49 additions & 12 deletions src-tauri/src/smc_client.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::io::{BufRead, BufReader, Write};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::time::Duration;

use crate::smc_protocol::{HelperRequest, HelperResponse, SOCKET_PATH};
use crate::smc_protocol::{HelperRequest, HelperResponse, HELPER_PROTOCOL_VERSION, SOCKET_PATH};
use crate::smc_writer::{SmcWriteApi, SmcWriteError};

pub struct SmcSocketClient {
socket_path: PathBuf,
}

const HELPER_REQUEST_TIMEOUT: Duration = Duration::from_secs(15);

impl SmcSocketClient {
pub fn new() -> Result<Self, SmcWriteError> {
Self::with_path(SOCKET_PATH)
Expand All @@ -19,17 +22,24 @@ impl SmcSocketClient {
let stream =
UnixStream::connect(&socket_path).map_err(|_| SmcWriteError::HelperNotRunning)?;
stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.set_read_timeout(Some(HELPER_REQUEST_TIMEOUT))
.map_err(|e| SmcWriteError::HelperError(e.to_string()))?;
stream
.set_write_timeout(Some(std::time::Duration::from_secs(5)))
.set_write_timeout(Some(HELPER_REQUEST_TIMEOUT))
.map_err(|e| SmcWriteError::HelperError(e.to_string()))?;
let response = send_request_on(&stream, &HelperRequest::Ping)?;
let response = send_request_on(&stream, &HelperRequest::GetProtocolVersion)?;
match response {
HelperResponse::Pong => Ok(Self { socket_path }),
HelperResponse::ProtocolVersion {
version: HELPER_PROTOCOL_VERSION,
} => Ok(Self { socket_path }),
HelperResponse::ProtocolVersion { version } => {
Err(SmcWriteError::HelperError(format!(
"helper update required: protocol {version}, expected {HELPER_PROTOCOL_VERSION}"
)))
}
HelperResponse::Error { message } => Err(SmcWriteError::HelperError(message)),
_ => Err(SmcWriteError::HelperError(
"unexpected ping response".to_string(),
"helper update required: incompatible protocol response".to_string(),
)),
}
}
Expand All @@ -46,10 +56,10 @@ fn send_request_on(
request: &HelperRequest,
) -> Result<HelperResponse, SmcWriteError> {
stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.set_read_timeout(Some(HELPER_REQUEST_TIMEOUT))
.map_err(|e| SmcWriteError::HelperError(e.to_string()))?;
stream
.set_write_timeout(Some(std::time::Duration::from_secs(5)))
.set_write_timeout(Some(HELPER_REQUEST_TIMEOUT))
.map_err(|e| SmcWriteError::HelperError(e.to_string()))?;
let mut writer = stream
.try_clone()
Expand Down Expand Up @@ -166,10 +176,12 @@ mod tests {
}

#[test]
fn connect_and_ping() {
fn connect_and_checks_protocol_version() {
with_mock_helper(
|req| match req {
HelperRequest::Ping => HelperResponse::Pong,
HelperRequest::GetProtocolVersion => HelperResponse::ProtocolVersion {
version: crate::smc_protocol::HELPER_PROTOCOL_VERSION,
},
_ => HelperResponse::Error {
message: "unexpected".into(),
},
Expand All @@ -181,11 +193,34 @@ mod tests {
);
}

#[test]
fn connect_rejects_an_incompatible_helper_protocol() {
with_mock_helper(
|req| match req {
HelperRequest::GetProtocolVersion => HelperResponse::ProtocolVersion {
version: crate::smc_protocol::HELPER_PROTOCOL_VERSION - 1,
},
_ => HelperResponse::Error {
message: "unexpected".into(),
},
},
|path| {
let error = match SmcSocketClient::with_path(path) {
Ok(_) => panic!("incompatible helper must be rejected"),
Err(error) => error,
};
assert!(error.to_string().contains("helper update required"));
},
);
}

#[test]
fn set_fan_auto_sends_correct_request() {
with_mock_helper(
|req| match req {
HelperRequest::Ping => HelperResponse::Pong,
HelperRequest::GetProtocolVersion => HelperResponse::ProtocolVersion {
version: HELPER_PROTOCOL_VERSION,
},
HelperRequest::SetFanAuto { fan_index: 0 } => HelperResponse::Ok,
_ => HelperResponse::Error {
message: "unexpected".into(),
Expand All @@ -203,7 +238,9 @@ mod tests {
fn error_response_maps_to_smc_write_error() {
with_mock_helper(
|req| match req {
HelperRequest::Ping => HelperResponse::Pong,
HelperRequest::GetProtocolVersion => HelperResponse::ProtocolVersion {
version: HELPER_PROTOCOL_VERSION,
},
HelperRequest::SetFanAuto { .. } => HelperResponse::Error {
message: "Insufficient privileges".into(),
},
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/smc_protocol.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use serde::{Deserialize, Serialize};

pub const SOCKET_PATH: &str = "/var/run/fanguard.sock";
pub const HELPER_PROTOCOL_VERSION: u32 = 2;

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "cmd", rename_all = "snake_case")]
pub enum HelperRequest {
GetProtocolVersion,
SetFanTargetRpm {
fan_index: u8,
rpm: f32,
Expand All @@ -23,6 +25,7 @@ pub enum HelperRequest {
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "snake_case")]
pub enum HelperResponse {
ProtocolVersion { version: u32 },
Ok,
OkDiagnose { lines: Vec<String> },
Pong,
Expand Down
Loading
Loading