add option to lock device on autosleep.

This commit is contained in:
bunnie 2023-05-22 15:44:46 +08:00
parent 6f9febccf6
commit d3dfe9fdde
5 changed files with 46 additions and 48 deletions

View file

@ -382,6 +382,10 @@ perform the Xous firmware upgrade. This requires running manual update commands,
- `gdb-stub` on hardware is now quite reliable, thanks to some performance improvements by @xobs. Just remember that single stepping does not work on XIP images (and by default now, apps are compiled as `xip`)
- "Notes" fields in `vault` app that start with the keyword 'bip39' will trigger a BIP39 password entry box (if the password field is blank), or BIP39 rendering of the hex data. Password generation prompt also uses the password 'bip39' to trigger the BIP39 generation sequence. It does mean that you can't use just 'bip39' as a regular password but...
- Add multi-line text editing to `vault` password fields, as well as left/right cursor movement to call up an insertion point that can be used to insert and delete characters in the middle of a field.
- Extra characters added to arrow keys and Fn keys from MacOS hosted mode cleaned up (thanks @wizzard0)
- Device will not try to suspend on lock if it is plugged in
- Autosleep timer option added. By default it is 0 (disabled). It is adjustable in increments of minutes. Note that if you try to lock the device while plugged in, and then unplug it without unlocking it, the system will stay "awake" because it can't consult the PDDB to know what the autosleep setting is.
- Autosleep can also optionally lock the device on sleep. However, by default it just sleeps.
## Roadmap
- Lots of testing and bug fixes

View file

