helpers, types: Switch to panic for errors

We're not doing explicit error handling, since the kernel is in charge
of safety if we crash. Just panic on anything.

Signed-off-by: Hector Martin <marcan@marcan.st>
This commit is contained in:
Hector Martin 2023-10-23 02:19:38 +09:00
parent 157ddc4e41
commit a476f7daf1
3 changed files with 12 additions and 37 deletions

View file

@ -5,23 +5,11 @@ use alsa;
use alsa::mixer::MilliBel; use alsa::mixer::MilliBel;
use configparser::ini::Ini; use configparser::ini::Ini;
/**
Failsafe: Limit speaker volume massively and bail.
TODO: enable TAS safe mode with IOCTL.
*/
pub fn fail() {
println!("A catastrophic error has occurred.");
std::process::exit(1);
}
pub fn open_card(card: &str) -> alsa::ctl::Ctl { pub fn open_card(card: &str) -> alsa::ctl::Ctl {
let ctldev: alsa::ctl::Ctl = match alsa::ctl::Ctl::new(card, false) { let ctldev: alsa::ctl::Ctl = match alsa::ctl::Ctl::new(card, false) {
Ok(ctldev) => ctldev, Ok(ctldev) => ctldev,
Err(e) => { Err(e) => {
println!("{}: Could not open sound card! Error: {}", card, e); panic!("{}: Could not open sound card! Error: {}", card, e);
fail();
std::process::exit(1);
} }
}; };
@ -104,9 +92,7 @@ pub fn new_elemvalue(t: alsa::ctl::ElemType) -> alsa::ctl::ElemValue {
let val = match alsa::ctl::ElemValue::new(t) { let val = match alsa::ctl::ElemValue::new(t) {
Ok(val) => val, Ok(val) => val,
Err(_e) => { Err(_e) => {
println!("Could not open a handle to an element!"); panic!("Could not open a handle to an element!");
fail();
std::process::exit(1);
} }
}; };
@ -121,12 +107,10 @@ pub fn read_ev(card: &alsa::ctl::Ctl, ev: &mut alsa::ctl::ElemValue, name: &str)
// alsa:Result<()> // alsa:Result<()>
Ok(val) => val, Ok(val) => val,
Err(e) => { Err(e) => {
println!( panic!(
"Could not read elem value {}. alsa-lib error: {:?}", "Could not read elem value {}. alsa-lib error: {:?}",
name, e name, e
); );
fail();
std::process::exit(1);
} }
}; };
} }
@ -139,12 +123,10 @@ pub fn write_ev(card: &alsa::ctl::Ctl, ev: &alsa::ctl::ElemValue, name: &str) {
// alsa:Result<()> // alsa:Result<()>
Ok(val) => val, Ok(val) => val,
Err(e) => { Err(e) => {
println!( panic!(
"Could not write elem value {}. alsa-lib error: {:?}", "Could not write elem value {}. alsa-lib error: {:?}",
name, e name, e
); );
fail();
std::process::exit(1);
} }
}; };
} }
@ -157,9 +139,7 @@ pub fn lock_el(card: &alsa::ctl::Ctl, el: &alsa::ctl::ElemId, name: &str) {
// alsa:Result<()> // alsa:Result<()>
Ok(val) => val, Ok(val) => val,
Err(e) => { Err(e) => {
println!("Could not lock elem {}. alsa-lib error: {:?}", name, e); panic!("Could not lock elem {}. alsa-lib error: {:?}", name, e);
fail();
std::process::exit(1);
} }
}; };
} }
@ -168,12 +148,10 @@ pub fn int_to_db(card: &alsa::ctl::Ctl, id: &alsa::ctl::ElemId, val: i32) -> Mil
let db = match card.convert_to_db(id, val.into()) { let db = match card.convert_to_db(id, val.into()) {
Ok(inner) => inner, Ok(inner) => inner,
Err(e) => { Err(e) => {
println!( panic!(
"Could not convert val {} to dB! alsa-lib error: {:?}", "Could not convert val {} to dB! alsa-lib error: {:?}",
val, e val, e
); );
fail();
std::process::exit(1);
} }
}; };
@ -185,12 +163,10 @@ pub fn db_to_int(card: &alsa::ctl::Ctl, id: &alsa::ctl::ElemId, val: f32) -> i32
let new_int = match card.convert_from_db(id, mb, alsa::Round::Floor) { let new_int = match card.convert_from_db(id, mb, alsa::Round::Floor) {
Ok(inner) => inner as i32, Ok(inner) => inner as i32,
Err(e) => { Err(e) => {
println!( panic!(
"Could not convert MilliBel {:?} to int! alsa-lib error: {:?}", "Could not convert MilliBel {:?} to int! alsa-lib error: {:?}",
val, e val, e
); );
fail();
std::process::exit(1);
} }
}; };

View file

@ -2,9 +2,10 @@
// (C) 2022 The Asahi Linux Contributors // (C) 2022 The Asahi Linux Contributors
/*! /*!
Handles speaker safety on Apple Silicon machines. This code is designed to Handles speaker safety on Apple Silicon machines. This code is designed to
fail safe. The speaker should not be enabled until this daemon has successfully fail safe. The kernel keeps the speakers capped at a low volume level until
initialised. If at any time we run into an unrecoverable error (we shouldn't), this daemon initializes. If at any time we run into an unrecoverable error
we gracefully bail and use an IOCTL to shut off the speakers. or a timeout, we panic and let the kernel put the speakers back into a safe
state.
*/ */
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::fs; use std::fs;

View file

@ -148,9 +148,7 @@ impl Mixer {
match self.level.val.set_integer(0, new_val) { match self.level.val.set_integer(0, new_val) {
Some(_) => {} Some(_) => {}
None => { None => {
println!("Could not set level for {}", self.drv); panic!("Could not set level for {}", self.drv);
helpers::fail();
std::process::exit(1);
} }
}; };