mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
refactor time setting modals into a separate thread
modals don't work when triggered in the status thread in hosted mode (but they do, somehow, on real hardware). I think this is an actual locking problem that would exhibit itself if we had a true multiprocessor system, but since real hardware is a single processor it doesn't show.
This commit is contained in:
parent
1130bda4a8
commit
bd78ba94f1
4 changed files with 250 additions and 220 deletions
|
|
@ -88,7 +88,7 @@ fn xmain() -> ! {
|
||||||
let _redraw_handle = thread::spawn({
|
let _redraw_handle = thread::spawn({
|
||||||
let op = Arc::clone(&op);
|
let op = Arc::clone(&op);
|
||||||
move || {
|
move || {
|
||||||
#[cfg(any(feature="tts", not(any(target_os = "none", target_os = "xous"))))]
|
#[cfg(feature="tts")]
|
||||||
let tt = ticktimer_server::Ticktimer::new().unwrap();
|
let tt = ticktimer_server::Ticktimer::new().unwrap();
|
||||||
// build the core data structure here
|
// build the core data structure here
|
||||||
let text_action = TextEntry {
|
let text_action = TextEntry {
|
||||||
|
|
@ -132,8 +132,6 @@ fn xmain() -> ! {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let msg = xous::receive_message(renderer_sid).unwrap();
|
let msg = xous::receive_message(renderer_sid).unwrap();
|
||||||
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
|
||||||
tt.sleep_ms(100).unwrap(); // hosted mode needs delays to work
|
|
||||||
log::debug!("renderer message: {:?}", msg);
|
log::debug!("renderer message: {:?}", msg);
|
||||||
match FromPrimitive::from_usize(msg.body.id()) {
|
match FromPrimitive::from_usize(msg.body.id()) {
|
||||||
Some(RendererOp::InitiateOp) => {
|
Some(RendererOp::InitiateOp) => {
|
||||||
|
|
@ -439,8 +437,6 @@ fn xmain() -> ! {
|
||||||
];
|
];
|
||||||
loop {
|
loop {
|
||||||
let mut msg = xous::receive_message(modals_sid).unwrap();
|
let mut msg = xous::receive_message(modals_sid).unwrap();
|
||||||
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
|
||||||
tt.sleep_ms(100).unwrap(); // hosted mode needs delays to work
|
|
||||||
log::debug!("message: {:?}", msg);
|
log::debug!("message: {:?}", msg);
|
||||||
match FromPrimitive::from_usize(msg.body.id()) {
|
match FromPrimitive::from_usize(msg.body.id()) {
|
||||||
Some(Opcode::GetMutex) => msg_blocking_scalar_unpack!(msg, t0, t1, t2, t3, {
|
Some(Opcode::GetMutex) => msg_blocking_scalar_unpack!(msg, t0, t1, t2, t3, {
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,12 @@ use xous::{msg_scalar_unpack, send_message, Message, CID};
|
||||||
use graphics_server::*;
|
use graphics_server::*;
|
||||||
use graphics_server::api::GlyphStyle;
|
use graphics_server::api::GlyphStyle;
|
||||||
use locales::t;
|
use locales::t;
|
||||||
use gam::modal::*;
|
|
||||||
use gam::{GamObjectList, GamObjectType};
|
use gam::{GamObjectList, GamObjectType};
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::io::Read;
|
|
||||||
|
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
not(any(target_os = "none", target_os = "xous")),
|
not(any(target_os = "none", target_os = "xous")),
|
||||||
|
|
@ -41,10 +39,6 @@ pub(crate) enum StatusOpcode {
|
||||||
BattStats,
|
BattStats,
|
||||||
/// indicates time for periodic update of the status bar
|
/// indicates time for periodic update of the status bar
|
||||||
Pump,
|
Pump,
|
||||||
/// Pulls up the time setting UI
|
|
||||||
UxSetTime,
|
|
||||||
/// Pulls up the time setting UI
|
|
||||||
UxSetTimeZone,
|
|
||||||
/// Initiates a reboot
|
/// Initiates a reboot
|
||||||
Reboot,
|
Reboot,
|
||||||
|
|
||||||
|
|
@ -328,19 +322,25 @@ fn xmain() -> ! {
|
||||||
com.req_batt_stats()
|
com.req_batt_stats()
|
||||||
.expect("Can't get battery stats from COM");
|
.expect("Can't get battery stats from COM");
|
||||||
|
|
||||||
|
// spawn a time UX manager thread
|
||||||
|
let time_sid = xous::create_server().unwrap();
|
||||||
|
let time_cid = xous::connect(time_sid).unwrap();
|
||||||
|
time::start_time_ux(time_sid);
|
||||||
|
// this is used by the main loop to get the localtime to show on the status bar
|
||||||
|
let mut localtime = llio::LocalTime::new();
|
||||||
|
// used to hide time when the PDDB is not mounted
|
||||||
|
let pddb_poller = pddb::PddbMountPoller::new();
|
||||||
|
|
||||||
|
// used to show notifications, e.g. can't sleep while power is engaged.
|
||||||
|
let modals = modals::Modals::new(&xns).unwrap();
|
||||||
|
|
||||||
log::debug!("starting main menu thread");
|
log::debug!("starting main menu thread");
|
||||||
create_main_menu(keys.clone(), xous::connect(status_sid).unwrap(), &com);
|
create_main_menu(keys.clone(), xous::connect(status_sid).unwrap(), &com, time_cid);
|
||||||
create_app_menu(xous::connect(status_sid).unwrap());
|
create_app_menu(xous::connect(status_sid).unwrap());
|
||||||
let kbd_mgr = xous::create_server().unwrap();
|
let kbd_mgr = xous::create_server().unwrap();
|
||||||
let kbd_menumatic = create_kbd_menu(xous::connect(status_sid).unwrap(), kbd_mgr);
|
let kbd_menumatic = create_kbd_menu(xous::connect(status_sid).unwrap(), kbd_mgr);
|
||||||
let kbd = keyboard::Keyboard::new(&xns).unwrap();
|
let kbd = keyboard::Keyboard::new(&xns).unwrap();
|
||||||
|
|
||||||
// some RTC UX structures
|
|
||||||
let modals = modals::Modals::new(&xns).unwrap();
|
|
||||||
let timeserver_cid = xous::connect(xous::SID::from_bytes(crate::time::TIME_SERVER_PUBLIC).unwrap()).unwrap();
|
|
||||||
let mut localtime = llio::LocalTime::new();
|
|
||||||
let pddb_poller = pddb::PddbMountPoller::new();
|
|
||||||
|
|
||||||
log::debug!("subscribe to wifi updates");
|
log::debug!("subscribe to wifi updates");
|
||||||
netmgr.wifi_state_subscribe(cb_cid, StatusOpcode::WifiStats.to_u32().unwrap()).unwrap();
|
netmgr.wifi_state_subscribe(cb_cid, StatusOpcode::WifiStats.to_u32().unwrap()).unwrap();
|
||||||
let mut wifi_status: WlanStatus = WlanStatus::from_ipc(WlanStatusIpc::default());
|
let mut wifi_status: WlanStatus = WlanStatus::from_ipc(WlanStatusIpc::default());
|
||||||
|
|
@ -572,135 +572,6 @@ fn xmain() -> ! {
|
||||||
|
|
||||||
stats_phase = stats_phase.wrapping_add(1);
|
stats_phase = stats_phase.wrapping_add(1);
|
||||||
}
|
}
|
||||||
Some(StatusOpcode::UxSetTime) => msg_scalar_unpack!(msg, _, _, _, _, {
|
|
||||||
if !pddb_poller.is_mounted_nonblocking() {
|
|
||||||
modals.show_notification(t!("stats.please_mount", xous::LANG)).expect("couldn't show notification");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
pump_run.store(false, Ordering::Relaxed); // stop status updates while we do this
|
|
||||||
|
|
||||||
let mut tz_set_handle = pddb::Pddb::new();
|
|
||||||
let mut tz_set = false;
|
|
||||||
let mut tz_offset_ms = 0i64;
|
|
||||||
let maybe_tz_set_key = tz_set_handle.get(
|
|
||||||
time::TIME_SERVER_DICT,
|
|
||||||
time::TIME_SERVER_TZ_OFFSET,
|
|
||||||
None, false, false,
|
|
||||||
None,
|
|
||||||
None::<fn()>
|
|
||||||
).ok();
|
|
||||||
if let Some(mut tz_set_key) = maybe_tz_set_key {
|
|
||||||
let mut tz_buf = [0u8; 8];
|
|
||||||
if tz_set_key.read(&mut tz_buf).unwrap_or(0) == 8 {
|
|
||||||
tz_offset_ms = i64::from_le_bytes(tz_buf);
|
|
||||||
tz_set = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// note that we don't do an "else" here because we also want to catch the case of
|
|
||||||
// a key exists, but nothing was written to it (length of key was 0 or inappropriate)
|
|
||||||
if !tz_set {
|
|
||||||
let tz = modals.get_text(
|
|
||||||
t!("rtc.timezone", xous::LANG),
|
|
||||||
Some(tz_ux_validator), None
|
|
||||||
).expect("couldn't get timezone").as_str()
|
|
||||||
.parse::<f32>().expect("pre-validated input failed to re-parse!");
|
|
||||||
log::info!("got tz offset {}", tz);
|
|
||||||
tz_offset_ms = (tz * 3600.0 * 1000.0) as i64;
|
|
||||||
xous::send_message(timeserver_cid,
|
|
||||||
Message::new_scalar(
|
|
||||||
crate::time::TimeOp::SetTzOffsetMs.to_usize().unwrap(),
|
|
||||||
(tz_offset_ms >> 32) as usize,
|
|
||||||
(tz_offset_ms & 0xFFFF_FFFF) as usize,
|
|
||||||
0, 0,
|
|
||||||
)
|
|
||||||
).expect("couldn't set timezone");
|
|
||||||
}
|
|
||||||
|
|
||||||
let secs: u8;
|
|
||||||
let mins: u8;
|
|
||||||
let hours: u8;
|
|
||||||
let months: u8;
|
|
||||||
let days: u8;
|
|
||||||
let years: u8;
|
|
||||||
|
|
||||||
months = modals.get_text(
|
|
||||||
t!("rtc.month", xous::LANG),
|
|
||||||
Some(rtc_ux_validator), Some(ValidatorOp::UxMonth.to_u32().unwrap())
|
|
||||||
).expect("couldn't get month").as_str()
|
|
||||||
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
|
||||||
log::debug!("got months {}", months);
|
|
||||||
|
|
||||||
days = modals.get_text(
|
|
||||||
t!("rtc.day", xous::LANG),
|
|
||||||
Some(rtc_ux_validator), Some(ValidatorOp::UxDay.to_u32().unwrap())
|
|
||||||
).expect("couldn't get month").as_str()
|
|
||||||
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
|
||||||
log::debug!("got days {}", days);
|
|
||||||
|
|
||||||
years = modals.get_text(
|
|
||||||
t!("rtc.year", xous::LANG),
|
|
||||||
Some(rtc_ux_validator), Some(ValidatorOp::UxYear.to_u32().unwrap())
|
|
||||||
).expect("couldn't get month").as_str()
|
|
||||||
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
|
||||||
log::debug!("got years {}", years);
|
|
||||||
|
|
||||||
hours = modals.get_text(
|
|
||||||
t!("rtc.hour", xous::LANG),
|
|
||||||
Some(rtc_ux_validator), Some(ValidatorOp::UxHour.to_u32().unwrap())
|
|
||||||
).expect("couldn't get hour").as_str()
|
|
||||||
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
|
||||||
log::debug!("got hours {}", hours);
|
|
||||||
|
|
||||||
mins = modals.get_text(
|
|
||||||
t!("rtc.minute", xous::LANG),
|
|
||||||
Some(rtc_ux_validator), Some(ValidatorOp::UxMinute.to_u32().unwrap())
|
|
||||||
).expect("couldn't get minutes").as_str()
|
|
||||||
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
|
||||||
log::debug!("got minutes {}", mins);
|
|
||||||
|
|
||||||
secs = modals.get_text(
|
|
||||||
t!("rtc.seconds", xous::LANG),
|
|
||||||
Some(rtc_ux_validator), Some(ValidatorOp::UxSeconds.to_u32().unwrap())
|
|
||||||
).expect("couldn't get seconds").as_str()
|
|
||||||
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
|
||||||
log::debug!("got seconds {}", secs);
|
|
||||||
|
|
||||||
log::info!("Setting time: {}/{}/{} {}:{}:{}", months, days, years, hours, mins, secs);
|
|
||||||
let new_dt = chrono::FixedOffset::east((tz_offset_ms / 1000) as i32).ymd(years as i32 + 2000, months as u32, days as u32)
|
|
||||||
.and_hms(hours as u32, mins as u32, secs as u32);
|
|
||||||
xous::send_message(timeserver_cid,
|
|
||||||
Message::new_scalar(
|
|
||||||
crate::time::TimeOp::SetUtcTimeMs.to_usize().unwrap(),
|
|
||||||
((new_dt.timestamp_millis() as u64) >> 32) as usize,
|
|
||||||
(new_dt.timestamp_millis() as u64 & 0xFFFF_FFFF) as usize,
|
|
||||||
0, 0,
|
|
||||||
)
|
|
||||||
).expect("couldn't set time");
|
|
||||||
pump_run.store(true, Ordering::Relaxed); // stop status updates while we do this
|
|
||||||
}),
|
|
||||||
Some(StatusOpcode::UxSetTimeZone) => {
|
|
||||||
if !pddb_poller.is_mounted_nonblocking() {
|
|
||||||
modals.show_notification(t!("stats.please_mount", xous::LANG)).expect("couldn't show notification");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
pump_run.store(false, Ordering::Relaxed); // stop status updates while we do this
|
|
||||||
let tz = modals.get_text(
|
|
||||||
t!("rtc.timezone", xous::LANG),
|
|
||||||
Some(tz_ux_validator), None
|
|
||||||
).expect("couldn't get timezone").as_str()
|
|
||||||
.parse::<f32>().expect("pre-validated input failed to re-parse!");
|
|
||||||
log::info!("got tz offset {}", tz);
|
|
||||||
let tzoff_ms = (tz * 3600.0 * 1000.0) as i64;
|
|
||||||
xous::send_message(timeserver_cid,
|
|
||||||
Message::new_scalar(
|
|
||||||
crate::time::TimeOp::SetTzOffsetMs.to_usize().unwrap(),
|
|
||||||
(tzoff_ms >> 32) as usize,
|
|
||||||
(tzoff_ms & 0xFFFF_FFFF) as usize,
|
|
||||||
0, 0,
|
|
||||||
)
|
|
||||||
).expect("couldn't set timezone");
|
|
||||||
pump_run.store(true, Ordering::Relaxed); // stop status updates while we do this
|
|
||||||
},
|
|
||||||
Some(StatusOpcode::Reboot) => {
|
Some(StatusOpcode::Reboot) => {
|
||||||
if ((llio.adc_vbus().unwrap() as f64) * 0.005033) > 1.5 {
|
if ((llio.adc_vbus().unwrap() as f64) * 0.005033) > 1.5 {
|
||||||
// power plugged in, do a reboot using a warm boot method
|
// power plugged in, do a reboot using a warm boot method
|
||||||
|
|
@ -806,70 +677,3 @@ fn xmain() -> ! {
|
||||||
xous::terminate_process(0)
|
xous::terminate_process(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RTC helper functions
|
|
||||||
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
|
||||||
pub(crate) enum ValidatorOp {
|
|
||||||
UxMonth,
|
|
||||||
UxDay,
|
|
||||||
UxYear,
|
|
||||||
UxHour,
|
|
||||||
UxMinute,
|
|
||||||
UxSeconds,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tz_ux_validator(input: TextEntryPayload, _opcode: u32) -> Option<ValidatorErr> {
|
|
||||||
let text_str = input.as_str();
|
|
||||||
match text_str.parse::<f32>() {
|
|
||||||
Ok(input) => if input < -12.0 || input > 14.0 {
|
|
||||||
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)));
|
|
||||||
},
|
|
||||||
_ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))),
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rtc_ux_validator(input: TextEntryPayload, opcode: u32) -> Option<ValidatorErr> {
|
|
||||||
let text_str = input.as_str();
|
|
||||||
let input_int = match text_str.parse::<u32>() {
|
|
||||||
Ok(input_int) => input_int,
|
|
||||||
_ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))),
|
|
||||||
};
|
|
||||||
log::trace!("validating input {}, parsed as {} for opcode {}", text_str, input_int, opcode);
|
|
||||||
match FromPrimitive::from_u32(opcode) {
|
|
||||||
Some(ValidatorOp::UxMonth) => {
|
|
||||||
if input_int < 1 || input_int > 12 {
|
|
||||||
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(ValidatorOp::UxDay) => {
|
|
||||||
if input_int < 1 || input_int > 31 {
|
|
||||||
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(ValidatorOp::UxYear) => {
|
|
||||||
if input_int > 99 {
|
|
||||||
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(ValidatorOp::UxHour) => {
|
|
||||||
if input_int > 23 {
|
|
||||||
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(ValidatorOp::UxMinute) => {
|
|
||||||
if input_int > 59 {
|
|
||||||
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(ValidatorOp::UxSeconds) => {
|
|
||||||
if input_int > 59 {
|
|
||||||
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
log::error!("internal error: invalid opcode was sent to validator: {:?}", opcode);
|
|
||||||
panic!("internal error: invalid opcode was sent to validator");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use num_traits::*;
|
||||||
use crate::StatusOpcode;
|
use crate::StatusOpcode;
|
||||||
|
|
||||||
#[allow(unused_variables)] // quiets a warning about unused com that is emitted in tts config. Would be nice to make this more targeted...
|
#[allow(unused_variables)] // quiets a warning about unused com that is emitted in tts config. Would be nice to make this more targeted...
|
||||||
pub fn create_main_menu(keys: Arc<Mutex<RootKeys>>, status_conn: xous::CID, com: &com::Com) {
|
pub fn create_main_menu(keys: Arc<Mutex<RootKeys>>, status_conn: xous::CID, com: &com::Com, time_ux_conn: xous::CID) {
|
||||||
let key_conn = keys.lock().unwrap().conn();
|
let key_conn = keys.lock().unwrap().conn();
|
||||||
|
|
||||||
let mut menuitems = Vec::<MenuItem>::new();
|
let mut menuitems = Vec::<MenuItem>::new();
|
||||||
|
|
@ -76,15 +76,15 @@ pub fn create_main_menu(keys: Arc<Mutex<RootKeys>>, status_conn: xous::CID, com:
|
||||||
|
|
||||||
menuitems.push(MenuItem {
|
menuitems.push(MenuItem {
|
||||||
name: String::from_str(t!("mainmenu.set_rtc", xous::LANG)),
|
name: String::from_str(t!("mainmenu.set_rtc", xous::LANG)),
|
||||||
action_conn: Some(status_conn),
|
action_conn: Some(time_ux_conn),
|
||||||
action_opcode: StatusOpcode::UxSetTime.to_u32().unwrap(),
|
action_opcode: crate::time::TimeUxOp::SetTime.to_u32().unwrap(),
|
||||||
action_payload: MenuPayload::Scalar([0, 0, 0, 0]),
|
action_payload: MenuPayload::Scalar([0, 0, 0, 0]),
|
||||||
close_on_select: true,
|
close_on_select: true,
|
||||||
});
|
});
|
||||||
menuitems.push(MenuItem {
|
menuitems.push(MenuItem {
|
||||||
name: String::from_str(t!("mainmenu.set_tz", xous::LANG)),
|
name: String::from_str(t!("mainmenu.set_tz", xous::LANG)),
|
||||||
action_conn: Some(status_conn),
|
action_conn: Some(time_ux_conn),
|
||||||
action_opcode: StatusOpcode::UxSetTimeZone.to_u32().unwrap(),
|
action_opcode: crate::time::TimeUxOp::SetTimeZone.to_u32().unwrap(),
|
||||||
action_payload: MenuPayload::Scalar([0, 0, 0, 0]),
|
action_payload: MenuPayload::Scalar([0, 0, 0, 0]),
|
||||||
close_on_select: true,
|
close_on_select: true,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,11 @@ use llio::*;
|
||||||
use pddb::{Pddb, PddbMountPoller};
|
use pddb::{Pddb, PddbMountPoller};
|
||||||
use std::io::{Read, Write, Seek, SeekFrom};
|
use std::io::{Read, Write, Seek, SeekFrom};
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
|
// imports for time ux
|
||||||
|
use locales::t;
|
||||||
|
use chrono::prelude::*;
|
||||||
|
use xous::Message;
|
||||||
|
use gam::modal::*;
|
||||||
|
|
||||||
/// This is a "well known name" used by `libstd` to connect to the time server
|
/// This is a "well known name" used by `libstd` to connect to the time server
|
||||||
/// Even thought it is "public" nobody connects to it directly, they connect to it via `libstd`
|
/// Even thought it is "public" nobody connects to it directly, they connect to it via `libstd`
|
||||||
|
|
@ -85,6 +90,13 @@ pub(crate) enum PrivTimeOp {
|
||||||
ResetRtc = 0,
|
ResetRtc = 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
||||||
|
pub(crate) enum TimeUxOp {
|
||||||
|
SetTime,
|
||||||
|
SetTimeZone,
|
||||||
|
Quit,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn start_time_server() {
|
pub fn start_time_server() {
|
||||||
let rtc_checked = Arc::new(AtomicBool::new(false));
|
let rtc_checked = Arc::new(AtomicBool::new(false));
|
||||||
|
|
||||||
|
|
@ -419,3 +431,221 @@ fn to_bcd(binary: u8) -> u8 {
|
||||||
|
|
||||||
(msd << 4) | lsd
|
(msd << 4) | lsd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn start_time_ux(sid: xous::SID) {
|
||||||
|
thread::spawn({
|
||||||
|
move || {
|
||||||
|
// some RTC UX structures
|
||||||
|
let xns = xous_names::XousNames::new().unwrap();
|
||||||
|
let modals = modals::Modals::new(&xns).unwrap();
|
||||||
|
let timeserver_cid = xous::connect(xous::SID::from_bytes(crate::time::TIME_SERVER_PUBLIC).unwrap()).unwrap();
|
||||||
|
let pddb_poller = pddb::PddbMountPoller::new();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let msg = xous::receive_message(sid).unwrap();
|
||||||
|
match FromPrimitive::from_usize(msg.body.id()) {
|
||||||
|
Some(TimeUxOp::SetTime) => xous::msg_scalar_unpack!(msg, _, _, _, _, {
|
||||||
|
if !pddb_poller.is_mounted_nonblocking() {
|
||||||
|
modals.show_notification(t!("stats.please_mount", xous::LANG)).expect("couldn't show notification");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let mut tz_set_handle = pddb::Pddb::new();
|
||||||
|
let mut tz_set = false;
|
||||||
|
let mut tz_offset_ms = 0i64;
|
||||||
|
let maybe_tz_set_key = tz_set_handle.get(
|
||||||
|
TIME_SERVER_DICT,
|
||||||
|
TIME_SERVER_TZ_OFFSET,
|
||||||
|
None, false, false,
|
||||||
|
None,
|
||||||
|
None::<fn()>
|
||||||
|
).ok();
|
||||||
|
if let Some(mut tz_set_key) = maybe_tz_set_key {
|
||||||
|
let mut tz_buf = [0u8; 8];
|
||||||
|
if tz_set_key.read(&mut tz_buf).unwrap_or(0) == 8 {
|
||||||
|
tz_offset_ms = i64::from_le_bytes(tz_buf);
|
||||||
|
tz_set = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// note that we don't do an "else" here because we also want to catch the case of
|
||||||
|
// a key exists, but nothing was written to it (length of key was 0 or inappropriate)
|
||||||
|
if !tz_set {
|
||||||
|
let tz = modals.get_text(
|
||||||
|
t!("rtc.timezone", xous::LANG),
|
||||||
|
Some(tz_ux_validator), None
|
||||||
|
).expect("couldn't get timezone").as_str()
|
||||||
|
.parse::<f32>().expect("pre-validated input failed to re-parse!");
|
||||||
|
log::info!("got tz offset {}", tz);
|
||||||
|
tz_offset_ms = (tz * 3600.0 * 1000.0) as i64;
|
||||||
|
xous::send_message(timeserver_cid,
|
||||||
|
Message::new_scalar(
|
||||||
|
crate::time::TimeOp::SetTzOffsetMs.to_usize().unwrap(),
|
||||||
|
(tz_offset_ms >> 32) as usize,
|
||||||
|
(tz_offset_ms & 0xFFFF_FFFF) as usize,
|
||||||
|
0, 0,
|
||||||
|
)
|
||||||
|
).expect("couldn't set timezone");
|
||||||
|
}
|
||||||
|
|
||||||
|
let secs: u8;
|
||||||
|
let mins: u8;
|
||||||
|
let hours: u8;
|
||||||
|
let months: u8;
|
||||||
|
let days: u8;
|
||||||
|
let years: u8;
|
||||||
|
|
||||||
|
months = modals.get_text(
|
||||||
|
t!("rtc.month", xous::LANG),
|
||||||
|
Some(rtc_ux_validator), Some(ValidatorOp::UxMonth.to_u32().unwrap())
|
||||||
|
).expect("couldn't get month").as_str()
|
||||||
|
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
||||||
|
log::debug!("got months {}", months);
|
||||||
|
|
||||||
|
days = modals.get_text(
|
||||||
|
t!("rtc.day", xous::LANG),
|
||||||
|
Some(rtc_ux_validator), Some(ValidatorOp::UxDay.to_u32().unwrap())
|
||||||
|
).expect("couldn't get month").as_str()
|
||||||
|
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
||||||
|
log::debug!("got days {}", days);
|
||||||
|
|
||||||
|
years = modals.get_text(
|
||||||
|
t!("rtc.year", xous::LANG),
|
||||||
|
Some(rtc_ux_validator), Some(ValidatorOp::UxYear.to_u32().unwrap())
|
||||||
|
).expect("couldn't get month").as_str()
|
||||||
|
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
||||||
|
log::debug!("got years {}", years);
|
||||||
|
|
||||||
|
hours = modals.get_text(
|
||||||
|
t!("rtc.hour", xous::LANG),
|
||||||
|
Some(rtc_ux_validator), Some(ValidatorOp::UxHour.to_u32().unwrap())
|
||||||
|
).expect("couldn't get hour").as_str()
|
||||||
|
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
||||||
|
log::debug!("got hours {}", hours);
|
||||||
|
|
||||||
|
mins = modals.get_text(
|
||||||
|
t!("rtc.minute", xous::LANG),
|
||||||
|
Some(rtc_ux_validator), Some(ValidatorOp::UxMinute.to_u32().unwrap())
|
||||||
|
).expect("couldn't get minutes").as_str()
|
||||||
|
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
||||||
|
log::debug!("got minutes {}", mins);
|
||||||
|
|
||||||
|
secs = modals.get_text(
|
||||||
|
t!("rtc.seconds", xous::LANG),
|
||||||
|
Some(rtc_ux_validator), Some(ValidatorOp::UxSeconds.to_u32().unwrap())
|
||||||
|
).expect("couldn't get seconds").as_str()
|
||||||
|
.parse::<u8>().expect("pre-validated input failed to re-parse!");
|
||||||
|
log::debug!("got seconds {}", secs);
|
||||||
|
|
||||||
|
log::info!("Setting time: {}/{}/{} {}:{}:{}", months, days, years, hours, mins, secs);
|
||||||
|
let new_dt = chrono::FixedOffset::east((tz_offset_ms / 1000) as i32).ymd(years as i32 + 2000, months as u32, days as u32)
|
||||||
|
.and_hms(hours as u32, mins as u32, secs as u32);
|
||||||
|
xous::send_message(timeserver_cid,
|
||||||
|
Message::new_scalar(
|
||||||
|
crate::time::TimeOp::SetUtcTimeMs.to_usize().unwrap(),
|
||||||
|
((new_dt.timestamp_millis() as u64) >> 32) as usize,
|
||||||
|
(new_dt.timestamp_millis() as u64 & 0xFFFF_FFFF) as usize,
|
||||||
|
0, 0,
|
||||||
|
)
|
||||||
|
).expect("couldn't set time");
|
||||||
|
}),
|
||||||
|
Some(TimeUxOp::SetTimeZone) => xous::msg_scalar_unpack!(msg, _, _, _, _, {
|
||||||
|
if !pddb_poller.is_mounted_nonblocking() {
|
||||||
|
modals.show_notification(t!("stats.please_mount", xous::LANG)).expect("couldn't show notification");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let tz = modals.get_text(
|
||||||
|
t!("rtc.timezone", xous::LANG),
|
||||||
|
Some(tz_ux_validator), None
|
||||||
|
).expect("couldn't get timezone").as_str()
|
||||||
|
.parse::<f32>().expect("pre-validated input failed to re-parse!");
|
||||||
|
log::info!("got tz offset {}", tz);
|
||||||
|
let tzoff_ms = (tz * 3600.0 * 1000.0) as i64;
|
||||||
|
xous::send_message(timeserver_cid,
|
||||||
|
Message::new_scalar(
|
||||||
|
crate::time::TimeOp::SetTzOffsetMs.to_usize().unwrap(),
|
||||||
|
(tzoff_ms >> 32) as usize,
|
||||||
|
(tzoff_ms & 0xFFFF_FFFF) as usize,
|
||||||
|
0, 0,
|
||||||
|
)
|
||||||
|
).expect("couldn't set timezone");
|
||||||
|
}),
|
||||||
|
Some(TimeUxOp::Quit) => {
|
||||||
|
xous::return_scalar(msg.sender, 0).unwrap();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
log::warn!("unhandled opcode: {:?}", msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xous::destroy_server(sid).ok();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// RTC Ux helper functions
|
||||||
|
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||||
|
pub(crate) enum ValidatorOp {
|
||||||
|
UxMonth,
|
||||||
|
UxDay,
|
||||||
|
UxYear,
|
||||||
|
UxHour,
|
||||||
|
UxMinute,
|
||||||
|
UxSeconds,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tz_ux_validator(input: TextEntryPayload, _opcode: u32) -> Option<ValidatorErr> {
|
||||||
|
let text_str = input.as_str();
|
||||||
|
match text_str.parse::<f32>() {
|
||||||
|
Ok(input) => if input < -12.0 || input > 14.0 {
|
||||||
|
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)));
|
||||||
|
},
|
||||||
|
_ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))),
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rtc_ux_validator(input: TextEntryPayload, opcode: u32) -> Option<ValidatorErr> {
|
||||||
|
let text_str = input.as_str();
|
||||||
|
let input_int = match text_str.parse::<u32>() {
|
||||||
|
Ok(input_int) => input_int,
|
||||||
|
_ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))),
|
||||||
|
};
|
||||||
|
log::trace!("validating input {}, parsed as {} for opcode {}", text_str, input_int, opcode);
|
||||||
|
match FromPrimitive::from_u32(opcode) {
|
||||||
|
Some(ValidatorOp::UxMonth) => {
|
||||||
|
if input_int < 1 || input_int > 12 {
|
||||||
|
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(ValidatorOp::UxDay) => {
|
||||||
|
if input_int < 1 || input_int > 31 {
|
||||||
|
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(ValidatorOp::UxYear) => {
|
||||||
|
if input_int > 99 {
|
||||||
|
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(ValidatorOp::UxHour) => {
|
||||||
|
if input_int > 23 {
|
||||||
|
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(ValidatorOp::UxMinute) => {
|
||||||
|
if input_int > 59 {
|
||||||
|
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(ValidatorOp::UxSeconds) => {
|
||||||
|
if input_int > 59 {
|
||||||
|
return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
log::error!("internal error: invalid opcode was sent to validator: {:?}", opcode);
|
||||||
|
panic!("internal error: invalid opcode was sent to validator");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue