예제 #1
0
        protected internal override void ConnectCard(Protocol protocol)
        {
            this.BeginAtomic();
            try
            {
                IntPtr Handle;
                try
                {
                    Handle = this.AcquireHandle();
                }
                catch
                {
                    throw new ScpException(LowLevelError.CardCommunicationError);
                }

                Scap ScpProtocol;
                switch (protocol)
                {
                case Protocol.T0:
                    ScpProtocol = Scap.T0;
                    break;

                case Protocol.T1:
                    ScpProtocol = Scap.T1;
                    break;

                default:
                    throw new Exception("Unknown Protocol");
                }

                ScpCommands.SelectApplicationProtocol(Handle, ScpProtocol);
                ScpCommands.PowerOn(Handle, true);

                this.connected = true;
            }
            finally
            {
                this.EndAtomic();
            }
        }
예제 #2
0
        private void PollCardReaderState()
        {
            CardReaderState NewState            = CardReaderState.Unknown;
            bool            SimulateCardRemoved = false;

            this.BeginAtomic();
            try
            {
                IntPtr Handle = this.AcquireHandle();

                try
                {
                    // Get Card status
                    ScpCommands.CardStatus Status = ScpCommands.GetStatus(Handle);
                    switch (Status)
                    {
                    case ScpCommands.CardStatus.NoCard:
                        NewState = CardReaderState.NoCard;
                        break;

                    case ScpCommands.CardStatus.CardInsertedPowerOff:
                        NewState = CardReaderState.CardPresent;
                        break;

                    case ScpCommands.CardStatus.CardInsertedPowerOn:
                        NewState = CardReaderState.CardExclusivelyInUse;
                        break;

                    default:
                        NewState = CardReaderState.Unknown;
                        break;
                    }
                    // Get ATR
                    if (Status == ScpCommands.CardStatus.CardInsertedPowerOff) //do NOT check power on cards, because the ATR cannot chnage when the card is powered!
                    {
                        ScpCommands.SelectApplicationProtocol(Handle, Scap.T0);
                        byte[] Atr = ScpCommands.PowerOn(Handle, false);
                        if (this.atr != null && !Atr.HasEqualValue(this.atr))
                        {
                            //Card was changed during polls -> simulate 'card removed' event
                            SimulateCardRemoved = true;
                        }
                        this.atr = Atr;
                        ScpCommands.PowerOff(Handle);
                    }
                    else if (Status == ScpCommands.CardStatus.CardInsertedPowerOn && this.handleCount == 0)
                    {
                        // the card is powered, but we didn't do it (is done at 'connect', but then the handle count must be greater)
                        NewState = CardReaderState.Unknown;
                    }
                }
// ReSharper disable EmptyGeneralCatchClause
                catch
                {
                    //Do nothing in case of error (next loop)
                }
                finally
                {
                    this.ReleaseHandle();
                }
            }
            catch
            {
                //Do nothing in case of error (next loop)
            }
            finally
            {
                this.EndAtomic();
            }
// ReSharper restore EmptyGeneralCatchClause


            if (SimulateCardRemoved)
            {
                this.state = CardReaderState.NoCard;
                this.InvokeStateChanged();
            }
            if (this.State != NewState)
            {
                this.state = NewState;
                this.InvokeStateChanged();
            }

            Thread.Sleep(1000);
        }