diff --git a/src/BTCPayServer.NTag424.PCSC/BTCPayServer.NTag424.PCSC.csproj b/src/BTCPayServer.NTag424.PCSC/BTCPayServer.NTag424.PCSC.csproj index cb0dba0..860137f 100644 --- a/src/BTCPayServer.NTag424.PCSC/BTCPayServer.NTag424.PCSC.csproj +++ b/src/BTCPayServer.NTag424.PCSC/BTCPayServer.NTag424.PCSC.csproj @@ -4,7 +4,7 @@ net6.0 enable enable - 1.0.10 + 1.0.11 diff --git a/src/BTCPayServer.NTag424.PCSC/PCSCContext.cs b/src/BTCPayServer.NTag424.PCSC/PCSCContext.cs index 234fbea..1e47c8c 100644 --- a/src/BTCPayServer.NTag424.PCSC/PCSCContext.cs +++ b/src/BTCPayServer.NTag424.PCSC/PCSCContext.cs @@ -1,3 +1,4 @@ +using System.Runtime.CompilerServices; using PCSC; using PCSC.Extensions; @@ -112,6 +113,33 @@ waitStateChange: }, TaskCreationOptions.LongRunning); } + public Task WaitForDisconnected(CancellationToken cancellationToken = default) + { + return Task.Factory.StartNew(() => + { + using var registration = cancellationToken.Register(() => Context.Cancel()); + IntPtr timeout = IntPtr.Zero; + var readerStates = new[] + { + new SCardReaderState() + { + ReaderName = CardReader.ReaderName, + CurrentState = SCRState.Unaware + } + }; +waitStateChange: + var res = Context.GetStatusChange(timeout, readerStates); + timeout = SCardContext.INFINITE; + readerStates[0].CurrentStateValue = readerStates[0].EventStateValue; + if (res == SCardError.Cancelled) + throw new OperationCanceledException(cancellationToken); + if (res != SCardError.Success) + return; + if (readerStates[0].EventState.CardIsAbsent()) + return; + goto waitStateChange; + }, TaskCreationOptions.LongRunning); + } public void Dispose() { CardReader.Dispose(); diff --git a/src/BTCPayServer.NTag424/BTCPayServer.NTag424.csproj b/src/BTCPayServer.NTag424/BTCPayServer.NTag424.csproj index 2fcb784..b3e2e9e 100644 --- a/src/BTCPayServer.NTag424/BTCPayServer.NTag424.csproj +++ b/src/BTCPayServer.NTag424/BTCPayServer.NTag424.csproj @@ -5,7 +5,7 @@ net6.0 10.0 enable - 1.0.10 + 1.0.11 diff --git a/tests/UnitTest1.cs b/tests/UnitTest1.cs index a345e3a..e03ffb5 100644 --- a/tests/UnitTest1.cs +++ b/tests/UnitTest1.cs @@ -207,7 +207,8 @@ public class UnitTest1 [Fact] public async Task CanWaitForCard() { - await PCSCContext.WaitForCard(); + using var ctx = await PCSCContext.WaitForCard(); + await ctx.WaitForDisconnected(); } [Fact]