move time services into the DNS crate

Time services are moved into the DNS crate so that the EC
update thread can remain running and consuming resources.

The problem is that we are out of connections in the connection table
(it's limited to 32 total), and in order to allow a "force update"
function for the EC we need to hold on to more connections than
are possible.

The "time" service is fairly self contained and could be moved
elsewhere. It sort of logically fit in the status crate initially
because the status crate manages preferences and displays the
current time on the screen, but it's modular enough that we could
put the service in any process space, at a minor trade-off of
the time UX function being public. This was avoided for a long
time because it could be a security risk but in the end the records
that store time zone and offsets are in the PDDB, which are accessible
to anyone who knows the names of these records, which is in the end
equivalent to knowing the name of the time UX server.

Thus the time UX server was made a dynamically registered "public"
server with just one connection (so only a minor downgrade in security,
the only vector exists in between boot and when the time servers
connect up), and the time server itself was slotted into the DNS
server's process space.

The DNS server was chosen because the DNS service itself is
very lightweight and will be unlikely to grow any heavier, so there
is little risk in merging the time service into the DNS crate's
process connection allocation.
This commit is contained in:
bunnie 2023-04-08 00:30:07 +08:00
parent 6e33ceba24
commit 430d3550b7
10 changed files with 58 additions and 26 deletions

11
Cargo.lock generated
View file

@ -1106,16 +1106,25 @@ dependencies = [
name = "dns"
version = "0.1.0"
dependencies = [
"chrono",
"gam",
"llio",
"locales",
"log",
"modals",
"net",
"num-derive",
"num-traits",
"pddb",
"rkyv",
"sntpc",
"trng",
"userprefs",
"utralib",
"xous 0.9.37 (registry+https://github.com/rust-lang/crates.io-index)",
"xous-api-log",
"xous-api-names",
"xous-api-susres",
"xous-api-ticktimer",
"xous-ipc 0.9.37 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -3817,6 +3826,7 @@ dependencies = [
"content-plugin-api",
"crossbeam",
"digest 0.9.0",
"dns",
"gam",
"graphics-server",
"keyboard",
@ -3831,7 +3841,6 @@ dependencies = [
"rkyv",
"root-keys",
"sha2",
"sntpc",
"trng",
"usb-device-xous",
"userprefs",

View file

@ -19,6 +19,19 @@ xous-ipc = "0.9.37"
rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]}
trng = {path = "../trng"}
# for the time UX wart
# time UX is stuck here because the DNS crate has a lot of connections available
# and the more logically grouped status crate has run out of resources.
llio = {path = "../llio"}
pddb = {path = "../pddb"}
chrono = {version = "0.4.19", default-features = false, features = ["std"]}
sntpc = {version = "0.3.1"} #, features = ["log"]
locales = {path = "../../locales"}
gam = {path = "../gam"}
susres = {package = "xous-api-susres", version = "0.9.32"}
userprefs = {path = "../../libs/userprefs"}
modals = {path = "../modals"}
utralib = { version = "0.1.16", optional = true, default-features = false }
[features]

View file

@ -80,3 +80,15 @@ pub struct DnsResponse {
pub addr: Option<NetIpAddr>,
pub code: DnsResponseCode,
}
// Time API items. Time is in the DNS crate because it has the resources
// to accommodate the time server, while the more logically grouped status
// crate does not.
pub const TIME_UX_NAME: &'static str = "_time UX server_";
/// Time API exports
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub enum TimeUxOp {
SetTime = 0,
SetTimeZone = 1,
Quit = 2,
}

View file

@ -1,6 +1,6 @@
pub mod api;
#[allow(unused_imports)]
use api::*;
pub use api::*;
#[cfg(any(feature="precursor", feature="renode"))]
mod hw;

View file

@ -2,6 +2,7 @@
#![cfg_attr(target_os = "none", no_main)]
mod api;
mod time; // why is this here? because it's the only place it'll fit. :-/
use api::*;
use net::NetIpAddr;
@ -484,6 +485,14 @@ fn main() -> ! {
log::set_max_level(log::LevelFilter::Info);
log::info!("my PID is {}", xous::process::id());
// Time is stuck in the DNS crate because the status crate is out of resources, and the DNS
// crate is fairly under-utilized and ideal for sticking a service like time in it.
//
// this kicks off the thread that services the `libstd` calls for time-related things.
// we want this started really early, because it sanity checks the RTC and a bunch of other stuff.
time::start_time_server();
time::start_time_ux();
let xns = xous_names::XousNames::new().unwrap();
let dns_sid = xns
.register_name(api::SERVER_NAME_DNS, None)

View file

@ -91,12 +91,6 @@ pub(crate) enum PrivTimeOp {
SusRes = 1,
}
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum TimeUxOp {
SetTime,
SetTimeZone,
Quit,
}
#[derive(Copy, Clone, Default)]
struct StdTimestampGen {
duration: std::time::Duration,
@ -556,11 +550,12 @@ fn simple_kilofloat_parse(input: &str) -> core::result::Result<i32, ParseIntErro
}
}
pub fn start_time_ux(sid: xous::SID) {
pub (crate) fn start_time_ux() {
thread::spawn({
move || {
// some RTC UX structures
let xns = xous_names::XousNames::new().unwrap();
let sid = xns.register_name(crate::TIME_UX_NAME, Some(1)).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();
@ -571,7 +566,7 @@ pub fn start_time_ux(sid: xous::SID) {
loop {
let msg = xous::receive_message(sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
Some(TimeUxOp::SetTime) => xous::msg_scalar_unpack!(msg, _, _, _, _, {
Some(crate::TimeUxOp::SetTime) => xous::msg_scalar_unpack!(msg, _, _, _, _, {
if !pddb_poller.is_mounted_nonblocking() {
modals.show_notification(t!("stats.please_mount", xous::LANG), None).expect("couldn't show notification");
continue;
@ -706,7 +701,7 @@ pub fn start_time_ux(sid: xous::SID) {
)
).expect("couldn't set time");
}),
Some(TimeUxOp::SetTimeZone) => xous::msg_scalar_unpack!(msg, _, _, _, _, {
Some(crate::TimeUxOp::SetTimeZone) => xous::msg_scalar_unpack!(msg, _, _, _, _, {
if !pddb_poller.is_mounted_nonblocking() {
modals.show_notification(t!("stats.please_mount", xous::LANG), None).expect("couldn't show notification");
continue;
@ -730,7 +725,7 @@ pub fn start_time_ux(sid: xous::SID) {
)
).expect("couldn't set timezone");
}),
Some(TimeUxOp::Quit) => {
Some(crate::TimeUxOp::Quit) => {
xous::return_scalar(msg.sender, 0).unwrap();
break;
}

View file

@ -29,13 +29,13 @@ keyboard = {path = "../keyboard"}
usb-device-xous = {path="../usb-device-xous"}
codec = {path="../codec"}
userprefs = {path = "../../libs/userprefs"}
dns = {path="../dns"}
num-derive = {version = "0.3.3", default-features = false}
num-traits = {version = "0.2.14", default-features = false}
rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]}
chrono = {version = "0.4.19", default-features = false, features = ["std"]}
sntpc = {version = "0.3.1"} #, features = ["log"]
crossbeam = "0.8.2"
sha2 = {path="../engine-sha512"}

View file

@ -259,7 +259,7 @@ pub(crate) fn ecupdate_thread(sid: xous::SID) {
do_fw = true;
}
log::info!("Auto-version check results: do_gw: {:?}, do_fw: {:?}", do_gw, do_fw);
if do_gw {
if do_gw || force {
log::info!("updating GW");
netmgr.connection_manager_stop().ok();
llio.com_event_enable(false).ok();
@ -272,7 +272,7 @@ pub(crate) fn ecupdate_thread(sid: xous::SID) {
}
susres.set_suspendable(true).unwrap(); // resume suspend/resume operations
}
if do_fw {
if do_fw || force {
log::info!("updating FW");
netmgr.connection_manager_stop().unwrap();
llio.com_event_enable(false).ok();

View file

@ -7,7 +7,6 @@ use mainmenu::*;
mod appmenu;
use appmenu::*;
mod app_autogen;
mod time;
mod ecup;
mod wifi;
mod preferences;
@ -152,10 +151,6 @@ fn wrapped_main() -> ! {
log::set_max_level(log::LevelFilter::Info);
log::info!("my PID is {}", xous::process::id());
// this kicks off the thread that services the `libstd` calls for time-related things.
// we want this started really early, because it sanity checks the RTC and a bunch of other stuff.
time::start_time_server();
// ------------------ acquire the status canvas GID
let xns = xous_names::XousNames::new().unwrap();
// 1 connection exactly -- from the GAM to set our canvas GID
@ -1385,7 +1380,6 @@ fn wrapped_main() -> ! {
thread::spawn({
let ecup_conn = ecup_conn.clone();
move || {
let xns = xous_names::XousNames::new().unwrap();
// send with an argument of '1', which forces the update
match send_message(ecup_conn,
Message::new_blocking_scalar(ecup::UpdateOp::UpdateAuto.to_usize().unwrap(), 1, 0, 0, 0)
@ -1393,6 +1387,7 @@ fn wrapped_main() -> ! {
xous::Result::Scalar1(r) => {
match FromPrimitive::from_usize(r) {
Some(ecup::UpdateResult::AutoDone) => {
let xns = xous_names::XousNames::new().unwrap();
let llio = llio::Llio::new(&xns);
let netmgr = net::NetManager::new();
// restore interrupts and connection manager
@ -1404,6 +1399,7 @@ fn wrapped_main() -> ! {
panic!("Force update responded with nothing to do. This is a bug, please report it in xous-core issues, and note the results of `ver ec`, `ver xous`, `ver soc`, `ver wf200`.");
},
Some(ecup::UpdateResult::Abort) => {
let xns = xous_names::XousNames::new().unwrap();
let modals = modals::Modals::new(&xns).unwrap();
modals.show_notification(t!("ecup.abort", xous::LANG), None).unwrap();
}

View file

@ -389,7 +389,7 @@ impl DevicePrefs {
xous::send_message(
self.time_ux_cid,
xous::Message::new_scalar(
crate::time::TimeUxOp::SetTime.to_usize().unwrap(),
dns::TimeUxOp::SetTime.to_usize().unwrap(),
0,
0,
0,
@ -407,7 +407,7 @@ impl DevicePrefs {
xous::send_message(
self.time_ux_cid,
xous::Message::new_scalar(
crate::time::TimeUxOp::SetTimeZone.to_usize().unwrap(),
dns::TimeUxOp::SetTimeZone.to_usize().unwrap(),
0,
0,
0,
@ -600,9 +600,7 @@ fn run_menu_thread(sid: xous::SID, status_cid: xous::CID) {
let menumatic_sid = xous::create_server().unwrap();
// --------------------------- spawn a time UX manager thread
let time_sid = xous::create_server().unwrap();
let time_cid = xous::connect(time_sid).unwrap();
crate::time::start_time_ux(time_sid);
let time_cid = xns.request_connection_blocking(dns::api::TIME_UX_NAME).unwrap();
#[cfg(not(feature="no-codec"))]
let codec = codec::Codec::new(&xns).unwrap();