public PcscCardReaderConnectionInformation(IntPtr readerHandle, SCardApi scardApi) { string SystemName = Encoding.Unicode.GetString(scardApi.GetAttribute(readerHandle, SCardAttributes.DeviceSystemName).GetSubArray(0, -2));//remove trailing null character this.SystemName = SystemName; uint ChannelInfo = scardApi.GetAttribute(readerHandle, SCardAttributes.ChannelID).ToUInt32(); this.Channel = PcscCardReaderConnectionInformation.GetChannelString(ChannelInfo); byte Characteristics = scardApi.GetAttribute(readerHandle, SCardAttributes.Characteristics)[0]; this.SupportsSwallowing = Characteristics.IsBitSet(1); this.SupportsEject = Characteristics.IsBitSet(2); this.SupportsCapture = Characteristics.IsBitSet(3); this.DefaultClockRate = PcscCardReaderConnectionInformation.ToUInt32(scardApi.GetAttribute(readerHandle, SCardAttributes.DefaultClock)); this.DefaultDataRate = PcscCardReaderConnectionInformation.ToUInt32(scardApi.GetAttribute(readerHandle, SCardAttributes.DefaultDataRate)); }
private void SetConnectionInformation(PcscCardReaderConnectionInformation readerInformation, PcscSmartCardConnectionInformation cardInformation) { this.SetAndInvoke(nameof(PcscCardReader.ReaderConnectionInformation), ref this.readerConnectionInformation, readerInformation); this.SetAndInvoke(nameof(PcscCardReader.CardConnectionInformation), ref this.cardConnectionInformation, cardInformation); }
public override void UpdateConnectionInformation() { IntPtr CardHandle; PcscCardReaderConnectionInformation ReaderInformation; PcscSmartCardConnectionInformation CardInformation; switch (this.State) { case Facades.SmartCard.CardReaderState.Unknown: //Unknown state, better not connect... throw new CardReaderUnavailableException(this); case Facades.SmartCard.CardReaderState.NoCard: case Facades.SmartCard.CardReaderState.CardMute: //No card or inaccessible card. Try to connect to reader in raw mode. CardHandle = this.scardApi.Connect(this.Name, SCardShareMode.Direct, SCardProtocol.None); ReaderInformation = new PcscCardReaderConnectionInformation(CardHandle, this.scardApi); CardInformation = null; this.scardApi.Disconnect(CardHandle, SCardDisposition.Leave); break; case Facades.SmartCard.CardReaderState.CardPresent: case Facades.SmartCard.CardReaderState.CardInUse: //card is inserted but not exclusively used. Connect card possible (or just read info if already connected) if (this.CardHandle != IntPtr.Zero) { ReaderInformation = new PcscCardReaderConnectionInformation(this.CardHandle, this.scardApi); CardInformation = new PcscSmartCardConnectionInformation(this.CardHandle, this.scardApi); } else { try { CardHandle = this.scardApi.Connect(this.Name, SCardShareMode.Shared, SCardProtocol.All); ReaderInformation = new PcscCardReaderConnectionInformation(CardHandle, this.scardApi); CardInformation = new PcscSmartCardConnectionInformation(CardHandle, this.scardApi); this.scardApi.Disconnect(CardHandle, SCardDisposition.Leave); } catch (Exception) { throw new CardReaderUnavailableException(this); } } break; case Facades.SmartCard.CardReaderState.CardExclusivelyInUse: //Possible, if we are connected, but not, if another app is connected if (this.CardHandle != IntPtr.Zero) { ReaderInformation = new PcscCardReaderConnectionInformation(this.CardHandle, this.scardApi); CardInformation = new PcscSmartCardConnectionInformation(this.CardHandle, this.scardApi); } else { throw new CardReaderUnavailableException(this); } break; default: throw new ArgumentOutOfRangeException(); } this.pcscSubsystem.UpdateReaderStates(); this.SetConnectionInformation(ReaderInformation, CardInformation); }