Updates for #287

Allows a variable to be set to the EMPTY string
- by not setting a value `/set myvar`

During initialization now verifies that PDDB is mounted.

Added warnings for PDDB not mounted and WiFi not connected.
- the WiFi check is not yet implemented

Signed-off-by: Tom Marble <tmarble@info9.net>
This commit is contained in:
Tom Marble 2023-01-17 12:19:33 -06:00 committed by Sean Cross
parent e48507a597
commit ba2e91fea0
7 changed files with 83 additions and 27 deletions

1
Cargo.lock generated
View file

@ -2254,6 +2254,7 @@ dependencies = [
"log", "log",
"num-derive", "num-derive",
"num-traits", "num-traits",
"pddb",
"percent-encoding", "percent-encoding",
"serde", "serde",
"trng", "trng",

View file

@ -29,6 +29,8 @@ codec = {path = "../../services/codec"}
percent-encoding = "2.2" percent-encoding = "2.2"
serde = { version = "1.0", features = [ "derive" ] } serde = { version = "1.0", features = [ "derive" ] }
ureq = { version = "2.5", features = ["json"] } ureq = { version = "2.5", features = ["json"] }
# Xous APIs
pddb = {path = "../../services/pddb" }
[features] [features]
default = [] default = []

View file

@ -64,6 +64,10 @@ Xous please see the [locales README](https://github.com/betrusted-io/xous-core/b
## Troubleshooting ## Troubleshooting
Prequisites: it is essential that the PDDB is mounted and WiFi is
connected before running mtxcli. You can verify these in Shellchat
with `net ping 1.1.1.1`.
If you see the message `WARNING: clock not set` that is likely because 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 the Precursor real time clock needs to be set (e.g. if the battery
has been completely discharged). Please go to the menu has been completely discharged). Please go to the menu

View file

@ -104,6 +104,13 @@
"ja": "not connected *EN*", "ja": "not connected *EN*",
"zh": "not connected *EN*" "zh": "not connected *EN*"
}, },
"mtxcli.pddb.warning": {
"en": "WARNING: pddb not mounted",
"en-tts": "WARNING: pddb not mounted",
"fr": "AVERTISSEMENT: pddb pas monté",
"ja": "WARNING: pddb not mounted *EN*",
"zh": "WARNING: pddb not mounted *EN*"
},
"mtxcli.please.set.password": { "mtxcli.please.set.password": {
"en": "please /set password my-password", "en": "please /set password my-password",
"en-tts": "please /set password my-password", "en-tts": "please /set password my-password",
@ -180,5 +187,12 @@
"fr": "/unset clé", "fr": "/unset clé",
"ja": "/unset key *EN*", "ja": "/unset key *EN*",
"zh": "/unset key *EN*" "zh": "/unset key *EN*"
},
"mtxcli.wifi.warning": {
"en": "WARNING: WiFi not connected",
"en-tts": "WARNING: WiFi not connected",
"fr": "AVERTISSEMENT: WiFi pas connecté",
"ja": "WARNING: WiFi not connected *EN*",
"zh": "WARNING: WiFi not connected *EN*"
} }
} }

View file

