Exemple #1
0
        /// <inheritdoc />
        public SCardReaderState[] GetReaderStatus(string[] readerNames)
        {
            if (readerNames == null)
            {
                throw new ArgumentNullException(nameof(readerNames));
            }

            if (readerNames.Length == 0)
            {
                throw new ArgumentException("You must specify at least one reader.");
            }

            ThrowOnInvalidContext();

            var states = new SCardReaderState[readerNames.Length];

            for (var i = 0; i < states.Length; i++)
            {
                states[i] = new SCardReaderState {
                    ReaderName   = readerNames[i],
                    CurrentState = SCRState.Unaware
                };
            }

            GetStatusChange(IntPtr.Zero, states)
            .ThrowIfNotSuccess();

            return(states);
        }
Exemple #2
0
        /// <summary>Returns the current reader status of all requested readers.</summary>
        /// <param name="readerNames">Requested reader names.</param>
        /// <returns>An array of <see cref="T:PCSC.SCardReaderState" />s that contains the current reader status of each requested reader.</returns>
        /// <remarks>
        ///     <para>This method uses the <see cref="M:PCSC.SCardContext.GetStatusChange(System.IntPtr,PCSC.SCardReaderState[])" /> method with a timeout of zero.</para>
        ///     <example>
        ///         <code lang="C#">
        /// // Retrieve the names of all installed readers.
        /// using (var ctx = new SCardContext()) {
        ///     ctx.Establish(SCardScope.System);
        ///     var readerNames = ctx.GetReaders();
        ///
        ///     // Get the current status for all readers.
        ///     var states = ctx.GetReaderStatus(readerNames);
        ///
        ///     foreach (var state in states) {
        ///         Console.WriteLine("------------------------------");
        ///         Console.WriteLine("Reader: " + state.ReaderName);
        ///         Console.WriteLine("CurrentState: " + state.CurrentState
        ///             + " EventState: " + state.EventState + "\n"
        ///             + "CurrentStateValue: " + state.CurrentStateValue
        ///             + " EventStateValue: " + state.EventStateValue);
        ///         Console.WriteLine("UserData: " + state.UserData.ToString()
        ///             + " CardChangeEventCnt: " + state.CardChangeEventCnt);
        ///         Console.WriteLine("ATR: " + BitConverter.ToString(state.Atr));
        ///     }
        /// }
        ///   </code>
        ///     </example>
        /// </remarks>
        public SCardReaderState[] GetReaderStatus(string[] readerNames)
        {
            if (readerNames == null)
            {
                throw new ArgumentNullException("readerNames");
            }
            if (readerNames.Length == 0)
            {
                throw new ArgumentException("You must specify at least one reader.");
            }

            var states = new SCardReaderState[readerNames.Length];

            for (var i = 0; i < states.Length; i++)
            {
                states[i] = new SCardReaderState {
                    ReaderName   = readerNames[i],
                    CurrentState = SCRState.Unaware
                };
            }

            var rc = GetStatusChange(IntPtr.Zero, states);

            if (rc != SCardError.Success)
            {
                throw new PCSCException(rc, SCardHelper.StringifyError(rc));
            }

            return(states);
        }
Exemple #3
0
        public SCardReaderState[] GetReaderStatus(string[] readernames)
        {
            SCardError rc;

            if (readernames == null)
                throw new ArgumentNullException("readernames");
            if (readernames.Length == 0)
                throw new ArgumentException("You must specify at least one reader.");

            SCardReaderState[] states = new SCardReaderState[readernames.Length];
            for (int i = 0; i < states.Length; i++)
            {
                states[i] = new SCardReaderState();
                states[i].ReaderName = readernames[i];
                states[i].CurrentState = SCRState.Unaware;
            }

            rc = GetStatusChange(IntPtr.Zero, states);
            if (rc != SCardError.Success)
                throw new PCSCException(rc, SCardHelper.StringifyError(rc));

            return states;
        }
        public SCardError GetStatusChange(
            IntPtr timeout,
            SCardReaderState[] readerstates)
        {
            if (contextPtr.Equals(IntPtr.Zero))
                throw new InvalidContextException(SCardError.InvalidHandle);

            SCardError rc = SCardAPI.Lib.GetStatusChange(
                contextPtr,
                timeout,
                readerstates);

            return rc;
        }
        public SCardReaderState[] GetReaderStatus(string[] readernames)
        {
            SCardError rc;

            if (readernames == null)
                throw new ArgumentNullException("readernames");
            if (readernames.Length == 0)
                throw new ArgumentException("You must specify at least one reader.");

            SCardReaderState[] states = new SCardReaderState[readernames.Length];
            for (int i = 0; i < states.Length; i++)
            {
                states[i] = new SCardReaderState();
                states[i].ReaderName = readernames[i];
                states[i].CurrentState = SCRState.Unaware;
            }

            rc = GetStatusChange(IntPtr.Zero, states);
            if (rc != SCardError.Success)
                throw new PCSCException(rc, SCardHelper.StringifyError(rc));

            return states;
        }
