Exemplo n.º 1
0
        private void RemovalChecker()
        {
            new Thread(new ThreadStart(delegate()
            {
                IReaderUnit readerUnit = DokanNFCConfig.CreateReaderUnitFromName(readerProvider, readerUnitName);
                if (readerUnit.ConnectToReader())
                {
                    if (readerUnit.WaitInsertion(1))
                    {
                        while (!readerUnit.WaitRemoval(500) && IsRunning)
                        {
                            ;
                        }

                        if (insertedChip != null)
                        {
                            ResetCard();
                        }
                    }
                    readerUnit.DisconnectFromReader();
                }

                GC.KeepAlive(readerUnit);
            }
                                       )).Start();
        }
Exemplo n.º 2
0
        public String ReadRFIDToken(int timeout)
        {
            // Explicitly use the PC/SC Reader Provider.
            readerProvider = new PCSCReaderProvider();

            // Create the default reader unit. On PC/SC, we will listen on all readers.
            readerUnit = readerProvider.CreateReaderUnit();

            if (readerUnit.ConnectToReader())
            {
                //("Waiting x seconds for a card insertion...");
                int    i        = 0;
                bool   readCard = false;
                String readID   = "";
                while (i < timeout && !waitCancelled && !readCard)
                {
                    i++;
                    if (readerUnit.WaitInsertion(1000))
                    {
                        if (readerUnit.Connect())
                        {
                            chip chip = readerUnit.GetSingleChip();
                            readerUnit.Disconnect();
                            readCard = true;
                            readID   = readerUnit.GetNumber(chip);
                        }
                    }
                }
                return(readID);
            }
            throw new Exception("No RFID Reader connected");
        }
Exemplo n.º 3
0
        public static IReaderUnit CreateReaderUnitFromName(IReaderProvider readerProvider, string name)
        {
            IReaderUnit readerUnit = null;

            if (readerProvider != null)
            {
                readerUnit = readerProvider.CreateReaderUnit();
                switch (readerProvider.RPType)
                {
                case RP_PCSC:
                {
                    IPCSCReaderUnit ru = readerUnit as IPCSCReaderUnit;
                    ru.SetName(name);
                }
                break;
                }
            }

            return(readerUnit);
        }
Exemplo n.º 4
0
        protected void Listen()
        {
            IsRunning         = true;
            waitRemoval       = new AutoResetEvent(false);
            insertedChip      = null;
            chipInsertionDate = null;

            log.Info(String.Format("Listening on {0} reader {1}...", readerProviderName, readerUnitName));

            readerProvider = DokanNFCConfig.CreateReaderProviderFromName(readerProviderName);
            readerUnit     = DokanNFCConfig.CreateReaderUnitFromName(readerProvider, readerUnitName);

            if (readerProvider != null && readerUnit != null)
            {
                if (readerUnit.ConnectToReader())
                {
                    DokanNFCConfig config = DokanNFCConfig.GetSingletonInstance();
                    switch (config.Mode)
                    {
                    case DisplayMode.RawRFID:
                        break;

                    case DisplayMode.NFC:
                        driver = new NFCDokanRFIDDriver(this);
                        break;
                    }

                    if (driver != null)
                    {
                        string mountPoint = config.MountPoint;
                        if (String.IsNullOrEmpty(mountPoint))
                        {
                            string[] mountPoints = DokanNFCConfig.GetAvailableMountPoints();
                            if (mountPoints.Length > 0)
                            {
                                mountPoint = mountPoints[0];
                            }
                        }

                        if (!String.IsNullOrEmpty(mountPoint))
                        {
                            DokanOptions options = DokanOptions.FixedDrive;
                            if (config.AlwaysMounted)
                            {
                                Mount(mountPoint, options);
                            }
                            else
                            {
                                options |= DokanOptions.RemovableDrive;
                            }
                            do
                            {
                                if (readerUnit.WaitInsertion(500))
                                {
                                    Thread.Sleep(400);
                                    chipInsertionDate = DateTime.Now;
                                    log.Info("Card inserted.");
                                    if (readerUnit.Connect())
                                    {
                                        insertedChip = readerUnit.GetSingleChip();
                                        if (insertedChip != null)
                                        {
                                            log.Info(String.Format("Chip type `{0}`", insertedChip.Type));
                                            if (!config.AlwaysMounted)
                                            {
                                                Mount(mountPoint, options);
                                            }

                                            RemovalChecker();
                                            while (!waitRemoval.WaitOne(500) && IsRunning)
                                            {
                                                ;
                                            }
                                            log.Info("Card removed.");

                                            if (!config.AlwaysMounted)
                                            {
                                                Unmount(mountPoint);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        log.Error("Cannot connect to the card.");
                                    }
                                    chipInsertionDate = null;
                                }
                            } while (IsRunning);

                            if (config.AlwaysMounted)
                            {
                                Unmount(mountPoint);
                            }
                        }
                        else
                        {
                            log.Error("No mount point.");
                        }
                    }
                    else
                    {
                        log.Error("No matching file system driver to mount.");
                    }
                    readerUnit.DisconnectFromReader();
                }
                else
                {
                    log.Error("Cannot connect to the reader.");
                }

                GC.KeepAlive(readerUnit);
                GC.KeepAlive(readerProvider);
            }
            else
            {
                log.Error("Error in RFID reader resource allocation.");
            }
        }
Exemplo n.º 5
0
        protected void Listen()
        {
            IsRunning = true;
            waitRemoval = new AutoResetEvent(false);
            insertedChip = null;
            chipInsertionDate = null;

            log.Info(String.Format("Listening on {0} reader {1}...", readerProviderName, readerUnitName));

            readerProvider = DokanNFCConfig.CreateReaderProviderFromName(readerProviderName);
            readerUnit = DokanNFCConfig.CreateReaderUnitFromName(readerProvider, readerUnitName);

            if (readerProvider != null && readerUnit != null)
            {
                if (readerUnit.ConnectToReader())
                {
                    DokanNFCConfig config = DokanNFCConfig.GetSingletonInstance();
                    switch (config.Mode)
                    {
                        case DisplayMode.RawRFID:
                            break;
                        case DisplayMode.NFC:
                            driver = new NFCDokanRFIDDriver(this);
                            break;
                    }

                    if (driver != null)
                    {
                        string mountPoint = config.MountPoint;
                        if (String.IsNullOrEmpty(mountPoint))
                        {
                            string[] mountPoints = DokanNFCConfig.GetAvailableMountPoints();
                            if (mountPoints.Length > 0)
                            {
                                mountPoint = mountPoints[0];
                            }
                        }

                        if (!String.IsNullOrEmpty(mountPoint))
                        {
                            DokanOptions options = DokanOptions.FixedDrive;
                            if (config.AlwaysMounted)
                            {
                                Mount(mountPoint, options);
                            }
                            else
                            {
                                options |= DokanOptions.RemovableDrive;
                            }
                            do
                            {
                                if (readerUnit.WaitInsertion(500))
                                {
                                    Thread.Sleep(400);
                                    chipInsertionDate = DateTime.Now;
                                    log.Info("Card inserted.");
                                    if (readerUnit.Connect())
                                    {
                                        insertedChip = readerUnit.GetSingleChip();
                                        if (insertedChip != null)
                                        {
                                            log.Info(String.Format("Chip type `{0}`", insertedChip.Type));
                                            if (!config.AlwaysMounted)
                                            {
                                                Mount(mountPoint, options);
                                            }

                                            RemovalChecker();
                                            while (!waitRemoval.WaitOne(500) && IsRunning) ;
                                            log.Info("Card removed.");

                                            if (!config.AlwaysMounted)
                                            {
                                                Unmount(mountPoint);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        log.Error("Cannot connect to the card.");
                                    }
                                    chipInsertionDate = null;
                                }
                            } while (IsRunning);

                            if (config.AlwaysMounted)
                            {
                                Unmount(mountPoint);
                            }
                        }
                        else
                        {
                            log.Error("No mount point.");
                        }
                    }
                    else
                    {
                        log.Error("No matching file system driver to mount.");
                    }
                    readerUnit.DisconnectFromReader();
                }
                else
                {
                    log.Error("Cannot connect to the reader.");
                }

                GC.KeepAlive(readerUnit);
                GC.KeepAlive(readerProvider);
            }
            else
            {
                log.Error("Error in RFID reader resource allocation.");
            }
        }