private void HandleSmartCard(String readerName) { using (ISCardContext ctx = contextFactory.Establish(SCardScope.System)) using (ICardReader reader = ctx.ConnectReader(readerName, SCardShareMode.Shared, SCardProtocol.Any)) { StatusMessage?.Invoke("Connected to tag"); IccDetection cardIdent = new IccDetection(reader); cardIdent.DetectCardType(); StatusMessage?.Invoke("Device class: " + cardIdent.PcscDeviceClass.ToString()); StatusMessage?.Invoke("Card name: " + cardIdent.PcscCardName.ToString()); StatusMessage?.Invoke("ATR: " + BitConverter.ToString(cardIdent.Atr)); if (cardIdent.PcscDeviceClass == DeviceClass.StorageClass && (cardIdent.PcscCardName == CardName.MifareUltralight || cardIdent.PcscCardName == CardName.MifareUltralightC || cardIdent.PcscCardName == CardName.MifareUltralightEV1)) { HandleMifareUL(reader); } else if (cardIdent.PcscDeviceClass == DeviceClass.StorageClass && (cardIdent.PcscCardName == CardName.MifareStandard1K || cardIdent.PcscCardName == CardName.MifareStandard4K)) { HandleMifareStandard(reader); } else { HandleHCEClient(reader); } } }
public Iso7816.ApduResponse SignalACR(string readerName, ACRReaderControl ctrl) { byte ledStateCtrl = 0; if (ctrl.finalRed) { ledStateCtrl |= 1 << 0; } if (ctrl.finalGreen) { ledStateCtrl |= 1 << 1; } if (ctrl.redMask) { ledStateCtrl |= 1 << 2; } if (ctrl.greenMask) { ledStateCtrl |= 1 << 3; } if (ctrl.initRedBlink) { ledStateCtrl |= 1 << 4; } if (ctrl.initGreenBlink) { ledStateCtrl |= 1 << 5; } if (ctrl.redBlinkMask) { ledStateCtrl |= 1 << 6; } if (ctrl.greenBlinkMask) { ledStateCtrl |= 1 << 7; } byte[] ctrlData = new byte[] { ctrl.t1Duration, ctrl.t2Duration, ctrl.reps, ctrl.buzzer }; blockCardEvents += 1; try { using (ISCardContext ctx = contextFactory.Establish(SCardScope.System)) using (ICardReader reader = ctx.ConnectReader(readerName, SCardShareMode.Direct, SCardProtocol.Unset)) { var apdu = new Iso7816.ApduCommand(0xFF, 0x00, 0x40, ledStateCtrl, ctrlData, null); return(reader.Control(apdu)); } } finally { new Thread(() => { Thread.Sleep(((ctrl.t1Duration * 100) + (ctrl.t2Duration * 100)) * ctrl.reps); blockCardEvents -= 1; }).Start(); } // For all I'm aware, this always returns a failure APDU, but the reader does perform the specified action. }
/// <summary> /// Receive the ATR of each reader in <paramref name="readerNames"/> /// by using the <see cref="ICardReader.GetAttrib(SCardAttribute)"/> function /// </summary> /// <param name="context">Connection context</param> /// <param name="readerNames">Readers from which the ATR should be requested</param> private static void DisplayAtrForEachReader(ISCardContext context, IEnumerable <string> readerNames) { foreach (var readerName in readerNames) { try { using (var reader = context.ConnectReader(readerName, SCardShareMode.Shared, SCardProtocol.Any)) { DisplayAtr(reader); } } catch (Exception exception) { Console.WriteLine("Could not connect to reader {0}. No smart card present? ({1})", readerName, exception.GetType()); } } }
/// <summary> /// Displays the card status of each reader in <paramref name="readerNames"/> /// </summary> /// <param name="context">Smartcard context to connect</param> /// <param name="readerNames">Smartcard readers</param> private static void DisplayReaderStatus(ISCardContext context, IEnumerable <string> readerNames) { foreach (var readerName in readerNames) { try { using (var reader = context.ConnectReader(readerName, SCardShareMode.Shared, SCardProtocol.Any)) { PrintReaderStatus(reader); Console.WriteLine(); } } catch (Exception exception) { Console.WriteLine( "No card inserted or reader '{0}' is reserved exclusively by another application.", readerName); Console.WriteLine("Error message: {0} ({1})\n", exception.Message, exception.GetType()); } } }
private string DisplayAtrForEachReader(ISCardContext context, IEnumerable <string> readerNames) { foreach (var readerName in readerNames) { try { using (var reader = context.ConnectReader(readerName, SCardShareMode.Shared, SCardProtocol.Any)) { return(DisplayAtr(reader)); } } catch (Exception exception) { outputError.Text = exception.Message; return(null); } } return(null); }
private void TryTurnOffBeep(string readerName) { if (!readerName.Contains("ACR")) { return; } try { using (ISCardContext ctx = contextFactory.Establish(SCardScope.System)) using (ICardReader reader = ctx.ConnectReader(readerName, SCardShareMode.Direct, SCardProtocol.Unset)) { var apdu = new Iso7816.ApduCommand(0xFF, 0x00, 0x52, 0x00, null, 0x00); var res = reader.Control(apdu); if (res.Succeeded) { StatusMessage?.Invoke("Turned off buzzer on " + reader.Name); } } } catch (Exception) { } }