@ -49,7 +49,7 @@ pub struct UserPrefs {
pub autobacklight_on_boot: bool,
pub autobacklight_timeout: u64,
pub autosleep_timeout: u64,
pub autounmount_timeout: u64,
pub reboot_on_autosleep: bool,
pub audio_enabled: bool,
pub earpiece_volume: u32,
pub headset_volume: u32,

View file

@ -595,19 +595,12 @@
"ja": "自動スリープ期間 (分単位) (無効にする場合は 0):",
"zh": "以分钟为单位的自动休眠持续时间 (0 表示禁用):"
},
"prefs.autounmount_duration": {
"en": "Auto-Unmount duration",
"en-tts": "Auto-Unmount duration",
"fr": "Durée du démontage automatique *MT*",
"ja": "自動アンマウント期間",
"zh": "自动卸载持续时间"
},
"prefs.autounmount_duration_in_mins": {
"en": "Auto-unmount duration in minutes (0 to disable):",
"en-tts": "Auto-unmount duration in minutes (0 to disable)",
"fr": "Durée du démontage automatique en minutes (0 pour désactiver): *MT*",
"ja": "自動アンマウント期間 (分単位) (無効にする場合は 0):",
"zh": "以分钟为单位的自动卸载持续时间 (0 表示禁用):"
"prefs.autosleep_reboot": {
"en": "Lock device on Auto-sleep",
"en-tts": "Lock device on Auto-sleep",
"fr": "Verrouiller l'appareil en veille automatique *MT*",
"ja": "デバイスをオートスリープにロックする",
"zh": "锁定设备自动休眠"
},
"prefs.wifi_connect_auto": {
"en": "Connect to WiFi automatically",

View file

@ -125,7 +125,7 @@ pub fn pump_thread(
pump_run: Arc<AtomicBool>,
last_key_hit_secs: Arc<AtomicU32>,
autosleep_duration_mins: Arc<AtomicU32>,
_autounmount_duration_mins: Arc<AtomicU32>,
reboot_on_autosleep: Arc<AtomicBool>,
) {
let ticktimer = ticktimer_server::Ticktimer::new().unwrap();
let xns = xous_names::XousNames::new().unwrap();
@ -160,11 +160,19 @@ pub fn pump_thread(
if last_key_hit_duration_mins >= asdm {
log::debug!("autosleep duration hit, trying to sleep");
if cur_power_state == false { // is_plugged_in() is false
log::info!("Autosleeping...");
send_message(
conn as u32,
Message::new_scalar(StatusOpcode::TrySuspend.to_usize().unwrap(), 0, 0, 0, 0),
).ok();
if reboot_on_autosleep.load(Ordering::SeqCst) {
log::info!("Autolocking...");
send_message(
conn as u32,
Message::new_scalar(StatusOpcode::Reboot.to_usize().unwrap(), 0, 0, 0, 0),
).ok();
} else {
log::info!("Autosleeping...");
send_message(
conn as u32,
Message::new_scalar(StatusOpcode::TrySuspend.to_usize().unwrap(), 0, 0, 0, 0),
).ok();
}
} else {
log::debug!("can't sleep, plugged in!");
}
@ -230,21 +238,21 @@ fn wrapped_main() -> ! {
// allocate shared variables for automatic timers that get polled in the pump thread
let last_key_hit_secs = Arc::new(AtomicU32::new(0)); // rolls over in 126 years. Can't AtomicU64 on a 32-bit platform.
let autosleep_duration_mins = Arc::new(AtomicU32::new(0));
let autounmount_duration_mins = Arc::new(AtomicU32::new(0));
let reboot_on_autosleep = Arc::new(AtomicBool::new(false));
let autobacklight_duration_secs = Arc::new(AtomicU32::new(0));
let pump_conn = xous::connect(status_sid).unwrap();
let _ = thread::spawn({
let pump_run = pump_run.clone();
let last_key_hit_secs = last_key_hit_secs.clone();
let autosleep_duration_mins = autosleep_duration_mins.clone();
let autounmount_duration_mins = autounmount_duration_mins.clone();
let reboot_on_autosleep = reboot_on_autosleep.clone();
move || {
pump_thread(
pump_conn as _,
pump_run,
last_key_hit_secs,
autosleep_duration_mins,
autounmount_duration_mins,
reboot_on_autosleep,
);
}
});
@ -749,7 +757,7 @@ fn wrapped_main() -> ! {
*/
std::thread::spawn({
let autosleep_duration_mins = autosleep_duration_mins.clone();
let autounmount_duration_mins = autounmount_duration_mins.clone();
let reboot_on_autosleep = reboot_on_autosleep.clone();
let autobacklight_duration_secs = autobacklight_duration_secs.clone();
move || {
let pddb = pddb::Pddb::new();
@ -846,7 +854,7 @@ fn wrapped_main() -> ! {
}
}
autosleep_duration_mins.store(prefs.autosleep_timeout_or_value(0).unwrap() as u32, Ordering::SeqCst);
autounmount_duration_mins.store(prefs.autounmount_timeout_or_value(0).unwrap() as u32, Ordering::SeqCst);
reboot_on_autosleep.store(prefs.reboot_on_autosleep_or_value(false).unwrap(), Ordering::SeqCst);
autobacklight_duration_secs.store(prefs.autobacklight_timeout_or_value(10).unwrap() as u32, Ordering::SeqCst);
}});
@ -892,7 +900,7 @@ fn wrapped_main() -> ! {
Some(StatusOpcode::ReloadPrefs) => {
let p = prefs.lock().unwrap(); // lock it once in this block
autosleep_duration_mins.store(p.autosleep_timeout_or_value(0).unwrap() as u32, Ordering::SeqCst);
autounmount_duration_mins.store(p.autounmount_timeout_or_value(0).unwrap() as u32, Ordering::SeqCst);
reboot_on_autosleep.store(p.reboot_on_autosleep_or_value(false).unwrap(), Ordering::SeqCst);
autobacklight_duration_secs.store(p.autobacklight_timeout_or_value(10).unwrap() as u32, Ordering::SeqCst);
}
Some(StatusOpcode::EnableAutomaticBacklight) => {

View file

@ -19,6 +19,7 @@ enum DevicePrefsOp {
AutobacklightTimeout,
AutoSleepTimeout,
AutoUnmountTimeout,
RebootOnAutoSleep,
KeyboardLayout,
WLANMenu,
SetTime,
@ -46,7 +47,7 @@ impl Display for DevicePrefsOp {
Self::AutobacklightOnBoot => write!(f, "{}", t!("prefs.autobacklight_enable", xous::LANG)),
Self::AutobacklightTimeout => write!(f, "{}", t!("prefs.autobacklight_duration", xous::LANG)),
Self::AutoSleepTimeout => write!(f, "{}", t!("prefs.autosleep_duration", xous::LANG)),
Self::AutoUnmountTimeout => write!(f, "{}", t!("prefs.autounmount_duration", xous::LANG)),
Self::RebootOnAutoSleep => write!(f, "{}", t!("prefs.autosleep_reboot", xous::LANG)),
Self::ConnectKnownNetworksOnBoot => write!(f, "{}", t!("prefs.wifi_connect_auto", xous::LANG)),
Self::WifiKill => write!(f, "{}", t!("prefs.wifi_kill", xous::LANG)),
Self::KeyboardLayout => write!(f, "{}", t!("prefs.keyboard_layout", xous::LANG)),
@ -209,7 +210,7 @@ impl DevicePrefs {
AutobacklightOnBoot,
AutobacklightTimeout,
AutoSleepTimeout,
// AutoUnmountTimeout, // placeholder for when the feature is implemented
RebootOnAutoSleep,
KeyboardLayout,
// Note: this vec sets the order of items in the preferences menu
// The CI system assumes that the time setting items are always at
@ -247,7 +248,7 @@ impl DevicePrefs {
ConnectKnownNetworksOnBoot => self.connect_known_networks_on_boot(),
AutobacklightTimeout => self.autobacklight_timeout(),
AutoSleepTimeout => self.autosleep_timeout(),
AutoUnmountTimeout => self.autounmount_timeout(),
RebootOnAutoSleep => self.reboot_on_autosleep(),
KeyboardLayout => self.keyboard_layout(),
WLANMenu => self.wlan_menu(),
SetTime => self.set_time_menu(),
@ -368,27 +369,19 @@ impl DevicePrefs {
Ok(self.up.set_autosleep_timeout(new_timeout)?)
}
fn autounmount_timeout(&self) -> Result<(), DevicePrefsError> {
let cv = self.up.autounmount_timeout_or_default()?;
fn reboot_on_autosleep(&self) -> Result<(), DevicePrefsError> {
let cv = self.up.reboot_on_autosleep_or_default()?;
let raw_timeout = self
.modals
.alert_builder(t!("prefs.autounmount_duration_in_mins", xous::LANG))
.field(
Some(cv.to_string()),
Some(|tf| match tf.as_str().parse::<u64>() {
Ok(_) => None,
Err(_) => Some(xous_ipc::String::from_str(
t!("prefs.autobacklight_err", xous::LANG),
)),
}),
)
.build()
.unwrap();
self.modals.add_list(vec![t!("prefs.yes", xous::LANG), t!("prefs.no", xous::LANG)]).unwrap();
let new_result = yes_no_to_bool(
self.modals
.get_radiobutton(&format!("{} {}", t!("prefs.current_setting", xous::LANG),
bool_to_yes_no(cv)))
.unwrap()
.as_str(),
);
let new_timeout = raw_timeout.first().as_str().parse::<u64>().unwrap(); // we know this is a number, we checked with validator;
Ok(self.up.set_autounmount_timeout(new_timeout)?)
Ok(self.up.set_reboot_on_autosleep(new_result)?)
}
fn wifi_kill(&mut self) -> Result<(), DevicePrefsError> {