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

C standards-conforming negative number support in HID report descriptors #2425

Open
1 task done
tlyu opened this issue Jan 24, 2024 · 0 comments
Open
1 task done

C standards-conforming negative number support in HID report descriptors #2425

tlyu opened this issue Jan 24, 2024 · 0 comments

Comments

@tlyu
Copy link
Contributor

tlyu commented Jan 24, 2024

Related area

HID report descriptors

Hardware specification

N/A

Is your feature request related to a problem?

There is a C standards conformance problem with the macros in class/hid/hid.h and negative 16-bit numbers. This makes it awkward to naturally specify negative numbers in HID report descriptors using the TinyUSB macros. These negative numbers often appear in the HID report descriptors of interfaces such as mice and joysticks. The existing templates in TinyUSB only use 8-bit values, and encode them as the equivalent positive hexadecimal numbers. I am converting some existing report descriptors that use 16-bit negative numbers.

Currently, using a macro like

HID_LOGICAL_MIN_N(-32767, 2)

in a report descriptor will result in a right shift of a negative number, which is implementation-defined in C. The expansion goes through the macros U16_TO_U8S_LE and TU_U16_HIGH in tusb_common.h, which do not cast their argument to an unsigned type. Interestingly, the TU_U32_BYTE macros do cast their argument to uint32_t.

I'm not currently aware of any compilers that produce incorrect code as a result of that standards provision. I think it's still better to produce an expression that has a uniquely-defined semantic according to the standards.

Describe the solution you'd like

Add casts to ensure that TU_U16_HIGH does not produce a right-shift of a negative number.

I can contribute a PR, but I'd like to know if the preferred solution is adding casts to the tusb_common.h macros, or instead to the class/hid/hid.h macros.

I have checked existing issues, dicussion and documentation

  • I confirm I have checked existing issues, dicussion and documentation.
tlyu added a commit to tlyu/Kaleidoscope that referenced this issue Jan 24, 2024
Disable some TInyUSB definitions that we don't use (yet) and/or conflict
with existing definitions in KeyboardioHID.

Also import some other TinyUSB support macros, with minor fixes. The
negative number issue with 16-bit report items is now
hathach/tinyusb#2425
tlyu added a commit to tlyu/Kaleidoscope that referenced this issue Jan 24, 2024
Disable some TInyUSB definitions that we don't use (yet) and/or conflict
with existing definitions in KeyboardioHID.

Also import some other TinyUSB support macros, with minor fixes. The
negative number issue with 16-bit report items is now
hathach/tinyusb#2425

Signed-off-by: Taylor Yu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant