@@ -5,16 +5,18 @@ use async_trait::async_trait;
55use std:: fmt;
66use std:: fmt:: { Debug , Display , Formatter } ;
77use std:: time:: Duration ;
8- use tokio:: sync:: mpsc;
8+ use tokio:: sync:: broadcast;
9+ use tokio:: sync:: mpsc:: { self , Sender } ;
910#[ allow( unused_imports) ]
1011use tracing:: { Level , debug, instrument, trace, warn} ;
1112
12- use crate :: UxUpdate ;
13+ use crate :: webauthn:: error:: Error ;
14+ use crate :: UvUpdate ;
1315use crate :: proto:: ctap1:: apdu:: { ApduRequest , ApduResponse } ;
1416use crate :: proto:: ctap2:: cbor:: { CborRequest , CborResponse } ;
1517use crate :: transport:: channel:: { AuthTokenData , Channel , ChannelStatus , Ctap2AuthTokenStore } ;
1618use crate :: transport:: device:: SupportedProtocols ;
17- use crate :: transport:: error:: { Error , TransportError } ;
19+ use crate :: transport:: error:: TransportError ;
1820
1921use super :: commands:: { command_ctap_msg, command_get_response} ;
2022
@@ -23,6 +25,8 @@ const SELECT_P2: u8 = 0x00;
2325const APDU_FIDO : & [ u8 ; 8 ] = b"\xa0 \x00 \x00 \x06 \x47 \x2f \x00 \x01 " ;
2426const SW1_MORE_DATA : u8 = 0x61 ;
2527
28+ pub type CancelNfcOperation = ( ) ;
29+
2630#[ derive( thiserror:: Error ) ]
2731pub enum NfcError {
2832 /// APDU error returned by the card.
@@ -68,15 +72,27 @@ pub trait HandlerInCtx<Ctx> {
6872
6973pub trait NfcBackend < Ctx > : HandlerInCtx < Ctx > + Display { }
7074
75+ #[ derive( Debug , Clone ) ]
76+ pub struct NfcChannelHandle {
77+ tx : Sender < CancelNfcOperation > ,
78+ }
79+
80+ impl NfcChannelHandle {
81+ pub async fn cancel_ongoing_operation ( & self ) {
82+ let _ = self . tx . send ( ( ) ) . await ;
83+ }
84+ }
85+
7186pub struct NfcChannel < Ctx >
7287where
7388 Ctx : Copy + Sync ,
7489{
7590 delegate : Box < dyn NfcBackend < Ctx > + Send + Sync > ,
7691 auth_token_data : Option < AuthTokenData > ,
77- tx : mpsc:: Sender < UxUpdate > ,
92+ ux_update_sender : broadcast:: Sender < UvUpdate > ,
93+ handle : NfcChannelHandle ,
7894 ctx : Ctx ,
79- apdu_response : Option < ApduResponse > ,
95+ _apdu_response : Option < ApduResponse > ,
8096 cbor_response : Option < CborResponse > ,
8197 supported : SupportedProtocols ,
8298 status : ChannelStatus ,
@@ -98,14 +114,17 @@ where
98114 pub fn new (
99115 delegate : Box < dyn NfcBackend < Ctx > + Send + Sync > ,
100116 ctx : Ctx ,
101- tx : mpsc:: Sender < UxUpdate > ,
102117 ) -> Self {
118+ let ( ux_update_sender, _) = broadcast:: channel ( 16 ) ;
119+ let ( handle_tx, _handle_rx) = mpsc:: channel ( 1 ) ;
120+ let handle = NfcChannelHandle { tx : handle_tx } ;
103121 NfcChannel {
104122 delegate,
105123 auth_token_data : None ,
106- tx,
124+ ux_update_sender,
125+ handle,
107126 ctx,
108- apdu_response : None ,
127+ _apdu_response : None ,
109128 cbor_response : None ,
110129 supported : SupportedProtocols {
111130 fido2 : false ,
@@ -115,6 +134,10 @@ where
115134 }
116135 }
117136
137+ pub fn get_handle ( & self ) -> NfcChannelHandle {
138+ self . handle . clone ( )
139+ }
140+
118141 #[ instrument( skip_all) ]
119142 pub async fn wink ( & mut self , _timeout : Duration ) -> Result < bool , Error > {
120143 warn ! ( "WINK capability is not supported" ) ;
@@ -192,6 +215,8 @@ impl<'a, Ctx> Channel for NfcChannel<Ctx>
192215where
193216 Ctx : Copy + Send + Sync + fmt:: Debug + Display ,
194217{
218+ type UxUpdate = UvUpdate ;
219+
195220 async fn supported_protocols ( & self ) -> Result < SupportedProtocols , Error > {
196221 Ok ( self . supported )
197222 }
@@ -205,7 +230,7 @@ where
205230 }
206231
207232 #[ instrument( level = Level :: DEBUG , skip_all) ]
208- async fn apdu_send ( & self , request : & ApduRequest , _timeout : Duration ) -> Result < ( ) , Error > {
233+ async fn apdu_send ( & self , _request : & ApduRequest , _timeout : Duration ) -> Result < ( ) , Error > {
209234 todo ! ( "apdu_send" )
210235 }
211236
@@ -272,8 +297,8 @@ where
272297 . ok_or ( Error :: Transport ( TransportError :: InvalidFraming ) )
273298 }
274299
275- fn get_state_sender ( & self ) -> & mpsc :: Sender < UxUpdate > {
276- & self . tx
300+ fn get_ux_update_sender ( & self ) -> & broadcast :: Sender < UvUpdate > {
301+ & self . ux_update_sender
277302 }
278303}
279304
0 commit comments