protected override void BecauseOf() { _monitor = _sut.Create(SCardScope.System); // Give the monitor thread 1 seconds (until we fail this test) Thread.Sleep(TimeSpan.FromSeconds(1)); }
public NFCReader(byte block = 0x08) { LSB = block; var contextFactory = ContextFactory.Instance; context = contextFactory.Establish(SCardScope.System); var monitorFactory = MonitorFactory.Instance; Monitor = monitorFactory.Create(SCardScope.System); var readerNames = context.GetReaders(); AttachToAllEvents(Monitor); if (NoReaderAvailable(readerNames)) { new Exception("You need at least one reader in order to run this application."); } readerName = ChooseReader(readerNames); if (readerName == null) { new Exception("The reader is not correctly selected."); } Monitor.Start(readerName); }
protected override void BecauseOf() { _monitor = _sut.Start(SCardScope.System, REQUESTED_READER); // Give the monitor thread 5 seconds (until we fail this test) _get_status_change_call.WaitOne(TimeSpan.FromSeconds(5)); }
static void AttachToAllEvents(ISCardMonitor monitor) { monitor.Initialized += (sender, args) => ev("Initialized", args); monitor.CardRemoved += (sender, args) => ev("Card Removed", args); monitor.CardInserted += (sender, args) => ev("Card Inserted", args); monitor.StatusChanged += sc; monitor.MonitorException += ex; }
private void AttachToAllEvents(ISCardMonitor monitor) { monitor.CardInserted += (sender, args) => CardInserted(sender, args); monitor.CardRemoved += (sender, args) => CardRemoved(args); monitor.Initialized += (sender, args) => MonitorInitialized(args); monitor.StatusChanged += StatusChanged; monitor.MonitorException += MonitorException; }
/// <summary> /// Releases the smart card monitor and its dependencies using the <see cref="IDisposable.Dispose"/> method. /// </summary> /// <param name="monitor">Smart card monitor that shall be stopped and disposed.</param> public void Release(ISCardMonitor monitor) { if (monitor == null) { throw new ArgumentNullException(nameof(monitor)); } monitor.Dispose(); }
private void AttachToAllEvents(ISCardMonitor monitor) { // Point the callback function(s) to the anonymous & static defined methods below. monitor.CardInserted += (sender, args) => CardInseted(args); monitor.CardRemoved += (sender, args) => CardRemoved(args); //monitor.Initialized += (sender, args) => CardInitialized(args); //monitor.StatusChanged += StatusChanged; //monitor.MonitorException += MonitorException; }
/// <summary> /// /// </summary> public void StopReaderMonitor() { if (readerMonitor != null) { readerMonitor.Cancel(); readerMonitor.Dispose(); readerMonitor = null; } }
/// <summary> /// Stops all comms /// </summary> public void Disconnect() { if (_monitor == null) { return; } _monitor.Cancel(); _monitor.Dispose(); _monitor = null; }
private void StartMonitoring() { string[] res = GetReaderNames(); if (res.Length > 0) { _monitor = MonitorFactory.Instance.Create(SCardScope.System); _monitor.CardInserted += _monitor_CardInserted; _monitor.CardRemoved += _monitor_CardRemoved; _monitor.Start(res); } }
public void Initialize(string explicitName) { try { monitor = MonitorFactory.Instance.Create(SCardScope.System); monitor.CardInserted += Monitor_CardInserted; monitor.Start(explicitName); LogAction?.Invoke($"SCardWatcher Initialized : Connected to {explicitName}"); } catch (Exception e) { LogAction?.Invoke($"SCardWatcher Initialize Error : Problem with connected to {explicitName}."); LogAction?.Invoke(e.ToString()); throw; } }
/* * public delegate void CardInsertedEventHandler(CardStatusEventArgs args); * * public delegate void CardRemovedEventHandler(CardStatusEventArgs args); * * public delegate void CardInitializedEventHandler(CardStatusEventArgs args); * * public delegate void CardStatusChangedEventHandler(StatusChangeEventArgs args); * * public delegate void CardMonitorExceptionEventHandler(PCSCException ex); * * public event CardInsertedEventHandler OnCardInserted; * * public event CardRemovedEventHandler OnCardRemoved; * * public event CardInitializedEventHandler OnInitialized; * * public event CardStatusChangedEventHandler OnCardStatusChanged; * * public event CardMonitorExceptionEventHandler OnCardMonitorException; */ /// <summary> /// /// </summary> /// <param name="readerName"></param> public bool StartReaderMonitor(string readerName, IReaderMonitor monitor) { StopReaderMonitor(); try { readerMonitor = MonitorFactory.Instance.Create(SCardScope.System); readerMonitor.CardInserted += (sender, args) => monitor.OnCardInserted(args); readerMonitor.CardRemoved += (sender, args) => monitor.OnCardRemoved(args); readerMonitor.StatusChanged += (sender, args) => monitor.OnStatusChanged(args); readerMonitor.Initialized += (sender, args) => monitor.OnInitialized(args); readerMonitor.MonitorException += (sender, args) => monitor.OnMonitorException(args); readerMonitor.Start(readerName); return(true); } catch (Exception) { return(false); } }
public void Initialize() { try { using (var context = ContextFactory.Instance.Establish(SCardScope.System)) { var readerNames = context.GetReaders(); monitor = MonitorFactory.Instance.Create(SCardScope.System); monitor.CardInserted += Monitor_CardInserted; monitor.Start(readerNames[0]); LogAction?.Invoke($"SCardWatcher Initialized : Connected to {readerNames[0]}"); } } catch (Exception e) { LogAction?.Invoke($"SCardWatcher Initialize Error : Problem with Auto connect."); LogAction?.Invoke(e.ToString()); throw; } }
/// <summary> /// Start comms with Client /// </summary> /// <param name="isOk">Callback to use when a card is read</param> public async void Connect(CallBacks.Authenticated isOk) { _isOk = isOk; // Retrieve the names of all installed readers. var readerNames = GetReaderNames(); if (NoReaderFound(readerNames)) { Console.WriteLine("There are currently no readers installed."); return; } // Create smartcard monitor using a context factory. // The context will be automatically released after monitor.Dispose() var monitorFactory = MonitorFactory.Instance; _monitor = monitorFactory.Create(SCardScope.System); AttachToAllEvents(_monitor); // Remember to detach, if you use this in production! ShowUserInfo(readerNames); _monitor.Start(readerNames); await Task.Delay(3000); }
private void InitializeDevice() { var contextFactory = ContextFactory.Instance; using (context = contextFactory.Establish(SCardScope.System)) { var readerNames = context.GetReaders(); var monitorFactory = MonitorFactory.Instance; monitor = monitorFactory.Create(SCardScope.System); if (readerNames.Length == 0) { cp.Invoke((MethodInvoker)(() => this.cp.DeviceInitialized(true))); return; } readerName = readerNames[0]; AttachToAllEvents(monitor); Console.WriteLine($"Initializing Reader: {readerNames[0]}"); monitor.Start(readerNames[0]); Console.WriteLine($"Reader Initialized: {readerNames[0]}"); } }
public void StartMonitoring(string[] readerNames = null) { try { if (monitor == null) { var monitorFactory = MonitorFactory.Instance; monitor = monitorFactory.Create(SCardScope.System); monitor.MonitorException += Monitor_MonitorException; monitor.CardInserted += Monitor_CardInserted; monitor.CardRemoved += Monitor_CardRemoved; } monitor.Cancel(); if (readerNames == null) { readerNames = GetReaderNames(); } foreach (string readerName in readerNames) { TryTurnOffBeep(readerName); } if (readerNames.Length == 0) { return; } monitor.Start(readerNames); } catch (PCSCException e) { throw new NfcHandlerException("Failed monitoring readers\n" + e.Message, e); } }
/// <summary> /// Listen to all smart card events of a given <see cref="ISCardMonitor"/>. /// </summary> /// <param name="monitor">The smart card monitor to listen on.</param> /// <param name="scheduler">The scheduler to run the add and remove event handler logic on.</param> /// <returns>An observable of all smart card events.</returns> public static IObservable <MonitorEvent> ObserveEvents(this ISCardMonitor monitor, IScheduler scheduler = null) { if (monitor == null) { throw new ArgumentNullException(nameof(monitor)); } var useScheduler = scheduler ?? Scheduler.ForCurrentContext(); var initialized = Observable.FromEventPattern <CardInitializedEvent, CardStatusEventArgs>( handler => monitor.Initialized += handler, handler => monitor.Initialized -= handler, useScheduler) .Select(ev => ev.EventArgs) .Select(args => new MonitorInitialized(args.ReaderName, args.Atr, args.State)); var cardInserted = Observable.FromEventPattern <CardInsertedEvent, CardStatusEventArgs>( handler => monitor.CardInserted += handler, handler => monitor.CardInserted -= handler, useScheduler) .Select(ev => ev.EventArgs) .Select(args => new CardInserted(args.ReaderName, args.Atr, args.State)); var cardRemoved = Observable.FromEventPattern <CardRemovedEvent, CardStatusEventArgs>( handler => monitor.CardRemoved += handler, handler => monitor.CardRemoved -= handler, useScheduler) .Select(ev => ev.EventArgs) .Select(args => new CardRemoved(args.ReaderName, args.Atr, args.State)); var statusChanged = Observable.FromEventPattern <StatusChangeEvent, StatusChangeEventArgs>( handler => monitor.StatusChanged += handler, handler => monitor.StatusChanged -= handler, useScheduler) .Select(ev => ev.EventArgs) .Select(args => new CardStatusChanged(args.ReaderName, args.Atr, args.LastState, args.NewState)); var monitorEvents = initialized .Cast <MonitorEvent>() .Merge(cardInserted) .Merge(cardRemoved) .Merge(statusChanged); var monitorException = Observable.FromEventPattern <MonitorExceptionEvent, PCSCException>( handler => monitor.MonitorException += handler, handler => monitor.MonitorException -= handler, useScheduler) .Select(ev => ev.EventArgs); return(Observable.Create <MonitorEvent>(obs => { var subscription = monitorEvents .Subscribe(obs); var exceptionSubscription = monitorException .Take(1) .Subscribe(ex => { subscription.Dispose(); obs.OnError(ex); }); return new CompositeDisposable( subscription, exceptionSubscription); })); }