WIP user prefs

This commit is contained in:
Gianguido Sorà 2022-10-10 13:36:34 +02:00
parent c1eed3e7ad
commit 8c9cdc4ec5
8 changed files with 353 additions and 8 deletions

61
Cargo.lock generated
View file

@ -239,6 +239,25 @@ dependencies = [
"serde",
]
[[package]]
name = "bincode"
version = "2.0.0-rc.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bb50c5a2ef4b9b1e7ae73e3a73b52ea24b20312d629f9c4df28260b7ad2c3c4"
dependencies = [
"bincode_derive",
"serde",
]
[[package]]
name = "bincode_derive"
version = "2.0.0-rc.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a45a23389446d2dd25dc8e73a7a3b3c43522b630cac068927f0649d43d719d2"
dependencies = [
"virtue",
]
[[package]]
name = "bit_field"
version = "0.9.0"
@ -912,7 +931,7 @@ dependencies = [
name = "curve25519-dalek-loader"
version = "3.1.0"
dependencies = [
"bincode",
"bincode 1.3.3",
"byteorder",
"criterion",
"digest 0.9.0",
@ -1098,7 +1117,7 @@ dependencies = [
name = "ed25519-dalek-loader"
version = "1.0.1"
dependencies = [
"bincode",
"bincode 1.3.3",
"criterion",
"curve25519-dalek-loader",
"ed25519",
@ -2638,6 +2657,16 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "prefsgenerator"
version = "0.1.0"
dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -2670,9 +2699,9 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
[[package]]
name = "proc-macro2"
version = "1.0.39"
version = "1.0.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f"
checksum = "94e2ef8dbfc347b10c094890f778ee2e36ca9bb4262e86dc99cd217e35f3470b"
dependencies = [
"unicode-ident",
]
@ -2774,9 +2803,9 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.18"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
@ -3684,9 +3713,9 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.95"
version = "1.0.102"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbaf6116ab8924f39d52792136fb74fd60a80194cf1b1c6ffa6453eef1c3f942"
checksum = "3fcd952facd492f9be3ef0d0b7032a6e442ee9b361d4acc2b1d0c4aaa5f613a1"
dependencies = [
"proc-macro2",
"quote",
@ -4170,6 +4199,16 @@ dependencies = [
"usb-device",
]
[[package]]
name = "userprefs"
version = "0.1.0"
dependencies = [
"bincode 2.0.0-rc.2",
"pddb",
"prefsgenerator",
"xous-api-names",
]
[[package]]
name = "utf-8"
version = "0.7.6"
@ -4287,6 +4326,12 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "virtue"
version = "0.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b60dcd6a64dd45abf9bd426970c9843726da7fc08f44cd6fcebf68c21220a63"
[[package]]
name = "void"
version = "1.0.2"

View file

@ -29,6 +29,7 @@ default-members = [
"services/dns",
"services/modals",
"services/usb-device-xous",
"lib/userprefs"
]
members = [
"xous-ipc",
@ -85,6 +86,7 @@ members = [
"tools/perflib",
"kernel",
"loader",
"libs/userprefs"
]
resolver = "2"

2
libs/userprefs/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
/Cargo.lock

10
libs/userprefs/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "userprefs"
version = "0.1.0"
edition = "2021"
[dependencies]
pddb = {path = "../../services/pddb"}
xous-names = {package = "xous-api-names", version = "0.9.4"}
bincode = {version = "2.0.0-rc.2"}
prefsgenerator = {path = "./prefsgenerator"}

22
libs/userprefs/README.md Normal file
View file

@ -0,0 +1,22 @@
# Xous user preference
Instead of having device settings sparse across the operating system UX, I propose we unify everything under a single umbrealla: a `Preferences` applet.
This would relieve `status` of the burden of having random stuff filed under its menu and streamline the codebase a little bit.
This applet would be comprised of two components:
- a `preference` service that abstract a way of accessing an entity's preferences: given a key, `preference` returns a blob
## Interesting toggles
- [ ] Turn on Wi-Fi subsystem on boot
- [ ] Connect to known networks on boot
- [ ] Default backlight state
- [ ] (With autobacklight on) Backlight timeout
- [ ] Ask for device password after sleep wake-up
- [ ] Close and unmount all bases on device lock (before sleeping, manual locking in status menu)
- [ ] Keyboard layout
- [ ] Timezone
- [ ] Date/time
- [ ] Speaker volume
- [ ] Wi-Fi submenu

View file

@ -0,0 +1,15 @@
[package]
name = "prefsgenerator"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
proc-macro-error = "1.0.4"
proc-macro2 = "1.0.46"
quote = "1.0.21"
syn = "1.0.102"
[lib]
proc-macro = true

View file

@ -0,0 +1,68 @@
use proc_macro::{self, TokenStream};
use proc_macro_error::{abort, proc_macro_error};
use quote::{format_ident, quote};
use syn::{parse_macro_input, DeriveInput, FieldsNamed};
#[proc_macro_error]
#[proc_macro_derive(GetterSetter)]
pub fn getter_setter(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
let mut rw_methods = quote! {};
match data {
syn::Data::Struct(s) => match s.fields {
syn::Fields::Named(FieldsNamed { named, .. }) => {
for field in &named {
let ident = field.ident.as_ref().unwrap().clone();
let typ = field.ty.clone();
let set_fn_name = format_ident!("set_{}", ident);
let fn_name = format_ident!("{}", ident);
let read = quote! {
impl Manager {
// pub fn #set_fn_name(&mut self, value: #typ) {
// self.prefs.#ident = value;
// self.pddb_store(&self.prefs).unwrap()
// }
// pub fn #fn_name(&mut self) -> #typ {
// self.prefs = self.pddb_get().unwrap();
// self.prefs.#ident
// }
pub fn #set_fn_name(&mut self, value: #typ) -> Result<(), Error> {
let bytes: Vec<u8> = match bincode::encode_to_vec(value, bincode::config::standard()) {
Ok(ret) => Ok(ret),
Err(err) => return Err(Error::EncodeError(err)),
};
self.pddb_store_key(#fn_name, &bytes)
}
pub fn #fn_name(&mut self) -> Result<#typ, Error> {
let bytes = self.pddb_get_key("#fn_name")?;
let ret: #typ = match bincode::decode_from_slice(&bytes, bincode::config::standard()) {
Ok((data, _)) => data,
Err(err) => return Err(Error::DecodeError(err)),
};
Ok(ret)
}
}
};
println!("code: {}", read);
panic!();
//rw_methods.extend(read);
}
}
_ => (),
},
_ => {
abort!(ident.span(), "This macro can only be used on structs");
}
};
rw_methods.into()
}

181
libs/userprefs/src/lib.rs Normal file
View file

@ -0,0 +1,181 @@
use bincode::error::{DecodeError, EncodeError};
use pddb::Pddb;
use std::io::Read;
use std::io::Write;
static PREFS_DICT: &str = "UserPrefsDict";
static PREFS_KEY: &str = "UserPrefsKey";
#[derive(Debug)]
pub enum Error {
EncodeError(EncodeError),
DecodeError(DecodeError),
IoError(std::io::Error),
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Self::IoError(err)
}
}
impl From<EncodeError> for Error {
fn from(err: EncodeError) -> Self {
Self::EncodeError(err)
}
}
impl From<DecodeError> for Error {
fn from(err: DecodeError) -> Self {
Self::DecodeError(err)
}
}
#[derive(
bincode::Encode, bincode::Decode, PartialEq, Debug, Default, prefsgenerator::GetterSetter,
)]
struct UserPrefs {
// TODO: optimize maybe by adding a dirty bit?
radio_on_on_boot: bool,
connect_known_networks_on_boot: bool,
autobacklight_on_boot: bool,
password_after_sleep: bool,
close_bases_on_lock: bool,
}
impl TryFrom<Vec<u8>> for UserPrefs {
fn try_from(data: Vec<u8>) -> Result<Self, Self::Error> {
if data.is_empty() {
return Ok(Self::default());
}
let decoded: UserPrefs =
match bincode::decode_from_slice(&data, bincode::config::standard()) {
Ok((data, _)) => data,
Err(err) => return Err(Error::DecodeError(err)),
};
Ok(decoded)
}
type Error = Error;
}
impl TryFrom<&UserPrefs> for Vec<u8> {
fn try_from(up: &UserPrefs) -> Result<Self, Self::Error> {
match bincode::encode_to_vec(up, bincode::config::standard()) {
Ok(ret) => Ok(ret),
Err(err) => Err(Error::EncodeError(err)),
}
}
type Error = Error;
}
pub struct Manager {
pddb_handle: Pddb,
prefs: UserPrefs,
}
impl Manager {
fn new() -> Self {
let mut us = Self {
pddb_handle: Pddb::new(),
prefs: UserPrefs::default(),
};
us.prefs = us.pddb_get().unwrap();
us
}
fn pddb_store(&self, payload: &UserPrefs) -> Result<(), Error> {
let payload: Vec<u8> = payload.try_into()?;
match self.pddb_handle.get(
PREFS_DICT,
PREFS_KEY,
Some(".System"),
true,
true,
None,
None::<fn()>,
) {
Ok(mut data) => match data.write(&payload) {
Ok(_) => Ok(self.pddb_handle.sync().unwrap_or(())),
Err(e) => Err(e.into()),
},
Err(e) => Err(e.into()),
}
}
fn pddb_get(&self) -> Result<UserPrefs, Error> {
match self.pddb_handle.get(
PREFS_DICT,
PREFS_KEY,
Some(".System"),
true,
true,
None,
None::<fn()>,
) {
Ok(mut record) => {
let mut data = Vec::<u8>::new();
record.read_to_end(&mut data)?;
Ok(data.try_into()?)
}
Err(e) => return Err(e.into()),
}
}
fn pddb_store_key(&self, key: &str, value: &[u8]) -> Result<(), Error> {
match self.pddb_handle.get(
PREFS_DICT,
key,
Some(".System"),
true,
true,
None,
None::<fn()>,
) {
Ok(mut data) => match data.write(value) {
Ok(_) => Ok(self.pddb_handle.sync().unwrap_or(())),
Err(e) => Err(e.into()),
},
Err(e) => Err(e.into()),
}
}
fn pddb_get_key(&self, key: &str) -> Result<Vec<u8>, Error> {
match self.pddb_handle.get(
PREFS_DICT,
PREFS_KEY,
Some(".System"),
true,
true,
None,
None::<fn()>,
) {
Ok(mut record) => {
let mut data = Vec::<u8>::new();
record.read_to_end(&mut data)?;
Ok(data)
}
Err(e) => return Err(e.into()),
}
}
}
impl Default for Manager {
fn default() -> Self {
Self::new()
}
}
mod test {
use crate::{Manager, UserPrefs};
#[test]
fn describe() {
//UserPrefs::describe();
let m = Manager::new();
}
}