Skip to content

Commit

Permalink
Update trussed to remove clients-? features
Browse files Browse the repository at this point in the history
This patch updates trussed to remove the need for the clients-? feature
and manage the endpoints in the runner instead.
  • Loading branch information
robin-nitrokey committed Jan 23, 2025
1 parent 4fe4e4e commit a0e9b85
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ delog = { version = "0.1.6", features = ["std-log"] }
heapless-bytes = "0.3"
littlefs2-core = "0.1"
pretty_env_logger = "0.4.0"
trussed = { version = "0.1", features = ["clients-1"] }
trussed = "0.1"

[features]
default = ["ctaphid", "ccid"]
ctaphid = ["ctaphid-dispatch", "usbd-ctaphid"]
ccid = ["apdu-dispatch", "usbd-ccid"]

[patch.crates-io]
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "6bba8fde36d05c0227769eb63345744e87d84b2b" }
trussed = { git = "https://github.com/trussed-dev/trussed.git", rev = "805fe7657e79e06b3220324481cd6325b94877d7" }
25 changes: 16 additions & 9 deletions examples/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ use clap::Parser;
use clap_num::maybe_hex;
use littlefs2_core::path;
use trussed::{
backend::CoreOnly,
client::{Client, ClientBuilder},
backend::{CoreOnly, NoId},
client::Client,
pipe::{ServiceEndpoint, TrussedChannel},
service::Service,
syscall,
types::Vec,
types::{CoreContext, NoData},
virt::{self, Platform, StoreProvider},
};
use trussed_usbip::Syscall;
Expand Down Expand Up @@ -57,7 +58,7 @@ struct DummyApp<C: Client> {
}

impl<C: Client> DummyApp<C> {
fn rng<const N: usize>(&mut self, response: &mut Vec<u8, N>) {
fn rng<const N: usize>(&mut self, response: &mut heapless_bytes::Bytes<N>) {
let bytes = syscall!(self.client.random_bytes(57)).bytes;
response.extend_from_slice(&bytes).unwrap();
}
Expand Down Expand Up @@ -95,11 +96,17 @@ impl<'a, S: StoreProvider> trussed_usbip::Apps<'a, S, CoreOnly>
{
type Data = ();

fn new(service: &mut Service<Platform<S>, CoreOnly>, syscall: Syscall, _data: ()) -> Self {
let client = ClientBuilder::new(path!("dummy"))
.prepare(service)
.unwrap()
.build(syscall);
fn new(
_service: &mut Service<Platform<S>, CoreOnly>,
endpoints: &mut Vec<ServiceEndpoint<'static, NoId, NoData>>,
syscall: Syscall,
_data: (),
) -> Self {
static CHANNEL: TrussedChannel = TrussedChannel::new();
let (requester, responder) = CHANNEL.split().unwrap();
let context = CoreContext::new(path!("dummy").into());
endpoints.push(ServiceEndpoint::new(responder, context, &[]));
let client = trussed_usbip::Client::new(requester, syscall, None);
let dummy = DummyApp { client };
Self { dummy }
}
Expand Down
21 changes: 16 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{

use trussed::{
backend::{CoreOnly, Dispatch},
pipe::ServiceEndpoint,
service::Service,
virt::{self, Platform, StoreProvider},
ClientImplementation,
Expand All @@ -31,7 +32,7 @@ pub fn set_waiting(waiting: bool) {
IS_WAITING.store(waiting, Ordering::Relaxed)
}

pub type Client<D = CoreOnly> = ClientImplementation<Syscall, D>;
pub type Client<D = CoreOnly> = ClientImplementation<'static, Syscall, D>;

pub type InitPlatform<S> = Box<dyn Fn(&mut Platform<S>)>;

Expand All @@ -52,7 +53,12 @@ impl Options {
pub trait Apps<'interrupt, S: StoreProvider, D: Dispatch> {
type Data;

fn new(service: &mut Service<Platform<S>, D>, syscall: Syscall, data: Self::Data) -> Self;
fn new(
service: &mut Service<Platform<S>, D>,
endpoints: &mut Vec<ServiceEndpoint<'static, D::BackendId, D::Context>>,
syscall: Syscall,
data: Self::Data,
) -> Self;

#[cfg(feature = "ctaphid")]
fn with_ctaphid_apps<T>(
Expand Down Expand Up @@ -80,7 +86,11 @@ pub struct Runner<S: StoreProvider, D, A> {
_marker: PhantomData<A>,
}

impl<'interrupt, S: StoreProvider, D: Dispatch, A: Apps<'interrupt, S, D>> Runner<S, D, A> {
impl<'interrupt, S: StoreProvider, D: Dispatch, A: Apps<'interrupt, S, D>> Runner<S, D, A>
where
D::BackendId: Send + Sync,
D::Context: Send + Sync,
{
pub fn builder(store: S, options: Options) -> Builder<S> {
Builder::new(store, options)
}
Expand All @@ -107,9 +117,10 @@ impl<'interrupt, S: StoreProvider, D: Dispatch, A: Apps<'interrupt, S, D>> Runne

let mut usb_device = build_device(&bus_allocator, &self.options);
let mut service = Service::with_dispatch(platform, self.dispatch);
let mut endpoints = Vec::new();
let (syscall_sender, syscall_receiver) = mpsc::channel();
let syscall = Syscall(syscall_sender);
let mut apps = A::new(&mut service, syscall, data);
let mut apps = A::new(&mut service, &mut endpoints, syscall, data);

log::info!("Ready for work");
thread::scope(|s| {
Expand Down Expand Up @@ -140,7 +151,7 @@ impl<'interrupt, S: StoreProvider, D: Dispatch, A: Apps<'interrupt, S, D>> Runne
// trussed task
s.spawn(move || {
for _ in syscall_receiver.iter() {
service.process()
service.process(&mut endpoints)
}
});

Expand Down

0 comments on commit a0e9b85

Please sign in to comment.