# hidtools.uhid module

class UHIDDevice

  Bases: `object`

  A uhid device. uhid is a kernel interface to create virtual HID devices based on a report descriptor.

  This class also acts as context manager for any `UHIDDevice` objects. See `dispatch()` for details.

  uniq

    A uniq string assigned to this device. This string is autogenerated and can be used to reliably identify the device.

  UHID_FEATURE_REPORT: Final = 0
  UHID_INPUT_REPORT: Final = 2
  UHID_OUTPUT_REPORT: Final = 1
  property bus: BusType | None

    The device’s bus type `hidtools.util.BusType`

  call_input_event(_data: Iterable[int]) → None

    Send an input event from this device.

    Parameters:

      data (list) – a list of 8-bit integers representing the HID report for this input event

  close() → None

    Called when a userspace client closes the created kernel device.

    Sending events on a closed device will not result in anyone reading it.

    This message is sent by the kernel, to receive this message you must call `dispatch()`

  create_kernel_device() → None

    Create a kernel device from this device. Note that the device is not immediately ready to go after creation, you must wait for `start()` and ideally for `open()` to be called.

    Raises:

      `UHIDIncompleteException` if the device does not have a name, report descriptor or the info bits set.

  create_report(data: Any, global_data=None, reportID: int | None = None, application: str | int | None = None) → List[int]

    Convert the data object to an array of ints representing the report. Each property of the given data object is matched against the field usage name (think `hasattr`) and filled in accordingly.:

        mouse = MouseData()
        mouse.b1 = int(l)
        mouse.b2 = int(r)
        mouse.b3 = int(m)
        mouse.x = x
        mouse.y = y

        data_bytes = uhid_device.create_report(mouse)

    The `UHIDDevice` will create the report according to the device’s report descriptor.

  destroy() → None

    Destroy the device. The kernel will trigger the appropriate messages in response before removing the device.

    This function is called automatically on __exit__()

  property device_nodes: List[str]

    A list of evdev nodes associated with this HID device. Populating this list requires the kernel to process the uhid device, and sometimes the kernel needs to talk to the uhid process. Ensure that `dispatch()` is called and that you wait for some reasonable time after creating the device.

  classmethod dispatch(timeout: float | None = None) → bool

    Process any events available on any internally registered file descriptor and deal with the events.

    The caller must call this function regularly to make sure things like udev events are processed correctly. There’s no indicator of when to call `dispatch()` yet, call it whenever you’re idle.

    Returns:

      True if data was processed, False otherwise

  property fd: int

    The fd to the `/dev/uhid` device node

  get_report(req: int, rnum: int, rtype: int) → Tuple[int, List[int]]

    Callback invoked when a process calls SetReport on this UHID device.

    Return `(0, [data bytes])` on success or `(errno, [])` on failure.

    The default method always returns `(EIO, [])` for a failure. Override this in your device if you want GetReport to succeed.

    Parameters:

      - req – the request identifier

      - rnum –

        ???

      - rtype – one of `UHID_FEATURE_REPORT`, `UHID_INPUT_REPORT`, or `UHID_OUTPUT_REPORT`

  property hidraw_nodes: List[str]

    A list of hidraw nodes associated with this HID device. Populating this list requires the kernel to process the uhid device, and sometimes the kernel needs to talk to the uhid process. Ensure that `dispatch()` is called and that you wait for some reasonable time after creating the device.

  property info: Tuple[int, int, int] | None

    The devices’s bus, vendor ID and product ID as tuple

  property name: str | None

    The devices HID name

  open() → None

    Called when a userspace client opens the created kernel device.

    This message is sent by the kernel, to receive this message you must call `dispatch()`

  output_report(data: List[int], size: int, rtype: int) → None

    Callback invoked when a process sends raw data to the device.

    Parameters:

      - data – the data sent by the kernel

      - size – size of the data

      - rtype – one of `UHID_FEATURE_REPORT`, `UHID_INPUT_REPORT`, or `UHID_OUTPUT_REPORT`

  property phys: str | None

    The device’s phys string

  property pid: int | None

    The device’s 16-bit product ID

  property rdesc: List[int] | None

    The device’s report descriptor

  set_report(req: int, rnum: int, rtype: int, data: List[int]) → int

    Callback invoked when a process calls SetReport on this UHID device.

    Return `0` on success or an errno on failure.

    The default method always returns `EIO` for a failure. Override this in your device if you want SetReport to succeed.

    Parameters:

      - req – the request identifier

      - rnum –

        ???

      - rtype – one of `UHID_FEATURE_REPORT`, `UHID_INPUT_REPORT`, or `UHID_OUTPUT_REPORT`

      - data (list) – a byte string with the data

  start(flags: int) → None

    Called when the uhid device is ready to accept IO.

    This message is sent by the kernel, to receive this message you must call `dispatch()`

  stop() → None

    Called when the uhid device no longer accepts IO.

    This message is sent by the kernel, to receive this message you must call `dispatch()`

  property sys_path: Path | None

    The device’s /sys path

  property vid: int | None

    The device’s 16-bit vendor ID

  walk_sysfs(kind: str, glob: str | None = None) → Tuple[Path, ...]

exception UHIDIncompleteException

  Bases: `Exception`

  An exception raised when a UHIDDevice does not have sufficient information to create a kernel device.