mirror of
https://gitlab.com/asus-linux/asusctl.git
synced 2026-02-06 00:15:04 +01:00
Handle config errors better
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
use asus_nb::aura_modes::AuraModes;
|
use asus_nb::aura_modes::AuraModes;
|
||||||
|
use log::{error, warn};
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
use std::fs::{File, OpenOptions};
|
use std::fs::{File, OpenOptions};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
@@ -9,7 +10,7 @@ pub static CONFIG_PATH: &str = "/etc/asusd/asusd.conf";
|
|||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub power_profile: u8,
|
pub power_profile: u8,
|
||||||
pub bat_charge_limit: u8,
|
pub bat_charge_limit: u8,
|
||||||
pub kbd_boot_brightness: u8,
|
pub kbd_led_brightness: u8,
|
||||||
pub kbd_backlight_mode: u8,
|
pub kbd_backlight_mode: u8,
|
||||||
pub kbd_backlight_modes: Vec<AuraModes>,
|
pub kbd_backlight_modes: Vec<AuraModes>,
|
||||||
pub power_profiles: FanModeProfile,
|
pub power_profiles: FanModeProfile,
|
||||||
@@ -24,42 +25,48 @@ impl Config {
|
|||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open(&CONFIG_PATH)
|
.open(&CONFIG_PATH)
|
||||||
.expect("config file error");
|
.unwrap(); // okay to cause panic here
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
if let Ok(l) = file.read_to_string(&mut buf) {
|
if let Ok(l) = file.read_to_string(&mut buf) {
|
||||||
if l == 0 {
|
if l == 0 {
|
||||||
// create a default config here
|
self = Config::create_default(&mut file, &supported_led_modes);
|
||||||
let mut c = Config::default();
|
|
||||||
c.bat_charge_limit = 100;
|
|
||||||
c.kbd_backlight_mode = 0;
|
|
||||||
c.kbd_boot_brightness = 1;
|
|
||||||
|
|
||||||
for n in supported_led_modes {
|
|
||||||
c.kbd_backlight_modes.push(AuraModes::from(*n))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Should be okay to unwrap this as is since it is a Default
|
|
||||||
let json = serde_json::to_string_pretty(&c).unwrap();
|
|
||||||
file.write_all(json.as_bytes())
|
|
||||||
.unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_PATH));
|
|
||||||
self = c;
|
|
||||||
} else {
|
} else {
|
||||||
self = serde_json::from_str(&buf)
|
self = serde_json::from_str(&buf).unwrap_or_else(|_| {
|
||||||
.unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_PATH));
|
warn!("Could not deserialise {}", CONFIG_PATH);
|
||||||
|
Config::create_default(&mut file, &supported_led_modes)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_default(file: &mut File, supported_led_modes: &[u8]) -> Self {
|
||||||
|
// create a default config here
|
||||||
|
let mut c = Config::default();
|
||||||
|
c.bat_charge_limit = 100;
|
||||||
|
c.kbd_backlight_mode = 0;
|
||||||
|
c.kbd_led_brightness = 1;
|
||||||
|
|
||||||
|
for n in supported_led_modes {
|
||||||
|
c.kbd_backlight_modes.push(AuraModes::from(*n))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should be okay to unwrap this as is since it is a Default
|
||||||
|
let json = serde_json::to_string_pretty(&c).unwrap();
|
||||||
|
file.write_all(json.as_bytes())
|
||||||
|
.unwrap_or_else(|_| panic!("Could not write {}", CONFIG_PATH));
|
||||||
|
c
|
||||||
|
}
|
||||||
|
|
||||||
pub fn read(&mut self) {
|
pub fn read(&mut self) {
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.read(true)
|
.read(true)
|
||||||
.open(&CONFIG_PATH)
|
.open(&CONFIG_PATH)
|
||||||
.expect("config file error");
|
.unwrap_or_else(|err| panic!("Error reading {}: {}", CONFIG_PATH, err));
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
if let Ok(l) = file.read_to_string(&mut buf) {
|
if let Ok(l) = file.read_to_string(&mut buf) {
|
||||||
if l == 0 {
|
if l == 0 {
|
||||||
panic!("Missing {}", CONFIG_PATH);
|
warn!("File is empty {}", CONFIG_PATH);
|
||||||
} else {
|
} else {
|
||||||
let x: Config = serde_json::from_str(&buf)
|
let x: Config = serde_json::from_str(&buf)
|
||||||
.unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_PATH));
|
.unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_PATH));
|
||||||
@@ -72,7 +79,7 @@ impl Config {
|
|||||||
let mut file = File::create(CONFIG_PATH).expect("Couldn't overwrite config");
|
let mut file = File::create(CONFIG_PATH).expect("Couldn't overwrite config");
|
||||||
let json = serde_json::to_string_pretty(self).expect("Parse config to JSON failed");
|
let json = serde_json::to_string_pretty(self).expect("Parse config to JSON failed");
|
||||||
file.write_all(json.as_bytes())
|
file.write_all(json.as_bytes())
|
||||||
.expect("Saving config failed");
|
.unwrap_or_else(|err| error!("Could not write config: {}", err));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_mode_data(&mut self, mode: AuraModes) {
|
pub fn set_mode_data(&mut self, mode: AuraModes) {
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ impl CtrlCharge {
|
|||||||
file.write_all(limit.to_string().as_bytes())
|
file.write_all(limit.to_string().as_bytes())
|
||||||
.unwrap_or_else(|err| error!("Could not write to {}, {:?}", BAT_CHARGE_PATH, err));
|
.unwrap_or_else(|err| error!("Could not write to {}, {:?}", BAT_CHARGE_PATH, err));
|
||||||
info!("Battery charge limit: {}", limit);
|
info!("Battery charge limit: {}", limit);
|
||||||
|
|
||||||
config.read();
|
config.read();
|
||||||
config.bat_charge_limit = limit;
|
config.bat_charge_limit = limit;
|
||||||
config.write();
|
config.write();
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ impl crate::Controller for CtrlKbdBacklight {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Reload brightness
|
// Reload brightness
|
||||||
let bright = config.kbd_boot_brightness;
|
let bright = config.kbd_led_brightness;
|
||||||
let bytes = aura_brightness_bytes(bright);
|
let bytes = aura_brightness_bytes(bright);
|
||||||
self.write_bytes(&bytes).await?;
|
self.write_bytes(&bytes).await?;
|
||||||
info!("Reloaded last used brightness");
|
info!("Reloaded last used brightness");
|
||||||
@@ -165,7 +165,7 @@ impl CtrlKbdBacklight {
|
|||||||
if let Some(num) = char::from(buf[0]).to_digit(10) {
|
if let Some(num) = char::from(buf[0]).to_digit(10) {
|
||||||
if config.power_profile != num as u8 {
|
if config.power_profile != num as u8 {
|
||||||
config.read();
|
config.read();
|
||||||
config.kbd_boot_brightness = num as u8;
|
config.kbd_led_brightness = num as u8;
|
||||||
config.write();
|
config.write();
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -225,7 +225,7 @@ impl CtrlKbdBacklight {
|
|||||||
let bytes: [u8; LED_MSG_LEN] = (&mode).into();
|
let bytes: [u8; LED_MSG_LEN] = (&mode).into();
|
||||||
self.write_bytes(&bytes).await?;
|
self.write_bytes(&bytes).await?;
|
||||||
config.read();
|
config.read();
|
||||||
config.kbd_boot_brightness = n;
|
config.kbd_led_brightness = n;
|
||||||
config.write();
|
config.write();
|
||||||
info!("LED brightness set to {:#?}", n);
|
info!("LED brightness set to {:#?}", n);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user