Skip to content

Commit

Permalink
Add usbip runner example
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Jan 11, 2023
1 parent 77133a9 commit b9c0507
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ members = [
name = "virtual"
required-features = ["virtual"]

[[example]]
name = "usbip"
required-features = ["apdu-dispatch"]

[dependencies]
heapless = "0.7"
heapless-bytes = "0.3"
Expand Down Expand Up @@ -50,6 +54,10 @@ ron = "0.8"
serde_cbor = "0.11"
hex = { version = "0.4", features = ["serde"] }

# usbip
trussed-usbip = { git = "https://github.com/trussed-dev/pc-usbip-runner", default-features = false, features = ["ccid"]}
usbd-ccid = { git = "https://github.com/Nitrokey/nitrokey-3-firmware", features = ["highspeed-usb"]}

[features]
std = []
virtual = ["std", "vpicc"]
Expand Down
56 changes: 56 additions & 0 deletions examples/usbip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (C) 2022 Nitrokey GmbH
// SPDX-License-Identifier: CC0-1.0

//! USB/IP runner for opcard.
//! Run with cargo run --example --features apdu-dispatch (and optionally rsa4096-gen)

use trussed::virt::{self, Ram, UserInterface};
use trussed::{ClientImplementation, Platform};

use trussed_usbip::Syscall;

const MANUFACTURER: &str = "Nitrokey";
const PRODUCT: &str = "Nitrokey 3";
const VID: u16 = 0x20a0;
const PID: u16 = 0x42b2;

struct OpcardApp {
opcard: opcard::Card<ClientImplementation<Syscall<virt::Platform<Ram>>>>,
}

impl trussed_usbip::Apps<ClientImplementation<Syscall<virt::Platform<Ram>>>, ()> for OpcardApp {
fn new(
make_client: impl Fn(&str) -> ClientImplementation<Syscall<virt::Platform<Ram>>>,
_data: (),
) -> Self {
OpcardApp {
opcard: opcard::Card::new(make_client("opcard"), opcard::Options::default()),
}
}

fn with_ccid_apps<T>(
&mut self,
f: impl FnOnce(&mut [&mut dyn apdu_dispatch::App<7609, 7609>]) -> T,
) -> T {
f(&mut [&mut self.opcard])
}
}

fn main() {
env_logger::init();

let options = trussed_usbip::Options {
manufacturer: Some(MANUFACTURER.to_owned()),
product: Some(PRODUCT.to_owned()),
serial_number: Some("TEST".into()),
vid: VID,
pid: PID,
};
trussed_usbip::Runner::new(virt::Ram::default(), options)
.init_platform(move |platform| {
let ui: Box<dyn trussed::platform::UserInterface + Send + Sync> =
Box::new(UserInterface::new());
platform.user_interface().set_inner(ui);
})
.exec::<OpcardApp, _, _>(|_platform| {});
}

0 comments on commit b9c0507

Please sign in to comment.