Exemple #6
0
        private void StartMonitor()
        {
            monitoring = true;

            SCardReaderState[] state = new SCardReaderState[readernames.Length];

            for (int i = 0; i < readernames.Length; i++)
            {
                state[i] = new SCardReaderState();
                state[i].ReaderName = readernames[i];
                state[i].CurrentState = SCRState.Unaware; /* force SCardGetStatusChange to return
                                                             immediately with the current reader state */
            }

            SCardError rc = context.GetStatusChange(IntPtr.Zero, state);

            if (rc == SCardError.Success)
            {
                // initialize event
                if (Initialized != null)
                {
                    for (int i = 0; i < state.Length; i++)
                    {
                        Initialized(this,
                                    new CardStatusEventArgs(readernames[i],
                                        (state[i].EventState & (~(SCRState.Changed))),
                                        state[i].ATR));
                        previousState[i] = state[i].EventState & (~(SCRState.Changed)); // remove "Changed"
                        previousStateValue[i] = state[i].EventStateValue;
                    }
                }

                while (true)
                {
                    for (int i = 0; i < state.Length; i++)
                    {
                        state[i].CurrentStateValue = previousStateValue[i];
                    }

                    // block until status change occurs
                    rc = context.GetStatusChange(SCardReader.Infinite, state);

                    // Cancel?
                    if (rc != SCardError.Success)
                        break;

                    for (int i = 0; i < state.Length; i++)
                    {
                        SCRState newState = state[i].EventState;
                        newState &= (~(SCRState.Changed)); // remove "Changed"

                        byte[] Atr = state[i].ATR;

                        // Status change
                        if (StatusChanged != null &&
                            (previousState[i] != newState))
                            StatusChanged(this,
                                          new StatusChangeEventArgs(readernames[i],
                                                                    previousState[i],
                                                                    newState,
                                                                    Atr));

                        // Card inserted
                        if (((newState & SCRState.Present) == SCRState.Present) &&
                            ((previousState[i] & SCRState.Empty) == SCRState.Empty))
                            if (CardInserted != null)
                                CardInserted(this,
                                             new CardStatusEventArgs(readernames[i],
                                                                     newState,
                                                                     Atr));

                        // Card removed
                        if (((newState & SCRState.Empty) == SCRState.Empty) &&
                            ((previousState[i] & SCRState.Present) == SCRState.Present))
                            if (CardRemoved != null)
                                CardRemoved(this,
                                            new CardStatusEventArgs(readernames[i],
                                                                    newState,
                                                                    Atr));

                        previousState[i] = newState;
                        previousStateValue[i] = state[i].EventStateValue;
                    }
                }
            }

            monitoring = false;

            if (rc != SCardError.Cancelled)
                if (MonitorException != null)
                    MonitorException(this, new PCSCException(rc,
                        "An error occured during SCardGetStatusChange(..)."));
        }
