Skip to content

Commit

Permalink
Add status property to SmartCardDeviceListener
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Domzalski committed Jun 29, 2022
1 parent d3b2dd0 commit 39a2765
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internal class DesktopSmartCardDeviceListener : SmartCardDeviceListener, IDispos
public DesktopSmartCardDeviceListener()
{
_log.LogInformation("Creating DesktopSmartCardDeviceListener.");
Status = DeviceListenerStatus.Stopped;

uint result = SCardEstablishContext(SCARD_SCOPE.USER, out SCardContext context);
_log.SCardApiCall(nameof(SCardEstablishContext), result);
Expand All @@ -57,6 +58,7 @@ public DesktopSmartCardDeviceListener()
context.Dispose(); // Needed to satisfy analyzer (even though it should be null already)
_context = new SCardContext(IntPtr.Zero);
_readerStates = Array.Empty<SCARD_READER_STATE>();
Status = DeviceListenerStatus.Error;
_log.LogWarning("SmartCardDeviceListener dormant as SDK was unable to establish a context to the PCSC service.");
return;
}
Expand All @@ -79,6 +81,7 @@ private void StartListening()
IsBackground = true
};
_isListening = true;
Status = DeviceListenerStatus.Started;
_listenerThread.Start();
}
}
Expand Down Expand Up @@ -153,6 +156,7 @@ private void StopListening()

ClearEventHandlers();
_isListening = false;
Status = DeviceListenerStatus.Stopped;
_listenerThread.Join();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2022 Yubico AB
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Yubico.Core.Devices.SmartCard
{
/// <summary>
/// An enumeration that represents the status of a device listener.
/// </summary>
public enum DeviceListenerStatus
{
/// <summary>
/// The listener has not been started, and is not currently listening for devices. No events will be raised.
/// </summary>
Stopped,

/// <summary>
/// The listener has been successfully started.
/// </summary>
Started,

/// <summary>
/// The listener encountered an error and could not start. No events will be raised.
/// </summary>
Error
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public abstract class SmartCardDeviceListener
/// </summary>
public event EventHandler<SmartCardDeviceEventArgs>? Removed;

/// <summary>
/// A status that indicates the state of the device listener.
/// </summary>
public DeviceListenerStatus Status { get; set; }

/// <summary>
/// Creates an instance of a <see cref="SmartCardDeviceListener"/>.
/// </summary>
Expand Down

0 comments on commit 39a2765

Please sign in to comment.