From e9f21396aa2432c56b1347c068de3a027d07b4e6 Mon Sep 17 00:00:00 2001 From: Tom Marble Date: Tue, 17 Jan 2023 10:11:34 -0600 Subject: [PATCH] Completes fix for #287 Adds migrations framework Delays mtxcli initialization until first user input, because we know: - PDDB is mounted - Timer is stable - User actually using mtxcli Adds send_async_msg scalar_async_msg - basis for automatically updating chatroom Signed-off-by: Tom Marble --- Cargo.lock | 2 +- apps/mtxcli/README.md | 13 ++ apps/mtxcli/locales/i18n.json | 21 ++++ apps/mtxcli/src/cmds.rs | 192 ++++++++++++++++++++--------- apps/mtxcli/src/cmds/get.rs | 2 +- apps/mtxcli/src/cmds/heap.rs | 2 +- apps/mtxcli/src/cmds/help.rs | 35 +++++- apps/mtxcli/src/cmds/login.rs | 2 +- apps/mtxcli/src/cmds/logout.rs | 2 +- apps/mtxcli/src/cmds/migrations.rs | 14 ++- apps/mtxcli/src/cmds/set.rs | 2 +- apps/mtxcli/src/cmds/status.rs | 2 +- apps/mtxcli/src/cmds/unset.rs | 2 +- apps/mtxcli/src/mtxcli.rs | 11 +- 14 files changed, 225 insertions(+), 77 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e74932c99..f206184e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2240,7 +2240,7 @@ dependencies = [ [[package]] name = "mtxcli" -version = "0.1.0" +version = "0.6.0" dependencies = [ "codec", "com", diff --git a/apps/mtxcli/README.md b/apps/mtxcli/README.md index a6378e9a1..9cefcfa89 100644 --- a/apps/mtxcli/README.md +++ b/apps/mtxcli/README.md @@ -62,6 +62,19 @@ Xous please see the [locales README](https://github.com/betrusted-io/xous-core/b ![screenshot](xous-french.png) +## Troubleshooting + +If you see the message `WARNING: clock not set` that is likely because +the Precursor real time clock needs to be set (e.g. if the battery +has been completely discharged). Please go to the menu +**Preferences | Set Timezone** to set the time zone +(and update the time via NTP). + +If you see the message `authentication failed` it might be because +the `user` and `password` variables are not set properly. +Alternatively it could be because TLS certificate validation +has failed because the clock has not been set (see above). + ## Acknowledgements This app is supported in part by a grant from the diff --git a/apps/mtxcli/locales/i18n.json b/apps/mtxcli/locales/i18n.json index 4c79128ee..a6dc0fdf4 100644 --- a/apps/mtxcli/locales/i18n.json +++ b/apps/mtxcli/locales/i18n.json @@ -1,4 +1,11 @@ { + "mtxcli.clock.warning": { + "en": "WARNING: clock not set", + "en-tts": "WARNING: clock not set", + "fr": "AVERTISSEMENT : horloge non réglée", + "ja": "WARNING: clock not set *EN*", + "zh": "WARNING: clock not set *EN*" + }, "mtxcli.filter.failed": { "en": "error: could not create filter", "en-tts": "error: could not create filter", @@ -83,6 +90,13 @@ "ja": "/logout *EN*", "zh": "/logout *EN*" }, + "mtxcli.migration.completed": { + "en": "migration completed", + "en-tts": "migration completed", + "fr": "migration terminée", + "ja": "migration completed *EN*", + "zh": "migration completed *EN*" + }, "mtxcli.not.connected": { "en": "not connected", "en-tts": "not conntected", @@ -125,6 +139,13 @@ "ja": "error: could not find room_id *EN*", "zh": "error: could not find room_id *EN*" }, + "mtxcli.running.migrations": { + "en": "Running migrations from version", + "en-tts": "Running migrations from version", + "fr": "Exécution des migrations à partir de la version", + "ja": "Running migrations from version *EN*", + "zh": "Running migrations from version *EN*" + }, "mtxcli.send.failed": { "en": "failed to send!", "en-tts": "failed to send!", diff --git a/apps/mtxcli/src/cmds.rs b/apps/mtxcli/src/cmds.rs index 53dd2c4a9..caca54299 100644 --- a/apps/mtxcli/src/cmds.rs +++ b/apps/mtxcli/src/cmds.rs @@ -1,4 +1,4 @@ -use xous::{MessageEnvelope}; +use xous::{MessageEnvelope, Message,StringBuffer}; use xous_ipc::String as XousString; use core::fmt::Write; use locales::t; @@ -8,11 +8,13 @@ use std::fs::File; use std::io::{Read, Write as StdWrite, Error, ErrorKind}; use std::path::PathBuf; use std::time::{SystemTime, UNIX_EPOCH}; // to help gen_txn_id +use core::str::FromStr; mod migrations; use migrations::run_migrations; mod url; mod web; +const DATE_2023: u64 = 1672538401; // for clock_not_set() const APP: &str = "mtxcli"; /// PDDB Dict for mtxcli keys @@ -36,6 +38,8 @@ const SERVER_MATRIX: &str = "https://matrix.org"; const EMPTY: &str = ""; const MTX_TIMEOUT: i32 = 300; // ms +pub const CLOCK_NOT_SET_ID: usize = 1; + /// Returns a version string which is more likely to string compare /// correctly vs. another version. FFI please see /// https://git-scm.com/docs/git-describe#_examples @@ -90,14 +94,11 @@ macro_rules! cmd_api { use trng::*; /////////////////////////// Command shell integration #[derive(Debug)] -#[allow(dead_code)] // there's more in the envornment right now than we need for the demo pub struct CommonEnv { - llio: llio::Llio, - com: com::Com, - codec: codec::Codec, ticktimer: ticktimer_server::Ticktimer, - gam: gam::Gam, cb_registrations: HashMap::>, + async_msg_callback_id: u32, + async_msg_conn: u32, trng: Trng, xns: xous_names::XousNames, user: String, @@ -110,8 +111,72 @@ pub struct CommonEnv { since: String, first_line: bool, version: String, + initialized: bool, } impl CommonEnv { + pub fn new() -> CommonEnv { + let xns = xous_names::XousNames::new().unwrap(); + let ticktimer = ticktimer_server::Ticktimer::new() + .expect("Couldn't connect to Ticktimer"); + let cb_registrations = HashMap::new(); + let async_msg_callback_id: u32 = 0; + let async_msg_conn: u32 = 0; + let common = CommonEnv { + ticktimer, + cb_registrations, + async_msg_callback_id, + async_msg_conn, + trng: Trng::new(&xns).unwrap(), + xns, + user: EMPTY.to_string(), + username: EMPTY.to_string(), + server: SERVER_MATRIX.to_string(), + token: EMPTY.to_string(), + logged_in: false, + room_id: EMPTY.to_string(), + filter: EMPTY.to_string(), + since: EMPTY.to_string(), + first_line: true, + version: EMPTY.to_string(), + initialized: false, + }; + common + } + + pub fn register_handler(&mut self, verb: XousString::<256>) -> u32 { + let mut key: u32; + loop { + key = self.trng.get_u32().unwrap(); + // reserve the bottom 1000 IDs for the main loop enums. + if !self.cb_registrations.contains_key(&key) && (key > 1000) { + break; + } + } + self.cb_registrations.insert(key, verb); + key + } + + // NOTE: Here the async callbacks will be managed by the command "help" + // as it is already an unsual command + pub fn register_async_msg(&mut self) { + self.async_msg_conn = self.xns.request_connection_blocking(crate::SERVER_NAME_MTXCLI).unwrap(); + self.async_msg_callback_id = self.register_handler(XousString::<256>::from_str("help")); + } + + pub fn scalar_async_msg(&self, async_msg_id: usize) { + let msg = Message::new_scalar(self.async_msg_callback_id as usize, + 0, 0, 0, async_msg_id); + xous::send_message(self.async_msg_conn, msg).unwrap(); + } + + pub fn send_async_msg(&self, async_msg: &str) { + let str_buf = StringBuffer::from_str(async_msg) + .expect("unable to create string message"); + str_buf.send(self.async_msg_conn, self.async_msg_callback_id) + .expect("unable to send string message"); + // let msg = str_buf.create_memory_message(self.async_msg_callback_id); + // xous::send_message(self.async_msg_conn, msg).unwrap(); + } pub fn gen_txn_id(&mut self) -> String { let mut txn_id = self.trng.get_u32() @@ -131,7 +196,7 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + // log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; @@ -139,9 +204,14 @@ impl CommonEnv { keypath.push(key); File::create(keypath)?.write_all(value.as_bytes())?; match key { // special case side effects - USER_KEY => { self.set_user(value); } + FILTER_KEY => { self.filter = value.to_string(); } PASSWORD_KEY => { self.set_password(); } + ROOM_ID_KEY => { self.room_id = value.to_string(); } ROOM_KEY => { self.set_room(); } + SERVER_KEY => { self.server = value.to_string(); } + SINCE_KEY => { self.since = value.to_string(); } + USERNAME_KEY => { self.username = value.to_string(); } + USER_KEY => { self.set_user(value); } _ => { } } Ok(()) @@ -194,8 +264,11 @@ impl CommonEnv { pub fn set_room(&mut self) { log::debug!("# ROOM_KEY set '{}' => clearing ROOM_ID_KEY, SINCE_KEY, FILTER_KEY", ROOM_KEY); self.unset_debug(ROOM_ID_KEY); + self.room_id = EMPTY.to_string(); self.unset_debug(SINCE_KEY); + self.since = EMPTY.to_string(); self.unset_debug(FILTER_KEY); + self.filter = EMPTY.to_string(); } pub fn unset(&mut self, key: &str) -> Result<(), Error> { @@ -207,7 +280,7 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + // log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; @@ -218,6 +291,15 @@ impl CommonEnv { std::fs::remove_file(keypath)?; } + match key { // special case side effects -- update cached values + FILTER_KEY => { self.filter = EMPTY.to_string(); } + ROOM_ID_KEY => { self.room_id = EMPTY.to_string(); } + SINCE_KEY => { self.since = EMPTY.to_string(); } + SERVER_KEY => { self.server = EMPTY.to_string(); } + USER_KEY => { self.user = EMPTY.to_string(); } + USERNAME_KEY => { self.username = EMPTY.to_string(); } + _ => { } + } Ok(()) } } @@ -242,7 +324,7 @@ impl CommonEnv { let mut keypath = PathBuf::new(); keypath.push(MTXCLI_DICT); if std::fs::metadata(&keypath).is_ok() { // keypath exists - log::info!("dict '{}' exists", MTXCLI_DICT); + // log::info!("dict '{}' exists", MTXCLI_DICT); } else { log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); std::fs::create_dir_all(&keypath)?; @@ -481,45 +563,21 @@ pub struct CmdEnv { unset_cmd: Unset, } impl CmdEnv { - pub fn new(xns: &xous_names::XousNames) -> CmdEnv { - let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer"); - let version = get_version(&ticktimer); - log::info!("creating CommonEnv"); - let mut common = CommonEnv { - llio: llio::Llio::new(&xns), - com: com::Com::new(&xns).expect("could't connect to COM"), - codec: codec::Codec::new(&xns).expect("couldn't connect to CODEC"), - ticktimer, - gam: gam::Gam::new(&xns).expect("couldn't connect to GAM"), - cb_registrations: HashMap::new(), - trng: Trng::new(&xns).unwrap(), - xns: xous_names::XousNames::new().unwrap(), - user: EMPTY.to_string(), - username: EMPTY.to_string(), - - server: SERVER_MATRIX.to_string(), - token: EMPTY.to_string(), - logged_in: false, - room_id: EMPTY.to_string(), - filter: EMPTY.to_string(), - since: EMPTY.to_string(), - first_line: true, - version, - }; - log::info!("done creating CommonEnv"); - run_migrations(&mut common); + pub fn new() -> CmdEnv { + let mut common_env = CommonEnv::new(); + common_env.register_async_msg(); CmdEnv { - common_env: common, + common_env, lastverb: XousString::<256>::new(), ///// 3. initialize your storage, by calling new() - get_cmd: Get::new(&xns), - heap_cmd: Heap::new(&xns), - help_cmd: Help::new(&xns), - login_cmd: Login::new(&xns), - logout_cmd: Logout::new(&xns), - set_cmd: Set::new(&xns), - status_cmd: Status::new(&xns), - unset_cmd: Unset::new(&xns), + get_cmd: Get::new(), + heap_cmd: Heap::new(), + help_cmd: Help::new(), + login_cmd: Login::new(), + logout_cmd: Logout::new(), + set_cmd: Set::new(), + status_cmd: Status::new(), + unset_cmd: Unset::new(), } } @@ -539,16 +597,27 @@ impl CmdEnv { &mut self.unset_cmd, ]; - if self.common_env.username.len() == 0 { // first time initialization - self.common_env.user = self.common_env.get_default(USER_KEY, EMPTY); - self.common_env.username = self.common_env.get_default(USERNAME_KEY, USERNAME_KEY); - self.common_env.server = self.common_env.get_default(SERVER_KEY, SERVER_MATRIX); - self.common_env.room_id = self.common_env.get_default(ROOM_ID_KEY, EMPTY); - self.common_env.filter = self.common_env.get_default(FILTER_KEY, EMPTY); - self.common_env.since = self.common_env.get_default(SINCE_KEY, EMPTY); - } - if let Some(cmdline) = maybe_cmdline { + // Initialization (must wait until PDDB is mounted and llio can + // report accurate time). Here we wait to check for initialization + // once the user has typed something + if ! self.initialized { + log::info!("initializing"); + if clock_not_set() { + self.common_env.scalar_async_msg(CLOCK_NOT_SET_ID); + } + self.common_env.user = self.common_env.get_default(USER_KEY, EMPTY); + self.common_env.username = self.common_env.get_default(USERNAME_KEY, USERNAME_KEY); + self.common_env.server = self.common_env.get_default(SERVER_KEY, SERVER_MATRIX); + self.common_env.room_id = self.common_env.get_default(ROOM_ID_KEY, EMPTY); + self.common_env.filter = self.common_env.get_default(FILTER_KEY, EMPTY); + self.common_env.since = self.common_env.get_default(SINCE_KEY, EMPTY); + self.common_env.version = get_version(&self.common_env.ticktimer); + + run_migrations(&mut self.common_env); + self.initialized = true; + } + let maybe_verb = tokenize(cmdline); let mut cmd_ret: Result>, xous::Error> = Ok(None); @@ -677,3 +746,16 @@ pub(crate) fn heap_usage() -> usize { }, } } + + +pub fn clock_not_set() -> bool { + let now = SystemTime::now(); + let since_the_epoch = now.duration_since(UNIX_EPOCH) + .expect("Time went backwards"); + let seconds: u64 = since_the_epoch.as_secs(); + if seconds < DATE_2023 { + true + } else { + false + } +} diff --git a/apps/mtxcli/src/cmds/get.rs b/apps/mtxcli/src/cmds/get.rs index b2904f23e..037ee4ee9 100644 --- a/apps/mtxcli/src/cmds/get.rs +++ b/apps/mtxcli/src/cmds/get.rs @@ -7,7 +7,7 @@ use locales::t; pub struct Get { } impl Get { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Get { } } diff --git a/apps/mtxcli/src/cmds/heap.rs b/apps/mtxcli/src/cmds/heap.rs index b8d71ce02..2a4f4c0f1 100644 --- a/apps/mtxcli/src/cmds/heap.rs +++ b/apps/mtxcli/src/cmds/heap.rs @@ -6,7 +6,7 @@ use core::fmt::Write; pub struct Heap { } impl Heap { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Heap { } } diff --git a/apps/mtxcli/src/cmds/help.rs b/apps/mtxcli/src/cmds/help.rs index 69a386923..3d8f4850f 100644 --- a/apps/mtxcli/src/cmds/help.rs +++ b/apps/mtxcli/src/cmds/help.rs @@ -1,4 +1,6 @@ -use crate::{ShellCmdApi,CommonEnv}; +use crate::{ShellCmdApi,CommonEnv, + cmds::CLOCK_NOT_SET_ID}; +use xous::{MessageEnvelope, Message,StringBuffer}; use xous_ipc::String as XousString; use core::fmt::Write; use locales::t; @@ -7,7 +9,7 @@ use locales::t; pub struct Help { } impl Help { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Help { } } @@ -68,7 +70,34 @@ impl<'a> ShellCmdApi<'a> for Help { } } } - Ok(Some(ret)) } + + // NOTE: the help callback is used to process async messages + fn callback(&mut self, msg: &MessageEnvelope, _env: &mut CommonEnv) -> Result>, xous::Error> { + match &msg.body { + Message::Scalar(xous::ScalarMessage{id: _, arg1: _, arg2: _, + arg3: _, arg4: async_msg_id}) => { + if *async_msg_id == CLOCK_NOT_SET_ID { + let mut ret = XousString::<1024>::new(); + let warning = t!("mtxcli.clock.warning", xous::LANG); + write!(ret, "{}", warning).unwrap(); + log::warn!("{}", warning); + return Ok(Some(ret)); + } + }, + Message::Move(mm) => { + let str_buf = unsafe { StringBuffer::from_memory_message(mm) }; + let msg = str_buf.to_str(); + let mut ret = XousString::<1024>::new(); + write!(ret, "{}", msg).unwrap(); + // log::info!("async message \"{}\"", msg); + return Ok(Some(ret)); + }, + _ => { + log::error!("received unknown callback type: {:?}", msg) + } + } + Ok(None) + } } diff --git a/apps/mtxcli/src/cmds/login.rs b/apps/mtxcli/src/cmds/login.rs index c0f463cfd..4ae0f32e3 100644 --- a/apps/mtxcli/src/cmds/login.rs +++ b/apps/mtxcli/src/cmds/login.rs @@ -5,7 +5,7 @@ use xous_ipc::String as XousString; pub struct Login { } impl Login { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Login { } } diff --git a/apps/mtxcli/src/cmds/logout.rs b/apps/mtxcli/src/cmds/logout.rs index 5e490d8b4..af1fc7e5d 100644 --- a/apps/mtxcli/src/cmds/logout.rs +++ b/apps/mtxcli/src/cmds/logout.rs @@ -5,7 +5,7 @@ use xous_ipc::String as XousString; pub struct Logout { } impl Logout { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Logout { } } diff --git a/apps/mtxcli/src/cmds/migrations.rs b/apps/mtxcli/src/cmds/migrations.rs index 6fa7cd953..1b610a92e 100644 --- a/apps/mtxcli/src/cmds/migrations.rs +++ b/apps/mtxcli/src/cmds/migrations.rs @@ -4,6 +4,7 @@ use std::io::Error; use crate::cmds::{CommonEnv,VERSION_KEY}; +use locales::t; mod v0_9_11_0120; use v0_9_11_0120::*; @@ -41,16 +42,21 @@ macro_rules! migration_api { pub fn run_migrations(common: &mut CommonEnv) { let version = common.get_default(VERSION_KEY, DEFAULT_VERSION); if version.ne(&common.version) { - log::info!("Running migrations from version {} to {}", - version, common.version); + let running_migrations = t!("mtxcli.running.migrations", xous::LANG); + let msg = format!("{} {} .. {}", + running_migrations, version, common.version); + log::info!("{}", msg); + common.send_async_msg(&msg); let mut migrations: Vec> = Vec::new(); migrations.push(Box::new(V0_9_11_0120::new())); for migration in migrations.iter() { if migration.applies(&version) { match migration.process(common) { Ok(boolean) => { - log::info!("migration competed: {}: {}", - migration.version(), boolean); + let migration_completed = t!("mtxcli.migration.completed", xous::LANG); + let msg = format!("{}: {}: {}", + migration_completed, migration.version(), boolean); + common.send_async_msg(&msg); if boolean { common.set(VERSION_KEY, migration.version()) .expect("cannot set _version"); diff --git a/apps/mtxcli/src/cmds/set.rs b/apps/mtxcli/src/cmds/set.rs index b0c23dda2..1513ea380 100644 --- a/apps/mtxcli/src/cmds/set.rs +++ b/apps/mtxcli/src/cmds/set.rs @@ -7,7 +7,7 @@ use locales::t; pub struct Set { } impl Set { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Set { } } diff --git a/apps/mtxcli/src/cmds/status.rs b/apps/mtxcli/src/cmds/status.rs index 3ef2f106d..05990973d 100644 --- a/apps/mtxcli/src/cmds/status.rs +++ b/apps/mtxcli/src/cmds/status.rs @@ -6,7 +6,7 @@ use locales::t; pub struct Status { } impl Status { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Status { } } diff --git a/apps/mtxcli/src/cmds/unset.rs b/apps/mtxcli/src/cmds/unset.rs index 60543e8ba..9b5fd4325 100644 --- a/apps/mtxcli/src/cmds/unset.rs +++ b/apps/mtxcli/src/cmds/unset.rs @@ -7,7 +7,7 @@ use locales::t; pub struct Unset { } impl Unset { - pub fn new(_xns: &xous_names::XousNames) -> Self { + pub fn new() -> Self { Unset { } } diff --git a/apps/mtxcli/src/mtxcli.rs b/apps/mtxcli/src/mtxcli.rs index 1d9ba6134..7d6655ca2 100644 --- a/apps/mtxcli/src/mtxcli.rs +++ b/apps/mtxcli/src/mtxcli.rs @@ -14,7 +14,6 @@ struct History { pub is_input: bool, } -#[allow(dead_code)] pub(crate) struct Mtxcli { // optional structures that indicate new input to the Mtxcli loop per iteration // an input string @@ -38,9 +37,6 @@ pub(crate) struct Mtxcli { // command environment env: CmdEnv, - - // our security token for making changes to our record on the GAM - token: [u32; 4], } impl Mtxcli{ pub(crate) fn new(xns: &xous_names::XousNames, sid: xous::SID) -> Self { @@ -60,10 +56,12 @@ impl Mtxcli{ let content = gam.request_content_canvas(token.unwrap()).expect("couldn't get content canvas"); let screensize = gam.get_canvas_bounds(content).expect("couldn't get dimensions of content canvas"); + let history: Vec:: = vec![History{text: String::from(t!("mtxcli.greeting", xous::LANG)), is_input: false}]; + let env = CmdEnv::new(); Mtxcli { input: None, msg: None, - history: vec![History{text: String::from(t!("mtxcli.greeting", xous::LANG)), is_input: false}], + history, history_len: 10, content, gam, @@ -73,8 +71,7 @@ impl Mtxcli{ bubble_margin: Point::new(4, 4), bubble_radius: 4, bubble_space: 4, - env: CmdEnv::new(xns), - token: token.unwrap(), + env, } }