protected void Poller()
        {
            SCW.SCARD_READERSTATE[] State = new SCW.SCARD_READERSTATE[1];
            State[0].RdrName      = this.Name;
            State[0].UserData     = IntPtr.Zero;
            State[0].RdrCurrState = SCW.SCARD_STATE_UNKNOWN;

            int retCode;

            retCode = SCW.SCardGetStatusChange(hContext, 100, State, 1);
            if (retCode != SCW.SCARD_S_SUCCESS)
            {
                throw new Exception("Failed initial get status change: " + SCW.GetScardErrMsg(retCode));
            }

            State[0].RdrCurrState = State[0].RdrEventState;

            while (!stopPollingSignal)
            {
                retCode = SCW.SCardGetStatusChange(hContext, 1000, State, 1);

                if (retCode == SCW.SCARD_E_TIMEOUT)
                {
                    continue;
                }
                if (retCode != SCW.SCARD_S_SUCCESS)
                {
                    throw new Exception("Failed cycling get status change: " + SCW.GetScardErrMsg(retCode));
                }

                if (((State[0].RdrEventState & SCW.SCARD_STATE_PRESENT) == SCW.SCARD_STATE_PRESENT) &&
                    ((State[0].RdrEventState & SCW.SCARD_STATE_CHANGED) == SCW.SCARD_STATE_CHANGED))
                {
                    NFCTag tag = Connect();
                    foreach (Delegate d in TagFound.GetInvocationList())
                    {
                        ISynchronizeInvoke syncer = d.Target as ISynchronizeInvoke;
                        if (syncer != null)
                        {
                            syncer.BeginInvoke(d, new NFCTag[] { tag });
                        }
                        else
                        {
                            d.DynamicInvoke(tag);
                        }
                    }
                }

                State[0].RdrCurrState = State[0].RdrEventState;
            }
        }
示例#2
0
        protected byte[] GetATR(IntPtr Handle, int Proto)
        {
            string rName = String.Empty;
            int    rLenght = 0, ATRLen = 33, dwState = 0;

            byte[] ATRBytes = new byte[32];

            int retCode = SCW.SCardStatus(Handle, rName,
                                          ref rLenght, ref dwState, ref Proto, ref ATRBytes[0], ref ATRLen);

            if (retCode != SCW.SCARD_S_SUCCESS)
            {
                throw new Exception("Failed querying tag status: " + SCW.GetScardErrMsg(retCode));
            }

            byte[] ATR = new byte[ATRLen];
            Array.Copy(ATRBytes, ATR, ATRLen);
            return(ATR);
        }
        public override byte[] Transmit(IntPtr Handle, int Proto, byte[] CmdBytes)
        {
            SCW.SCARD_IO_REQUEST ioRequest = new SCW.SCARD_IO_REQUEST();
            ioRequest.dwProtocol  = Proto;
            ioRequest.cbPciLength = 8;

            int rcvLenght = 32; // Use 260 to handle more intelligent smartcards

            byte[] rcvBytes = new byte[rcvLenght];

            int retCode = SCW.SCardTransmit(Handle,
                                            ref ioRequest, CmdBytes, CmdBytes.Length,
                                            ref ioRequest, rcvBytes, out rcvLenght);

            if (retCode != SCW.SCARD_S_SUCCESS)
            {
                throw new Exception("Failed querying tag: " + SCW.GetScardErrMsg(retCode));
            }

            if (!(rcvBytes[rcvLenght - 2] == 0x90 && rcvBytes[rcvLenght - 1] == 0x00))
            {
                if (rcvBytes[rcvLenght - 2] == 0x63 && rcvBytes[rcvLenght - 1] == 0x00)
                {
                    throw new Exception("Operation failed!");
                }

                if (rcvBytes[rcvLenght - 2] == 0x6A && rcvBytes[rcvLenght - 1] == 0x81)
                {
                    throw new Exception("Operation not supported!");
                }

                throw new Exception("Operation returned: " + rcvBytes[rcvLenght - 2].ToString("X2") + rcvBytes[rcvLenght - 1].ToString("X2"));
            }

            byte[] returnBytes = new byte[rcvLenght - 2];
            Array.Copy(rcvBytes, returnBytes, rcvLenght - 2);
            return(returnBytes);
        }
        public static void Init()
        {
            if (IsInitialized)
            {
                Release();
            }

            int retCode;

            // Get context
            retCode = SCW.SCardEstablishContext(SCW.SCARD_SCOPE_USER, 0, 0, out hContext);

            if (retCode != SCW.SCARD_S_SUCCESS)
            {
                throw new Exception("Failed extablishing context: " + SCW.GetScardErrMsg(retCode));
            }

            // Get PC/SC readers available
            int pcchReaders = 0;

            retCode = SCW.SCardListReaders(hContext, null, null, ref pcchReaders);

            if (retCode == SCW.SCARD_E_NO_READERS_AVAILABLE)
            {
                return;
            }
            else if (retCode != SCW.SCARD_S_SUCCESS)
            {
                throw new Exception("Failed listing readers: " + SCW.GetScardErrMsg(retCode));
            }

            // Fill reader list
            readers.Clear();
            byte[] ReadersList = new byte[pcchReaders];
            retCode = SCW.SCardListReaders(hContext, null, ReadersList, ref pcchReaders);

            // Convert reader buffer to string
            int    idxBytes = 0, idxNames = 0;
            string rdrName = "";

            string[] readersNames = new string[pcchReaders];
            while (ReadersList[idxBytes] != 0)
            {
                while (ReadersList[idxBytes] != 0)
                {
                    rdrName = rdrName + (char)ReadersList[idxBytes];
                    idxBytes++;
                }

                if (rdrName.StartsWith("DUALi"))
                {
                    readers.Add(new Readers.DualiReader(hContext, rdrName));
                }
                else if (rdrName.StartsWith("ACS"))
                {
                    readers.Add(new Readers.ACSReader(hContext, rdrName));
                }
                else if (rdrName.StartsWith("SCM") || (rdrName.StartsWith("Identive") && rdrName.Contains("Contactless")))
                {
                    readers.Add(new Readers.IdentiveReader(hContext, rdrName));
                }

                rdrName = "";
                idxBytes++;
                idxNames++;
            }

            DefaultReader = 0;
        }