This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
296 lines (262 loc) · 10.1 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
mod sensors;
use self::sensors::Sensors;
use serde::Deserialize;
use std::cmp::Ordering;
use std::fs;
use std::io;
use std::process::Command;
use std::str::FromStr;
use std::thread;
use std::time::Duration;
#[derive(Deserialize)]
struct Config {
request_shutdown_battery_percent: Option<f64>,
force_shutdown_timeout_secs: Option<f64>,
}
const DEVICEPATH: &str = if let Ok(product_name) = std::process::Command::new("cat")
.arg("/sys/devices/virtual/dmi/id/product_name")
.output()
.map(|o| o.stdout)
.map(String::from_utf8)
.map_err(|e| format!("Failed to decode command output: {}", e))
.and_then(|opt| opt.map_err(|e| format!("Invalid utf-8 in command output: {}", e)))
{
if product_name.trim() == "Jupiter 1" {
"/sys/class/power_supply/BAT1/"
} else {
"/sys/class/power_supply/BAT*/"
}
} else {
"/sys/class/power_supply/BAT*/"
};
fn read_battery_string(var_name: &str) -> Option<String> {
let path = format!("{}/{}", DEVICEPATH, var_name);
match fs::read_to_string(&path) {
Err(err) => {
eprintln!("read {path}: {err}");
None
}
Ok(string) => Some(string.trim().to_owned()),
}
}
fn read_battery_f64(var_name: &str) -> Option<f64> {
let path = format!("{}/{}", DEVICEPATH, var_name);
match fs::read_to_string(&path) {
Err(err) => {
eprintln!("read {path}: {err}");
None
}
Ok(string) => match f64::from_str(string.trim()) {
Err(err) => {
eprintln!("read {path}: {err}");
None
}
Ok(val) => {
if !val.is_finite() {
eprintln!("read {path}: {val} is not finite");
None
} else {
Some(val)
}
}
},
}
}
fn write_str(dir_path: &str, var_name: &str, val: Option<&str>) {
let val = match val {
Some(val) => val,
None => return,
};
if let Err(err) = fs::create_dir(dir_path) {
if err.kind() != io::ErrorKind::AlreadyExists {
eprintln!("mkdir {dir_path}: {err}");
return;
}
}
// Write to a temporary path first.
let dot_path = format!("{dir_path}/.{var_name}");
if let Err(err) = fs::write(&dot_path, format!("{val}\n")) {
eprintln!("write {dot_path}: {err}");
return;
}
// Then move into place for atomicity.
let final_path = format!("{dir_path}/{var_name}");
if let Err(err) = fs::rename(&dot_path, &final_path) {
eprintln!("rename {dot_path} -> {final_path}: {err}");
}
}
fn write_f64(dir_path: &str, var_name: &str, val: Option<f64>) {
if let Some(val) = val {
write_str(dir_path, var_name, Some(&val.to_string()))
}
}
fn main() {
// Read /etc/vpower.toml
let config_path = "/etc/vpower.toml";
let mut request_shutdown_battery_percent = 0.49999998;
let mut force_shutdown_timeout_secs = 10.0;
match fs::read(config_path) {
Err(err) => eprintln!("read {config_path}: {err}"),
Ok(bytes) => match toml::from_slice::<Config>(&bytes) {
Err(err) => eprintln!("read {config_path}: {err}"),
Ok(config) => {
if let Some(value) = config.request_shutdown_battery_percent {
request_shutdown_battery_percent = value;
}
if let Some(value) = config.force_shutdown_timeout_secs {
force_shutdown_timeout_secs = value;
}
}
},
}
println!("request_shutdown_battery_percent: {request_shutdown_battery_percent}");
println!("force_shutdown_timeout_secs: {force_shutdown_timeout_secs}");
// Initialize libsensors.
let sensors = Sensors::new();
// Keep for heuristics.
let mut prev_ac_status: Option<&str> = None;
let mut prev_battery_percent: Option<f64> = None;
// Start.
println!("Running.");
// Every second:
loop {
// Read battery variables.
let charge_full = read_battery_f64("charge_full");
let charge_now = read_battery_f64("charge_now");
let current_now = read_battery_f64("current_now");
let pdam = sensors.pdam();
let pdcs = sensors.pdcs();
let pdvl = sensors.pdvl();
let status = read_battery_string("status");
let voltage_min_design = read_battery_f64("voltage_min_design");
let voltage_now = read_battery_f64("voltage_now");
// Derive battery variables.
let charge_shutdown = charge_full.map(|charge_full| {
let rsbp = request_shutdown_battery_percent;
charge_full * (rsbp / 100.0)
});
let power_now = match (voltage_now, current_now) {
(Some(voltage_now), Some(current_now)) => Some(voltage_now * current_now),
_ => None,
};
// Calculate ac_status.
let ac_status = if let Some(pdcs) = pdcs {
let connected = (pdcs & (1 << 0)) != 0;
let sink = (pdcs & (1 << 4)) == 0;
if connected && sink {
let was_disconnected = prev_ac_status == Some("Disconnected");
let pd_power = match (pdvl, pdam) {
(Some(pdvl), Some(pdam)) => pdvl * pdam, // Watts.
_ => 0.0,
};
// Basically all power supplies get reported as low power for ~0.5 seconds
// after connecting, so ignore it on the first iteration after connecting.
if !was_disconnected && pd_power > 0.0 && pd_power < 30.0 {
Some("Connected slow")
} else {
Some("Connected")
}
} else {
Some("Disconnected")
}
} else {
match status.as_deref() {
Some("Full" | "Charging") => Some("Connected"),
Some("Discharging") => Some("Disconnected"),
_ => None,
}
};
// Calculate battery_percent.
let battery_percent = match (charge_now, charge_full) {
(Some(charge_now), Some(charge_full)) => Some(charge_now / charge_full * 100.0),
_ => None,
};
// Calculate battery_status.
let battery_status = match (ac_status, status.as_deref()) {
(_, Some("Full")) => Some("Full"),
(_, Some("Discharging")) => Some("Discharging"),
(Some("Connected"), Some("Charging")) => Some("Charging"),
_ => {
// Probably "Unknown" or "Not charging". Use heuristics as a fallback.
let ordering = match (battery_percent, prev_battery_percent) {
(Some(lhs), Some(rhs)) => lhs.partial_cmp(&rhs),
_ => None,
};
match ordering {
Some(Ordering::Less) => Some("Discharging"),
Some(Ordering::Greater) => Some("Charging"),
_ => {
if battery_percent.unwrap_or(0.0) >= 89.5 {
// Some batteries won't charge when plugged in above ~90%.
// We call this "Full".
Some("Full")
} else {
None
}
}
}
}
};
// Calculate secs_until_battery_full.
let vars = (charge_full, charge_now, voltage_min_design, power_now);
let secs_until_battery_full = match vars {
(Some(charge_full), Some(charge_now), Some(voltage_min_design), Some(power_now)) => {
let charge_delta = charge_full - charge_now;
let hours = charge_delta * voltage_min_design / power_now;
Some(hours * 3600.0)
}
_ => None,
};
// Calcuate secs_until_shutdown_request.
let vars = (charge_now, charge_shutdown, voltage_min_design, power_now);
let secs_until_shutdown_request = match vars {
(
Some(charge_now),
Some(charge_shutdown),
Some(voltage_min_design),
Some(power_now),
) => {
if charge_now > charge_shutdown {
let charge_delta = charge_now - charge_shutdown;
let hours = charge_delta * voltage_min_design / power_now;
Some(hours * 3600.0)
} else {
match ac_status {
// Avoid shutdown request while connected.
Some("Connected") => Some(1.0),
_ => Some(0.0),
}
}
}
_ => None,
};
// Write to /run/vpower/*
let dir_path = "/run/vpower";
write_str(dir_path, "ac_status", ac_status);
write_f64(dir_path, "battery_percent", battery_percent);
write_str(dir_path, "battery_status", battery_status);
let val = secs_until_battery_full;
write_f64(dir_path, "secs_until_battery_full", val);
let val = secs_until_shutdown_request;
write_f64(dir_path, "secs_until_shutdown_request", val);
// Force shutdown after timeout.
if secs_until_shutdown_request.map_or(false, |x| x == 0.0) {
println!("Reached {request_shutdown_battery_percent}% battery.");
println!("Forcing shutdown in {force_shutdown_timeout_secs} seconds.");
thread::sleep(Duration::from_secs_f64(force_shutdown_timeout_secs));
println!("Shutting down now.");
match Command::new("poweroff").status() {
Err(err) => panic!("poweroff: {err}"),
Ok(status) => match status.success() {
false => panic!("poweroff: {status}"),
true => return,
},
}
}
// Update prev_*.
prev_ac_status = ac_status;
prev_battery_percent = battery_percent;
// Sleep until next iteration.
thread::sleep(Duration::from_secs(1));
}
}