PowerTools/backend/src/main.rs

358 lines
12 KiB
Rust
Raw Normal View History

mod api;
2022-07-30 21:33:31 +01:00
mod persist;
mod settings;
mod state;
2022-07-30 21:33:31 +01:00
2022-08-26 22:00:43 +01:00
mod consts;
use consts::*;
2023-03-26 15:49:17 +01:00
mod power_worker;
mod resume_worker;
//mod save_worker;
mod api_worker;
mod utility;
use settings::OnSet;
use simplelog::{LevelFilter, WriteLogger};
2022-07-30 21:33:31 +01:00
use usdpl_back::core::serdes::Primitive;
use usdpl_back::Instance;
2022-07-30 21:33:31 +01:00
fn main() -> Result<(), ()> {
2022-09-05 22:02:02 +01:00
#[cfg(debug_assertions)]
let log_filepath = usdpl_back::api::dirs::home()
.unwrap_or_else(|| "/tmp/".into())
2023-03-28 02:11:22 +01:00
.join(PACKAGE_NAME.to_owned() + ".log");
2022-09-05 22:02:02 +01:00
#[cfg(not(debug_assertions))]
let log_filepath = std::path::Path::new("/tmp").join(format!("{}.log", PACKAGE_NAME));
println!("Logging to: {:?}", log_filepath);
#[cfg(debug_assertions)]
let old_log_filepath = usdpl_back::api::dirs::home()
.unwrap_or_else(|| "/tmp/".into())
2023-03-28 02:11:22 +01:00
.join(PACKAGE_NAME.to_owned() + ".log.old");
#[cfg(debug_assertions)]
{
if std::path::Path::new(&log_filepath).exists() {
2023-03-28 02:11:22 +01:00
std::fs::copy(&log_filepath, &old_log_filepath)
.expect("Unable to increment logs. Do you have write permissions?");
}
}
2022-07-30 21:33:31 +01:00
WriteLogger::init(
#[cfg(debug_assertions)]
{
LevelFilter::Debug
},
#[cfg(not(debug_assertions))]
{
LevelFilter::Info
},
2022-07-30 21:33:31 +01:00
Default::default(),
std::fs::File::create(&log_filepath).expect("Failed to create log file"),
//std::fs::File::create("/home/deck/powertools-rs.log").unwrap(),
)
.unwrap();
log::debug!("Logging to: {:?}.", log_filepath);
2022-07-30 21:33:31 +01:00
log::info!("Starting back-end ({} v{})", PACKAGE_NAME, PACKAGE_VERSION);
println!("Starting back-end ({} v{})", PACKAGE_NAME, PACKAGE_VERSION);
2023-03-28 02:11:22 +01:00
log::info!(
"Current dir `{}`",
std::env::current_dir().unwrap().display()
);
println!(
"Current dir `{}`",
std::env::current_dir().unwrap().display()
);
log::info!("home dir: {:?}", usdpl_back::api::dirs::home());
2023-03-28 02:11:03 +01:00
log::info!("Last version file: {}", utility::read_version_file());
if let Err(e) = utility::save_version_file() {
log::error!("Error storing version: {}", e);
} else {
log::info!("Updated version file succesfully");
}
let _limits_handle = crate::settings::limits_worker_spawn();
2022-11-19 20:21:09 +00:00
2023-03-28 02:11:22 +01:00
let mut loaded_settings =
persist::FileJson::open(utility::settings_dir().join(DEFAULT_SETTINGS_FILE))
.map(|mut file| file.variants.remove(&0)
2024-01-27 20:05:41 +00:00
.map(|settings| settings::Settings::from_json(DEFAULT_SETTINGS_NAME.into(), settings, DEFAULT_SETTINGS_FILE.into(), 0))
.unwrap_or_else(|| settings::Settings::system_default(
DEFAULT_SETTINGS_FILE.into(),
2024-01-27 20:05:41 +00:00
0,
DEFAULT_SETTINGS_NAME.into(),
0,
DEFAULT_SETTINGS_VARIANT_NAME.into())))
2023-03-28 02:11:22 +01:00
.unwrap_or_else(|_| {
settings::Settings::system_default(
DEFAULT_SETTINGS_FILE.into(),
2024-01-27 20:05:41 +00:00
0,
2023-03-28 02:11:22 +01:00
DEFAULT_SETTINGS_NAME.into(),
0,
DEFAULT_SETTINGS_VARIANT_NAME.into(),
2023-03-28 02:11:22 +01:00
)
});
log::info!(
"Detected device automatically {:?}, using driver: {:?} (This can be overriden)",
crate::settings::auto_detect_provider(), loaded_settings.cpus.provider()
);
log::debug!("Settings: {:?}", loaded_settings);
let (api_handler, api_sender) = crate::api::handler::ApiMessageHandler::new();
//let (_save_handle, save_sender) = save_worker::spawn(loaded_settings.clone());
let _resume_handle = resume_worker::spawn(api_sender.clone());
2023-03-26 15:49:17 +01:00
let _power_handle = power_worker::spawn(api_sender.clone());
2023-07-04 03:00:18 +01:00
let (message_getter, message_dismisser) = api::message::MessageHandler::new().to_callables();
let instance = Instance::new(PORT)
.register("V_INFO", |_: Vec<Primitive>| {
#[cfg(debug_assertions)]
2023-03-28 02:11:22 +01:00
{
vec![format!("v{}-dbg", PACKAGE_VERSION).into()]
}
#[cfg(not(debug_assertions))]
2023-03-28 02:11:22 +01:00
{
vec![format!("v{}-rls", PACKAGE_VERSION).into()]
}
})
2023-03-28 02:11:22 +01:00
.register("NAME", |_: Vec<Primitive>| vec![PACKAGE_NAME.into()])
.register("LOG", api::general::log_it())
// battery API functions
2023-03-28 02:11:22 +01:00
.register_async(
"BATTERY_current_now",
api::battery::current_now(api_sender.clone()),
)
.register_async(
"BATTERY_charge_now",
api::battery::charge_now(api_sender.clone()),
)
.register_async(
"BATTERY_charge_full",
api::battery::charge_full(api_sender.clone()),
)
.register_async(
"BATTERY_charge_design",
api::battery::charge_design(api_sender.clone()),
)
.register_async(
"BATTERY_charge_power",
api::battery::charge_power(api_sender.clone()),
)
.register(
"BATTERY_set_charge_rate",
api::battery::set_charge_rate(api_sender.clone()),
)
.register(
"BATTERY_get_charge_rate",
api::battery::get_charge_rate(api_sender.clone()),
)
.register(
"BATTERY_unset_charge_rate",
api::battery::unset_charge_rate(api_sender.clone()),
)
.register(
"BATTERY_set_charge_mode",
api::battery::set_charge_mode(api_sender.clone()),
)
.register(
"BATTERY_get_charge_mode",
api::battery::get_charge_mode(api_sender.clone()),
)
.register(
"BATTERY_unset_charge_mode",
api::battery::unset_charge_mode(api_sender.clone()),
)
2023-03-26 15:49:17 +01:00
.register(
"BATTERY_set_charge_limit",
api::battery::set_charge_limit(api_sender.clone()),
)
.register(
"BATTERY_unset_charge_limit",
api::battery::unset_charge_limit(api_sender.clone()),
)
.register_async(
"BATTERY_get_charge_limit",
api::battery::get_charge_limit(api_sender.clone()),
)
// cpu API functions
.register("CPU_count", api::cpu::max_cpus)
.register(
"CPU_set_online",
2023-03-28 02:11:22 +01:00
api::cpu::set_cpu_online(api_sender.clone()),
)
.register(
"CPU_set_onlines",
2023-03-28 02:11:22 +01:00
api::cpu::set_cpus_online(api_sender.clone()),
)
.register_async(
"CPU_get_onlines",
2023-03-28 02:11:22 +01:00
api::cpu::get_cpus_online(api_sender.clone()),
)
2023-03-28 02:11:22 +01:00
.register_async("CPU_set_smt", api::cpu::set_smt(api_sender.clone()))
.register_async("CPU_get_smt", api::cpu::get_smt(api_sender.clone()))
.register(
"CPU_set_clock_limits",
2023-03-28 02:11:22 +01:00
api::cpu::set_clock_limits(api_sender.clone()),
)
.register(
"CPU_get_clock_limits",
2023-03-28 02:11:22 +01:00
api::cpu::get_clock_limits(api_sender.clone()),
)
.register(
"CPU_unset_clock_limits",
2023-03-28 02:11:22 +01:00
api::cpu::unset_clock_limits(api_sender.clone()),
)
.register(
"CPU_set_governor",
2023-03-28 02:11:22 +01:00
api::cpu::set_cpu_governor(api_sender.clone()),
)
.register(
"CPU_set_governors",
2023-03-28 02:11:22 +01:00
api::cpu::set_cpus_governors(api_sender.clone()),
)
.register(
"CPU_get_governors",
2023-03-28 02:11:22 +01:00
api::cpu::get_cpu_governors(api_sender.clone()),
)
// gpu API functions
2023-03-28 02:11:22 +01:00
.register("GPU_set_ppt", api::gpu::set_ppt(api_sender.clone()))
.register_async("GPU_get_ppt", api::gpu::get_ppt(api_sender.clone()))
.register("GPU_unset_ppt", api::gpu::unset_ppt(api_sender.clone()))
.register(
"GPU_set_clock_limits",
2023-03-28 02:11:22 +01:00
api::gpu::set_clock_limits(api_sender.clone()),
)
.register_async(
"GPU_get_clock_limits",
2023-03-28 02:11:22 +01:00
api::gpu::get_clock_limits(api_sender.clone()),
)
.register(
"GPU_unset_clock_limits",
2023-03-28 02:11:22 +01:00
api::gpu::unset_clock_limits(api_sender.clone()),
)
.register(
"GPU_set_slow_memory",
2023-03-28 02:11:22 +01:00
api::gpu::set_slow_memory(api_sender.clone()),
)
.register_async(
"GPU_get_slow_memory",
2023-03-28 02:11:22 +01:00
api::gpu::get_slow_memory(api_sender.clone()),
)
.register(
"GPU_unset_slow_memory",
api::gpu::unset_slow_memory(api_sender.clone()),
)
2022-08-26 22:00:43 +01:00
// general API functions
.register(
"GENERAL_set_persistent",
2023-03-28 02:11:22 +01:00
api::general::set_persistent(api_sender.clone()),
2022-08-26 22:00:43 +01:00
)
.register(
"GENERAL_get_persistent",
2023-03-28 02:11:22 +01:00
api::general::get_persistent(api_sender.clone()),
2022-08-26 22:00:43 +01:00
)
.register(
"GENERAL_load_settings",
2023-03-28 02:11:22 +01:00
api::general::load_settings(api_sender.clone()),
)
.register(
"GENERAL_load_variant",
api::general::load_variant(api_sender.clone()),
)
.register(
"GENERAL_load_default_settings",
2023-03-28 02:11:22 +01:00
api::general::load_default_settings(api_sender.clone()),
2022-08-26 22:00:43 +01:00
)
.register(
"GENERAL_load_system_settings",
2023-03-28 02:11:22 +01:00
api::general::load_system_settings(api_sender.clone()),
)
.register_async(
2022-08-26 22:00:43 +01:00
"GENERAL_get_name",
2023-03-28 02:11:22 +01:00
api::general::get_name(api_sender.clone()),
)
.register_async(
"GENERAL_get_path",
2023-03-28 02:11:22 +01:00
api::general::get_path(api_sender.clone()),
)
.register_async(
"GENERAL_wait_for_unlocks",
2023-03-28 02:11:22 +01:00
api::general::lock_unlock_all(api_sender.clone()),
2022-11-19 20:21:09 +00:00
)
.register_blocking(
2022-11-19 20:21:09 +00:00
"GENERAL_get_limits",
2023-03-28 02:11:22 +01:00
api::general::get_limits(api_sender.clone()),
)
.register_async(
"GENERAL_get_provider",
2023-03-28 02:11:22 +01:00
api::general::get_provider(api_sender.clone()),
)
.register("GENERAL_idk", api::general::gunter)
.register(
"GENERAL_apply_now",
2023-03-28 02:11:22 +01:00
api::general::force_apply(api_sender.clone()),
2023-03-26 15:49:17 +01:00
)
.register(
"GENERAL_on_pluggedin",
2023-03-28 02:11:22 +01:00
api::battery::on_plugged(api_sender.clone()),
2023-03-26 15:49:17 +01:00
)
.register(
"GENERAL_on_unplugged",
2023-03-28 02:11:22 +01:00
api::battery::on_unplugged(api_sender.clone()),
2023-07-04 03:00:18 +01:00
)
.register_async(
"GENERAL_get_periodicals",
api::general::get_periodicals(api_sender.clone())
)
.register_async(
"GENERAL_get_all_variants",
api::general::get_all_variants(api_sender.clone())
)
.register_async(
"GENERAL_get_current_variant",
api::general::get_current_variant(api_sender.clone())
)
2023-07-04 03:00:18 +01:00
.register_async("MESSAGE_get", message_getter)
.register_async("MESSAGE_dismiss", message_dismisser)
.register_async(
"WEB_search_by_app",
api::web::search_by_app_id()
)
.register_async(
"WEB_download_new",
api::web::download_new_config(api_sender.clone())
2024-01-27 20:05:41 +00:00
)
.register_async(
"WEB_upload_new",
api::web::upload_current_variant(api_sender.clone())
);
utility::ioperm_power_ec();
#[cfg(debug_assertions)]
std::thread::spawn(|| {
utility::ioperm_power_ec();
settings::steam_deck::flash_led();
});
if let Err(e) = loaded_settings.on_set() {
2023-03-28 02:11:22 +01:00
e.iter()
.for_each(|e| log::error!("Startup Settings.on_set() error: {}", e));
} else {
log::info!("Startup Settings.on_set() success");
}
if let Err(e) = utility::chown_settings_dir() {
log::warn!("Failed to change config dir permissions: {}", e);
}
api_worker::spawn(loaded_settings, api_handler);
2023-03-28 02:11:22 +01:00
instance.run_blocking()
2022-07-30 21:33:31 +01:00
}