Exemple #7
0
        private void StartMonitor()
        {
            _monitoring = true;

            var readerStates = new SCardReaderState[_readernames.Length];

            for (var i = 0; i < _readernames.Length; i++) {
                readerStates[i] = new SCardReaderState {
                    ReaderName = _readernames[i],
                    CurrentState = SCRState.Unaware
                };
            }

            var rc = _context.GetStatusChange(IntPtr.Zero, readerStates);

            if (rc == SCardError.Success) {
                // initialize event
                var onInitializedHandler = Initialized;
                if (onInitializedHandler != null) {
                    for (var i = 0; i < readerStates.Length; i++) {
                        onInitializedHandler(this,
                            new CardStatusEventArgs(
                                _readernames[i],
                                (readerStates[i].EventState & (~(SCRState.Changed))),
                                readerStates[i].Atr));

                        _previousState[i] = readerStates[i].EventState & (~(SCRState.Changed)); // remove "Changed"
                        _previousStateValue[i] = readerStates[i].EventStateValue;
                    }
                }

                while (true) {
                    for (var i = 0; i < readerStates.Length; i++) {
                        readerStates[i].CurrentStateValue = _previousStateValue[i];
                    }

                    // block until status change occurs
                    rc = _context.GetStatusChange(_context.Infinite, readerStates);

                    // Cancel?
                    if (rc != SCardError.Success) {
                        break;
                    }

                    for (var i = 0; i < readerStates.Length; i++) {
                        var newState = readerStates[i].EventState;
                        newState &= (~(SCRState.Changed)); // remove "Changed"

                        var atr = readerStates[i].Atr;

                        // Status change
                        var onStatusChangedHandler = StatusChanged;
                        if (onStatusChangedHandler != null && (_previousState[i] != newState)) {
                            onStatusChangedHandler(this,
                                new StatusChangeEventArgs(_readernames[i],
                                    _previousState[i],
                                    newState,
                                    atr));
                        }

                        // Card inserted
                        if (((newState & SCRState.Present) == SCRState.Present) &&
                            ((_previousState[i] & SCRState.Empty) == SCRState.Empty)) {
                            var onCardInsertedHandler = CardInserted;
                            if (onCardInsertedHandler != null) {
                                onCardInsertedHandler(this,
                                    new CardStatusEventArgs(_readernames[i],
                                        newState,
                                        atr));
                            }
                        }

                        // Card removed
                        if (((newState & SCRState.Empty) == SCRState.Empty) &&
                            ((_previousState[i] & SCRState.Present) == SCRState.Present)) {
                            var onCardRemovedHandler = CardRemoved;
                            if (onCardRemovedHandler != null) {
                                onCardRemovedHandler(this,
                                    new CardStatusEventArgs(_readernames[i],
                                        newState,
                                        atr));
                            }
                        }

                        _previousState[i] = newState;
                        _previousStateValue[i] = readerStates[i].EventStateValue;
                    }
                }
            }

            _monitoring = false;

            foreach (var state in readerStates) {
                state.Dispose();
            }

            if (rc == SCardError.Cancelled) {
                return;
            }

            var monitorExceptionHandler = MonitorException;
            if (monitorExceptionHandler != null) {
                monitorExceptionHandler(this, new PCSCException(rc, "An error occured during SCardGetStatusChange(..)."));
            }
        }
        private void StartMonitor()
        {
            try {
                _monitoring = true;

                var readerStates = new SCardReaderState[_readernames.Length];

                for (var i = 0; i < _readernames.Length; i++)
                {
                    readerStates[i] = new SCardReaderState {
                        ReaderName   = _readernames[i],
                        CurrentState = SCRState.Unaware
                    };
                }

                var rc = _context.GetStatusChange(IntPtr.Zero, readerStates);

                if (rc == SCardError.Success)
                {
                    // initialize event
                    for (var i = 0; i < readerStates.Length; i++)
                    {
                        var initState = readerStates[i].EventState & (~(SCRState.Changed));

                        OnInitialized(readerStates[i].Atr, _readernames[i], initState);

                        _previousStates[i]      = initState; // remove "Changed"
                        _previousStateValues[i] = readerStates[i].EventStateValue;
                    }

                    while (!_is_disposed)
                    {
                        for (var i = 0; i < readerStates.Length; i++)
                        {
                            readerStates[i].CurrentStateValue = _previousStateValues[i];
                        }

                        // block until status change occurs
                        rc = _context.GetStatusChange(_context.Infinite, readerStates);

                        // Cancel?
                        if (rc != SCardError.Success)
                        {
                            break;
                        }

                        for (var i = 0; i < readerStates.Length; i++)
                        {
                            var newState = readerStates[i].EventState;
                            newState &= (~(SCRState.Changed)); // remove "Changed"

                            var atr           = readerStates[i].Atr;
                            var previousState = _previousStates[i];
                            var readerName    = _readernames[i];

                            // Status change
                            if (previousState != newState)
                            {
                                OnStatusChanged(atr, readerName, previousState, newState);
                            }

                            // Card inserted
                            if (newState.CardIsPresent() && previousState.CardIsAbsent())
                            {
                                OnCardInserted(atr, readerName, newState);
                            }

                            // Card removed
                            if (newState.CardIsAbsent() && previousState.CardIsPresent())
                            {
                                OnCardRemoved(atr, readerName, newState);
                            }

                            _previousStates[i]      = newState;
                            _previousStateValues[i] = readerStates[i].EventStateValue;
                        }
                    }
                }

                foreach (var state in readerStates)
                {
                    state.Dispose();
                }

                if (!_is_disposed && (rc != SCardError.Cancelled))
                {
                    OnMonitorException(rc, "An error occured during SCardGetStatusChange(..).");
                }
            } finally {
                _readernames         = null;
                _previousStateValues = null;
                _previousStates      = null;

                _monitoring    = false;
                _monitorthread = null;
            }
        }
