mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
start to lay in the end user API, beginning with basis management commands
This commit is contained in:
parent
6e5506a23d
commit
20e28395d8
4 changed files with 117 additions and 13 deletions
|
|
@ -62,6 +62,9 @@ pub(crate) const PDDB_MODAL_NAME: &'static str = "pddb modal";
|
||||||
|
|
||||||
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
||||||
pub(crate) enum Opcode {
|
pub(crate) enum Opcode {
|
||||||
|
ListBasis,
|
||||||
|
LatestBasis,
|
||||||
|
|
||||||
KeyRequest,
|
KeyRequest,
|
||||||
|
|
||||||
ReadKey,
|
ReadKey,
|
||||||
|
|
@ -73,6 +76,13 @@ pub(crate) enum Opcode {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ApiToken = [u32; 3];
|
pub type ApiToken = [u32; 3];
|
||||||
|
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||||
|
pub struct PddbBasisList {
|
||||||
|
/// the first 63 that fit in the list -- generally we anticipate not more than a few being open at a time, so this should be enough.
|
||||||
|
pub list: [[u8; BASIS_NAME_LEN]; 63],
|
||||||
|
/// total number of basis open. Should be <= 63, but we allow it to be larger to indicate cases where this structure wasn't big enough.
|
||||||
|
pub num: u32,
|
||||||
|
}
|
||||||
|
|
||||||
/// A structure for requesting a token to access a particular key/value pair
|
/// A structure for requesting a token to access a particular key/value pair
|
||||||
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||||
|
|
|
||||||
|
|
@ -887,7 +887,6 @@ impl BasisCache {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub(crate) fn basis_list(&self) -> Vec<String> {
|
pub(crate) fn basis_list(&self) -> Vec<String> {
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
for bcache in &self.cache {
|
for bcache in &self.cache {
|
||||||
|
|
@ -895,6 +894,14 @@ impl BasisCache {
|
||||||
}
|
}
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
pub(crate) fn basis_latest(&mut self) -> Option<String> {
|
||||||
|
if let Some(basis_index) = self.select_basis(None) {
|
||||||
|
let basis = &mut self.cache[basis_index];
|
||||||
|
Some(basis.name.clone())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn basis_add(&mut self, basis: BasisCacheEntry) {
|
pub(crate) fn basis_add(&mut self, basis: BasisCacheEntry) {
|
||||||
self.cache.push(basis);
|
self.cache.push(basis);
|
||||||
|
|
|
||||||
|
|
@ -8,39 +8,98 @@ pub use frontend::*;
|
||||||
use num_traits::*;
|
use num_traits::*;
|
||||||
use std::io::{Result, Error, ErrorKind};
|
use std::io::{Result, Error, ErrorKind};
|
||||||
use xous::CID;
|
use xous::CID;
|
||||||
|
use xous_ipc::Buffer;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use core::sync::atomic::{AtomicU32, Ordering};
|
use core::sync::atomic::{AtomicU32, Ordering};
|
||||||
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
||||||
|
|
||||||
|
pub struct PddbBasisManager {
|
||||||
|
conn: CID,
|
||||||
|
}
|
||||||
|
impl PddbBasisManager {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
||||||
|
let xns = xous_names::XousNames::new().unwrap();
|
||||||
|
let conn = xns.request_connection_blocking(api::SERVER_NAME_PDDB).expect("can't connect to Pddb server");
|
||||||
|
PddbBasisManager {
|
||||||
|
conn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// return a list of all open bases
|
||||||
|
pub fn list_basis(&self) -> Vec::<String> {
|
||||||
|
let list_alloc = PddbBasisList {
|
||||||
|
list: [[0u8; BASIS_NAME_LEN]; 63],
|
||||||
|
num: 0
|
||||||
|
};
|
||||||
|
let mut buf = Buffer::into_buf(list_alloc).expect("Couldn't convert to memory structure");
|
||||||
|
buf.lend_mut(self.conn, Opcode::ListBasis.to_u32().unwrap()).expect("Couldn't execute ListBasis opcode");
|
||||||
|
let list = buf.to_original::<PddbBasisList, _>().expect("couldn't restore list structure");
|
||||||
|
if list.num > list.list.len() as u32 {
|
||||||
|
log::warn!("Number of open basis larger than our IPC structure. May need to refactor this API.");
|
||||||
|
}
|
||||||
|
let mut ret = Vec::<String>::new();
|
||||||
|
for (index, name) in list.list.iter().enumerate() {
|
||||||
|
if index as u32 == list.num {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ret.push(cstr_to_string(name));
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
/// returns the latest basis that is opened -- this is where all new values are being sent by default
|
||||||
|
/// if the PDDB is not mounted, returns None
|
||||||
|
pub fn latest_basis(&self) -> Option<String> {
|
||||||
|
// just re-use this structure -- because we have to clear the whole page anyways to share it
|
||||||
|
let list_alloc = PddbBasisList {
|
||||||
|
list: [[0u8; BASIS_NAME_LEN]; 63],
|
||||||
|
num: 0
|
||||||
|
};
|
||||||
|
let mut buf = Buffer::into_buf(list_alloc).expect("Couldn't convert to memory structure");
|
||||||
|
buf.lend_mut(self.conn, Opcode::LatestBasis.to_u32().unwrap()).expect("Couldn't execute ListBasis opcode");
|
||||||
|
let list = buf.to_original::<PddbBasisList, _>().expect("couldn't restore list structure");
|
||||||
|
if list.num == 1 {
|
||||||
|
Some(cstr_to_string(&list.list[0]))
|
||||||
|
} else if list.num == 0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
log::error!("Either 1 or 0 items should be returned by the latest basis call");
|
||||||
|
panic!("Either 1 or 0 items should be returned by the latest basis call");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn create(basis_name: &str) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn open(basis_name: &str) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
pub fn close(basis_name: &str) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// this structure can be shared on the user side?
|
// this structure can be shared on the user side?
|
||||||
pub struct Pddb<'a> {
|
pub struct Pddb<'a> {
|
||||||
conn: CID,
|
conn: CID,
|
||||||
contents: HashMap<PddbKey<'a>, &'a [u8]>,
|
contents: HashMap<PddbKey<'a>, &'a [u8]>,
|
||||||
callback: Box<dyn FnMut() + 'a>,
|
callback: Option<Box<dyn FnMut() + 'a>>,
|
||||||
}
|
}
|
||||||
impl<'a> Pddb<'a> {
|
impl<'a> Pddb<'a> {
|
||||||
/// return a list of all open bases
|
|
||||||
pub fn list_basis() -> Vec::<String> {
|
|
||||||
Vec::new()
|
|
||||||
}
|
|
||||||
/// returns the latest basis that is opened -- this is where all new values are being sent by default
|
|
||||||
pub fn get_current_basis() -> String {
|
|
||||||
String::new()
|
|
||||||
}
|
|
||||||
|
|
||||||
// opens a dictionary only if it exists
|
// opens a dictionary only if it exists
|
||||||
pub fn open(_dict_name: &str) -> Option<Self> { None }
|
pub fn open(_dict_name: &str) -> Option<Self> { None }
|
||||||
// creates a dictionary only if it does not already exist
|
// creates a dictionary only if it does not already exist
|
||||||
pub fn create(dict_name: &str) -> Option<Self> {
|
pub fn create(dict_name: &str) -> Option<Self> {
|
||||||
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
|
||||||
|
let xns = xous_names::XousNames::new().unwrap();
|
||||||
|
let conn = xns.request_connection_blocking(api::SERVER_NAME_PDDB).expect("can't connect to Pddb server");
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// returns a key only if it exists
|
/// returns a key only if it exists
|
||||||
pub fn get(&mut self, _key_name: &str, key_changed_cb: impl FnMut() + 'a) -> Result<Option<PddbKey>> {
|
pub fn get(&mut self, _key_name: &str, key_changed_cb: impl FnMut() + 'a) -> Result<Option<PddbKey>> {
|
||||||
self.callback = Box::new(key_changed_cb);
|
self.callback = Some(Box::new(key_changed_cb));
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
/// closes the key, de-allocating the OS memory to track it.
|
/// closes the key, de-allocating the OS memory to track it.
|
||||||
|
|
@ -69,4 +128,9 @@ impl<'a> Drop for Pddb<'a> {
|
||||||
// if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance),
|
// 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
|
// de-allocate those items here. They don't need a reference count because they are object-specific
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn cstr_to_string(cstr: &[u8]) -> String {
|
||||||
|
let null_index = cstr.iter().position(|&c| c == 0).expect("couldn't find null terminator on c string");
|
||||||
|
String::from_utf8(cstr[..null_index].to_vec()).expect("c string has invalid characters")
|
||||||
}
|
}
|
||||||
|
|
@ -557,8 +557,31 @@ fn xmain() -> ! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
loop {
|
loop {
|
||||||
let msg = xous::receive_message(pddb_sid).unwrap();
|
let mut msg = xous::receive_message(pddb_sid).unwrap();
|
||||||
match FromPrimitive::from_usize(msg.body.id()) {
|
match FromPrimitive::from_usize(msg.body.id()) {
|
||||||
|
Some(Opcode::ListBasis) => {
|
||||||
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
|
let mut list_ipc = buffer.to_original::<PddbBasisList, _>().unwrap();
|
||||||
|
let basis_list = basis_cache.basis_list();
|
||||||
|
for (src, dst) in basis_list.iter().zip(list_ipc.list.iter_mut()) {
|
||||||
|
for (&s, d) in src.as_bytes().iter().zip(dst.iter_mut()) {
|
||||||
|
*d = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list_ipc.num = basis_list.len() as u32;
|
||||||
|
buffer.replace(list_ipc).unwrap();
|
||||||
|
}
|
||||||
|
Some(Opcode::LatestBasis) => {
|
||||||
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
||||||
|
let mut list_ipc = buffer.to_original::<PddbBasisList, _>().unwrap();
|
||||||
|
if let Some(name) = basis_cache.basis_latest() {
|
||||||
|
for (&src, dst) in name.as_bytes().iter().zip(list_ipc.list[0].iter_mut()) { *dst = src }
|
||||||
|
list_ipc.num = 1;
|
||||||
|
} else {
|
||||||
|
list_ipc.num = 0;
|
||||||
|
}
|
||||||
|
buffer.replace(list_ipc).unwrap();
|
||||||
|
}
|
||||||
Some(Opcode::KeyRequest) => {
|
Some(Opcode::KeyRequest) => {
|
||||||
// placeholder
|
// placeholder
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue