public static void ThrowIfNotSuccess(this SCardError error, PcscExceptionHandler onException = null) { if (error != SCardError.Successs) { Throw(error, onException); } }
public void Cancel(PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscContext), nameof(Cancel)); } Provider.SCardCancel(Handle).ThrowIfNotSuccess(onException); }
private void ReleaseInternal(PcscExceptionHandler onException = null) { if (!IsEstablished) { return; } Provider.SCardReleaseContext(Handle).ThrowIfNotSuccess(onException); Handle = SCardContext.Default; }
public PcscConnection BeginTransaction(PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscConnection), nameof(BeginTransaction)); } Provider.SCardBeginTransaction(Handle).ThrowIfNotSuccess(onException); return(this); }
public PcscContext Release(PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscContext), nameof(Release)); } ReleaseInternal(onException); return(this); }
public PcscContext Validate(PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscContext), nameof(Validate)); } Provider.SCardIsValidContext(Handle).ThrowIfNotSuccess(onException); return(this); }
public static void Throw(this SCardError error, PcscExceptionHandler onException = null) { var exception = new PcscException(error); if (onException != null) { onException(exception); if (!exception.ThrowIt) { return; } } throw exception; }
public unsafe PcscContext Establish(SCardScope scope, PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscContext), nameof(Establish)); } if (IsEstablished) { throw new InvalidOperationException("Context has been established."); } SCardContext handle; Provider.SCardEstablishContext(scope, null, null, &handle).ThrowIfNotSuccess(onException); Handle = handle; return(this); }
public unsafe byte[] Control(int code, byte[] send, int bufferSize, PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscConnection), nameof(Control)); } if (bufferSize <= 0) { if (send == null) { Provider.SCardControl(Handle, code, null, 0, null, 0, &bufferSize).ThrowIfNotSuccess(onException); } else { fixed(byte *fSend = send) { Provider.SCardControl(Handle, code, fSend, send.Length, null, 0, &bufferSize).ThrowIfNotSuccess(onException); } } return(new byte[0]); } else { var recv = new byte[bufferSize]; fixed(byte *fRecv = recv) { if (send == null) { Provider.SCardControl(Handle, code, null, 0, fRecv, bufferSize, &bufferSize).ThrowIfNotSuccess(onException); } else { fixed(byte *fSend = send) { Provider.SCardControl(Handle, code, fSend, send.Length, fRecv, bufferSize, &bufferSize).ThrowIfNotSuccess(onException); } } } if (bufferSize <= 0) { return(new byte[0]); } Array.Resize(ref recv, bufferSize); return(recv); } }
public IEnumerable <string> GetReaderNames(string group, PcscExceptionHandler onException = null) { var readerNames = Provider.GetReaderNames(SCardContext.Default, group, onException); if (readerNames == null) { yield break; } for (int offset = 0, offsetNull, length = readerNames.Length; ;) { if (offset >= length || (offsetNull = readerNames.IndexOf('\0', offset)) <= offset) { yield break; } yield return(readerNames.Substring(offset, offsetNull - offset)); offset = offsetNull + 1; } }
public IEnumerable <string> GetReaderGroupNames(PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscContext), nameof(GetReaderGroupNames)); } var groupNames = Provider.GetReaderGroupNames(Handle, onException); if (groupNames == null) { yield break; } for (int offset = 0, offsetNull, length = groupNames.Length; ;) { if (offset >= length || (offsetNull = groupNames.IndexOf('\0', offset)) <= offset) { yield break; } yield return(groupNames.Substring(offset, offsetNull - offset)); offset = offsetNull + 1; } }
public static IEnumerable <string> GetReaderNames(SCardReaderGroup group = SCardReaderGroup.NotSpecified, PcscExceptionHandler onException = null) { return(Instance.GetReaderNames(group.GetDefinedValue(), onException)); }
public static IEnumerable <string> GetReaderNames(string group, PcscExceptionHandler onException = null) { return(Instance.GetReaderNames(group, onException)); }
public PcscConnection Disconnect(SCardDisposition disposition = SCardDisposition.Leave, PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscConnection), nameof(Disconnect)); } DisconnectInternal(disposition, onException); return(this); }
public static PcscContext EstablishContext(SCardScope scope, PcscExceptionHandler onException = null) { return(Instance.EstablishContext(scope, onException)); }
public PcscConnection EndTransaction(SCardDisposition disposition = SCardDisposition.Leave, PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscConnection), nameof(EndTransaction)); } Provider.SCardEndTransaction(Handle, disposition).ThrowIfNotSuccess(onException); return(this); }
public PcscContext EstablishContext(SCardScope scope, PcscExceptionHandler onException = null) { return(CreateContext().Establish(scope, onException)); }
public unsafe byte[] Transmit(byte[] send, int bufferSize, PcscExceptionHandler onException = null) { return(Transmit(send, null, bufferSize, onException)); }
public unsafe PcscConnection Reconnect(SCardShare shareMode, SCardProtocols protocol, SCardDisposition initializationDisposition = SCardDisposition.Leave, PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscConnection), nameof(Reconnect)); } Provider.SCardReconnect(Handle, shareMode, protocol, initializationDisposition, &protocol).ThrowIfNotSuccess(onException); ShareMode = shareMode; Protocol = protocol; return(this); }
public unsafe static string GetReaderNames(this IPcscProvider provider, SCardContext handle, string group, PcscExceptionHandler onException) { string readerNames = null; byte * pReaderNames; var charCount = PcscProvider.SCardAutoAllocate; var err = provider.SCardListReaders(handle, group, &pReaderNames, &charCount); try { switch (err) { case SCardError.NoReadersAvailable: // In Windows, it seems to still return a `NULL` character with `SCardError.Success` status even none of reader names is found. break; case SCardError.Successs: /* * Providers can use ANSI (e.g., WinSCard and pcsc-lite) or Unicode (e.g., WinSCard) for the encoding of characters. * In ANSI, `charCount` means the byte count of string. * In Unicode, `charCount` means the number of Unicode characters of string. */ readerNames = provider.AllocateString(pReaderNames, charCount); break; default: err.Throw(onException); break; } } finally { if (pReaderNames != null) { provider.SCardFreeMemory(handle, pReaderNames).ThrowIfNotSuccess(onException); } } return(readerNames); }
public byte[] Control(SCardControlFunction function, byte[] send, int bufferSize, PcscExceptionHandler onException = null) { return(Control(Provider.SCardCtlCode(function), send, bufferSize, onException)); }
public void Cancel(PcscExceptionHandler onException = null) { Context.Cancel(onException); }
private void DisconnectInternal(SCardDisposition disposition = SCardDisposition.Leave, PcscExceptionHandler onException = null) { if (!IsConnect) { return; } Provider.SCardDisconnect(Handle, disposition).ThrowIfNotSuccess(onException); Handle = SCardHandle.Default; ShareMode = SCardShare.Undefined; Protocol = SCardProtocols.Undefined; }
public unsafe byte[] Transmit(byte[] send, byte[] sendInformation, int bufferSize, int recvInformationLength, out byte[] recvInformation, PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscConnection), nameof(Transmit)); } var recv = new byte[bufferSize]; var sendIORequest = Provider.AllocateIORequest(sendInformation?.Length ?? 0); var recvIORequest = Provider.AllocateIORequest(recvInformationLength); fixed(byte *fSend = send, fRecv = recv, fSendIORequest = sendIORequest, fRecvIORequest = recvIORequest) { var pSendIORequest = fSendIORequest; var pRecvIORequest = fRecvIORequest; Provider.WriteIORequest(pSendIORequest, Protocol, sendIORequest.Length, sendInformation); Provider.WriteIORequest(pRecvIORequest, SCardProtocols.Undefined, recvIORequest.Length, null); Provider.SCardTransmit(Handle, pSendIORequest, fSend, send.Length, pRecvIORequest, fRecv, &bufferSize).ThrowIfNotSuccess(onException); SCardProtocols protocol; Provider.ReadIORequest(pRecvIORequest, out protocol, out recvInformation); Protocol = protocol; } Array.Resize(ref recv, bufferSize); return(recv); }
public unsafe byte[] Transmit(byte[] send, byte[] sendInformation, int recvInformationLength, out byte[] recvInformation, PcscExceptionHandler onException = null) { return(Transmit(send, sendInformation, TransmitBufferSize, recvInformationLength, out recvInformation, onException)); }
public unsafe byte[] Transmit(byte[] send, byte[] sendInformation, PcscExceptionHandler onException = null) { return(Transmit(send, sendInformation, TransmitBufferSize, onException)); }
public unsafe PcscReaderStatus WaitForChanged(int timeout = Timeout.Infinite, PcscExceptionHandler onException = null) { if (Context.IsDisposed) throw new ObjectDisposedException(nameof(PcscContext), nameof(WaitForChanged)); fixed(byte *fReaderStates = _readerStates) { var pReaderStates = fReaderStates; try { for (var i = 0; i < Count; ++i) { Provider.WriteReaderState(pReaderStates, i, Provider.AllocateString(Items[i].ReaderName)); } Provider.SCardGetStatusChange(Context.Handle, timeout, pReaderStates, Count).ThrowIfNotSuccess(onException); } finally { for (var i = 0; i < Count; ++i) { Provider.ReadReaderState(pReaderStates, i, out void *pReaderName, out SCardReaderStates currentState, out SCardReaderStates eventState, out byte[] atr); Provider.WriteReaderState(pReaderStates, i, eventState); var readerState = Items[i]; readerState.Atr = atr; readerState.EventNumber = ((int)eventState >> 16) & 0x0000FFFF; readerState.State = eventState & (SCardReaderStates)0x0000FFFF; Provider.FreeString(pReaderName); } } } return(this); }
public unsafe PcscConnection Connect(SCardShare shareMode, SCardProtocols protocol, PcscExceptionHandler onException = null) { if (IsDisposed) { throw new ObjectDisposedException(nameof(PcscConnection), nameof(Connect)); } SCardHandle handle; Provider.SCardConnect(Context.Handle, ReaderName, shareMode, protocol, &handle, &protocol).ThrowIfNotSuccess(onException); Handle = handle; ShareMode = shareMode; Protocol = protocol; return(this); }
public unsafe static string GetReaderGroupNames(this IPcscProvider provider, SCardContext handle, PcscExceptionHandler onException) { byte *pGroupNames = null; var charCount = PcscProvider.SCardAutoAllocate; try { provider.SCardListReaderGroups(handle, &pGroupNames, &charCount).ThrowIfNotSuccess(onException); return(provider.AllocateString(pGroupNames, charCount)); } finally { if (pGroupNames != null) { provider.SCardFreeMemory(handle, pGroupNames).ThrowIfNotSuccess(onException); } } }
public byte[] Control(int code, byte[] send, PcscExceptionHandler onException = null) { return(Control(code, send, ControlBufferSize, onException)); }