Skip to content

Commit

Permalink
Detect non-connected devices in QueryDevice
Browse files Browse the repository at this point in the history
If the SDK returns the ERROR_DEVICE_NOT_CONNECTED error when calling
the QueryDevice API, do not throw a NativeCallException but instead
detect this error and return a device info struct where the connected
count is set to zero.

A new enum member "Unknown" is added to the DeviceType enumeration
to cover this use case.

Fixes #284.
  • Loading branch information
Sharparam committed Mar 1, 2021
1 parent be3841d commit baba344
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Colore/Data/DeviceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ namespace Colore.Data
[JsonConverter(typeof(StringEnumConverter))]
public enum DeviceType
{
/// <summary>
/// Unknown device.
/// </summary>
[PublicAPI]
[EnumMember(Value = "unknown")]
Unknown = 0,

/// <summary>
/// A keyboard device.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Colore/Data/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ namespace Colore.Data
[JsonConverter(typeof(ResultConverter))]
public struct Result : IEquatable<int>, IEquatable<Result>
{
/// <summary>
/// The device is not connected.
/// </summary>
[PublicAPI]
public static readonly Result DeviceNotConnected = 1167;

/// <summary>
/// Access denied.
/// </summary>
Expand Down Expand Up @@ -106,6 +112,7 @@ public struct Result : IEquatable<int>, IEquatable<Result>
private static readonly Dictionary<Result, KeyValuePair<string, string>> Mappings =
new Dictionary<Result, KeyValuePair<string, string>>
{
{ 1167, new KeyValuePair<string, string>(nameof(DeviceNotConnected), "The device is not connected.") },
{ 5, new KeyValuePair<string, string>(nameof(RzAccessDenied), "Access denied.") },
{ unchecked(-2147467259), new KeyValuePair<string, string>(nameof(RzFailed), "General failure.") },
{ -1, new KeyValuePair<string, string>(nameof(RzInvalid), "Invalid.") },
Expand Down
7 changes: 7 additions & 0 deletions src/Colore/Native/NativeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ public Task<SdkDeviceInfo> QueryDeviceAsync(Guid deviceId)
var result = _nativeSdkMethods.QueryDevice(deviceId, ptr);

if (!result)
{
if (result == Result.DeviceNotConnected)
{
return Task.FromResult(new SdkDeviceInfo(DeviceType.Unknown, 0));
}

throw new NativeCallException("QueryDevice", result);
}

if (ptr == IntPtr.Zero)
throw new ColoreException("Device query failed, ptr NULL.");
Expand Down

0 comments on commit baba344

Please sign in to comment.