Skip to content

Commit adb51a2

Browse files
Make App trait generic over response size
The buffer size is an implementation detail of the dispatch implementation. Applications should not depend on it. This patch makes the App trait generic over the response size and changes the request argument to use a slice instead of a reference to a heapless::Vec. If applications need a minimum response size, they can still use a const assertion to enforce it.
1 parent 21f0931 commit adb51a2

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

app/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
#![no_std]
22

3+
use heapless::Vec;
34
use trussed_core::InterruptFlag;
45

56
mod command;
67

78
pub use command::{Command, VendorCommand};
89

9-
pub type Message = heapless::Vec<u8, 7609>;
10-
1110
/// trait interface for a CTAPHID application.
1211
/// The application chooses which commands to register to, and will be called upon
1312
/// when the commands are received in the CTAPHID layer. Only one application can be registered to a particular command.
14-
pub trait App<'interrupt> {
13+
pub trait App<'interrupt, const N: usize> {
1514
/// Get access to the app interrupter
1615
fn interrupt(&self) -> Option<&'interrupt InterruptFlag> {
1716
None
@@ -27,8 +26,8 @@ pub trait App<'interrupt> {
2726
fn call(
2827
&mut self,
2928
command: Command,
30-
request: &Message,
31-
response: &mut Message,
29+
request: &[u8],
30+
response: &mut Vec<u8, N>,
3231
) -> Result<(), Error>;
3332
}
3433

dispatch/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
- Optimize stack usage of `Dispatch::poll`
99
- Replace `trussed` dependency with `trussed-core`.
1010
- Move the `app` and `command` modules into a separate crate, `ctaphid-app`, and re-export it.
11+
- Make `App` trait generic over the response size.
1112

1213
## [0.1.1] - 2022-08-22
1314
- adjust to `interchange` API change

dispatch/src/dispatch.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::sync::atomic::Ordering;
22

3-
use crate::types::{InterchangeResponse, Message, Responder};
3+
use crate::types::{InterchangeResponse, Message, Responder, MESSAGE_SIZE};
44

55
use ctaphid_app::{App, Command, Error};
66
use ref_swap::OptionRefSwap;
@@ -33,8 +33,8 @@ impl<'pipe, 'interrupt> Dispatch<'pipe, 'interrupt> {
3333

3434
fn find_app<'a, 'b>(
3535
command: Command,
36-
apps: &'a mut [&'b mut dyn App<'interrupt>],
37-
) -> Option<&'a mut &'b mut dyn App<'interrupt>> {
36+
apps: &'a mut [&'b mut dyn App<'interrupt, MESSAGE_SIZE>],
37+
) -> Option<&'a mut &'b mut dyn App<'interrupt, MESSAGE_SIZE>> {
3838
apps.iter_mut()
3939
.find(|app| app.commands().contains(&command))
4040
}
@@ -73,7 +73,12 @@ impl<'pipe, 'interrupt> Dispatch<'pipe, 'interrupt> {
7373
}
7474

7575
#[inline(never)]
76-
fn call_app(&mut self, app: &mut dyn App<'interrupt>, command: Command, request: &Message) {
76+
fn call_app(
77+
&mut self,
78+
app: &mut dyn App<'interrupt, MESSAGE_SIZE>,
79+
command: Command,
80+
request: &Message,
81+
) {
7782
let response_buffer = self
7883
.responder
7984
.response_mut()
@@ -104,7 +109,7 @@ impl<'pipe, 'interrupt> Dispatch<'pipe, 'interrupt> {
104109
}
105110

106111
#[inline(never)]
107-
pub fn poll(&mut self, apps: &mut [&mut dyn App<'interrupt>]) -> bool {
112+
pub fn poll(&mut self, apps: &mut [&mut dyn App<'interrupt, MESSAGE_SIZE>]) -> bool {
108113
// We could call take_request directly, but for some reason this doubles stack usage.
109114
let mut message_buffer = Message::new();
110115
if let Ok((command, message)) = self.responder.request() {

dispatch/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub mod types;
1818

1919
pub mod app {
2020
pub use crate::types::AppResult;
21-
pub use ctaphid_app::{App, Command, Error, Message};
21+
pub use ctaphid_app::{App, Command, Error};
2222
}
2323

2424
pub mod command {

dispatch/src/types.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use ctaphid_app::{Error, Message};
1+
pub use ctaphid_app::Error;
22

33
// // 7609 bytes is max message size for ctaphid
44
// type U6144 = <heapless::consts::U4096 as core::ops::Add<heapless::consts::U2048>>::Output;
@@ -7,6 +7,10 @@ pub use ctaphid_app::{Error, Message};
77
// pub type U7609 = heapless::consts::U4096;
88

99
// TODO: find reasonable size
10+
// pub type Message = heapless::Vec<u8, 3072>;
11+
pub const MESSAGE_SIZE: usize = 7609;
12+
13+
pub type Message = heapless::Vec<u8, MESSAGE_SIZE>;
1014
pub type AppResult = core::result::Result<(), Error>;
1115
pub type ShortMessage = heapless::Vec<u8, 1024>;
1216

0 commit comments

Comments
 (0)