Skip to content

Dennisdyallo/hid share mode #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Yubico.Core/src/Yubico/Core/CoreCompatSwitches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ public static class CoreCompatSwitches
/// Default is false / shared.
/// </summary>
public const string OpenSmartCardHandlesExclusively = nameof(OpenSmartCardHandlesExclusively);

/// <summary>
/// If set to true, Yubico.Core will attempt to open HID handles exclusively. False will open shared.
/// Default is false / shared.
/// </summary>
public const string OpenHidHandlesExclusively = nameof(OpenHidHandlesExclusively);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ private void SetupConnection()
throw new PlatformApiException(ExceptionMessages.IOKitCannotOpenDevice);
}

int result = IOHIDDeviceOpen(_deviceHandle, 0x01);
int shareMode =
AppContext.TryGetSwitch(
CoreCompatSwitches.OpenHidHandlesExclusively,
out bool isEnabled) && isEnabled
? IOKitHidConstants.kIOHIDOptionsTypeSeizeDevice
: IOKitHidConstants.kIOHIDOptionsTypeNone;

int result = IOHIDDeviceOpen(_deviceHandle, shareMode);
_log.IOKitApiCall(nameof(IOHIDDeviceOpen), (kern_return_t)result);

if (result != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,30 @@ internal sealed class WindowsHidFeatureReportConnection : IHidConnection
{
// The SDK device instance that created this connection instance.
private readonly WindowsHidDevice _device;

// The underlying Windows HID device used for communication.
private IHidDDevice HidDDevice { get; set; }
private readonly HidDDevice _hidDDevice;

public int InputReportSize { get; private set; }
public int OutputReportSize { get; private set; }

internal WindowsHidFeatureReportConnection(WindowsHidDevice device, string path)
{
_device = device;
HidDDevice = new HidDDevice(path);
_hidDDevice = new HidDDevice(path);
SetupConnection();
}

private void SetupConnection()
{
HidDDevice.OpenFeatureConnection();
InputReportSize = HidDDevice.FeatureReportByteLength;
OutputReportSize = HidDDevice.FeatureReportByteLength;
_hidDDevice.OpenFeatureConnection();
InputReportSize = _hidDDevice.FeatureReportByteLength;
OutputReportSize = _hidDDevice.FeatureReportByteLength;
}

public byte[] GetReport()
{
byte[] data = HidDDevice.GetFeatureReport();
byte[] data = _hidDDevice.GetFeatureReport();

_device.LogDeviceAccessTime();

Expand All @@ -52,23 +53,23 @@ public byte[] GetReport()

public void SetReport(byte[] report)
{
HidDDevice.SetFeatureReport(report);
_hidDDevice.SetFeatureReport(report);
_device.LogDeviceAccessTime();
}

#region IDisposable Support
private bool disposedValue; // To detect redundant calls
private bool _disposedValue; // To detect redundant calls

private void Dispose(bool disposing)
{
if (!disposedValue)
if (!_disposedValue)
{
if (disposing)
{
HidDDevice.Dispose();
_hidDDevice.Dispose();
}

disposedValue = true;
_disposedValue = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,12 @@ public override ISmartCardConnection Connect()

try
{
SCARD_SHARE shareMode = SCARD_SHARE.SHARED;

if (AppContext.TryGetSwitch(CoreCompatSwitches.OpenSmartCardHandlesExclusively, out bool enabled) &&
enabled)
{
shareMode = SCARD_SHARE.EXCLUSIVE;
}
SCARD_SHARE shareMode =
AppContext.TryGetSwitch(
CoreCompatSwitches.OpenSmartCardHandlesExclusively,
out bool isEnabled) && isEnabled
? SCARD_SHARE.EXCLUSIVE
: SCARD_SHARE.SHARED;

result = SCardConnect(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,20 @@ internal static class IOKitHidConstants
public const int kIOHidReportTypeInput = 0;
public const int kIOHidReportTypeOutput = 1;
public const int kIOHidReportTypeFeature = 2;

/// <summary>
/// No options specified.
/// </summary>
public const int kIOHIDOptionsTypeNone = 0x00;

/// <summary>
/// Seize the device, preventing other applications from accessing it.
/// </summary>
public const int kIOHIDOptionsTypeSeizeDevice = 0x01;

/// <summary>
/// Ignore the device, preventing it from being opened.
/// </summary>
public const int kIOHIDOptionsTypeIgnore = 0x02;
}
}
Loading