2021-10-04 06:50:30 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
|
|
|
|
|
pub mod api;
|
2021-10-31 14:58:20 +00:00
|
|
|
pub use api::*;
|
|
|
|
|
pub mod frontend;
|
|
|
|
|
pub use frontend::*;
|
|
|
|
|
|
2021-10-04 06:50:30 +00:00
|
|
|
use xous::{CID, send_message};
|
2021-10-31 20:30:19 +00:00
|
|
|
use xous_ipc::Buffer;
|
|
|
|
|
|
2021-10-04 06:50:30 +00:00
|
|
|
use num_traits::*;
|
2021-10-31 20:30:19 +00:00
|
|
|
use std::io::{Result, Error, ErrorKind};
|
2021-11-01 19:26:16 +00:00
|
|
|
use std::path::{Path, Component};
|
2021-10-31 20:30:19 +00:00
|
|
|
use std::format;
|
2021-10-04 06:50:30 +00:00
|
|
|
|
2021-10-31 20:30:19 +00:00
|
|
|
pub const PDDB_MAX_DICT_NAME_LEN: usize = 64;
|
|
|
|
|
pub const PDDB_MAX_KEY_NAME_LEN: usize = 256;
|
2021-10-31 14:58:20 +00:00
|
|
|
|
2021-10-31 20:30:19 +00:00
|
|
|
pub struct PddbKey {
|
2021-10-04 06:50:30 +00:00
|
|
|
conn: CID,
|
2021-10-31 20:30:19 +00:00
|
|
|
dict: String,
|
|
|
|
|
key: String,
|
|
|
|
|
token: [u32; 3],
|
2021-10-04 06:50:30 +00:00
|
|
|
}
|
2021-10-31 20:30:19 +00:00
|
|
|
impl PddbKey {
|
|
|
|
|
pub fn get<P: AsRef<Path>>(path: P) -> Result<PddbKey> {
|
|
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
2021-10-04 06:50:30 +00:00
|
|
|
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
2021-10-31 14:58:20 +00:00
|
|
|
let conn = xns.request_connection_blocking(api::SERVER_NAME_PDDB).expect("Can't connect to Pddb server");
|
|
|
|
|
|
2021-11-01 19:26:16 +00:00
|
|
|
if !path.as_ref().is_absolute() {
|
|
|
|
|
return Err(Error::new(ErrorKind::InvalidInput, "All PDDB keys must be fully specified relative to a dictionary"));
|
2021-10-31 20:30:19 +00:00
|
|
|
}
|
|
|
|
|
let mut dict = String::new();
|
|
|
|
|
let mut key = String::new();
|
2021-11-01 19:26:16 +00:00
|
|
|
let mut components = path.as_ref().components();
|
|
|
|
|
match components.next().unwrap() {
|
|
|
|
|
Component::Prefix(prefix_component) => {
|
|
|
|
|
if let Some(dictstr) = prefix_component.as_os_str().to_str() {
|
|
|
|
|
if dictstr.len() <= PDDB_MAX_DICT_NAME_LEN {
|
2021-10-31 20:30:19 +00:00
|
|
|
dict.push_str(dictstr);
|
|
|
|
|
} else {
|
2021-11-01 19:26:16 +00:00
|
|
|
return Err(Error::new(ErrorKind::InvalidInput, format!("PDDB dictionary names must be shorter than {} bytes", PDDB_MAX_DICT_NAME_LEN)));
|
2021-10-31 20:30:19 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2021-11-01 19:26:16 +00:00
|
|
|
return Err(Error::new(ErrorKind::InvalidInput, "PDDB dictionary names must valid UTF-8"));
|
2021-10-31 20:30:19 +00:00
|
|
|
}
|
2021-11-01 19:26:16 +00:00
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
return Err(Error::new(ErrorKind::InvalidInput, "All PDDB entries must be of the format `dict:key`, where `dict` is treated as a Prefix"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// collect the remaining components into the key
|
|
|
|
|
for comps in components {
|
|
|
|
|
if let Some(keystr) = comps.as_os_str().to_str() {
|
|
|
|
|
key.push_str(keystr);
|
2021-10-31 20:30:19 +00:00
|
|
|
} else {
|
2021-11-01 19:26:16 +00:00
|
|
|
return Err(Error::new(ErrorKind::InvalidInput, "PDDB dictionary names must valid UTF-8"));
|
2021-10-31 20:30:19 +00:00
|
|
|
}
|
2021-11-01 19:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if key.len() > PDDB_MAX_KEY_NAME_LEN {
|
|
|
|
|
return Err(Error::new(ErrorKind::InvalidInput, format!("PDDB key names must be shorter than {} bytes", PDDB_MAX_DICT_NAME_LEN)));
|
2021-10-31 20:30:19 +00:00
|
|
|
}
|
2021-10-31 14:58:20 +00:00
|
|
|
|
2021-10-31 20:30:19 +00:00
|
|
|
let request = PddbKeyRequest {
|
|
|
|
|
dict: xous_ipc::String::<PDDB_MAX_DICT_NAME_LEN>::from_str(dict.as_str()),
|
|
|
|
|
key: xous_ipc::String::<PDDB_MAX_KEY_NAME_LEN>::from_str(key.as_str()),
|
|
|
|
|
token: None,
|
|
|
|
|
};
|
|
|
|
|
let mut buf = Buffer::into_buf(request)
|
|
|
|
|
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
|
|
|
|
buf.lend_mut(conn, Opcode::KeyRequest.to_u32().unwrap())
|
|
|
|
|
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
|
|
|
|
|
|
|
|
|
|
let response = buf.to_original::<PddbKeyRequest, _>().unwrap();
|
|
|
|
|
if let Some(token) = response.token {
|
|
|
|
|
Ok(PddbKey {
|
|
|
|
|
conn,
|
|
|
|
|
dict,
|
|
|
|
|
key,
|
|
|
|
|
token,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::new(ErrorKind::PermissionDenied, "Dict/Key access denied"))
|
|
|
|
|
}
|
2021-10-04 06:50:30 +00:00
|
|
|
}
|
2021-10-08 12:01:13 +00:00
|
|
|
|
2021-10-31 14:58:20 +00:00
|
|
|
pub(crate) fn conn(&self) -> CID {
|
|
|
|
|
self.conn
|
|
|
|
|
}
|
2021-10-04 06:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use core::sync::atomic::{AtomicU32, Ordering};
|
|
|
|
|
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
2021-10-31 20:30:19 +00:00
|
|
|
impl Drop for PddbKey {
|
2021-10-04 06:50:30 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
|
// the connection to the server side must be reference counted, so that multiple instances of this object within
|
|
|
|
|
// a single process do not end up de-allocating the CID on other threads before they go out of scope.
|
|
|
|
|
// Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object".
|
|
|
|
|
if REFCOUNT.load(Ordering::Relaxed) == 0 {
|
|
|
|
|
unsafe{xous::disconnect(self.conn).unwrap();}
|
|
|
|
|
}
|
|
|
|
|
// if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance),
|
|
|
|
|
// de-allocate those items here. They don't need a reference count because they are object-specific
|
|
|
|
|
}
|
2021-10-31 14:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use xous::MemoryRange;
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::io::SeekFrom;
|
|
|
|
|
|
|
|
|
|
// this is an intenal structure for managing the overall PDDB
|
|
|
|
|
pub(crate) struct PddbManager {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
impl PddbManager {
|
|
|
|
|
// return a list of open bases
|
|
|
|
|
fn list_basis() {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this is an internal struct for managing a basis
|
|
|
|
|
pub(crate) struct PddbBasis {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
impl PddbBasis {
|
|
|
|
|
// opening and closing basis will side-effect PddbDicts and PddbKeys
|
|
|
|
|
fn open() {} // will result in a password box being triggered to open a basis
|
|
|
|
|
fn close() {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this structure can be shared on the user side?
|
|
|
|
|
pub struct PddbDict<'a> {
|
2021-10-31 20:30:19 +00:00
|
|
|
contents: HashMap<PddbKey, &'a [u8]>,
|
2021-10-31 14:58:20 +00:00
|
|
|
callback: Box<dyn FnMut() + 'a>,
|
|
|
|
|
}
|
|
|
|
|
impl<'a> PddbDict<'a> {
|
|
|
|
|
// opens a dictionary only if it exists
|
|
|
|
|
pub fn open(dict_name: &str) -> Option<PddbDict> { None }
|
|
|
|
|
// creates a dictionary only if it does not already exist
|
|
|
|
|
pub fn create(dict_name: &str) -> Option<PddbDict> { None }
|
|
|
|
|
|
|
|
|
|
// returns a key only if it exists
|
|
|
|
|
pub fn get(&mut self, key_name: &str, key_changed_cb: impl FnMut() + 'a) -> Result<Option<PddbKey>> {
|
|
|
|
|
self.callback = Box::new(key_changed_cb);
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
// updates an existing key's value. mainly used by write().
|
|
|
|
|
pub fn update(&mut self, key: PddbKey) -> Result<Option<PddbKey>> { Ok(None) }
|
|
|
|
|
// creates a key or overwrites it
|
|
|
|
|
pub fn insert(&mut self, key: PddbKey) -> Option<PddbKey> { None } // may return the displaced key
|
|
|
|
|
// deletes a key within the dictionary
|
|
|
|
|
pub fn remove(&mut self, key: PddbKey) -> Result<()> { Ok(()) }
|
|
|
|
|
// deletes the entire dictionary
|
|
|
|
|
pub fn delete(&mut self) {}
|
|
|
|
|
}
|
|
|
|
|
|