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(); }
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."); } }