Exemple #9
0
        private void StartMonitor()
        {
            _monitoring = true;

            var readerStates = new SCardReaderState[_readernames.Length];

            for (var i = 0; i < _readernames.Length; i++)
            {
                readerStates[i] = new SCardReaderState {
                    ReaderName   = _readernames[i],
                    CurrentState = SCRState.Unaware
                };
            }

            var rc = _context.GetStatusChange(IntPtr.Zero, readerStates);

            if (rc == SCardError.Success)
            {
                // initialize event
                var onInitializedHandler = Initialized;
                if (onInitializedHandler != null)
                {
                    for (var i = 0; i < readerStates.Length; i++)
                    {
                        onInitializedHandler(this,
                                             new CardStatusEventArgs(
                                                 _readernames[i],
                                                 (readerStates[i].EventState & (~(SCRState.Changed))),
                                                 readerStates[i].Atr));

                        _previousState[i]      = readerStates[i].EventState & (~(SCRState.Changed)); // remove "Changed"
                        _previousStateValue[i] = readerStates[i].EventStateValue;
                    }
                }

                while (true)
                {
                    for (var i = 0; i < readerStates.Length; i++)
                    {
                        readerStates[i].CurrentStateValue = _previousStateValue[i];
                    }

                    // block until status change occurs
                    rc = _context.GetStatusChange(_context.Infinite, readerStates);

                    // Cancel?
                    if (rc != SCardError.Success)
                    {
                        break;
                    }

                    for (var i = 0; i < readerStates.Length; i++)
                    {
                        var newState = readerStates[i].EventState;
                        newState &= (~(SCRState.Changed)); // remove "Changed"

                        var atr = readerStates[i].Atr;

                        // Status change
                        var onStatusChangedHandler = StatusChanged;
                        if (onStatusChangedHandler != null && (_previousState[i] != newState))
                        {
                            onStatusChangedHandler(this,
                                                   new StatusChangeEventArgs(_readernames[i],
                                                                             _previousState[i],
                                                                             newState,
                                                                             atr));
                        }

                        // Card inserted
                        if (((newState & SCRState.Present) == SCRState.Present) &&
                            ((_previousState[i] & SCRState.Empty) == SCRState.Empty))
                        {
                            var onCardInsertedHandler = CardInserted;
                            if (onCardInsertedHandler != null)
                            {
                                onCardInsertedHandler(this,
                                                      new CardStatusEventArgs(_readernames[i],
                                                                              newState,
                                                                              atr));
                            }
                        }

                        // Card removed
                        if (((newState & SCRState.Empty) == SCRState.Empty) &&
                            ((_previousState[i] & SCRState.Present) == SCRState.Present))
                        {
                            var onCardRemovedHandler = CardRemoved;
                            if (onCardRemovedHandler != null)
                            {
                                onCardRemovedHandler(this,
                                                     new CardStatusEventArgs(_readernames[i],
                                                                             newState,
                                                                             atr));
                            }
                        }

                        _previousState[i]      = newState;
                        _previousStateValue[i] = readerStates[i].EventStateValue;
                    }
                }
            }

            _monitoring = false;

            foreach (var state in readerStates)
            {
                state.Dispose();
            }

            if (rc == SCardError.Cancelled)
            {
                return;
            }

            var monitorExceptionHandler = MonitorException;

            if (monitorExceptionHandler != null)
            {
                monitorExceptionHandler(this, new PCSCException(rc, "An error occured during SCardGetStatusChange(..)."));
            }
        }
