mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
feat: move timezone and utc offset into Preference lib
This commit is contained in:
parent
6176987fec
commit
dd5928a7c3
2 changed files with 90 additions and 64 deletions
|
|
@ -5,6 +5,13 @@ use std::io::Write;
|
||||||
|
|
||||||
static PREFS_DICT: &str = "UserPrefsDict";
|
static PREFS_DICT: &str = "UserPrefsDict";
|
||||||
|
|
||||||
|
// Time-related consts
|
||||||
|
|
||||||
|
/// This is the UTC offset from the current hardware RTC reading. This should be fixed once time is set.
|
||||||
|
const TIME_SERVER_UTC_OFFSET: &'static str = "utc_offset";
|
||||||
|
/// This is the offset from UTC to the display time zone. This can vary when the user changes time zones.
|
||||||
|
const TIME_SERVER_TZ_OFFSET: &'static str = "tz_offset";
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
EncodeError(EncodeError),
|
EncodeError(EncodeError),
|
||||||
|
|
@ -91,6 +98,50 @@ impl Manager {
|
||||||
Err(e) => return Err(e.into()),
|
Err(e) => return Err(e.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn store_i64(&self, value: i64, key: &str) -> Result<(), Error> {
|
||||||
|
let offset_bytes = value.to_le_bytes();
|
||||||
|
|
||||||
|
self.pddb_store_key(key, &offset_bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This impl block is here because some toggles/data (like date/time stuff) needs particular
|
||||||
|
// serialization/deserialization routines.
|
||||||
|
impl Manager {
|
||||||
|
pub fn timezone_offset(&self) -> Result<Option<i64>, Error> {
|
||||||
|
let tz_set_key = self.pddb_get_key(TIME_SERVER_TZ_OFFSET)?;
|
||||||
|
|
||||||
|
if tz_set_key.len() != 8 {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let sl = &tz_set_key[..];
|
||||||
|
let sl: [u8; 8] = sl.try_into().unwrap();
|
||||||
|
|
||||||
|
return Ok(Some(i64::from_le_bytes(sl)));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_timezone_offset(&self, offset: i64) -> Result<(), Error> {
|
||||||
|
self.store_i64(offset, TIME_SERVER_TZ_OFFSET)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn utc_offset(&self) -> Result<i64, Error> {
|
||||||
|
let utc_set_key = self.pddb_get_key(TIME_SERVER_UTC_OFFSET)?;
|
||||||
|
|
||||||
|
if utc_set_key.len() != 8 {
|
||||||
|
return Ok(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
let sl = &utc_set_key[..];
|
||||||
|
let sl: [u8; 8] = sl.try_into().unwrap();
|
||||||
|
|
||||||
|
return Ok(i64::from_le_bytes(sl));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_utc_offset(&self, offset: i64) -> Result<(), Error> {
|
||||||
|
self.store_i64(offset, TIME_SERVER_UTC_OFFSET)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Manager {
|
impl Default for Manager {
|
||||||
|
|
|
||||||
|
|
@ -25,8 +25,7 @@ use std::thread;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use llio::*;
|
use llio::*;
|
||||||
use pddb::{Pddb, PddbMountPoller};
|
use pddb::PddbMountPoller;
|
||||||
use std::io::{Read, Write, Seek, SeekFrom};
|
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
// imports for time ux
|
// imports for time ux
|
||||||
use locales::t;
|
use locales::t;
|
||||||
|
|
@ -42,13 +41,6 @@ use std::num::ParseIntError;
|
||||||
/// Anyone who wants to check if time has been initialized would use this name.
|
/// Anyone who wants to check if time has been initialized would use this name.
|
||||||
pub const TIME_SERVER_PUBLIC: &'static [u8; 16] = b"timeserverpublic";
|
pub const TIME_SERVER_PUBLIC: &'static [u8; 16] = b"timeserverpublic";
|
||||||
|
|
||||||
/// Dictionary for RTC settings.
|
|
||||||
pub(crate) const TIME_SERVER_DICT: &'static str = "sys.rtc";
|
|
||||||
/// This is the UTC offset from the current hardware RTC reading. This should be fixed once time is set.
|
|
||||||
const TIME_SERVER_UTC_OFFSET: &'static str = "utc_offset";
|
|
||||||
/// This is the offset from UTC to the display time zone. This can vary when the user changes time zones.
|
|
||||||
pub(crate) const TIME_SERVER_TZ_OFFSET: &'static str = "tz_offset";
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
const CTL3: usize = 0;
|
const CTL3: usize = 0;
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|
@ -249,9 +241,8 @@ pub fn start_time_server() {
|
||||||
move || {
|
move || {
|
||||||
let xns = xous_names::XousNames::new().unwrap();
|
let xns = xous_names::XousNames::new().unwrap();
|
||||||
let llio = llio::Llio::new(&xns);
|
let llio = llio::Llio::new(&xns);
|
||||||
|
let prefs = userprefs::Manager::new();
|
||||||
|
|
||||||
let mut utc_offset_ms = 0i64;
|
|
||||||
let mut tz_offset_ms = 0i64;
|
|
||||||
let tt = ticktimer_server::Ticktimer::new().unwrap();
|
let tt = ticktimer_server::Ticktimer::new().unwrap();
|
||||||
// this routine can't proceed until the RTC has passed its power-on sanity checks
|
// this routine can't proceed until the RTC has passed its power-on sanity checks
|
||||||
while !rtc_checked.load(Ordering::SeqCst) {
|
while !rtc_checked.load(Ordering::SeqCst) {
|
||||||
|
|
@ -320,32 +311,17 @@ pub fn start_time_server() {
|
||||||
_ => log::warn!("Time server can't handle this message yet: {:?}", msg),
|
_ => log::warn!("Time server can't handle this message yet: {:?}", msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// once the PDDB is mounted, read in the time zone offsets and then restart the
|
|
||||||
// loop handler using the offsets.
|
let mut utc_offset_ms = prefs.utc_offset().unwrap_or_else(|error| {
|
||||||
let offset_handle = Pddb::new();
|
log::error!("cannot read utc offset: {:?}", error);
|
||||||
let mut offset_key = offset_handle.get(
|
0
|
||||||
TIME_SERVER_DICT,
|
});
|
||||||
TIME_SERVER_UTC_OFFSET,
|
|
||||||
Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), true, true,
|
let mut tz_offset_ms = prefs.timezone_offset().unwrap_or_else(|error| {
|
||||||
Some(8),
|
log::error!("cannot read timezone offset: {:?}", error);
|
||||||
None::<fn()>
|
None
|
||||||
).expect("couldn't open UTC offset key");
|
}).unwrap_or_default();
|
||||||
let tz_handle = Pddb::new();
|
|
||||||
let mut tz_key = tz_handle.get(
|
|
||||||
TIME_SERVER_DICT,
|
|
||||||
TIME_SERVER_TZ_OFFSET,
|
|
||||||
Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), true, true,
|
|
||||||
Some(8),
|
|
||||||
None::<fn()>
|
|
||||||
).expect("couldn't open TZ offset key");
|
|
||||||
let mut offset_buf = [0u8; 8];
|
|
||||||
if offset_key.read(&mut offset_buf).unwrap_or(0) == 8 {
|
|
||||||
utc_offset_ms = i64::from_le_bytes(offset_buf);
|
|
||||||
} // else 0 is the error value, so leave it at that.
|
|
||||||
let mut tz_buf = [0u8; 8];
|
|
||||||
if tz_key.read(&mut tz_buf).unwrap_or(0) == 8 {
|
|
||||||
tz_offset_ms = i64::from_le_bytes(tz_buf);
|
|
||||||
}
|
|
||||||
log::debug!("offset_key: {}", utc_offset_ms / 1000);
|
log::debug!("offset_key: {}", utc_offset_ms / 1000);
|
||||||
log::debug!("tz_key: {}", tz_offset_ms / 1000);
|
log::debug!("tz_key: {}", tz_offset_ms / 1000);
|
||||||
log::debug!("start_rtc_secs: {}", start_rtc_secs);
|
log::debug!("start_rtc_secs: {}", start_rtc_secs);
|
||||||
|
|
@ -411,12 +387,10 @@ pub fn start_time_server() {
|
||||||
utc_time_ms -
|
utc_time_ms -
|
||||||
(start_rtc_secs as i64) * 1000;
|
(start_rtc_secs as i64) * 1000;
|
||||||
utc_offset_ms = offset;
|
utc_offset_ms = offset;
|
||||||
offset_key.seek(SeekFrom::Start(0)).expect("couldn't seek");
|
|
||||||
log::info!("setting offset to {} secs", offset / 1000);
|
prefs.set_utc_offset(offset).unwrap_or_else(|err| {
|
||||||
if offset_key.write(&offset.to_le_bytes()).unwrap_or(0) != 8 {
|
log::error!("cannot set utc offset: {:?}", err);
|
||||||
log::warn!("Couldn't commit the latest time update. Is the PDDB mounted?");
|
});
|
||||||
}
|
|
||||||
offset_key.flush().expect("couldn't flush PDDB");
|
|
||||||
}),
|
}),
|
||||||
Some(TimeOp::SetTzOffsetMs) => xous::msg_scalar_unpack!(msg, tz_hi_ms, tz_lo_ms, _, _, {
|
Some(TimeOp::SetTzOffsetMs) => xous::msg_scalar_unpack!(msg, tz_hi_ms, tz_lo_ms, _, _, {
|
||||||
let tz_ms = ((tz_hi_ms as i64) << 32) | (tz_lo_ms as i64);
|
let tz_ms = ((tz_hi_ms as i64) << 32) | (tz_lo_ms as i64);
|
||||||
|
|
@ -428,12 +402,10 @@ pub fn start_time_server() {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
tz_offset_ms = tz_ms;
|
tz_offset_ms = tz_ms;
|
||||||
tz_key.seek(SeekFrom::Start(0)).expect("couldn't seek");
|
|
||||||
log::info!("setting tz offset to {} secs", tz_ms / 1000);
|
prefs.set_timezone_offset(tz_ms).unwrap_or_else(|err| {
|
||||||
if tz_key.write(&tz_ms.to_le_bytes()).unwrap_or(0) != 8 {
|
log::error!("cannot set timezone offset: {:?}", err);
|
||||||
log::warn!("Couldn't commit the latest timezone update. Is the PDDB mounted?");
|
});
|
||||||
}
|
|
||||||
tz_key.flush().expect("couldn't flush PDDB");
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
Some(TimeOp::WallClockTimeInit) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
Some(TimeOp::WallClockTimeInit) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
||||||
|
|
@ -541,6 +513,8 @@ pub fn start_time_ux(sid: xous::SID) {
|
||||||
let pddb_poller = pddb::PddbMountPoller::new();
|
let pddb_poller = pddb::PddbMountPoller::new();
|
||||||
let trng = trng::Trng::new(&xns).unwrap();
|
let trng = trng::Trng::new(&xns).unwrap();
|
||||||
|
|
||||||
|
let prefs = userprefs::Manager::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let msg = xous::receive_message(sid).unwrap();
|
let msg = xous::receive_message(sid).unwrap();
|
||||||
match FromPrimitive::from_usize(msg.body.id()) {
|
match FromPrimitive::from_usize(msg.body.id()) {
|
||||||
|
|
@ -549,23 +523,24 @@ pub fn start_time_ux(sid: xous::SID) {
|
||||||
modals.show_notification(t!("stats.please_mount", xous::LANG), None).expect("couldn't show notification");
|
modals.show_notification(t!("stats.please_mount", xous::LANG), None).expect("couldn't show notification");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let tz_set_handle = pddb::Pddb::new();
|
|
||||||
let mut tz_set = false;
|
let mut tz_set = false;
|
||||||
let mut tz_offset_ms = 0i64;
|
let mut tz_offset_ms = match prefs.timezone_offset() {
|
||||||
let maybe_tz_set_key = tz_set_handle.get(
|
Ok(offset) => match offset {
|
||||||
TIME_SERVER_DICT,
|
Some(data) => {
|
||||||
TIME_SERVER_TZ_OFFSET,
|
tz_set = true;
|
||||||
Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS), false, false,
|
data
|
||||||
None,
|
},
|
||||||
None::<fn()>
|
None => {
|
||||||
).ok();
|
tz_set = false;
|
||||||
if let Some(mut tz_set_key) = maybe_tz_set_key {
|
0i64
|
||||||
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);
|
Err(error) => {
|
||||||
tz_set = true;
|
log::error!("cannot read timezone offset: {:?}", error);
|
||||||
|
0i64
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// note that we don't do an "else" here because we also want to catch the case of
|
// 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)
|
// a key exists, but nothing was written to it (length of key was 0 or inappropriate)
|
||||||
if !tz_set {
|
if !tz_set {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue