2022-08-10 01:56:22 +01:00
|
|
|
mod api;
|
2022-07-30 21:33:31 +01:00
|
|
|
mod persist;
|
|
|
|
mod settings;
|
2022-08-10 01:56:22 +01:00
|
|
|
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;
|
2022-08-10 01:56:22 +01:00
|
|
|
mod resume_worker;
|
2022-11-10 03:09:05 +00:00
|
|
|
//mod save_worker;
|
|
|
|
mod api_worker;
|
2022-08-10 01:56:22 +01:00
|
|
|
mod utility;
|
|
|
|
|
2022-09-01 01:18:15 +01:00
|
|
|
use settings::OnSet;
|
|
|
|
|
2022-08-10 01:56:22 +01:00
|
|
|
use simplelog::{LevelFilter, WriteLogger};
|
2022-07-30 21:33:31 +01:00
|
|
|
|
|
|
|
use usdpl_back::core::serdes::Primitive;
|
2022-08-10 01:56:22 +01:00
|
|
|
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)]
|
2022-11-29 00:04:03 +00:00
|
|
|
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))]
|
2022-12-05 23:51:49 +00:00
|
|
|
let log_filepath = std::path::Path::new("/tmp").join(format!("{}.log", PACKAGE_NAME));
|
2023-12-11 04:15:31 +00:00
|
|
|
println!("Logging to: {:?}", log_filepath);
|
2022-11-29 00:04:03 +00:00
|
|
|
#[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");
|
2022-09-05 19:24:01 +01:00
|
|
|
#[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-09-05 19:24:01 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-30 21:33:31 +01:00
|
|
|
WriteLogger::init(
|
2022-08-10 01:56:22 +01:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
LevelFilter::Debug
|
|
|
|
},
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
{
|
|
|
|
LevelFilter::Info
|
|
|
|
},
|
2022-07-30 21:33:31 +01:00
|
|
|
Default::default(),
|
2023-12-11 04:15:31 +00:00
|
|
|
std::fs::File::create(&log_filepath).expect("Failed to create log file"),
|
2023-02-22 02:28:59 +00:00
|
|
|
//std::fs::File::create("/home/deck/powertools-rs.log").unwrap(),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-11-29 00:04:03 +00:00
|
|
|
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()
|
|
|
|
);
|
2022-08-10 01:56:22 +01:00
|
|
|
|
2023-02-22 02:28:59 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2022-12-03 02:12:13 +00:00
|
|
|
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 =
|
2023-11-18 20:17:56 +00:00
|
|
|
persist::FileJson::open(utility::settings_dir().join(DEFAULT_SETTINGS_FILE))
|
2023-11-18 22:16:58 +00:00
|
|
|
.map(|mut file| file.variants.remove(&0)
|
2023-11-18 20:17:56 +00:00
|
|
|
.map(|settings| settings::Settings::from_json(DEFAULT_SETTINGS_NAME.into(), settings, DEFAULT_SETTINGS_FILE.into()))
|
|
|
|
.unwrap_or_else(|| settings::Settings::system_default(
|
|
|
|
DEFAULT_SETTINGS_FILE.into(),
|
|
|
|
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(),
|
|
|
|
DEFAULT_SETTINGS_NAME.into(),
|
2023-11-18 20:17:56 +00:00
|
|
|
0,
|
|
|
|
DEFAULT_SETTINGS_VARIANT_NAME.into(),
|
2023-03-28 02:11:22 +01:00
|
|
|
)
|
|
|
|
});
|
2022-08-10 01:56:22 +01:00
|
|
|
|
2023-08-06 19:49:19 +01:00
|
|
|
log::info!(
|
|
|
|
"Detected device automatically {:?}, using driver: {:?} (This can be overriden)",
|
|
|
|
crate::settings::auto_detect_provider(), loaded_settings.cpus.provider()
|
|
|
|
);
|
|
|
|
|
2022-09-01 01:18:15 +01:00
|
|
|
log::debug!("Settings: {:?}", loaded_settings);
|
|
|
|
|
2022-11-10 03:09:05 +00:00
|
|
|
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());
|
2022-08-10 01:56:22 +01:00
|
|
|
|
2023-07-04 03:00:18 +01:00
|
|
|
let (message_getter, message_dismisser) = api::message::MessageHandler::new().to_callables();
|
|
|
|
|
2022-11-10 03:09:05 +00:00
|
|
|
let instance = Instance::new(PORT)
|
2022-09-01 01:18:15 +01:00
|
|
|
.register("V_INFO", |_: Vec<Primitive>| {
|
2023-02-24 02:07:17 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2023-03-28 02:11:22 +01:00
|
|
|
{
|
|
|
|
vec![format!("v{}-dbg", PACKAGE_VERSION).into()]
|
|
|
|
}
|
2023-02-24 02:07:17 +00:00
|
|
|
#[cfg(not(debug_assertions))]
|
2023-03-28 02:11:22 +01:00
|
|
|
{
|
|
|
|
vec![format!("v{}-rls", PACKAGE_VERSION).into()]
|
|
|
|
}
|
2022-08-10 01:56:22 +01:00
|
|
|
})
|
2023-03-28 02:11:22 +01:00
|
|
|
.register("NAME", |_: Vec<Primitive>| vec![PACKAGE_NAME.into()])
|
2023-01-05 00:42:59 +00:00
|
|
|
.register("LOG", api::general::log_it())
|
2022-08-10 01:56:22 +01:00
|
|
|
// 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()),
|
|
|
|
)
|
2023-06-17 03:00:57 +01:00
|
|
|
.register_async(
|
|
|
|
"BATTERY_charge_power",
|
|
|
|
api::battery::charge_power(api_sender.clone()),
|
|
|
|
)
|
2022-08-10 01:56:22 +01:00
|
|
|
.register(
|
|
|
|
"BATTERY_set_charge_rate",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::battery::set_charge_rate(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"BATTERY_get_charge_rate",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::battery::get_charge_rate(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"BATTERY_unset_charge_rate",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::battery::unset_charge_rate(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
2022-11-21 22:00:43 +00:00
|
|
|
.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()),
|
|
|
|
)
|
2022-08-10 01:56:22 +01:00
|
|
|
// 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()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
2022-09-06 02:36:01 +01:00
|
|
|
.register(
|
|
|
|
"CPU_set_onlines",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::cpu::set_cpus_online(api_sender.clone()),
|
2022-09-06 02:36:01 +01:00
|
|
|
)
|
2022-11-10 03:09:05 +00:00
|
|
|
.register_async(
|
2022-08-23 22:50:46 +01:00
|
|
|
"CPU_get_onlines",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::cpu::get_cpus_online(api_sender.clone()),
|
2023-01-19 02:05:04 +00:00
|
|
|
)
|
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()))
|
2022-08-10 01:56:22 +01:00
|
|
|
.register(
|
|
|
|
"CPU_set_clock_limits",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::cpu::set_clock_limits(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"CPU_get_clock_limits",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::cpu::get_clock_limits(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"CPU_unset_clock_limits",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::cpu::unset_clock_limits(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"CPU_set_governor",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::cpu::set_cpu_governor(api_sender.clone()),
|
2022-11-10 03:09:05 +00:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"CPU_set_governors",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::cpu::set_cpus_governors(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"CPU_get_governors",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::cpu::get_cpu_governors(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
// 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()))
|
2022-08-10 01:56:22 +01:00
|
|
|
.register(
|
|
|
|
"GPU_set_clock_limits",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::gpu::set_clock_limits(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
2022-11-10 03:09:05 +00:00
|
|
|
.register_async(
|
2022-08-10 01:56:22 +01:00
|
|
|
"GPU_get_clock_limits",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::gpu::get_clock_limits(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"GPU_unset_clock_limits",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::gpu::unset_clock_limits(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"GPU_set_slow_memory",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::gpu::set_slow_memory(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
2022-11-10 03:09:05 +00:00
|
|
|
.register_async(
|
2022-08-10 01:56:22 +01:00
|
|
|
"GPU_get_slow_memory",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::gpu::get_slow_memory(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
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()),
|
2022-09-01 01:18:15 +01:00
|
|
|
)
|
|
|
|
.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(
|
2022-11-10 03:09:05 +00:00
|
|
|
"GENERAL_load_system_settings",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::general::load_system_settings(api_sender.clone()),
|
2022-11-10 03:09:05 +00:00
|
|
|
)
|
|
|
|
.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()),
|
2022-09-01 01:18:15 +01:00
|
|
|
)
|
2022-11-10 03:09:05 +00:00
|
|
|
.register_async(
|
2023-03-25 17:46:58 +00:00
|
|
|
"GENERAL_get_path",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::general::get_path(api_sender.clone()),
|
2023-03-25 17:46:58 +00:00
|
|
|
)
|
|
|
|
.register_async(
|
2022-09-01 01:18:15 +01:00
|
|
|
"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
|
|
|
)
|
2022-11-21 22:00:43 +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()),
|
2022-11-21 22:00:43 +00:00
|
|
|
)
|
2023-01-03 02:22:53 +00:00
|
|
|
.register_async(
|
|
|
|
"GENERAL_get_provider",
|
2023-03-28 02:11:22 +01:00
|
|
|
api::general::get_provider(api_sender.clone()),
|
2023-01-03 02:22:53 +00:00
|
|
|
)
|
2023-03-23 02:15:36 +00:00
|
|
|
.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
|
|
|
)
|
2023-08-07 22:08:24 +01:00
|
|
|
.register_async(
|
|
|
|
"GENERAL_get_periodicals",
|
|
|
|
api::general::get_periodicals(api_sender.clone())
|
|
|
|
)
|
2024-01-06 18:26:35 +00:00
|
|
|
.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)
|
2024-01-06 18:26:35 +00:00
|
|
|
.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())
|
|
|
|
);
|
2022-11-10 03:09:05 +00:00
|
|
|
|
2023-01-29 17:00:08 +00:00
|
|
|
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));
|
2023-01-29 17:00:08 +00:00
|
|
|
} else {
|
|
|
|
log::info!("Startup Settings.on_set() success");
|
|
|
|
}
|
|
|
|
|
2023-02-22 02:28:59 +00:00
|
|
|
if let Err(e) = utility::chown_settings_dir() {
|
|
|
|
log::warn!("Failed to change config dir permissions: {}", e);
|
|
|
|
}
|
|
|
|
|
2022-11-10 03:09:05 +00:00
|
|
|
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
|
|
|
}
|