From 2751aa03d38c345dac53d5841f194e4e4de32940 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 27 Mar 2022 04:08:36 +0800 Subject: [PATCH] add a localtime server plus other annotations std::time::SystemTime returns time in UTC. That's great, but a lot of times we want local time. Unfortunately, we can't use `chrono` to do that, because chrono figures out what your time zone is usind `libc`, which is a thing Xous doesn't have. the `LocalTime` server is Xous' solution to that problem. it returns the corret time offset based on the time zone. You can still use `chrono` to convert the returned time into strings and to compute durations. --- Cargo.lock | 1 + services/llio/locales/i18n.json | 20 ++++++++--- services/llio/src/lib.rs | 59 +++++++++++++++++++++++++++++++++ services/llio/src/main.rs | 27 +++++++++++---- 4 files changed, 96 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 77ceadb0f..e75750994 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2566,6 +2566,7 @@ name = "shellchat" version = "0.1.0" dependencies = [ "aes", + "chrono", "cipher", "codec", "com", diff --git a/services/llio/locales/i18n.json b/services/llio/locales/i18n.json index ca626853d..0b3289f91 100644 --- a/services/llio/locales/i18n.json +++ b/services/llio/locales/i18n.json @@ -17,11 +17,17 @@ "zh": "输入年份 (最后两位数)", "en-tts": "Enter last two digits of year" }, + "rtc.bodge": { + "en": "Enter the system time in ⚠UTC⚠.\nThis is likely not the same thing as your local time.", + "ja": "システム時刻を⚠UTC⚠に入力します。\nこれは現地時間と同じではない可能性があります。", + "zh": "以⚠UTC⚠输入系统时间。\n这可能与您的本地时间不同。", + "en-tts": "Enter the system time in ⚠UTC⚠.\nThis is likely not the same thing as your local time." + }, "rtc.hour": { - "en": "Enter hours (0-23)", - "ja": "時間(0-23)を入力してください。", - "zh": "输入小时 (0-23)", - "en-tts": "Enter hours as 24 hour format" + "en": "Enter hours for UTC (0-23)", + "ja": "UTC時間(0-23)を入力してください。", + "zh": "输入UTC小时 (0-23)", + "en-tts": "Enter hours for UTC as 24 hour format" }, "rtc.minute": { "en": "Enter minutes (0-59)", @@ -83,6 +89,12 @@ "zh": "星期日", "en-tts": "Sunday" }, + "rtc.timezone": { + "en": "Please enter your local offset from UTC in hours (-12.0 to +14.0 hours).\nNote: this offset does not yet track daylight savings.", + "ja": "UTCからのローカルオフセットを時間単位で入力してください(-12.0〜 + 14.0時間):", + "zh": "请以小时为单位输入您与 UTC 的本地偏移量(-12.0 到 +14.0 小时):", + "en-tts": "Please enter your local offset from UTC in hours (-12.0 to +14.0 hours):" + }, "rtc.integer_err": { "en": "Error: entry was not numeric", "ja": "エラー:エントリは数値ではありませんでした。", diff --git a/services/llio/src/lib.rs b/services/llio/src/lib.rs index 0fa580829..627069da9 100644 --- a/services/llio/src/lib.rs +++ b/services/llio/src/lib.rs @@ -9,3 +9,62 @@ pub mod i2c_lib; pub use i2c_lib::I2c; pub mod llio_lib; pub use llio_lib::Llio; + +use core::sync::atomic::{AtomicU32, Ordering}; +static TIME_REFCOUNT: AtomicU32 = AtomicU32::new(0); + +pub struct LocalTime { + conn: xous::CID, +} +impl LocalTime { + pub fn new() -> LocalTime { + TIME_REFCOUNT.fetch_add(1, Ordering::Relaxed); + let conn = xous::connect(xous::SID::from_bytes(b"timeserverpublic").unwrap()).unwrap(); + LocalTime { + conn, + } + } + /// Returns the local time as milliseconds since EPOCH, assuming the time zone is set + /// This is provided because we don't have a `libc` to do time zone lookups with `Chrono`. + pub fn get_local_time_ms(&self) -> Option { + match xous::send_message(self.conn, + xous::Message::new_blocking_scalar( + 6, // WallClockTimeInit -- this should not change because it's a libstd mapping + 0, 0, 0, 0 + ) + ).expect("couldn't get init status") { + xous::Result::Scalar1(is_init) => { + if is_init == 0 { + log::warn!("Time offsets are not initialized, can't report local time"); + return None; + } + } + _ => { + log::error!("error retrieving time"); + return None; + } + } + match xous::send_message(self.conn, + xous::Message::new_blocking_scalar( + 4, + 0, 0, 0, 0 + ) + ).expect("couldn't get time") { + xous::Result::Scalar2(hi, lo) => { + Some((hi as u64) << 32 | (lo as u64)) + } + _ => { + log::error!("error retrieving time"); + return None; + } + } + } + // Note: to get the UTC time since EPOCH, use the std::SystemTime::now() +} +impl Drop for LocalTime { + fn drop(&mut self) { + if TIME_REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { + unsafe{xous::disconnect(self.conn).unwrap();} + } + } +} \ No newline at end of file diff --git a/services/llio/src/main.rs b/services/llio/src/main.rs index 382854b40..d0732af35 100644 --- a/services/llio/src/main.rs +++ b/services/llio/src/main.rs @@ -123,7 +123,7 @@ fn xmain() -> ! { // create the I2C handler thread // - codec - // - rtc + // - time server // - llio // I2C can be used to set time, which can have security implications; we are more strict on counting who can have access to this resource. let i2c_sid = xns.register_name(api::SERVER_NAME_I2C, Some(3)).expect("can't register I2C thread"); @@ -530,6 +530,7 @@ fn xmain() -> ! { continue; }, }; + log::debug!("GetRtcValue regs: {:?}", settings); let total_secs = match rtc_to_seconds(&settings) { Some(s) => s, None => { @@ -638,6 +639,18 @@ mod tests { use llio::rtc_to_seconds; use rand::Rng; + fn to_bcd(binary: u8) -> u8 { + let mut lsd: u8 = binary % 10; + if lsd > 9 { + lsd = 9; + } + let mut msd: u8 = binary / 10; + if msd > 9 { + msd = 9; + } + (msd << 4) | lsd + } + #[test] fn test_rtc_to_secs() { let mut rng = rand::thread_rng(); @@ -677,13 +690,13 @@ mod tests { let diff = rtc_test.signed_duration_since(rtc_base); let settings = [ (Control3::BATT_STD_BL_EN).bits(), - s as u8, - m as u8, - h as u8, - day as u8, + to_bcd(s as u8), + to_bcd(m as u8), + to_bcd(h as u8), + to_bcd(day as u8), 0, - month as u8, - year as u8, + to_bcd(month as u8), + to_bcd(year as u8), ]; if diff.num_seconds() != rtc_to_seconds(&settings).unwrap() as i64 { println!("{} vs {}", diff.num_seconds(), rtc_to_seconds(&settings).unwrap());