88//!
99//! Apps need to implement the App trait to be managed.
1010//!
11- use crate :: command:: SIZE as CommandSize ;
1211use crate :: response:: SIZE as ResponseSize ;
1312use crate :: App ;
1413use crate :: {
1514 interchanges:: { self , Responder } ,
1615 response, Command ,
1716} ;
1817
19- use iso7816:: { command:: FromSliceError , Aid , Instruction , Result , Status } ;
18+ use iso7816:: {
19+ command:: { CommandView , FromSliceError } ,
20+ Aid , Instruction , Result , Status ,
21+ } ;
2022
2123/// Maximum length of a data field of a response that can fit in an interchange message after
2224/// concatenation of SW1SW2
@@ -50,17 +52,17 @@ struct ApduBuffer {
5052}
5153
5254impl ApduBuffer {
53- fn request < const S : usize > ( & mut self , command : & iso7816 :: Command < S > ) {
55+ fn request ( & mut self , command : CommandView < ' _ > ) {
5456 match & mut self . raw {
5557 RawApduBuffer :: Request ( buffered) => {
56- buffered. extend_from_command ( command) . ok ( ) ;
58+ buffered. extend_from_command_view ( command) . ok ( ) ;
5759 }
5860 _ => {
5961 if self . raw != RawApduBuffer :: None {
6062 info ! ( "Was buffering the last response, but aborting that now for this new request." ) ;
6163 }
6264 let mut new_cmd = iso7816:: Command :: try_from ( & [ 0 , 0 , 0 , 0 ] ) . unwrap ( ) ;
63- new_cmd. extend_from_command ( command) . ok ( ) ;
65+ new_cmd. extend_from_command_view ( command) . ok ( ) ;
6466 self . raw = RawApduBuffer :: Request ( new_cmd) ;
6567 }
6668 }
@@ -84,7 +86,7 @@ pub struct ApduDispatch<'pipe> {
8486}
8587
8688impl < ' pipe > ApduDispatch < ' pipe > {
87- fn apdu_type < const S : usize > ( apdu : & iso7816 :: Command < S > , interface : Interface ) -> RequestType {
89+ fn apdu_type ( apdu : CommandView < ' _ > , interface : Interface ) -> RequestType {
8890 info ! ( "instruction: {:?} {}" , apdu. instruction( ) , apdu. p1) ;
8991 if apdu. instruction ( ) == Instruction :: Select && ( apdu. p1 & 0x04 ) != 0 {
9092 Aid :: try_new ( apdu. data ( ) ) . map_or_else (
@@ -119,8 +121,8 @@ impl<'pipe> ApduDispatch<'pipe> {
119121 // but that won't work due to ownership rules
120122 fn find_app < ' a , ' b > (
121123 aid : Option < & Aid > ,
122- apps : & ' a mut [ & ' b mut dyn App < CommandSize , ResponseSize > ] ,
123- ) -> Option < & ' a mut & ' b mut dyn App < CommandSize , ResponseSize > > {
124+ apps : & ' a mut [ & ' b mut dyn App < ResponseSize > ] ,
125+ ) -> Option < & ' a mut & ' b mut dyn App < ResponseSize > > {
124126 // match aid {
125127 // Some(aid) => apps.iter_mut().find(|app| aid.starts_with(app.rid())),
126128 // None => None,
@@ -145,9 +147,9 @@ impl<'pipe> ApduDispatch<'pipe> {
145147 }
146148
147149 #[ inline( never) ]
148- fn buffer_chained_apdu_if_needed < const S : usize > (
150+ fn buffer_chained_apdu_if_needed (
149151 & mut self ,
150- command : iso7816 :: Command < S > ,
152+ command : CommandView < ' _ > ,
151153 interface : Interface ,
152154 ) -> RequestType {
153155 // iso 7816-4 5.1.1
@@ -156,7 +158,7 @@ impl<'pipe> ApduDispatch<'pipe> {
156158 let is_chaining = matches ! ( self . buffer. raw, RawApduBuffer :: Request ( _) ) ;
157159
158160 if is_chaining {
159- self . buffer . request ( & command) ;
161+ self . buffer . request ( command) ;
160162
161163 // Response now needs to be chained.
162164 self . was_request_chained = true ;
@@ -167,12 +169,12 @@ impl<'pipe> ApduDispatch<'pipe> {
167169 if self . buffer . raw == RawApduBuffer :: None {
168170 self . was_request_chained = false ;
169171 }
170- let apdu_type = Self :: apdu_type ( & command, interface) ;
172+ let apdu_type = Self :: apdu_type ( command, interface) ;
171173 match apdu_type {
172174 // Keep buffer the same in case of GetResponse
173175 RequestType :: GetResponse => ( ) ,
174176 // Overwrite for everything else.
175- _ => self . buffer . request ( & command) ,
177+ _ => self . buffer . request ( command) ,
176178 }
177179 apdu_type
178180 }
@@ -193,7 +195,7 @@ impl<'pipe> ApduDispatch<'pipe> {
193195
194196 if !command. data ( ) . is_empty ( ) {
195197 info ! ( "chaining {} bytes" , command. data( ) . len( ) ) ;
196- self . buffer . request ( & command) ;
198+ self . buffer . request ( command) ;
197199 }
198200
199201 // Nothing for the application to consume yet.
@@ -259,7 +261,7 @@ impl<'pipe> ApduDispatch<'pipe> {
259261 Ok ( command) => {
260262 self . response_len_expected = command. expected ( ) ;
261263 // The Apdu may be standalone or part of a chain.
262- self . buffer_chained_apdu_if_needed ( command, interface)
264+ self . buffer_chained_apdu_if_needed ( command. as_view ( ) , interface)
263265 }
264266 Err ( response) => {
265267 // If not a valid APDU, return error and don't pass to app.
@@ -364,7 +366,7 @@ impl<'pipe> ApduDispatch<'pipe> {
364366 #[ inline( never) ]
365367 fn handle_app_select (
366368 & mut self ,
367- apps : & mut [ & mut dyn App < CommandSize , ResponseSize > ] ,
369+ apps : & mut [ & mut dyn App < ResponseSize > ] ,
368370 aid : Aid ,
369371 interface : Interface ,
370372 ) {
@@ -393,7 +395,9 @@ impl<'pipe> ApduDispatch<'pipe> {
393395 info ! ( "Selected app" ) ;
394396 let mut response = response:: Data :: new ( ) ;
395397 let result = match & self . buffer . raw {
396- RawApduBuffer :: Request ( apdu) => app. select ( interface, apdu, & mut response) ,
398+ RawApduBuffer :: Request ( apdu) => {
399+ app. select ( interface, apdu. as_view ( ) , & mut response)
400+ }
397401 _ => panic ! ( "Unexpected buffer state." ) ,
398402 } ;
399403
@@ -409,14 +413,14 @@ impl<'pipe> ApduDispatch<'pipe> {
409413 #[ inline( never) ]
410414 fn handle_app_command (
411415 & mut self ,
412- apps : & mut [ & mut dyn App < CommandSize , ResponseSize > ] ,
416+ apps : & mut [ & mut dyn App < ResponseSize > ] ,
413417 interface : Interface ,
414418 ) {
415419 // if there is a selected app, send it the command
416420 let mut response = response:: Data :: new ( ) ;
417421 if let Some ( app) = Self :: find_app ( self . current_aid . as_ref ( ) , apps) {
418422 let result = match & self . buffer . raw {
419- RawApduBuffer :: Request ( apdu) => app. call ( interface, apdu, & mut response) ,
423+ RawApduBuffer :: Request ( apdu) => app. call ( interface, apdu. as_view ( ) , & mut response) ,
420424 _ => panic ! ( "Unexpected buffer state." ) ,
421425 } ;
422426 self . handle_app_response ( & result, & response) ;
@@ -426,10 +430,7 @@ impl<'pipe> ApduDispatch<'pipe> {
426430 } ;
427431 }
428432
429- pub fn poll (
430- & mut self ,
431- apps : & mut [ & mut dyn App < CommandSize , ResponseSize > ] ,
432- ) -> Option < Interface > {
433+ pub fn poll ( & mut self , apps : & mut [ & mut dyn App < ResponseSize > ] ) -> Option < Interface > {
433434 // Only take on one transaction at a time.
434435 let request_type = self . check_for_request ( ) ;
435436
0 commit comments