Exemple #10
0
        private void StartMonitor()
        {
            monitoring = true;

            SCardReaderState[] state = new SCardReaderState[readernames.Length];

            for (int i = 0; i < readernames.Length; i++)
            {
                state[i]              = new SCardReaderState();
                state[i].ReaderName   = readernames[i];
                state[i].CurrentState = SCRState.Unaware; /* force SCardGetStatusChange to return
                                                           * immediately with the current reader state */
            }

            SCardError rc = context.GetStatusChange(IntPtr.Zero, state);

            if (rc == SCardError.Success)
            {
                // initialize event
                if (Initialized != null)
                {
                    for (int i = 0; i < state.Length; i++)
                    {
                        Initialized(this,
                                    new CardStatusEventArgs(readernames[i],
                                                            (state[i].EventState & (~(SCRState.Changed))),
                                                            state[i].ATR));
                        previousState[i]      = state[i].EventState & (~(SCRState.Changed)); // remove "Changed"
                        previousStateValue[i] = state[i].EventStateValue;
                    }
                }

                while (true)
                {
                    for (int i = 0; i < state.Length; i++)
                    {
                        state[i].CurrentStateValue = previousStateValue[i];
                    }

                    // block until status change occurs
                    rc = context.GetStatusChange(SCardReader.Infinite, state);

                    // Cancel?
                    if (rc != SCardError.Success)
                    {
                        break;
                    }

                    for (int i = 0; i < state.Length; i++)
                    {
                        SCRState newState = state[i].EventState;
                        newState &= (~(SCRState.Changed)); // remove "Changed"

                        byte[] Atr = state[i].ATR;

                        // Status change
                        if (StatusChanged != null &&
                            (previousState[i] != newState))
                        {
                            StatusChanged(this,
                                          new StatusChangeEventArgs(readernames[i],
                                                                    previousState[i],
                                                                    newState,
                                                                    Atr));
                        }

                        // Card inserted
                        if (((newState & SCRState.Present) == SCRState.Present) &&
                            ((previousState[i] & SCRState.Empty) == SCRState.Empty))
                        {
                            if (CardInserted != null)
                            {
                                CardInserted(this,
                                             new CardStatusEventArgs(readernames[i],
                                                                     newState,
                                                                     Atr));
                            }
                        }

                        // Card removed
                        if (((newState & SCRState.Empty) == SCRState.Empty) &&
                            ((previousState[i] & SCRState.Present) == SCRState.Present))
                        {
                            if (CardRemoved != null)
                            {
                                CardRemoved(this,
                                            new CardStatusEventArgs(readernames[i],
                                                                    newState,
                                                                    Atr));
                            }
                        }

                        previousState[i]      = newState;
                        previousStateValue[i] = state[i].EventStateValue;
                    }
                }
            }

            monitoring = false;

            if (rc != SCardError.Cancelled)
            {
                if (MonitorException != null)
                {
                    MonitorException(this, new PCSCException(rc,
                                                             "An error occured during SCardGetStatusChange(..)."));
                }
            }
        }
        private void StartMonitor()
        {
            try {
                _monitoring = true;

                var readerStates = new SCardReaderState[_readernames.Length];

                for (var i = 0; i < _readernames.Length; i++) {
                    readerStates[i] = new SCardReaderState {
                        ReaderName = _readernames[i],
                        CurrentState = SCRState.Unaware
                    };
                }

                var rc = _context.GetStatusChange(IntPtr.Zero, readerStates);

                if (rc == SCardError.Success) {
                    // initialize event
                    for (var i = 0; i < readerStates.Length; i++) {
                        var initState = readerStates[i].EventState & (~(SCRState.Changed));

                        OnInitialized(readerStates[i].Atr, _readernames[i], initState);

                        _previousStates[i] = initState; // remove "Changed"
                        _previousStateValues[i] = readerStates[i].EventStateValue;
                    }

                    while (!_is_disposed) {
                        for (var i = 0; i < readerStates.Length; i++) {
                            readerStates[i].CurrentStateValue = _previousStateValues[i];
                        }

                        // block until status change occurs
                        rc = _context.GetStatusChange(_context.Infinite, readerStates);

                        // Cancel?
                        if (rc != SCardError.Success) {
                            break;
                        }

                        for (var i = 0; i < readerStates.Length; i++) {
                            var newState = readerStates[i].EventState;
                            newState &= (~(SCRState.Changed)); // remove "Changed"

                            var atr = readerStates[i].Atr;
                            var previousState = _previousStates[i];
                            var readerName = _readernames[i];

                            // Status change
                            if (previousState != newState) {
                                OnStatusChanged(atr, readerName, previousState, newState);
                            }

                            // Card inserted
                            if (newState.CardIsPresent() && previousState.CardIsAbsent()) {
                                OnCardInserted(atr, readerName, newState);
                            }

                            // Card removed
                            if (newState.CardIsAbsent() && previousState.CardIsPresent()) {
                                OnCardRemoved(atr, readerName, newState);
                            }

                            _previousStates[i] = newState;
                            _previousStateValues[i] = readerStates[i].EventStateValue;
                        }
                    }
                }

                foreach (var state in readerStates) {
                    state.Dispose();
                }

                if (!_is_disposed && (rc != SCardError.Cancelled)) {
                    OnMonitorException(rc, "An error occured during SCardGetStatusChange(..).");
                }

            } finally {
                _readernames = null;
                _previousStateValues = null;
                _previousStates = null;

                _monitoring = false;
                _monitorthread = null;
            }
        }
