Skip to content

Consolidate HID report descriptor parsing across backends #833

Description

@Youw

Problem

HIDAPI currently has several private implementations that decode or walk HID report descriptors:

  • libusb/hid.c has its own item walker for Usage Page/Usage extraction.
  • PR Properly determine libusb read size for large reports (Fixes #274) #728 adds libusb/hidapi_libusb_report_descriptor.h to calculate maximum Input/Output/Feature report sizes.
  • linux/hid.c independently implements report-item decoding and Usage Page/Usage extraction.
  • netbsd/hid.c contains another closely related implementation.

These routines duplicate short/long item decoding, bounds checks, global-item state, Usage handling, and malformed-descriptor behavior. Keeping the new libusb parser under the backend directory also makes reuse by the hidraw and NetBSD backends unnecessarily difficult.

Windows' hidapi_descriptor_reconstruct.c performs the inverse operation—reconstructing a descriptor from preparsed data—so it may remain separate, but shared item definitions or test data could still be evaluated.

Proposal

Introduce a private common report-descriptor module, for example under src/ or another backend-neutral internal directory, and migrate reusable decoding logic into it.

The common layer should:

  • Decode HID short and long items safely.
  • Expose enough parser state or callbacks to derive Usage Page/Usage and per-report-ID sizes.
  • Keep transport-specific descriptor retrieval in each backend.
  • Remain an internal implementation detail with no public ABI additions.
  • Compile as both C and C++ under HIDAPI_BUILD_AS_CXX.
  • Preserve each backend's existing fallback and malformed-descriptor behavior.

Acceptance criteria

  • libusb, Linux hidraw, and NetBSD reuse the common descriptor walker where practical.
  • Duplicated byte/item decoding helpers are removed.
  • Existing backend behavior remains covered by tests.
  • The Windows fixture corpus and focused malformed/boundary cases provide shared parser coverage.
  • The eventual location and naming of hidapi_libusb_report_descriptor.h no longer imply that generic parsing belongs exclusively to libusb.

Related to #728.

Related to #796.

Generated by Codex (GPT-5.6 Sol).

Metadata

Metadata

Assignees

No one assigned

    Labels

    CoreRelated to common codes like hidapi.h

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions