diff --git a/src/lib.rs b/src/lib.rs index 88012a2..dac3615 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,15 +7,14 @@ use std::{ marker::PhantomData, sync::{ atomic::{AtomicBool, Ordering}, - mpsc::{self, Receiver, Sender}, + mpsc::{self, Sender}, }, thread, time::{Duration, Instant}, }; use trussed::{ - backend::{BackendId, CoreOnly, Dispatch}, - client, + backend::{CoreOnly, Dispatch}, service::Service, virt::{self, Platform, StoreProvider}, ClientImplementation, @@ -50,10 +49,10 @@ impl Options { } } -pub trait Apps<'interrupt, C: trussed::Client, D: Dispatch> { +pub trait Apps<'interrupt, S: StoreProvider, D: Dispatch> { type Data; - fn new>(builder: &mut B, data: Self::Data) -> Self; + fn new(service: &mut Service, D>, syscall: Syscall, data: Self::Data) -> Self; #[cfg(feature = "ctaphid")] fn with_ctaphid_apps( @@ -68,10 +67,6 @@ pub trait Apps<'interrupt, C: trussed::Client, D: Dispatch> { ) -> T; } -pub trait ClientBuilder { - fn build(&mut self, id: &str, backends: &'static [BackendId]) -> C; -} - pub struct Runner { store: S, options: Options, @@ -80,7 +75,7 @@ pub struct Runner { _marker: PhantomData, } -impl<'interrupt, S: StoreProvider, D: Dispatch, A: Apps<'interrupt, Client, D>> Runner { +impl<'interrupt, S: StoreProvider, D: Dispatch, A: Apps<'interrupt, S, D>> Runner { pub fn builder(store: S, options: Options) -> Builder { Builder::new(store, options) } @@ -106,8 +101,10 @@ impl<'interrupt, S: StoreProvider, D: Dispatch, A: Apps<'interrupt, Client, D let (mut ccid, mut apdu_dispatch) = ccid::setup(&bus_allocator, &contact, &contactless); let mut usb_device = build_device(&bus_allocator, &self.options); - let mut trussed = Trussed::new(platform, self.dispatch); - let mut apps = A::new(&mut trussed, data); + let mut service = Service::with_dispatch(platform, self.dispatch); + let (syscall_sender, syscall_receiver) = mpsc::channel(); + let syscall = Syscall(syscall_sender); + let mut apps = A::new(&mut service, syscall, data); log::info!("Ready for work"); thread::scope(|s| { @@ -136,7 +133,11 @@ impl<'interrupt, S: StoreProvider, D: Dispatch, A: Apps<'interrupt, Client, D }); // trussed task - s.spawn(move || trussed.process()); + s.spawn(move || { + for _ in syscall_receiver.iter() { + service.process() + } + }); // apps task loop { @@ -189,7 +190,7 @@ impl Builder { } impl Builder { - pub fn build<'interrupt, A: Apps<'interrupt, Client, D>>(self) -> Runner { + pub fn build<'interrupt, A: Apps<'interrupt, S, D>>(self) -> Runner { Runner { store: self.store, options: self.options, @@ -200,41 +201,6 @@ impl Builder { } } -struct Trussed { - service: Service, D>, - syscall: Syscall, - receiver: Receiver<()>, -} - -impl Trussed { - fn new(platform: Platform, dispatch: D) -> Self { - let service = Service::with_dispatch(platform, dispatch); - let (sender, receiver) = mpsc::channel(); - let syscall = Syscall(sender); - Self { - service, - syscall, - receiver, - } - } - - fn process(&mut self) { - for _ in self.receiver.iter() { - self.service.process() - } - } -} - -impl ClientBuilder, D> for Trussed { - fn build(&mut self, id: &str, backends: &'static [BackendId]) -> Client { - client::ClientBuilder::new(id) - .backends(backends) - .prepare(&mut self.service) - .expect("failed to create client") - .build(self.syscall.clone()) - } -} - #[derive(Clone)] pub struct Syscall(Sender<()>);