Exemple #12
0
        /// <summary>Blocks execution until the current availability of the cards in a specific set of readers changes.</summary>
        /// <param name="timeout">Maximum waiting time (in milliseconds) for status change, zero or <see cref="INFINITE" /> for infinite.</param>
        /// <param name="readerStates">Structures of readers with current states.</param>
        /// <returns>
        ///     <para>A <see cref="T:PCSC.SCardError" /> indicating an error or the success.The caller receives status changes through the <see cref="T:PCSC.SCardReaderState" /> array.</para>
        ///     <list type="table">
        ///         <listheader>
        ///             <term>Return value</term>
        ///             <description>Description</description>
        ///         </listheader>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.Success" />
        ///             </term>
        ///             <description>Successful (SCARD_S_SUCCESS)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.NoService" />
        ///             </term>
        ///             <description>Server is not running (SCARD_E_NO_SERVICE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.InvalidParameter" />
        ///             </term>
        ///             <description>
        ///                 <paramref name="readerStates" /> is invalid or <see langword="null" /> (SCARD_E_INVALID_PARAMETER)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.InvalidValue" />
        ///             </term>
        ///             <description>Invalid States, reader name, etc (SCARD_E_INVALID_VALUE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.InvalidHandle" />
        ///             </term>
        ///             <description>Invalid context (SCARD_E_INVALID_HANDLE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.ReaderUnavailable" />
        ///             </term>
        ///             <description>The reader is unavailable (SCARD_E_READER_UNAVAILABLE)</description>
        ///         </item>
        ///         <item>
        ///             <term>
        ///                 <see cref="F:PCSC.SCardError.Timeout" />
        ///             </term>
        ///             <description>The user-specified timeout value has expired (SCARD_E_TIMEOUT)</description>
        ///         </item>
        ///     </list>
        /// </returns>
        /// <remarks>
        ///     <para>This method receives a structure or list of structures containing reader names. It then blocks for a change in state to occur for a maximum blocking time of <paramref name="timeout" /> or forever if <see cref="INFINITE" /> is used.</para>
        ///     <para>The new event state will be contained in <see cref="P:PCSC.SCardReaderState.EventState" />. A status change might be a card insertion or removal event, a change in ATR, etc.</para>
        ///     <para>To wait for a reader event (reader added or removed) you may use the special reader name "\\?PnP?\Notification". If a reader event occurs the state of this reader will change and the bit <see cref="F:PCSC.SCRState.Changed" /> will be set.</para>
        ///     <para>This method calls the API function SCardGetStatusChange().</para>
        /// </remarks>
        public SCardError GetStatusChange(IntPtr timeout, SCardReaderState[] readerStates) {
            if (_contextPtr.Equals(IntPtr.Zero)) {
                throw new InvalidContextException(SCardError.InvalidHandle);
            }

            return Platform.Lib.GetStatusChange(_contextPtr, timeout, readerStates);
        }
