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",
"num-derive",
"num-traits",
"pddb",
"percent-encoding",
"serde",
"trng",

View file

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

View file

@ -64,6 +64,10 @@ Xous please see the [locales README](https://github.com/betrusted-io/xous-core/b
## 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
the Precursor real time clock needs to be set (e.g. if the battery
has been completely discharged). Please go to the menu

View file

@ -104,6 +104,13 @@
"ja": "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": {
"en": "please /set password my-password",
"en-tts": "please /set password my-password",
@ -180,5 +187,12 @@
"fr": "/unset clé",
"ja": "/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
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
/// correctly vs. another version. FFI please see
@ -196,7 +198,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)?;
@ -280,15 +282,19 @@ 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)?;
}
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
log::info!("dict:key = '{}:{}' exists.. deleting it", MTXCLI_DICT, key);
std::fs::remove_file(keypath)?;
}
match key { // special case side effects -- update cached values
@ -324,7 +330,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)?;
@ -333,7 +339,7 @@ impl CommonEnv {
if let Ok(mut file)= File::open(keypath) {
let mut value = String::new();
file.read_to_string(&mut value)?;
// log::info!("get '{}' = '{}'", key, value);
log::info!("get '{}' = '{}'", key, value);
Ok(Some(value))
} else {
Ok(None)
@ -601,21 +607,30 @@ impl CmdEnv {
// 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 {
if ! self.common_env.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);
} else {
let pddb = pddb::PddbMountPoller::new();
if ! pddb.is_mounted_nonblocking() {
self.common_env.scalar_async_msg(PDDB_NOT_MOUNTED_ID);
} else {
// FUTURE: Check if Wifi is connected
// if not then
// 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);
self.initialized = true;
run_migrations(&mut self.common_env);
self.common_env.initialized = true;
}
}
}
let maybe_verb = tokenize(cmdline);

View file

@ -1,5 +1,5 @@
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_ipc::String as XousString;
use core::fmt::Write;
@ -78,13 +78,24 @@ impl<'a> ShellCmdApi<'a> for Help {
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));
}
let mut ret = XousString::<1024>::new();
let warning = match *async_msg_id {
CLOCK_NOT_SET_ID => {
t!("mtxcli.clock.warning", xous::LANG)
},
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) => {
let str_buf = unsafe { StringBuffer::from_memory_message(mm) };

View file

@ -30,21 +30,30 @@ impl<'a> ShellCmdApi<'a> for Set {
match value {
"" => {
write!(ret, "{}", t!("mtxcli.set.help", xous::LANG)).unwrap();
}
}
_ => {
match env.set(key, value) {
Ok(()) => {
write!(ret, "set {}", key).unwrap();
},
Err(e) => {
// write!(ret, "error setting key {}: {:?}", key, e).unwrap();
log::error!("error setting key {}: {:?}", key, e);
}
}
}
}
} 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);
}
}
}
}
}