first stab at refactoring filter to be faster

This commit is contained in:
bunnie 2023-06-05 04:57:26 +08:00
parent 118716da7b
commit 9bb3ac0db7
4 changed files with 596 additions and 368 deletions

View file

@ -1,11 +1,10 @@
use core::convert::TryFrom;
use std::{thread, io::ErrorKind};
use std::io::ErrorKind;
use gam::TextEntryPayload;
use pddb::BasisRetentionPolicy;
use std::sync::{Arc, Mutex, atomic::{AtomicBool, Ordering}};
use num_traits::*;
use xous::{SID, msg_blocking_scalar_unpack, Message, send_message};
use xous_ipc::Buffer;
use xous::{Message, send_message};
use locales::t;
use std::io::{Write, Read};
use passwords::PasswordGenerator;
@ -16,7 +15,7 @@ use vault::{
VAULT_PASSWORD_DICT, VAULT_TOTP_DICT, VAULT_ALLOC_HINT, utc_now,
atime_to_str
};
use crate::ux::framework::ListItem;
use crate::ListItem;
use crate::{ItemLists, VaultMode, SelectedEntry};
use crate::storage::{self, PasswordRecord, StorageContent};
use crate::totp::TotpAlgorithm;
@ -52,11 +51,11 @@ pub enum ActionOp {
/// Testing
GenerateTests,
}
pub(crate) fn start_actions_thread(
/*
pub(crate) fn start_actions_thread<'a> (
main_conn: xous::CID,
sid: SID, mode: Arc::<Mutex::<VaultMode>>,
item_lists: Arc::<Mutex::<ItemLists>>,
item_lists: Arc::<Mutex::<ItemLists<'a>>>,
action_active: Arc::<AtomicBool>,
opensk_mutex: Arc::<Mutex::<i32>>,
) {
@ -140,8 +139,9 @@ pub(crate) fn start_actions_thread(
}
});
}
*/
struct ActionManager<'a> {
pub struct ActionManager<'a> {
modals: modals::Modals,
storage: RefCell<storage::Manager>,
@ -149,7 +149,7 @@ struct ActionManager<'a> {
trng: RefCell::<trng::Trng>,
mode: Arc::<Mutex::<VaultMode>>,
item_lists: Arc::<Mutex::<ItemLists>>,
pub item_lists: Arc::<Mutex::<ItemLists>>,
pddb: RefCell::<pddb::Pddb>,
tt: ticktimer_server::Ticktimer,
action_active: Arc::<AtomicBool>,
@ -559,7 +559,7 @@ impl<'a> ActionManager<'a> {
let mut desc = String::with_capacity(256);
make_pw_name(&pw.description, &pw.username, &mut desc);
let key = ListItem::key_from_parts(&desc, guid);
self.item_lists.lock().unwrap().pw.remove(&key);
self.item_lists.lock().unwrap().remove(entry.mode, key);
/*
if self.item_lists.lock().unwrap().pw.remove(&key).is_some() {
self.modals.show_notification(&format!("deleted UX {}", &key), None).ok();
@ -608,8 +608,10 @@ impl<'a> ActionManager<'a> {
count: pw.count,
};
log::debug!("updating {} to list item {}", li.extra, li.key());
let il = &mut self.item_lists.lock().unwrap().pw;
assert!(il.insert(li.key(), li).is_some(), "Somehow, the autotyped record isn't in the UX list for updating!");
assert!(
self.item_lists.lock().unwrap().insert(entry.mode, li.key(), li).is_some(),
"Somehow, the autotyped record isn't in the UX list for updating!")
;
},
_ => {
// no cached data, no action
@ -756,7 +758,8 @@ impl<'a> ActionManager<'a> {
// remove the entry from the old UX list
let mut desc = String::new();
make_pw_name(&pw.description, &pw.username, &mut desc);
self.item_lists.lock().unwrap().pw.remove(&ListItem::key_from_parts(&desc, key_name));
self.item_lists.lock().unwrap().remove(VaultMode::Password, ListItem::key_from_parts(&desc, key_name));
// display previous data for edit
let edit_data = if pw.notes != t!("vault.notes", xous::LANG) {
@ -950,7 +953,6 @@ impl<'a> ActionManager<'a> {
log::debug!("heap usage A: {}", heap_usage());
match self.mode_cache {
VaultMode::Password => {
let il = &mut self.item_lists.lock().unwrap().pw;
let start = self.tt.elapsed_ms();
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 1, std::line!());
@ -965,6 +967,7 @@ impl<'a> ActionManager<'a> {
let mut extra = String::with_capacity(256);
let mut desc = String::with_capacity(256);
let mut lookup_key = String::with_capacity(384);
let il = self.item_lists.lock().unwrap();
for key in keys {
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 2, std::line!());
@ -985,13 +988,13 @@ impl<'a> ActionManager<'a> {
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_NONE, 2, std::line!());
if let Some(prev_entry) = il.get_mut(&lookup_key) {
if let Some(prev_entry) = il.get(self.mode_cache, &lookup_key) {
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 3, std::line!());
prev_entry.dirty = true;
prev_entry.lock().unwrap().dirty = true;
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!());
if prev_entry.atime != pw_rec.atime || prev_entry.count != pw_rec.count {
if prev_entry.lock().unwrap().atime != pw_rec.atime || prev_entry.lock().unwrap().count != pw_rec.count {
// this is expensive, so don't run it unless we have to
let human_time = atime_to_str(pw_rec.atime);
// note this code is duplicated in update_db_entry()
@ -999,20 +1002,20 @@ impl<'a> ActionManager<'a> {
extra.push_str("; ");
extra.push_str(t!("vault.u2f.appinfo.authcount", xous::LANG));
extra.push_str(&pw_rec.count.to_string());
prev_entry.extra.clear();
prev_entry.extra.push_str(&extra);
prev_entry.lock().unwrap().extra.clear();
prev_entry.lock().unwrap().extra.push_str(&extra);
}
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!());
if prev_entry.name != desc { // this check should be redundant, but, leave it in to be safe
prev_entry.name.clear();
prev_entry.name.push_str(&desc);
if prev_entry.lock().unwrap().name != desc { // this check should be redundant, but, leave it in to be safe
prev_entry.lock().unwrap().name.clear();
prev_entry.lock().unwrap().name.push_str(&desc);
}
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!());
if prev_entry.guid != key.name {
prev_entry.guid.clear();
prev_entry.guid.push_str(&key.name);
if prev_entry.lock().unwrap().guid != key.name {
prev_entry.lock().unwrap().guid.clear();
prev_entry.lock().unwrap().guid.push_str(&key.name);
}
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_ENDBLOCK, 3, std::line!());
@ -1035,7 +1038,8 @@ impl<'a> ActionManager<'a> {
atime: pw_rec.atime,
count: pw_rec.count,
};
il.insert(li.key(), li); // this push is very slow, but we only have to do it once on boot
self.item_lists.lock().unwrap()
.insert(self.mode_cache, li.key(), li); // this push is very slow, but we only have to do it once on boot
#[cfg(feature="vaultperf")]
self.perfentry(&self.pm, PERFMETA_ENDBLOCK, 4, std::line!());
}
@ -1087,9 +1091,8 @@ impl<'a> ActionManager<'a> {
VaultMode::Fido => {
// first assemble U2F records
log::debug!("listing in {}", U2F_APP_DICT);
let il = &mut self.item_lists.lock().unwrap().fido;
// regen from scratch every time, It's slow, but we're counting on <20 FIDO entries on average
il.clear();
self.item_lists.lock().unwrap().clear(self.mode_cache);
match self.pddb.borrow().read_dict(U2F_APP_DICT, None, Some(256 * 1024)) {
Ok(keys) => {
let mut oom_keys = 0;
@ -1110,7 +1113,7 @@ impl<'a> ActionManager<'a> {
count: ai.count,
atime: ai.atime,
};
il.insert(li.key(), li);
self.item_lists.lock().unwrap().insert(self.mode_cache, li.key(), li);
} else {
let err = format!("{}:{}:{}: ({})[moved data]...",
key.basis, U2F_APP_DICT, key.name, key.len
@ -1168,7 +1171,7 @@ impl<'a> ActionManager<'a> {
count: 0,
atime: 0,
};
il.insert(li.key(), li);
self.item_lists.lock().unwrap().insert(self.mode_cache, li.key(), li);
}
None => {
// Probably more indicative of a mismatch in OpenSK key range mapping, rather than a hard error.
@ -1204,8 +1207,7 @@ impl<'a> ActionManager<'a> {
}
}
VaultMode::Totp => {
let il = &mut self.item_lists.lock().unwrap().totp;
il.clear();
self.item_lists.lock().unwrap().clear(self.mode_cache);
match self.pddb.borrow().read_dict(VAULT_TOTP_DICT, None, Some(256 * 1024)) {
Ok(keys) => {
let mut oom_keys = 0;
@ -1228,7 +1230,7 @@ impl<'a> ActionManager<'a> {
count: 0,
atime: 0,
};
il.insert(li.key(), li);
self.item_lists.lock().unwrap().insert(self.mode_cache, li.key(), li);
} else {
let err = format!("{}:{}:{}: ({})[moved data]...",
key.basis, VAULT_TOTP_DICT, key.name, key.len
@ -1301,9 +1303,7 @@ impl<'a> ActionManager<'a> {
Ok(_) => {
log::debug!("Basis {} unlocked", name);
// clear local caches
self.item_lists.lock().unwrap().pw.clear();
self.item_lists.lock().unwrap().fido.clear();
self.item_lists.lock().unwrap().totp.clear();
self.item_lists.lock().unwrap().clear_all();
},
Err(e) => match e.kind() {
ErrorKind::PermissionDenied => {
@ -1330,9 +1330,7 @@ impl<'a> ActionManager<'a> {
Ok(_) => {
log::debug!("basis {} locked", b);
// clear local caches
self.item_lists.lock().unwrap().pw.clear();
self.item_lists.lock().unwrap().fido.clear();
self.item_lists.lock().unwrap().totp.clear();
self.item_lists.lock().unwrap().clear_all();
},
Err(e) => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)),
}
@ -1361,9 +1359,7 @@ impl<'a> ActionManager<'a> {
Ok(_) => {
log::debug!("Basis {} unlocked", name);
// clear local caches
self.item_lists.lock().unwrap().pw.clear();
self.item_lists.lock().unwrap().fido.clear();
self.item_lists.lock().unwrap().totp.clear();
self.item_lists.lock().unwrap().clear_all();
},
Err(e) => match e.kind() {
ErrorKind::PermissionDenied => {

331
apps/vault/src/itemcache.rs Normal file
View file

@ -0,0 +1,331 @@
use core::num::NonZeroUsize;
use std::collections::BTreeMap;
use std::cmp::Ordering;
use crate::{VaultMode, SelectedEntry};
use crate::ux::framework::NavDir;
use std::sync::{Arc, Mutex};
/// Display list for items. "name" is the key by which the list is sorted.
/// "extra" is more information about the item, which should not be part of the sort.
#[derive(Debug)]
pub struct ListItem {
pub name: String,
pub extra: String,
pub dirty: bool,
/// this is the name of the key used to refer to the item
pub guid: String,
/// stash a copy so we can compare to the DB record and avoid re-generating the atime/count string if it hasn't changed.
pub atime: u64,
pub count: u64,
}
impl ListItem {
pub fn clone(&self) -> ListItem {
ListItem {
name: self.name.to_string(),
extra: self.extra.to_string(),
dirty: self.dirty,
guid: self.guid.to_string(),
atime: self.atime,
count: self.count,
}
}
/// This is made available for edit/delete routines to generate the key without having to
/// make a whole ListItem record (which is somewhat expensive).
pub fn key_from_parts(name: &str, guid: &str) -> String {
name.to_lowercase() + &guid.to_string()
}
pub fn key(&self) -> String {
Self::key_from_parts(&self.name, &self.guid)
}
}
impl Ord for ListItem {
fn cmp(&self, other: &Self) -> Ordering {
match self.name.cmp(&other.name) {
Ordering::Equal => {
self.guid.cmp(&other.guid)
},
Ordering::Greater => Ordering::Greater,
Ordering::Less => Ordering::Less,
}
}
}
impl PartialOrd for ListItem {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for ListItem {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.guid == other.guid
}
}
impl Eq for ListItem {}
pub struct ItemLists {
fido: BTreeMap::<String, Arc<Mutex<ListItem>>>,
totp: BTreeMap::<String, Arc<Mutex<ListItem>>>,
pw: BTreeMap::<String, Arc<Mutex<ListItem>>>,
/// create a mostly static list of references that we can use to index into the corresponding database
/// this gets around both lifetime issues by putting the references in this scope, and also gets
/// around slow alloc issues having to deal with copies when avoiding lifetimes.
filtered_list: Vec::<Option<Arc<Mutex<ListItem>>>>,
/// records the longest possible length of all three lists, so filtered_list() capacity can be adjusted accordingly
max_len: usize,
/// memoize the filtered length so we don't have to search it every time
valid_len: usize,
items_per_screen: NonZeroUsize,
selection_index: usize,
}
impl ItemLists {
pub fn new() -> Self {
ItemLists {
fido: BTreeMap::new(),
totp: BTreeMap::new(),
pw: BTreeMap::new(),
filtered_list: Vec::new(),
max_len: 0,
valid_len: 0,
items_per_screen: NonZeroUsize::new(1).unwrap(),
selection_index: 0,
}
}
pub fn insert(&mut self, list_type: VaultMode, key: String, item: ListItem) -> Option<ListItem> {
let maybe_replaced = match list_type {
VaultMode::Fido => self.fido.insert(key, Arc::new(Mutex::new(item))),
VaultMode::Totp => self.totp.insert(key, Arc::new(Mutex::new(item))),
VaultMode::Password => self.pw.insert(key, Arc::new(Mutex::new(item))),
};
let new_len = match list_type {
VaultMode::Fido => self.fido.len(),
VaultMode::Totp => self.totp.len(),
VaultMode::Password => self.pw.len(),
};
for _ in 0..new_len.saturating_sub(self.filtered_list.len()) {
// fills in any growth with null records, guaranteeing that
// we can always index into the vector.
self.filtered_list.push(None);
}
self.max_len = new_len;
if let Some(replaced) = maybe_replaced {
Some(
Arc::<_>::try_unwrap(replaced).unwrap().into_inner().unwrap()
)
} else {
None
}
}
pub fn get(&self, list_type: VaultMode, key: &String) -> Option<&Arc<Mutex<ListItem>>> {
match list_type {
VaultMode::Fido => self.fido.get(key),
VaultMode::Password => self.pw.get(key),
VaultMode::Totp => self.pw.get(key),
}
}
pub fn remove(&mut self, list_type: VaultMode, key: String) -> Option<ListItem> {
let maybe_item = match list_type {
VaultMode::Fido => self.fido.remove(&key),
VaultMode::Totp => self.totp.remove(&key),
VaultMode::Password => self.pw.remove(&key),
};
if let Some(item) = maybe_item {
self.filtered_list.retain(|x|
if let Some(filter_item) = x {
if filter_item.lock().unwrap().guid == item.lock().unwrap().guid {
false // don't retain
} else {
true
}
} else {
true
}
);
Some(
Arc::<_>::try_unwrap(item).unwrap().into_inner().unwrap()
)
} else {
None
}
}
pub fn set_items_per_screen(&mut self, ips: i16) {
self.items_per_screen = NonZeroUsize::new(ips as usize).unwrap_or(NonZeroUsize::new(1).unwrap());
}
pub fn clear_filter(&mut self) {
self.filtered_list.iter_mut().for_each(|i| *i = None);
self.valid_len = 0;
self.selection_index = 0;
}
pub fn clear(&mut self, list_type: VaultMode) {
self.filtered_list.iter_mut().for_each(|i| *i = None);
self.valid_len = 0;
match list_type {
VaultMode::Fido => self.fido.clear(),
VaultMode::Totp => self.totp.clear(),
VaultMode::Password => self.pw.clear(),
}
// maybe do a search for what is the new max_len? for now we can just "leave it", it just means we have some excess capacity which is good: less mallocs.
}
pub fn clear_all(&mut self) {
self.filtered_list.iter_mut().for_each(|i| *i = None);
self.valid_len = 0;
self.fido.clear();
self.totp.clear();
self.pw.clear();
self.max_len = 0;
}
/// Sets up a filter for the selected list type, returns a default selection index.
pub fn filter(&mut self, list_type: VaultMode, criteria: &str) {
let tt = ticktimer_server::Ticktimer::new().unwrap();
let mut ts = [0u64; 5];
ts[0] = tt.elapsed_ms();
// clear the list
self.filtered_list.iter_mut().for_each(|i| *i = None);
ts[1] = tt.elapsed_ms();
self.valid_len = 0;
let itemlist = match list_type {
VaultMode::Fido => &mut self.fido,
VaultMode::Totp => &mut self.totp,
VaultMode::Password => &mut self.pw,
};
ts[2] = tt.elapsed_ms();
let mut filter_index = 0;
for item in itemlist.values_mut() {
if item.lock().unwrap().name.to_lowercase().starts_with(criteria) {
item.lock().unwrap().dirty = true;
self.filtered_list[filter_index] = Some(item.clone());
filter_index += 1;
}
}
ts[3] = tt.elapsed_ms();
self.valid_len = filter_index;
if self.selection_index >= self.valid_len {
if self.valid_len > 0 {
self.selection_index = self.valid_len - 1;
} else {
self.selection_index = 0;
}
}
ts[4] = tt.elapsed_ms();
for(index, &elapsed) in ts[1..].iter().enumerate() {
log::info!("{}: {}", index + 1, elapsed - ts[0]);
}
}
pub fn nav(&mut self, list_type: VaultMode, dir: NavDir) {
match dir {
NavDir::Up => {
if self.selection_index > 0 {
let starting_page = self.get_page();
self.mark_as_dirty(self.selection_index);
self.selection_index -= 1;
self.mark_as_dirty(self.selection_index);
if starting_page != self.get_page() {
self.mark_screen_as_dirty(self.selection_index);
}
}
}
NavDir::Down => {
if self.selection_index < self.valid_len {
let starting_page = self.get_page();
self.mark_as_dirty(self.selection_index);
self.selection_index += 1;
self.mark_as_dirty(self.selection_index);
if starting_page != self.get_page() {
self.mark_screen_as_dirty(self.selection_index);
}
}
}
NavDir::PageUp => {
if self.selection_index > self.items_per_screen.get() {
self.mark_screen_as_dirty(self.selection_index);
self.selection_index -= self.items_per_screen.get();
self.mark_screen_as_dirty(self.selection_index);
} else {
self.mark_as_dirty(self.selection_index);
self.selection_index = 0;
self.mark_as_dirty(self.selection_index);
}
}
NavDir::PageDown => {
if self.selection_index < self.valid_len - self.items_per_screen.get() {
self.mark_screen_as_dirty(self.selection_index);
self.selection_index += self.items_per_screen.get();
self.mark_screen_as_dirty(self.selection_index);
} else {
self.mark_as_dirty(self.selection_index);
self.selection_index = self.valid_len - 1;
self.mark_as_dirty(self.selection_index);
}
}
}
}
fn mark_as_dirty(&mut self, index: usize) {
if self.valid_len > 0 {
if let Some(record) = self.filtered_list[index.min(self.valid_len - 1)].as_mut() {
record.lock().unwrap().dirty = true;
}
}
}
fn mark_screen_as_dirty(&mut self, index: usize) {
let page = index as i16 / self.items_per_screen.get() as i16;
for item in self.filtered_list[
((page as usize) * self.items_per_screen.get()).min(self.valid_len) ..
((1 + page as usize) * self.items_per_screen.get()).min(self.valid_len)
].iter_mut() {
if let Some(record) = item {
record.lock().unwrap().dirty = true;
}
}
}
pub fn get_page(&self) -> usize {
self.selection_index / self.items_per_screen.get()
}
pub fn selected_index(&self) -> usize {
self.selection_index % self.items_per_screen.get()
}
pub fn selected_page(&mut self) -> &mut [Option<Arc<Mutex<ListItem>>>] {
let page = self.get_page();
&mut self.filtered_list[
(page * self.items_per_screen.get()).min(self.valid_len) ..
((1 + page) * self.items_per_screen.get()).min(self.valid_len)
]
}
pub fn selected_guid(&self) -> String {
// we unwrap() here because this function is responsible for consistency of the filtered list.
self.filtered_list[self.selection_index].as_ref().unwrap().lock().unwrap().guid.to_owned()
}
pub fn selected_extra(&self) -> String {
// we unwrap() here because this function is responsible for consistency of the filtered list.
self.filtered_list[self.selection_index].as_ref().unwrap().lock().unwrap().extra.to_owned()
}
pub fn selected_update_extra(&mut self, extra: String) {
self.filtered_list[self.selection_index].as_mut().unwrap().lock().unwrap().extra = extra;
self.filtered_list[self.selection_index].as_mut().unwrap().lock().unwrap().dirty = true;
}
pub fn selected_update_atime(&mut self, atime: u64) {
self.filtered_list[self.selection_index].as_mut().unwrap().lock().unwrap().atime = atime;
self.filtered_list[self.selection_index].as_mut().unwrap().lock().unwrap().dirty = true;
}
pub fn mark_selected_as_dirty(&mut self) {
// we unwrap() here because this function is responsible for consistency of the filtered list.
self.filtered_list[self.selection_index].as_mut().unwrap().lock().unwrap().dirty = true;
}
pub fn selected_entry(&self, mode: VaultMode) -> Option<SelectedEntry> {
if self.selection_index > self.valid_len {
None
} else {
if let Some(entry) = &self.filtered_list[self.selection_index] {
Some(
SelectedEntry {
key_name: xous_ipc::String::from_str(entry.lock().unwrap().guid.to_string()),
description: xous_ipc::String::from_str(entry.lock().unwrap().name.to_string()),
mode
}
)
} else {
None
}
}
}
}

View file

@ -9,6 +9,8 @@ mod prereqs;
mod vendor_commands;
mod storage;
mod migration_v1;
mod itemcache;
use itemcache::*;
use locales::t;
@ -19,14 +21,14 @@ use vault::{
SELF_CONN, Transport, VaultOp
};
use actions::{ActionOp, start_actions_thread};
use crate::ux::framework::{ListItem, NavDir};
use actions::ActionOp;
use crate::ux::framework::NavDir;
use crate::prereqs::ntp_updater;
use crate::vendor_commands::VendorSession;
use ux::framework::{VaultUx, DEFAULT_FONT, FONT_LIST, name_to_style};
use xous_ipc::Buffer;
use xous::{send_message, Message};
use xous::{send_message, Message, msg_blocking_scalar_unpack};
use usbd_human_interface_device::device::fido::*;
use num_traits::*;
@ -34,8 +36,6 @@ use std::thread;
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use std::collections::BTreeMap;
// CTAP2 testing notes:
//
@ -108,16 +108,6 @@ pub struct SelectedEntry {
pub description: xous_ipc::String::<256>,
pub mode: VaultMode,
}
pub struct ItemLists {
pub fido: BTreeMap::<String, ListItem>,
pub totp: BTreeMap::<String, ListItem>,
pub pw: BTreeMap::<String, ListItem>,
}
impl ItemLists {
pub fn new() -> Self {
ItemLists { fido: BTreeMap::new(), totp: BTreeMap::new(), pw: BTreeMap::new() }
}
}
const ERR_TIMEOUT_MS: usize = 5000;
@ -147,14 +137,92 @@ fn main() -> ! {
// redraws of the background list to block/fail.
let actions_sid = xous::create_server().unwrap();
SELF_CONN.store(conn, Ordering::SeqCst);
start_actions_thread(
conn,
actions_sid,
mode.clone(),
item_lists.clone(),
action_active.clone(),
opensk_mutex.clone(),
);
let _ = thread::spawn({
let main_conn = conn.clone();
let sid = actions_sid.clone();
let mode = mode.clone();
let item_lists = item_lists.clone();
let action_active = action_active.clone();
let opensk_mutex = opensk_mutex.clone();
move || {
let mut manager = crate::actions::ActionManager::new(main_conn, mode, item_lists, action_active, opensk_mutex);
loop {
let msg = xous::receive_message(sid).unwrap();
let opcode: Option<ActionOp> = FromPrimitive::from_usize(msg.body.id());
log::debug!("{:?}", opcode);
match opcode {
Some(ActionOp::MenuAddnew) => {
manager.activate();
manager.menu_addnew();
// this is necessary so the next redraw shows the newly added entry
// no cache clear is called for because new entries will always add to the list;
// there is no risk of "stale" entries persisting
manager.retrieve_db();
manager.deactivate();
},
Some(ActionOp::MenuDeleteStage2) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let entry = buffer.to_original::<SelectedEntry, _>().unwrap();
manager.activate();
manager.menu_delete(entry);
manager.retrieve_db();
manager.deactivate();
},
Some(ActionOp::MenuEditStage2) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let entry = buffer.to_original::<SelectedEntry, _>().unwrap();
manager.activate();
manager.menu_edit(entry);
manager.retrieve_db();
manager.deactivate();
},
Some(ActionOp::MenuUnlockBasis) => {
manager.activate();
manager.unlock_basis();
manager.item_lists.lock().unwrap().clear(VaultMode::Password); // clear the cached item list for passwords (totp/fido are not cached and don't need clearing)
manager.retrieve_db();
manager.deactivate();
},
Some(ActionOp::MenuManageBasis) => {
manager.activate();
manager.manage_basis();
manager.item_lists.lock().unwrap().clear(VaultMode::Password); // clear the cached item list for passwords
manager.retrieve_db();
manager.deactivate();
}
Some(ActionOp::MenuClose) => {
// dummy activate/de-activate cycle because we have to trigger a redraw of the underlying UX
manager.activate();
manager.deactivate();
},
Some(ActionOp::UpdateOneItem) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let entry = buffer.to_original::<SelectedEntry, _>().unwrap();
manager.activate();
manager.update_db_entry(entry);
manager.deactivate();
}
Some(ActionOp::UpdateMode) => msg_blocking_scalar_unpack!(msg, _, _, _, _,{
manager.retrieve_db();
xous::return_scalar(msg.sender, 1).unwrap();
}),
Some(ActionOp::Quit) => {
break;
}
None => {
log::error!("msg could not be decoded {:?}", msg);
}
#[cfg(feature="vault-testing")]
Some(ActionOp::GenerateTests) => {
manager.populate_tests();
manager.retrieve_db();
}
}
}
xous::destroy_server(sid).ok();
}
});
let actions_conn = xous::connect(actions_sid).unwrap();
// spawn the FIDO USB->UX update kicker thread. It is responsible for issuing a UX refresh command
@ -369,7 +437,7 @@ fn main() -> ! {
menu_mgr,
actions_conn,
mode.clone(),
item_lists,
item_lists.clone(),
action_active.clone()
);
vaultux.update_mode();

View file

@ -13,66 +13,10 @@ use std::sync::atomic::Ordering as AtomicOrdering;
use std::sync::{Arc, Mutex};
use std::sync::atomic::AtomicBool;
use std::convert::TryFrom;
use std::cmp::Ordering;
use usb_device_xous::UsbDeviceType;
use locales::t;
use num_traits::*;
/// Display list for items. "name" is the key by which the list is sorted.
/// "extra" is more information about the item, which should not be part of the sort.
pub struct ListItem {
pub name: String,
pub extra: String,
pub dirty: bool,
/// this is the name of the key used to refer to the item
pub guid: String,
/// stash a copy so we can compare to the DB record and avoid re-generating the atime/count string if it hasn't changed.
pub atime: u64,
pub count: u64,
}
impl ListItem {
pub fn clone(&self) -> ListItem {
ListItem {
name: self.name.to_string(),
extra: self.extra.to_string(),
dirty: self.dirty,
guid: self.guid.to_string(),
atime: self.atime,
count: self.count,
}
}
/// This is made available for edit/delete routines to generate the key without having to
/// make a whole ListItem record (which is somewhat expensive).
pub fn key_from_parts(name: &str, guid: &str) -> String {
name.to_lowercase() + &guid.to_string()
}
pub fn key(&self) -> String {
Self::key_from_parts(&self.name, &self.guid)
}
}
impl Ord for ListItem {
fn cmp(&self, other: &Self) -> Ordering {
match self.name.cmp(&other.name) {
Ordering::Equal => {
self.guid.cmp(&other.guid)
},
Ordering::Greater => Ordering::Greater,
Ordering::Less => Ordering::Less,
}
}
}
impl PartialOrd for ListItem {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for ListItem {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.guid == other.guid
}
}
impl Eq for ListItem {}
use crate::ListItem;
pub enum NavDir {
Up,
@ -103,8 +47,6 @@ pub struct VaultUx {
item_lists: Arc::<Mutex::<ItemLists>>,
/// list of items displayable after filtering
filtered_list: Vec::<ListItem>,
/// the index into the item_list that is selected
selection_index: usize,
/// last filter query, so we can re-use it when mode is changed
last_query: String,
@ -186,6 +128,7 @@ impl VaultUx {
let glyph_height = gam.glyph_height_hint(style).unwrap();
let item_height = (glyph_height * 2) as i16 + margin.y * 2 + 2; // +2 because of the border width
let items_per_screen = available_height / item_height;
item_lists.lock().unwrap().set_items_per_screen(items_per_screen);
let current_time = get_current_unix_time().unwrap_or(0);
@ -198,7 +141,6 @@ impl VaultUx {
mode,
title_dirty: true,
item_lists,
selection_index: 0,
filtered_list: Vec::new(),
pddb: RefCell::new(pddb),
style,
@ -217,17 +159,18 @@ impl VaultUx {
}
pub(crate) fn basis_change(&mut self) {
self.item_lists.lock().unwrap().pw.clear();
self.item_lists.lock().unwrap().fido.clear();
self.item_lists.lock().unwrap().totp.clear();
self.item_lists.lock().unwrap().clear_all();
}
pub(crate) fn update_mode(&mut self) {
self.title_dirty = true;
self.filtered_list.clear();
self.selection_index = 0;
let query = self.last_query.to_string();
self.filter(&query);
{
let mut guarded_list = self.item_lists.lock().unwrap();
guarded_list.clear_filter();
let query = self.last_query.to_string();
guarded_list
.filter(self.mode.lock().unwrap().clone(), &query);
}
self.swap_submenu();
}
@ -307,6 +250,7 @@ impl VaultUx {
let glyph_height = self.gam.glyph_height_hint(self.style).unwrap();
self.item_height = (glyph_height * 2) as i16 + self.margin.y * 2 + 2; // +2 because of the border width
self.items_per_screen = available_height / self.item_height;
self.item_lists.lock().unwrap().set_items_per_screen(self.items_per_screen);
}
pub(crate) fn set_glyph_style(&mut self, style: GlyphStyle) {
self.pddb.borrow().delete_key(VAULT_CONFIG_DICT, VAULT_CONFIG_KEY_FONT, Some(pddb::PDDB_DEFAULT_SYSTEM_BASIS))
@ -326,79 +270,8 @@ impl VaultUx {
self.pddb.borrow().sync().ok();
self.get_glyph_style();
}
fn mark_as_dirty(&mut self, index: usize) {
let list_len = self.filtered_list.len();
if list_len > 0 {
self.filtered_list[index.min(list_len - 1)].dirty = true;
}
}
fn mark_screen_as_dirty(&mut self, index: usize) {
let page = index as i16 / self.items_per_screen;
let list_len = self.filtered_list.len();
for item in self.filtered_list[
((page as usize) * self.items_per_screen as usize).min(list_len) ..
((1 + page as usize) * self.items_per_screen as usize).min(list_len)
].iter_mut() {
item.dirty = true;
}
}
fn backpropagate_item(&mut self, mut updated_item: ListItem) -> bool {
let il = &mut self.item_lists.lock().unwrap();
let item_list = match self.mode.lock().unwrap().clone() {
VaultMode::Fido => &mut il.fido,
VaultMode::Totp => &mut il.totp,
VaultMode::Password => &mut il.pw,
};
updated_item.dirty = true;
item_list.insert(updated_item.key(), updated_item).is_some()
}
pub(crate) fn nav(&mut self, dir: NavDir) {
match dir {
NavDir::Up => {
if self.selection_index > 0 {
let starting_page = self.get_page();
self.mark_as_dirty(self.selection_index);
self.selection_index -= 1;
self.mark_as_dirty(self.selection_index);
if starting_page != self.get_page() {
self.mark_screen_as_dirty(self.selection_index);
}
}
}
NavDir::Down => {
if self.selection_index < self.filtered_list.len() {
let starting_page = self.get_page();
self.mark_as_dirty(self.selection_index);
self.selection_index += 1;
self.mark_as_dirty(self.selection_index);
if starting_page != self.get_page() {
self.mark_screen_as_dirty(self.selection_index);
}
}
}
NavDir::PageUp => {
if self.selection_index > self.items_per_screen as usize {
self.mark_screen_as_dirty(self.selection_index);
self.selection_index -= self.items_per_screen as usize;
self.mark_screen_as_dirty(self.selection_index);
} else {
self.mark_as_dirty(self.selection_index);
self.selection_index = 0;
self.mark_as_dirty(self.selection_index);
}
}
NavDir::PageDown => {
if self.selection_index < self.filtered_list.len() - self.items_per_screen as usize {
self.mark_screen_as_dirty(self.selection_index);
self.selection_index += self.items_per_screen as usize;
self.mark_screen_as_dirty(self.selection_index);
} else {
self.mark_as_dirty(self.selection_index);
self.selection_index = self.filtered_list.len() - 1;
self.mark_as_dirty(self.selection_index);
}
}
}
self.item_lists.lock().unwrap().nav(self.mode.lock().unwrap().clone(), dir);
}
/// accept a new input string
pub(crate) fn input(&mut self, line: &str) -> Result<(), xous::Error> {
@ -407,9 +280,6 @@ impl VaultUx {
self.last_query = line.to_string();
Ok(())
}
fn get_page(&self) -> i16 {
self.selection_index as i16 / self.items_per_screen
}
fn clear_area(&mut self) {
let items_height = self.items_per_screen * self.item_height;
@ -446,39 +316,41 @@ impl VaultUx {
let mut dirty_tl: Option<Point> = None;
let mut dirty_br: Option<Point> = None;
let page = self.get_page();
let list_len = self.filtered_list.len();
for item in self.filtered_list[
((page as usize) * self.items_per_screen as usize).min(list_len) ..
((1 + page as usize) * self.items_per_screen as usize).min(list_len)
].iter() {
if item.dirty && dirty_tl.is_none() {
// start the dirty area
dirty_tl = Some(Point::new(0, insert_at));
}
if !item.dirty && dirty_tl.is_some() && dirty_br.is_none() {
// end the dirty area
dirty_br = Some(Point::new(self.screensize.y, insert_at));
}
if let Some(tl) = dirty_tl {
if let Some(br) = dirty_br {
// start & end found: now draw a rectangle over it
self.gam.draw_rectangle(self.content,
Rectangle::new_with_style(
tl,
br,
DrawStyle {
fill_color: Some(PixelColor::Light),
stroke_color: None,
stroke_width: 0
}
)).expect("can't clear content area");
// reset the search
dirty_tl = None;
dirty_br = None;
let mut guarded_list = self.item_lists.lock().unwrap();
let current_page = guarded_list.selected_page();
for item in current_page.iter() {
if let Some(item_mutex) = item {
let item = item_mutex.lock().unwrap();
if item.dirty && dirty_tl.is_none() {
// start the dirty area
dirty_tl = Some(Point::new(0, insert_at));
}
if !item.dirty && dirty_tl.is_some() && dirty_br.is_none() {
// end the dirty area
dirty_br = Some(Point::new(self.screensize.y, insert_at));
}
if let Some(tl) = dirty_tl {
if let Some(br) = dirty_br {
// start & end found: now draw a rectangle over it
self.gam.draw_rectangle(self.content,
Rectangle::new_with_style(
tl,
br,
DrawStyle {
fill_color: Some(PixelColor::Light),
stroke_color: None,
stroke_width: 0
}
)).expect("can't clear content area");
// reset the search
dirty_tl = None;
dirty_br = None;
}
}
insert_at += self.item_height;
} else {
log::error!("Filtered list consistency error!")
}
insert_at += self.item_height;
}
if let Some(tl) = dirty_tl {
// handle the case that we were dirty all the way to the bottom
@ -595,78 +467,80 @@ impl VaultUx {
return Ok(());
}
// ---- draw list body area ----
let page = self.get_page();
let selected = self.selection_index as i16 % self.items_per_screen;
let list_len = self.filtered_list.len();
for (index, item) in self.filtered_list[
((page as usize) * self.items_per_screen as usize).min(list_len) ..
((1 + page as usize) * self.items_per_screen as usize).min(list_len)
].iter_mut().enumerate() {
let selected = self.item_lists.lock().unwrap().selected_index();
let mut guarded_list = self.item_lists.lock().unwrap();
let current_page = guarded_list.selected_page();
for (index, maybe_item) in current_page.iter_mut().enumerate() {
if insert_at - 1 > self.screensize.y - self.item_height { // -1 because of the overlapping border
break;
}
if item.dirty {
let mut box_text = TextView::new(self.content,
graphics_server::TextBounds::BoundingBox(
Rectangle::new(
Point::new(0, insert_at),
Point::new(self.screensize.x, insert_at + self.item_height)
if let Some(item_mutex) = maybe_item {
let mut item = item_mutex.lock().unwrap();
if item.dirty {
let mut box_text = TextView::new(self.content,
graphics_server::TextBounds::BoundingBox(
Rectangle::new(
Point::new(0, insert_at),
Point::new(self.screensize.x, insert_at + self.item_height)
)
)
)
);
box_text.draw_border = true;
box_text.rounded_border = None;
box_text.clear_area = true;
box_text.style = self.style;
box_text.margin = self.margin;
if index == selected as usize {
box_text.border_width = 4;
}
match mode_at_entry {
VaultMode::Fido | VaultMode::Password => {write!(box_text, "{}\n{}", item.name, item.extra).ok();},
VaultMode::Totp => {
let fields = item.extra.split(':').collect::<Vec<&str>>();
if fields.len() == 5 {
let shared_secret = base32::decode(
base32::Alphabet::RFC4648 { padding: false }, fields[0])
.unwrap_or(vec![]);
let digit_count = u8::from_str_radix(fields[1], 10).unwrap_or(6);
let step_seconds = u64::from_str_radix(fields[2], 10).unwrap_or(30);
let algorithm = TotpAlgorithm::try_from(fields[3]).unwrap_or(TotpAlgorithm::HmacSha1);
let is_hotp = fields[4].to_uppercase() == "HOTP";
let totp = TotpEntry {
step_seconds: if !is_hotp {step_seconds} else {1}, // step_seconds is re-used by hotp as the code.
shared_secret,
digit_count,
algorithm
};
if !is_hotp {
let code = generate_totp_code(
get_current_unix_time().unwrap_or(0),
&totp
).unwrap_or(t!("vault.error.record_error", xous::LANG).to_string());
// why code on top? because the item.name can be very long, and it can wrap which would cause
// the code to become hidden.
write!(box_text, "{}\n{}", code, item.name).ok();
);
box_text.draw_border = true;
box_text.rounded_border = None;
box_text.clear_area = true;
box_text.style = self.style;
box_text.margin = self.margin;
if index == selected {
box_text.border_width = 4;
}
match mode_at_entry {
VaultMode::Fido | VaultMode::Password => {write!(box_text, "{}\n{}", item.name, item.extra).ok();},
VaultMode::Totp => {
let fields = item.extra.split(':').collect::<Vec<&str>>();
if fields.len() == 5 {
let shared_secret = base32::decode(
base32::Alphabet::RFC4648 { padding: false }, fields[0])
.unwrap_or(vec![]);
let digit_count = u8::from_str_radix(fields[1], 10).unwrap_or(6);
let step_seconds = u64::from_str_radix(fields[2], 10).unwrap_or(30);
let algorithm = TotpAlgorithm::try_from(fields[3]).unwrap_or(TotpAlgorithm::HmacSha1);
let is_hotp = fields[4].to_uppercase() == "HOTP";
let totp = TotpEntry {
step_seconds: if !is_hotp {step_seconds} else {1}, // step_seconds is re-used by hotp as the code.
shared_secret,
digit_count,
algorithm
};
if !is_hotp {
let code = generate_totp_code(
get_current_unix_time().unwrap_or(0),
&totp
).unwrap_or(t!("vault.error.record_error", xous::LANG).to_string());
// why code on top? because the item.name can be very long, and it can wrap which would cause
// the code to become hidden.
write!(box_text, "{}\n{}", code, item.name).ok();
} else {
let code = generate_totp_code(
step_seconds,
&totp
).unwrap_or(t!("vault.error.record_error", xous::LANG).to_string());
// why code on top? because the item.name can be very long, and it can wrap which would cause
// the code to become hidden.
write!(box_text, "HOTP {}\n{}", code, item.name).ok();
}
} else {
let code = generate_totp_code(
step_seconds,
&totp
).unwrap_or(t!("vault.error.record_error", xous::LANG).to_string());
// why code on top? because the item.name can be very long, and it can wrap which would cause
// the code to become hidden.
write!(box_text, "HOTP {}\n{}", code, item.name).ok();
write!(box_text, "{}", t!("vault.error.record_error", xous::LANG)).ok();
}
} else {
write!(box_text, "{}", t!("vault.error.record_error", xous::LANG)).ok();
}
},
},
}
self.gam.post_textview(&mut box_text).expect("couldn't post list item");
item.dirty = false;
}
self.gam.post_textview(&mut box_text).expect("couldn't post list item");
item.dirty = false;
}
insert_at += self.item_height;
insert_at += self.item_height;
} else {
log::error!("Consistency bug! Filtered list has invalid items");
}
}
log::trace!("vault app redraw##");
@ -684,55 +558,23 @@ impl VaultUx {
}
pub(crate) fn filter(&mut self, criteria: &str) {
let tt = ticktimer_server::Ticktimer::new().unwrap();
let mut ts = [0u64; 5];
ts[0] = tt.elapsed_ms();
self.filtered_list.clear();
ts[1] = tt.elapsed_ms();
let il = &self.item_lists.lock().unwrap();
let item_list = match self.mode.lock().unwrap().clone() {
VaultMode::Fido => &il.fido,
VaultMode::Totp => &il.totp,
VaultMode::Password => &il.pw,
};
ts[2] = tt.elapsed_ms();
for item in item_list.values() {
if item.name.to_lowercase().starts_with(criteria) {
let mut staged_item = item.clone();
staged_item.dirty = true;
self.filtered_list.push(staged_item);
}
}
ts[3] = tt.elapsed_ms();
// the selection index must always be at a valid point
if self.selection_index >= self.filtered_list.len() {
if self.filtered_list.len() > 0 {
self.selection_index = self.filtered_list.len() - 1;
} else {
self.selection_index = 0;
}
}
ts[4] = tt.elapsed_ms();
for(index, &elapsed) in ts[1..].iter().enumerate() {
log::debug!("{}: {}", index + 1, elapsed - ts[0]);
}
self.item_lists.lock().unwrap()
.filter(self.mode.lock().unwrap().clone(), criteria);
}
pub(crate) fn set_autotype_delay_ms(&self, rate: usize) {
self.usb_dev.set_autotype_delay_ms(rate);
}
pub(crate) fn autotype(&mut self) -> Result<(), xous::Error> {
if self.selection_index >= self.filtered_list.len() {
return Err(xous::Error::InvalidPID);
}
let mode_cache = (*self.mode.lock().unwrap()).clone();
match mode_cache {
VaultMode::Password => {
let entry = &self.filtered_list[self.selection_index].guid;
let entry = self.item_lists.lock().unwrap().selected_guid();
// we re-fetch the entry for autotype, because the PDDB could have unmounted a basis.
let atime = utc_now().timestamp() as u64;
let updated_pw = match self.pddb.borrow().get(
vault::VAULT_PASSWORD_DICT,
entry,
&entry,
None,
false, false, None,
Some(vault::basis_change)
@ -745,7 +587,7 @@ impl VaultUx {
match self.usb_dev.send_str(&pw.password) {
Ok(_) => {
pw.count += 1;
pw.atime = utc_now().timestamp() as u64;
pw.atime = atime;
pw
},
Err(e) => {
@ -773,7 +615,7 @@ impl VaultUx {
// this get determines which basis the key is in
let basis = match self.pddb.borrow().get(
vault::VAULT_PASSWORD_DICT,
entry,
&entry,
None, true, true,
Some(256), Some(vault::basis_change)
) {
@ -787,14 +629,14 @@ impl VaultUx {
}
};
match self.pddb.borrow().delete_key(vault::VAULT_PASSWORD_DICT, entry, Some(&basis)) {
match self.pddb.borrow().delete_key(vault::VAULT_PASSWORD_DICT, &entry, Some(&basis)) {
Ok(_) => {}
Err(_e) => {
return Err(xous::Error::InternalError);
}
}
match self.pddb.borrow().get(
vault::VAULT_PASSWORD_DICT, entry, Some(&basis),
vault::VAULT_PASSWORD_DICT, &entry, Some(&basis),
false, true, Some(vault::VAULT_ALLOC_HINT),
Some(vault::basis_change)
) {
@ -815,11 +657,11 @@ impl VaultUx {
}
self.pddb.borrow().sync().ok();
// force a redraw of the record as the access count updated
self.filtered_list[self.selection_index].dirty = true;
self.backpropagate_item(self.filtered_list[self.selection_index].clone());
self.item_lists.lock().unwrap().selected_update_atime(atime);
}
VaultMode::Totp => {
let fields = self.filtered_list[self.selection_index].extra.split(':').collect::<Vec<&str>>();
let extra = self.item_lists.lock().unwrap().selected_extra();
let fields = extra.split(':').collect::<Vec<&str>>();
if fields.len() == 5 {
let shared_secret = base32::decode(
base32::Alphabet::RFC4648 { padding: false }, fields[0])
@ -849,7 +691,7 @@ impl VaultUx {
Ok(_) => {
if is_hotp {
// update the count once the HOTP has been typed successfully
let entry = self.filtered_list[self.selection_index].guid.to_string();
let entry = self.item_lists.lock().unwrap().selected_guid();
// this get determines which basis the key is in
let (basis, hotp_rec) = match self.pddb.borrow().get(
@ -892,15 +734,15 @@ impl VaultUx {
}
}
// update the "extra" field, because the timestep field has been altered
self.filtered_list[self.selection_index].extra = format!("{}:{}:{}:{}:{}",
hotp_rec.secret,
hotp_rec.digits,
hotp_rec.timestep,
hotp_rec.algorithm,
if hotp_rec.is_hotp {"HOTP"} else {"TOTP"}
self.item_lists.lock().unwrap().selected_update_extra(
format!("{}:{}:{}:{}:{}",
hotp_rec.secret,
hotp_rec.digits,
hotp_rec.timestep,
hotp_rec.algorithm,
if hotp_rec.is_hotp {"HOTP"} else {"TOTP"}
)
);
self.filtered_list[self.selection_index].dirty = true;
self.backpropagate_item(self.filtered_list[self.selection_index].clone());
// now write to disk
match self.pddb.borrow().get(
vault::VAULT_TOTP_DICT, &entry, Some(&basis),
@ -934,18 +776,9 @@ impl VaultUx {
Ok(())
}
pub(crate) fn selected_entry(&self) -> Option<SelectedEntry> {
if self.selection_index >= self.filtered_list.len() {
None
} else {
let entry = &self.filtered_list[self.selection_index];
Some(
SelectedEntry {
key_name: xous_ipc::String::from_str(entry.guid.to_string()),
mode: (*self.mode.lock().unwrap()).clone(),
description: xous_ipc::String::from_str(entry.name.to_string()),
}
)
}
self.item_lists.lock().unwrap().selected_entry(
(*self.mode.lock().unwrap()).clone()
)
}
pub(crate) fn ensure_hid(&self) {
self.usb_dev.ensure_core(self.usb_type).unwrap();