Skip to content
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

Bug in EncodedItem when creating instance and setting DataValueSigned #22

Open
DBuergin opened this issue Mar 22, 2023 · 1 comment
Open
Labels
bug Something isn't working

Comments

@DBuergin
Copy link

TL;DR

When creating instance of EncodedItem

if DataValueSigned = X where

(X > Int16.MinValue && X < 0) || (X > Int16.MaxValue)

Then DataValueSigned cannot be round tripped and later accessed DataItems have a wrong value.

Use case

  • Windows 10
  • Visual Studio 2022
  • C# .Net Framework 4.8

I'm trying to get Logical min and max values from a USB device under Windows. The logical minimum in the hid descriptor is -512.
The later accessed DataItem contains a logical minimum of 0.

I've managed to narrow it down to EncodedItem.cs, line 162

But I cannot for the life of me understand the logic behind EncodedItem.Data.

Steps to reproduce with VS 2022:

  1. Open HidSharp project

  2. In Visual Studio Menu, go to View > Other Windows > C# Interactive

  3. In Solution Explorer, right click HidSharp Project and choose Initialize Interactive with Project

  4. In Interactive Console, enter the following:

> using HidSharp.Reports.Encodings;
> var item = new EncodedItem() { ItemType = ItemType.Global, TagForGlobal = GlobalItemTag.LogicalMinimum, DataValueSigned = -32768 };
> item.DataValueSigned

EncodedItem.DataValueSigned = 0

Observations

In       Out
-32769 = -32769
-32768 = 0
-1     = 0
1      = 1
32767  = 32767
32768  = -32768

If anyone has an Idea how to correct this without breaking anything else, I would appreciate the help.

Diego

@benedekkupper benedekkupper added the bug Something isn't working label Mar 26, 2023
@DBuergin
Copy link
Author

DBuergin commented May 2, 2023

Changing the following two lines fixes the round-trip problem, but probably breaks reconstruction of the HID descriptor:

EncodedItem.cs : L160 change
{ DataValue = (uint)(sbyte)value; if (value < 0) { Data.Add(0); } }
to
{ DataValue = (uint)(sbyte)value; }

and EncodedItem:L162 change
{ DataValue = (uint)(short)value; if (value < 0) { Data.Add(0); Data.Add(0); } }
to
{ DataValue = (uint)(short)value; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants