commit db5ec13f46b1521ec23c73ace1ef83413221dce7 Author: Lain Iwakura Date: Fri Jul 11 20:56:56 2025 +0300 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..002aadc --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,647 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.59.0", +] + +[[package]] +name = "anyhow" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "backtrace" +version = "0.3.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6806a6321ec58106fea15becdad98371e28d92ccbc7c8f1b3b6dd724fe8f1002" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets", +] + +[[package]] +name = "bitflags" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" + +[[package]] +name = "bytes" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" + +[[package]] +name = "cfg-if" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" + +[[package]] +name = "clap" +version = "4.5.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be92d32e80243a54711e5d7ce823c35c41c9d929dc4ab58e1276f625841aadf9" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "707eab41e9622f9139419d573eca0900137718000c517d47da73045f54331c3d" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef4f52386a59ca4c860f7393bcf8abd8dfd91ecccc0f774635ff68e92eeef491" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "env_logger" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + +[[package]] +name = "humantime" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b112acc8b3adf4b107a8ec20977da0273a8c386765a3ec0229bd500a1443f9f" + +[[package]] +name = "io-uring" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b86e202f00093dcba4275d4636b93ef9dd75d025ae560d2521b45ea28ab49013" +dependencies = [ + "bitflags", + "cfg-if", + "libc", +] + +[[package]] +name = "is-terminal" +version = "0.4.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys 0.59.0", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + +[[package]] +name = "libc" +version = "0.2.174" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" + +[[package]] +name = "lock_api" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + +[[package]] +name = "memchr" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.59.0", +] + +[[package]] +name = "object" +version = "0.36.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" + +[[package]] +name = "parking_lot" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70d58bf43669b5795d1576d0641cfb6fbb2057bf629506267a92807158584a13" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d04b7d0ee6b4a0207a0a7adb104d23ecb0b47d6beae7152d0fa34b692b29fd6" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "rustc-demangle" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "989e6739f80c4ad5b13e0fd7fe89531180375b18520cc8c82080e4dc4035b84f" + +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9203b8055f63a2a00e2f593bb0510367fe707d7ff1e5c872de2f537b339e5410" +dependencies = [ + "libc", +] + +[[package]] +name = "slab" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04dc19736151f35336d325007ac991178d504a119863a2fcb3758cdb5e52c50d" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "socket2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thinkfand" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "env_logger", + "log", + "serde", + "serde_json", + "tokio", +] + +[[package]] +name = "tokio" +version = "1.46.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cc3a2344dafbe23a245241fe8b09735b521110d30fcefbbd5feb1797ca35d17" +dependencies = [ + "backtrace", + "bytes", + "io-uring", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "slab", + "socket2", + "tokio-macros", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-macros" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..08dc038 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "thinkfand" +version = "0.1.0" +edition = "2021" +license = "MIT" +description = "ThinkPad fan control daemon" +keywords = ["thinkpad", "fan", "daemon", "control"] +categories = ["system"] + +[[bin]] +name = "thinkfand" +path = "src/main.rs" + +[[bin]] +name = "thinkfanctl" +path = "src/bin/thinkfanctl.rs" + +[dependencies] +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +tokio = { version = "1.0", features = ["full"] } +clap = { version = "4.0", features = ["derive"] } +log = "0.4" +env_logger = "0.10" +anyhow = "1.0" diff --git a/README.md b/README.md new file mode 100644 index 0000000..74537a4 --- /dev/null +++ b/README.md @@ -0,0 +1,91 @@ +# ThinkFanD + +ThinkPad fan control daemon с поддержкой автоматического управления температурой. + +## Установка + +```bash +cargo build --release +sudo cp target/release/thinkfand /usr/local/bin/ +sudo cp target/release/thinkfanctl /usr/local/bin/ +sudo cp thinkfan.conf.example /etc/thinkfan.conf +``` + +## Использование + +### Демон + +Запуск демона: +```bash +sudo thinkfand -d +``` + +### Управление + +Установка уровня вентилятора (0-7): +```bash +sudo thinkfand -l 5 +``` + +Автоматический режим: +```bash +sudo thinkfand -a +``` + +Просмотр статуса: +```bash +thinkfand +``` + +### ThinkFanCtl + +Управление демоном: +```bash +# Запуск +sudo thinkfanctl start + +# Остановка +sudo thinkfanctl stop + +# Перезапуск +sudo thinkfanctl restart + +# Статус +thinkfanctl status + +# Установка уровня +sudo thinkfanctl level 5 + +# Автоматический режим +sudo thinkfanctl auto + +# Обновление конфига +sudo thinkfanctl config /path/to/config.json +``` + +## Конфигурация + +Файл `/etc/thinkfan.conf` в формате JSON: + +```json +{ + "sensors": ["thinkpad-isa-0000", "k10temp-pci-00c3"], + "levels": [ + {"temp": 0.0, "level": 0}, + {"temp": 50.0, "level": 2}, + {"temp": 60.0, "level": 4}, + {"temp": 70.0, "level": 6}, + {"temp": 80.0, "level": 7} + ] +} +``` + +## Systemd + +Установка сервиса: +```bash +sudo cp thinkfand.service /etc/systemd/system/ +sudo systemctl daemon-reload +sudo systemctl enable thinkfand +sudo systemctl start thinkfand +``` \ No newline at end of file diff --git a/src/bin/thinkfanctl.rs b/src/bin/thinkfanctl.rs new file mode 100644 index 0000000..ee57be7 --- /dev/null +++ b/src/bin/thinkfanctl.rs @@ -0,0 +1,128 @@ +use std::process::Command; +use clap::{Parser, Subcommand}; +use anyhow::Result; + +#[derive(Parser)] +#[command(name = "thinkfanctl")] +#[command(about = "ThinkPad fan control utility")] +struct Args { + #[command(subcommand)] + command: Commands, +} + +#[derive(Subcommand)] +enum Commands { + Start, + Stop, + Restart, + Status, + Level { level: u8 }, + Auto, + Config { path: String }, +} + +fn main() -> Result<()> { + let args = Args::parse(); + + match args.command { + Commands::Start => start_daemon(), + Commands::Stop => stop_daemon(), + Commands::Restart => restart_daemon(), + Commands::Status => show_status(), + Commands::Level { level } => set_level(level), + Commands::Auto => set_auto(), + Commands::Config { path } => set_config(path), + } +} + +fn start_daemon() -> Result<()> { + let status = Command::new("systemctl") + .args(["start", "thinkfand"]) + .status()?; + + if status.success() { + println!("Daemon started"); + } else { + println!("Failed to start daemon"); + } + Ok(()) +} + +fn stop_daemon() -> Result<()> { + let status = Command::new("systemctl") + .args(["stop", "thinkfand"]) + .status()?; + + if status.success() { + println!("Daemon stopped"); + } else { + println!("Failed to stop daemon"); + } + Ok(()) +} + +fn restart_daemon() -> Result<()> { + let status = Command::new("systemctl") + .args(["restart", "thinkfand"]) + .status()?; + + if status.success() { + println!("Daemon restarted"); + } else { + println!("Failed to restart daemon"); + } + Ok(()) +} + +fn show_status() -> Result<()> { + let output = Command::new("systemctl") + .args(["status", "thinkfand"]) + .output()?; + + println!("{}", String::from_utf8_lossy(&output.stdout)); + Ok(()) +} + +fn set_level(level: u8) -> Result<()> { + if level > 7 { + return Err(anyhow::anyhow!("Level must be 0-7")); + } + + let status = Command::new("thinkfand") + .args(["-l", &level.to_string()]) + .status()?; + + if status.success() { + println!("Fan level set to {}", level); + } else { + println!("Failed to set fan level"); + } + Ok(()) +} + +fn set_auto() -> Result<()> { + let status = Command::new("thinkfand") + .arg("-a") + .status()?; + + if status.success() { + println!("Fan set to auto mode"); + } else { + println!("Failed to set auto mode"); + } + Ok(()) +} + +fn set_config(path: String) -> Result<()> { + let status = Command::new("cp") + .args([&path, "/etc/thinkfan.conf"]) + .status()?; + + if status.success() { + println!("Config updated"); + restart_daemon()?; + } else { + println!("Failed to update config"); + } + Ok(()) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b1881c4 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,384 @@ +#![allow(dead_code)] +use std::fs::File; +use std::io::{Read}; +use std::process::Command; +use std::time::Duration; +use std::path::Path; + +use clap::Parser; +use log::{info, warn, error}; +use serde::{Deserialize, Serialize}; +use tokio::time::sleep; + +static PATH_FAN: &str = "/proc/acpi/ibm/fan"; + +#[derive(Parser)] +#[command(name = "thinkfand")] +#[command(about = "ThinkPad fan control daemon")] +struct Args { + #[arg(short, long, default_value = "/etc/thinkfand.conf")] + config: String, + + #[arg(short, long)] + daemon: bool, + + #[arg(short, long)] + level: Option, + + #[arg(short, long)] + auto: bool, + + +} + +#[derive(Debug, Clone)] +struct Input { + name: String, + temp: f64, +} + +#[derive(Debug, Clone)] +struct Adapter { + name: String, + inputs: Vec, +} + +#[derive(Debug, Serialize, Deserialize)] +struct FanConfig { + sensors: Vec, + levels: Vec, +} + +#[derive(Debug, Serialize, Deserialize)] +struct FanLevel { + temp: f64, + level: u8, +} + +struct ThinkFanDaemon { + config: FanConfig, + current_level: u8, + auto_mode: bool, +} + +impl ThinkFanDaemon { + fn new(config_path: &str) -> anyhow::Result { + let config = if Path::new(config_path).exists() { + let content = std::fs::read_to_string(config_path)?; + serde_json::from_str(&content)? + } else { + FanConfig { + sensors: vec!["thinkpad-isa-0000".to_string()], + levels: vec![ + FanLevel { temp: 0.0, level: 0 }, + FanLevel { temp: 50.0, level: 2 }, + FanLevel { temp: 60.0, level: 4 }, + FanLevel { temp: 70.0, level: 6 }, + FanLevel { temp: 80.0, level: 7 }, + ], + } + }; + + Ok(Self { + config, + current_level: 0, + auto_mode: true, + }) + } + + fn read_temperatures(&self) -> Vec { + let output = match Command::new("sensors").arg("-j").output() { + Ok(output) => output, + Err(e) => { + error!("Failed to run sensors command: {}", e); + return vec![]; + } + }; + + let json_str = match std::str::from_utf8(&output.stdout) { + Ok(s) => s, + Err(e) => { + error!("Invalid UTF-8 in sensors output: {}", e); + return vec![]; + } + }; + + parse_adapters(json_str) + } + + fn get_max_temp(&self, adapters: &[Adapter]) -> f64 { + let mut max_temp: f64 = 0.0; + + for adapter in adapters { + for input in &adapter.inputs { + if self.config.sensors.iter().any(|s| adapter.name.contains(s)) { + max_temp = max_temp.max(input.temp); + } + } + } + + max_temp + } + + fn calculate_fan_level(&self, temp: f64) -> u8 { + let mut level = 0; + + for fan_level in &self.config.levels { + if temp >= fan_level.temp { + level = fan_level.level; + } + } + + level + } + + fn set_fan_level(&mut self, level: u8) -> anyhow::Result<()> { + if self.current_level == level { + return Ok(()); + } + + let command = format!("level {}", level); + match std::fs::write(PATH_FAN, &command) { + Ok(_) => { + self.current_level = level; + info!("Fan level set to {}", level); + Ok(()) + } + Err(e) => { + let error_str = e.to_string(); + if error_str.contains("Invalid argument") { + warn!("Invalid argument error, reloading thinkpad_acpi module"); + self.reload_thinkpad_acpi()?; + + match std::fs::write(PATH_FAN, &command) { + Ok(_) => { + self.current_level = level; + info!("Fan level set to {} after module reload", level); + Ok(()) + } + Err(e2) => { + error!("Failed to set fan level after reload: {}", e2); + Err(anyhow::anyhow!("Failed to write to fan control after reload")) + } + } + } else { + error!("Failed to set fan level: {}", e); + Err(anyhow::anyhow!("Failed to write to fan control")) + } + } + } + } + + fn set_auto_mode(&mut self) -> anyhow::Result<()> { + let command = "level auto"; + match std::fs::write(PATH_FAN, command) { + Ok(_) => { + self.auto_mode = true; + info!("Fan set to auto mode"); + Ok(()) + } + Err(e) => { + let error_str = e.to_string(); + if error_str.contains("Invalid argument") { + warn!("Invalid argument error, reloading thinkpad_acpi module"); + self.reload_thinkpad_acpi()?; + + match std::fs::write(PATH_FAN, command) { + Ok(_) => { + self.auto_mode = true; + info!("Fan set to auto mode after module reload"); + Ok(()) + } + Err(e2) => { + error!("Failed to set auto mode after reload: {}", e2); + Err(anyhow::anyhow!("Failed to write to fan control after reload")) + } + } + } else { + error!("Failed to set auto mode: {}", e); + Err(anyhow::anyhow!("Failed to write to fan control")) + } + } + } + } + + fn read_fan_status(&self) -> anyhow::Result { + let mut file = File::open(PATH_FAN)?; + let mut buffer = [0; 64]; + let bytes_read = file.read(&mut buffer)?; + + let content = std::str::from_utf8(&buffer[..bytes_read])?; + Ok(content.to_string()) + } + + fn reload_thinkpad_acpi(&self) -> anyhow::Result<()> { + info!("Reloading thinkpad_acpi module"); + + let unload_status = Command::new("modprobe") + .args(["-r", "thinkpad_acpi"]) + .status(); + + match unload_status { + Ok(status) => { + if !status.success() { + warn!("Failed to unload thinkpad_acpi module"); + } + } + Err(e) => { + warn!("Error unloading thinkpad_acpi module: {}", e); + } + } + + std::thread::sleep(Duration::from_millis(1000)); + + let load_status = Command::new("modprobe") + .args(["thinkpad_acpi", "fan_control=1"]) + .status(); + + match load_status { + Ok(status) => { + if status.success() { + info!("thinkpad_acpi module reloaded successfully"); + std::thread::sleep(Duration::from_millis(2000)); + Ok(()) + } else { + error!("Failed to reload thinkpad_acpi module"); + Err(anyhow::anyhow!("Failed to reload thinkpad_acpi module")) + } + } + Err(e) => { + error!("Error reloading thinkpad_acpi module: {}", e); + Err(anyhow::anyhow!("Error reloading thinkpad_acpi module")) + } + } + } + + async fn run_loop(&mut self) -> anyhow::Result<()> { + loop { + if self.auto_mode { + let adapters = self.read_temperatures(); + let max_temp = self.get_max_temp(&adapters); + let new_level = self.calculate_fan_level(max_temp); + + if let Err(e) = self.set_fan_level(new_level) { + warn!("Failed to set fan level: {}", e); + } + + info!("Max temp: {:.1}°C, Fan level: {}", max_temp, new_level); + } + + sleep(Duration::from_secs(5)).await; + } + } +} + +fn parse_adapters(json_str: &str) -> Vec { + let json: serde_json::Value = match serde_json::from_str(json_str) { + Ok(j) => j, + Err(e) => { + error!("Failed to parse JSON: {}", e); + return vec![]; + } + }; + + let json_obj = match json.as_object() { + Some(obj) => obj, + None => { + error!("Invalid JSON structure"); + return vec![]; + } + }; + + let mut adapters = Vec::new(); + + for adapter_name in json_obj.keys() { + let mut curr_inputs: Vec<(String, String)> = Vec::new(); + let adapter = match json_obj[adapter_name].as_object() { + Some(adapter) => adapter, + None => continue, + }; + + for input_key in adapter.keys() { + let input = adapter[input_key].clone(); + if !input.is_object() { + continue; + } + + let input_obj = match input.as_object() { + Some(obj) => obj, + None => continue, + }; + + for temp_key in input_obj.keys() { + if !temp_key.contains("temp") || !temp_key.contains("input") { + continue; + } + + if input_obj[temp_key] == 0.0 { + continue; + } + + curr_inputs.push((input_key.clone(), input_obj[temp_key].to_string())); + } + } + + if curr_inputs.is_empty() { + continue; + } + + let mut adapter = Adapter { + name: adapter_name.clone(), + inputs: Vec::new(), + }; + + for (name, temp) in curr_inputs { + let temp_value = temp.trim().parse::().unwrap_or(-99.0); + adapter.inputs.push(Input { + name, + temp: temp_value, + }); + } + adapters.push(adapter); + } + + adapters +} + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + env_logger::init(); + + let args = Args::parse(); + + + + if !Path::new(PATH_FAN).exists() { + error!("Fan control file not found: {}", PATH_FAN); + return Err(anyhow::anyhow!("Fan control not available")); + } + + let mut daemon = ThinkFanDaemon::new(&args.config)?; + + if args.auto { + daemon.set_auto_mode()?; + return Ok(()); + } + + if let Some(level) = args.level { + if level > 7 { + return Err(anyhow::anyhow!("Fan level must be 0-7")); + } + daemon.set_fan_level(level)?; + return Ok(()); + } + + if args.daemon { + info!("Starting thinkfand daemon"); + daemon.run_loop().await?; + } else { + let status = daemon.read_fan_status()?; + println!("{}", status); + } + + Ok(()) +} diff --git a/thinkfan.conf.example b/thinkfan.conf.example new file mode 100644 index 0000000..70a36a3 --- /dev/null +++ b/thinkfan.conf.example @@ -0,0 +1,16 @@ +{ + "sensors": [ + "thinkpad-isa-0000", + "k10temp-pci-00c3" + ], + "levels": [ + { "temp": 0.0, "level": 0 }, + { "temp": 45.0, "level": 1 }, + { "temp": 50.0, "level": 2 }, + { "temp": 55.0, "level": 3 }, + { "temp": 60.0, "level": 4 }, + { "temp": 65.0, "level": 5 }, + { "temp": 70.0, "level": 6 }, + { "temp": 75.0, "level": 7 } + ] +} \ No newline at end of file diff --git a/thinkfand.service b/thinkfand.service new file mode 100644 index 0000000..c4bfad6 --- /dev/null +++ b/thinkfand.service @@ -0,0 +1,12 @@ +[Unit] +Description=ThinkPad Fan Control Daemon +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/thinkfand -d +Restart=always +User=root + +[Install] +WantedBy=multi-user.target \ No newline at end of file