Exemple #13
0
        /// <summary>Returns the current reader status of all requested readers.</summary>
        /// <param name="readerNames">Requested reader names.</param>
        /// <returns>An array of <see cref="T:PCSC.SCardReaderState" />s that contains the current reader status of each requested reader.</returns>
        /// <remarks>
        ///     <para>This method uses the <see cref="M:PCSC.SCardContext.GetStatusChange(System.IntPtr,PCSC.SCardReaderState[])" /> method with a timeout of zero.</para>
        ///     <example>
        ///         <code lang="C#">
        /// // Retrieve the names of all installed readers.
        /// using (var ctx = new SCardContext()) {
        ///     ctx.Establish(SCardScope.System);
        ///     var readerNames = ctx.GetReaders();
        /// 
        ///     // Get the current status for all readers.
        ///     var states = ctx.GetReaderStatus(readerNames);
        /// 
        ///     foreach (var state in states) {
        /// 	    Console.WriteLine("------------------------------");
        /// 	    Console.WriteLine("Reader: " + state.ReaderName);
        /// 	    Console.WriteLine("CurrentState: " + state.CurrentState 
        /// 		    + " EventState: " + state.EventState + "\n"
        /// 		    + "CurrentStateValue: " + state.CurrentStateValue 
        /// 		    + " EventStateValue: " + state.EventStateValue);
        /// 	    Console.WriteLine("UserData: " + state.UserData.ToString()
        /// 		    + " CardChangeEventCnt: " + state.CardChangeEventCnt);
        /// 	    Console.WriteLine("ATR: " + BitConverter.ToString(state.Atr));
        ///     }
        /// }
        ///   </code>
        ///     </example>
        /// </remarks>
        public SCardReaderState[] GetReaderStatus(string[] readerNames) {
            if (readerNames == null) {
                throw new ArgumentNullException("readerNames");
            }
            if (readerNames.Length == 0) {
                throw new ArgumentException("You must specify at least one reader.");
            }

            var states = new SCardReaderState[readerNames.Length];
            for (var i = 0; i < states.Length; i++) {
                states[i] = new SCardReaderState {
                    ReaderName = readerNames[i],
                    CurrentState = SCRState.Unaware
                };
            }

            GetStatusChange(IntPtr.Zero, states)
				.ThrowIfNotSuccess();

            return states;
        }
        private void StartMonitor(Monitor monitor)
        {
            var readerStates = new SCardReaderState[monitor.ReaderNames.Length];

            for (var i = 0; i < monitor.ReaderNames.Length; i++)
            {
                readerStates[i] = new SCardReaderState {
                    ReaderName   = monitor.ReaderNames[i],
                    CurrentState = SCRState.Unaware
                };
            }

            var rc = monitor.Context.GetStatusChange(IntPtr.Zero, readerStates);

            if (rc == SCardError.Success)
            {
                // initialize event
                for (var i = 0; i < readerStates.Length; i++)
                {
                    var initState = readerStates[i].EventState & (~(SCRState.Changed));

                    OnInitialized(readerStates[i].Atr, monitor.ReaderNames[i], initState);

                    monitor.PreviousStates[i]      = initState; // remove "Changed"
                    monitor.PreviousStateValues[i] = readerStates[i].EventStateValue;
                }

                while (!_isDisposed && !monitor.CancelRequested)
                {
                    for (var i = 0; i < readerStates.Length; i++)
                    {
                        readerStates[i].CurrentStateValue = monitor.PreviousStateValues[i];
                    }

                    // block until status change occurs
                    rc = monitor.Context.GetStatusChange(monitor.Context.Infinite, readerStates);

                    // Cancel?
                    if (rc != SCardError.Success)
                    {
                        break;
                    }

                    for (var i = 0; i < readerStates.Length; i++)
                    {
                        var newState = readerStates[i].EventState;
                        newState &= (~(SCRState.Changed)); // remove "Changed"

                        var atr           = readerStates[i].Atr;
                        var previousState = monitor.PreviousStates[i];
                        var readerName    = monitor.ReaderNames[i];

                        // Status change
                        if (previousState != newState)
                        {
                            OnStatusChanged(atr, readerName, previousState, newState);
                        }

                        // Card inserted
                        if (newState.CardIsPresent() && previousState.CardIsAbsent())
                        {
                            OnCardInserted(atr, readerName, newState);
                        }

                        // Card removed
                        if (newState.CardIsAbsent() && previousState.CardIsPresent())
                        {
                            OnCardRemoved(atr, readerName, newState);
                        }

                        monitor.PreviousStates[i]      = newState;
                        monitor.PreviousStateValues[i] = readerStates[i].EventStateValue;
                    }
                }
            }

            foreach (var state in readerStates)
            {
                state.Dispose();
            }

            if (_isDisposed || monitor.CancelRequested || rc == SCardError.Cancelled)
            {
                return;
            }

            OnMonitorException(rc, "An error occured during SCardGetStatusChange(..).");
        }