@@ -179,19 +179,7 @@ impl<'pipe> ApduDispatch<'pipe> {
179179 apdu_type
180180 }
181181 } else {
182- match interface {
183- // acknowledge
184- Interface :: Contact => {
185- self . contact
186- . respond ( Status :: Success . try_into ( ) . unwrap ( ) )
187- . expect ( "Could not respond" ) ;
188- }
189- Interface :: Contactless => {
190- self . contactless
191- . respond ( Status :: Success . try_into ( ) . unwrap ( ) )
192- . expect ( "Could not respond" ) ;
193- }
194- }
182+ self . respond ( Status :: Success . try_into ( ) . unwrap ( ) ) ;
195183
196184 if !command. data ( ) . is_empty ( ) {
197185 info ! ( "chaining {} bytes" , command. data( ) . len( ) ) ;
@@ -462,9 +450,27 @@ impl<'pipe> ApduDispatch<'pipe> {
462450 #[ inline( never) ]
463451 fn respond ( & mut self , message : interchanges:: Data ) {
464452 debug ! ( "<< {}" , hex_str!( message. as_slice( ) , sep: "" ) ) ;
465- match self . current_interface {
466- Interface :: Contactless => self . contactless . respond ( message) . expect ( "cant respond" ) ,
467- Interface :: Contact => self . contact . respond ( message) . expect ( "cant respond" ) ,
453+ let ( res, responder) = match self . current_interface {
454+ Interface :: Contactless => ( self . contactless . respond ( message) , & mut self . contactless ) ,
455+ Interface :: Contact => ( self . contact . respond ( message) , & mut self . contact ) ,
456+ } ;
457+ if res. is_ok ( ) {
458+ return ;
459+ }
460+
461+ let state = responder. state ( ) ;
462+
463+ match state {
464+ interchange:: State :: Idle
465+ | interchange:: State :: BuildingRequest
466+ | interchange:: State :: BuildingResponse
467+ | interchange:: State :: Requested
468+ | interchange:: State :: Responded => panic ! ( "Unexpected state: {state:?}" ) ,
469+ interchange:: State :: CancelingRequested
470+ | interchange:: State :: CancelingBuildingResponse
471+ | interchange:: State :: Canceled => {
472+ responder. acknowledge_cancel ( ) . expect ( "failed to cancel" )
473+ }
468474 }
469475 }
470476}
0 commit comments