@ -39,6 +39,8 @@ const EMPTY: &str = "";
const MTX_TIMEOUT: i32 = 300; // ms const MTX_TIMEOUT: i32 = 300; // ms
pub const CLOCK_NOT_SET_ID: usize = 1; pub const CLOCK_NOT_SET_ID: usize = 1;
pub const PDDB_NOT_MOUNTED_ID: usize = 2;
pub const WIFI_NOT_CONNECTED_ID: usize = 3;
/// Returns a version string which is more likely to string compare /// Returns a version string which is more likely to string compare
/// correctly vs. another version. FFI please see /// correctly vs. another version. FFI please see
@ -196,7 +198,7 @@ impl CommonEnv {
let mut keypath = PathBuf::new(); let mut keypath = PathBuf::new();
keypath.push(MTXCLI_DICT); keypath.push(MTXCLI_DICT);
if std::fs::metadata(&keypath).is_ok() { // keypath exists if std::fs::metadata(&keypath).is_ok() { // keypath exists
// log::info!("dict '{}' exists", MTXCLI_DICT); log::info!("dict '{}' exists", MTXCLI_DICT);
} else { } else {
log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT);
std::fs::create_dir_all(&keypath)?; std::fs::create_dir_all(&keypath)?;
@ -280,15 +282,19 @@ impl CommonEnv {
let mut keypath = PathBuf::new(); let mut keypath = PathBuf::new();
keypath.push(MTXCLI_DICT); keypath.push(MTXCLI_DICT);
if std::fs::metadata(&keypath).is_ok() { // keypath exists if std::fs::metadata(&keypath).is_ok() { // keypath exists
// log::info!("dict '{}' exists", MTXCLI_DICT); log::info!("dict '{}' exists", MTXCLI_DICT);
} else { } else {
log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT);
std::fs::create_dir_all(&keypath)?; std::fs::create_dir_all(&keypath)?;
} }
keypath.push(key); keypath.push(key);
// NOTE: the std:fs::metadata call may return Ok() even when
// the file does not exist.
// As a workaround WE COULD set the variable to the empty string
// log::info!("dict:key = '{}:{}' set to empty string (not deleting it)", MTXCLI_DICT, key);
// File::create(keypath)?.write_all(EMPTY.as_bytes())?;
if std::fs::metadata(&keypath).is_ok() { // keypath exists if std::fs::metadata(&keypath).is_ok() { // keypath exists
log::info!("dict:key = '{}:{}' exists.. deleting it", MTXCLI_DICT, key); log::info!("dict:key = '{}:{}' exists.. deleting it", MTXCLI_DICT, key);
std::fs::remove_file(keypath)?; std::fs::remove_file(keypath)?;
} }
match key { // special case side effects -- update cached values match key { // special case side effects -- update cached values
@ -324,7 +330,7 @@ impl CommonEnv {
let mut keypath = PathBuf::new(); let mut keypath = PathBuf::new();
keypath.push(MTXCLI_DICT); keypath.push(MTXCLI_DICT);
if std::fs::metadata(&keypath).is_ok() { // keypath exists if std::fs::metadata(&keypath).is_ok() { // keypath exists
// log::info!("dict '{}' exists", MTXCLI_DICT); log::info!("dict '{}' exists", MTXCLI_DICT);
} else { } else {
log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT); log::info!("dict '{}' does NOT exist.. creating it", MTXCLI_DICT);
std::fs::create_dir_all(&keypath)?; std::fs::create_dir_all(&keypath)?;
@ -333,7 +339,7 @@ impl CommonEnv {
if let Ok(mut file)= File::open(keypath) { if let Ok(mut file)= File::open(keypath) {
let mut value = String::new(); let mut value = String::new();
file.read_to_string(&mut value)?; file.read_to_string(&mut value)?;
// log::info!("get '{}' = '{}'", key, value); log::info!("get '{}' = '{}'", key, value);
Ok(Some(value)) Ok(Some(value))
} else { } else {
Ok(None) Ok(None)
@ -601,21 +607,30 @@ impl CmdEnv {
// Initialization (must wait until PDDB is mounted and llio can // Initialization (must wait until PDDB is mounted and llio can
// report accurate time). Here we wait to check for initialization // report accurate time). Here we wait to check for initialization
// once the user has typed something // once the user has typed something
if ! self.initialized { if ! self.common_env.initialized {
log::info!("initializing"); log::info!("initializing");
if clock_not_set() { if clock_not_set() {
self.common_env.scalar_async_msg(CLOCK_NOT_SET_ID); self.common_env.scalar_async_msg(CLOCK_NOT_SET_ID);
} } else {
self.common_env.user = self.common_env.get_default(USER_KEY, EMPTY); let pddb = pddb::PddbMountPoller::new();
self.common_env.username = self.common_env.get_default(USERNAME_KEY, USERNAME_KEY); if ! pddb.is_mounted_nonblocking() {
self.common_env.server = self.common_env.get_default(SERVER_KEY, SERVER_MATRIX); self.common_env.scalar_async_msg(PDDB_NOT_MOUNTED_ID);
self.common_env.room_id = self.common_env.get_default(ROOM_ID_KEY, EMPTY); } else {
self.common_env.filter = self.common_env.get_default(FILTER_KEY, EMPTY); // FUTURE: Check if Wifi is connected
self.common_env.since = self.common_env.get_default(SINCE_KEY, EMPTY); // if not then
self.common_env.version = get_version(&self.common_env.ticktimer); // self.common_env.scalar_async_msg(WIFI_NOT_CONNECTED_ 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); run_migrations(&mut self.common_env);
self.initialized = true; self.common_env.initialized = true;
}
}
} }
let maybe_verb = tokenize(cmdline); let maybe_verb = tokenize(cmdline);

View file

@ -1,5 +1,5 @@
use crate::{ShellCmdApi,CommonEnv, use crate::{ShellCmdApi,CommonEnv,
cmds::CLOCK_NOT_SET_ID}; cmds::{CLOCK_NOT_SET_ID,PDDB_NOT_MOUNTED_ID,WIFI_NOT_CONNECTED_ID}};
use xous::{MessageEnvelope, Message,StringBuffer}; use xous::{MessageEnvelope, Message,StringBuffer};
use xous_ipc::String as XousString; use xous_ipc::String as XousString;
use core::fmt::Write; use core::fmt::Write;
@ -78,13 +78,24 @@ impl<'a> ShellCmdApi<'a> for Help {
match &msg.body { match &msg.body {
Message::Scalar(xous::ScalarMessage{id: _, arg1: _, arg2: _, Message::Scalar(xous::ScalarMessage{id: _, arg1: _, arg2: _,
arg3: _, arg4: async_msg_id}) => { arg3: _, arg4: async_msg_id}) => {
if *async_msg_id == CLOCK_NOT_SET_ID { let mut ret = XousString::<1024>::new();
let mut ret = XousString::<1024>::new(); let warning = match *async_msg_id {
let warning = t!("mtxcli.clock.warning", xous::LANG); CLOCK_NOT_SET_ID => {
write!(ret, "{}", warning).unwrap(); t!("mtxcli.clock.warning", xous::LANG)
log::warn!("{}", warning); },
return Ok(Some(ret)); PDDB_NOT_MOUNTED_ID => {
} t!("mtxcli.pddb.warning", xous::LANG)
},
WIFI_NOT_CONNECTED_ID => {
t!("mtxcli.wifi.warning", xous::LANG)
},
_ => {
"unknown async_msg_id"
},
};
write!(ret, "{}", warning).unwrap();
log::warn!("{}", warning);
return Ok(Some(ret));
}, },
Message::Move(mm) => { Message::Move(mm) => {
let str_buf = unsafe { StringBuffer::from_memory_message(mm) }; let str_buf = unsafe { StringBuffer::from_memory_message(mm) };

View file

@ -30,21 +30,30 @@ impl<'a> ShellCmdApi<'a> for Set {
match value { match value {
"" => { "" => {
write!(ret, "{}", t!("mtxcli.set.help", xous::LANG)).unwrap(); write!(ret, "{}", t!("mtxcli.set.help", xous::LANG)).unwrap();
} }
_ => { _ => {
match env.set(key, value) { match env.set(key, value) {
Ok(()) => { Ok(()) => {
write!(ret, "set {}", key).unwrap(); write!(ret, "set {}", key).unwrap();
}, },
Err(e) => { Err(e) => {
// write!(ret, "error setting key {}: {:?}", key, e).unwrap();
log::error!("error setting key {}: {:?}", key, e); log::error!("error setting key {}: {:?}", key, e);
} }
} }
} }
} }
} else { } else {
write!(ret, "{}", t!("mtxcli.set.help", xous::LANG)).unwrap(); // Instead of an error -- set to the empty string
// write!(ret, "{}", t!("mtxcli.set.help", xous::LANG)).unwrap();
// write!(ret, "{}", t!("mtxcli.set.help", xous::LANG)).unwrap();
match env.set(key, "") {
Ok(()) => {
write!(ret, "set {} EMPTY", key).unwrap();
},
Err(e) => {
log::error!("error setting key {}: {:?}", key, e);
}
}
} }
} }
} }