Skip to content

Commit 1da4c3f

Browse files
committed
detect when the wifi device is soft/hard blocked
1 parent f5fe15c commit 1da4c3f

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ pub mod adapter;
2929
pub mod access_point;
3030

3131
pub mod cli;
32+
33+
pub mod rfkill;

src/main.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use impala::app::{App, AppResult};
2-
use impala::cli;
32
use impala::config::Config;
43
use impala::event::{Event, EventHandler};
54
use impala::handler::handle_key_events;
65
use impala::help::Help;
76
use impala::tui::Tui;
7+
use impala::{cli, rfkill};
88
use iwdrs::modes::Mode;
99
use ratatui::backend::CrosstermBackend;
1010
use ratatui::Terminal;
@@ -15,6 +15,8 @@ use std::sync::Arc;
1515
async fn main() -> AppResult<()> {
1616
let args = cli::cli().get_matches();
1717

18+
rfkill::check()?;
19+
1820
let config = Arc::new(Config::new());
1921

2022
let help = Help::new(config.clone());

src/rfkill.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::fs;
2+
3+
use crate::app::AppResult;
4+
5+
pub fn check() -> AppResult<()> {
6+
let entries = fs::read_dir("/sys/class/rfkill/")?;
7+
8+
for entry in entries {
9+
let entry = entry?;
10+
let entry_path = entry.path();
11+
12+
if let Some(_file_name) = entry_path.file_name() {
13+
let name = fs::read_to_string(entry_path.join("type"))?;
14+
15+
if name.trim() == "wlan" {
16+
let state_path = entry_path.join("state");
17+
let state = fs::read_to_string(state_path)?.trim().parse::<u8>()?;
18+
19+
// https://www.kernel.org/doc/Documentation/ABI/stable/sysfs-class-rfkill
20+
match state {
21+
0 => {
22+
eprintln!(
23+
r#"
24+
The wifi device is soft blocked
25+
Run the following command to unblock it
26+
$ sudo rfkill unblock wlan
27+
"#
28+
);
29+
std::process::exit(1);
30+
}
31+
2 => {
32+
eprintln!("The wifi device is hard blocked");
33+
std::process::exit(1);
34+
}
35+
_ => {}
36+
}
37+
break;
38+
}
39+
}
40+
}
41+
Ok(())
42+
}

0 commit comments

Comments
 (0)