internal void UpdateReaderStates()
        {
            //suspend poll thread & wait until current update is finished
            this.synchronousStateUpdateInProgress.Reset();
            this.scardApi.Cancel();
            this.currentStatusUpdateFinished.WaitOne();

            SCardCardReaderState[] CardReaderstates = new SCardCardReaderState[this.Readers.Count];

            for (int Index = 0; Index < this.Readers.Count; Index++)
            {
                CardReaderstates[Index] = ((PcscCardReader)this.Readers[Index]).CardReaderState;
                CardReaderstates[Index].dwCurrentState = SCardReaderState.Unaware;
            }

            if (this.scardApi.GetStatusChange(-1, CardReaderstates))
            {
                //Only update if not cancelled (at app end)
                foreach (SCardCardReaderState Readerstate in CardReaderstates)
                {
                    if ((Readerstate.dwEventState & SCardReaderState.Changed) != 0)
                    {
                        this.UpdateReaderState(Readerstate, Readerstate.szCardReader);
                    }
                }
            }

            //resume polling thread
            this.synchronousStateUpdateInProgress.Set();
        }
        private void UpdateReaderState(SCardCardReaderState state, string cardReaderName)
        {
            PcscCardReader CardReader = (PcscCardReader)this.Readers[cardReaderName];

            SCardReaderState NewState = state.dwEventState;

            if ((NewState & SCardReaderState.Unavailable) != 0 && this.scardApi.ListReaders().Contains(cardReaderName) == false)
            {
                //CardReader was removed, if state changes to unavailiable _and_ reader is not in the list anymore.
                //Otherwise, reader was opened in direct mode
                this.RemoveCardReader(CardReader);
                return;
            }
            else
            {
                try
                {
                    // CardReader state changed
                    CardReader.CardReaderState = state;
                    return;
                }
                catch (Exception Error)
                {
                    Trace.Fail($"Exception was thrown inside status change handler. This may lead to a blocking application! Origianl Exception was: {Error.Message}");
                    throw;
                }
            }
        }
            private void UpdateCardReaderStatus()
            {
                SCardCardReaderState[] CardReaderstates = new SCardCardReaderState[this.owner.Readers.Count + 1];

                for (int Index = 0; Index < this.owner.Readers.Count; Index++)
                {
                    CardReaderstates[Index] = ((PcscCardReader)this.owner.Readers[Index]).CardReaderState;
                }

                SCardCardReaderState PnpNotificationReaderstate = new SCardCardReaderState
                {
                    szCardReader   = PollThread.PnpNotificationDeviceName,
                    dwCurrentState =
                        this.pnpNotificationDeviceState
                };

                CardReaderstates[CardReaderstates.Length - 1] = PnpNotificationReaderstate;

                if (this.scardApi.GetStatusChange(-1, CardReaderstates))
                {
                    //Only update if not cancelled (at app end)
                    foreach (SCardCardReaderState Readerstate in CardReaderstates)
                    {
                        if ((Readerstate.dwEventState & SCardReaderState.Changed) != 0)
                        {
                            this.HandleCardReaderStateChange(Readerstate.szCardReader, Readerstate);
                        }
                    }
                }
            }
Example #4
0
 internal PcscCardReader(PcscSmartCardSubsystem pcscSubsystem, SCardApi scardApi, string name)
     : base(name)
 {
     this.pcscSubsystem   = pcscSubsystem;
     this.scardApi        = scardApi;
     this.cardReaderState = new SCardCardReaderState
     {
         dwCurrentState = SCardReaderState.Unaware,
         szCardReader   = name
     };
 }
 private void HandleCardReaderStateChange(string cardReaderName, SCardCardReaderState state)
 {
     if (cardReaderName == PollThread.PnpNotificationDeviceName)
     {
         this.pnpNotificationDeviceState = state.dwEventState;
         this.owner.InsertNewCardReaders();
     }
     else
     {
         this.owner.UpdateReaderState(state, cardReaderName);
         return;
     }
 }