|
hidapi
|
Macros | |
| #define | HID_API_VERSION_MAJOR 0 |
| Static/compile-time major version of the library. | |
| #define | HID_API_VERSION_MINOR 16 |
| Static/compile-time minor version of the library. | |
| #define | HID_API_VERSION_PATCH 0 |
| Static/compile-time patch version of the library. | |
| #define | HID_API_MAKE_VERSION(mj, mn, p) |
| Coverts a version as Major/Minor/Patch into a number: <8 bit major><16 bit minor><8 bit patch>. | |
| #define | HID_API_VERSION HID_API_MAKE_VERSION(HID_API_VERSION_MAJOR, HID_API_VERSION_MINOR, HID_API_VERSION_PATCH) |
| Static/compile-time version of the library. | |
| #define | HID_API_VERSION_STR HID_API_TO_VERSION_STR(HID_API_VERSION_MAJOR, HID_API_VERSION_MINOR, HID_API_VERSION_PATCH) |
| Static/compile-time string version of the library. | |
| #define | HID_API_MAX_REPORT_DESCRIPTOR_SIZE 4096 |
| Maximum expected HID Report descriptor size in bytes. | |
Enumerations | |
| enum | hid_bus_type { HID_API_BUS_UNKNOWN = 0x00 , HID_API_BUS_USB = 0x01 , HID_API_BUS_BLUETOOTH = 0x02 , HID_API_BUS_I2C = 0x03 , HID_API_BUS_SPI = 0x04 , HID_API_BUS_VIRTUAL = 0x05 } |
| HID underlying bus types. More... | |
Functions | |
| int HID_API_EXPORT HID_API_CALL | hid_init (void) |
| Initialize the HIDAPI library. | |
| int HID_API_EXPORT HID_API_CALL | hid_exit (void) |
| Finalize the HIDAPI library. | |
| struct hid_device_info HID_API_EXPORT *HID_API_CALL | hid_enumerate (unsigned short vendor_id, unsigned short product_id) |
| Enumerate the HID Devices. | |
| void HID_API_EXPORT HID_API_CALL | hid_free_enumeration (struct hid_device_info *devs) |
| Free an enumeration Linked List. | |
| HID_API_EXPORT hid_device *HID_API_CALL | hid_open (unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number) |
| Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number. | |
| HID_API_EXPORT hid_device *HID_API_CALL | hid_open_path (const char *path) |
| Open a HID device by its path name. | |
| int HID_API_EXPORT HID_API_CALL | hid_write (hid_device *dev, const unsigned char *data, size_t length) |
| Write an Output report to a HID device. | |
| int HID_API_EXPORT HID_API_CALL | hid_read_timeout (hid_device *dev, unsigned char *data, size_t length, int milliseconds) |
| Read an Input report from a HID device with timeout. | |
| int HID_API_EXPORT HID_API_CALL | hid_read (hid_device *dev, unsigned char *data, size_t length) |
| Read an Input report from a HID device. | |
| HID_API_EXPORT const wchar_t *HID_API_CALL | hid_read_error (hid_device *dev) |
| Get a string describing the last error which occurred during hid_read/hid_read_timeout. | |
| int HID_API_EXPORT HID_API_CALL | hid_set_nonblocking (hid_device *dev, int nonblock) |
| Set the device handle to be non-blocking. | |
| int HID_API_EXPORT HID_API_CALL | hid_set_num_input_buffers (hid_device *dev, int num_buffers) |
| Set the number of input report buffers queued per device. | |
| void HID_API_EXPORT HID_API_CALL | hid_read_interrupt (hid_device *dev) |
| Asynchronously interrupt blocking hid_read/hid_read_timeout call. | |
| int HID_API_EXPORT HID_API_CALL | hid_is_read_interrupted (hid_device *dev) |
| Query whether the read pipeline is currently interrupted. | |
| void HID_API_EXPORT HID_API_CALL | hid_read_clear_interrupt (hid_device *dev) |
| Clear the read-interrupt state, allowing reads to resume. | |
| int HID_API_EXPORT HID_API_CALL | hid_send_feature_report (hid_device *dev, const unsigned char *data, size_t length) |
| Send a Feature report to the device. | |
| int HID_API_EXPORT HID_API_CALL | hid_get_feature_report (hid_device *dev, unsigned char *data, size_t length) |
| Get a feature report from a HID device. | |
| int HID_API_EXPORT HID_API_CALL | hid_send_output_report (hid_device *dev, const unsigned char *data, size_t length) |
| Send a Output report to the device. | |
| int HID_API_EXPORT HID_API_CALL | hid_get_input_report (hid_device *dev, unsigned char *data, size_t length) |
| Get a input report from a HID device. | |
| void HID_API_EXPORT HID_API_CALL | hid_close (hid_device *dev) |
| Close a HID device. | |
| int HID_API_EXPORT_CALL | hid_get_manufacturer_string (hid_device *dev, wchar_t *string, size_t maxlen) |
| Get The Manufacturer String from a HID device. | |
| int HID_API_EXPORT_CALL | hid_get_product_string (hid_device *dev, wchar_t *string, size_t maxlen) |
| Get The Product String from a HID device. | |
| int HID_API_EXPORT_CALL | hid_get_serial_number_string (hid_device *dev, wchar_t *string, size_t maxlen) |
| Get The Serial Number String from a HID device. | |
| struct hid_device_info HID_API_EXPORT *HID_API_CALL | hid_get_device_info (hid_device *dev) |
| Get The struct hid_device_info from a HID device. | |
| int HID_API_EXPORT_CALL | hid_get_indexed_string (hid_device *dev, int string_index, wchar_t *string, size_t maxlen) |
| Get a string from a HID device, based on its string index. | |
| int HID_API_EXPORT_CALL | hid_get_report_descriptor (hid_device *dev, unsigned char *buf, size_t buf_size) |
| Get a report descriptor from a HID device. | |
| HID_API_EXPORT const wchar_t *HID_API_CALL | hid_error (hid_device *dev) |
| Get a string describing the last error which occurred. | |
| HID_API_EXPORT const struct hid_api_version *HID_API_CALL | hid_version (void) |
| Get a runtime version of the library. | |
| HID_API_EXPORT const char *HID_API_CALL | hid_version_str (void) |
| Get a runtime version string of the library. | |
| HID_API_EXPORT hid_device *HID_API_CALL | hid_libusb_wrap_sys_device (intptr_t sys_dev, int interface_num) |
| Open a HID device using libusb_wrap_sys_device. See https://libusb.sourceforge.io/api-1.0/group__libusb__dev.html#ga98f783e115ceff4eaf88a60e6439563c, for details on libusb_wrap_sys_device. | |
| HID_API_EXPORT int HID_API_CALL | hid_libusb_error (hid_device *dev) |
| Similar to hid_error but gives a libusb error code. | |
| HID_API_EXPORT void HID_API_CALL | hid_libusb_set_write_timeout (hid_device *dev, unsigned int timeout) |
| Sets the timeout for hid_write operation. | |
| HID_API_EXPORT void HID_API_CALL | hid_libusb_set_send_output_report_timeout (hid_device *dev, unsigned int timeout) |
| Sets the timeout for hid_send_output_report operation. | |
| HID_API_EXPORT void HID_API_CALL | hid_libusb_set_send_feature_report_timeout (hid_device *dev, unsigned int timeout) |
| Sets the timeout for hid_send_feature_report operation. | |
| int HID_API_EXPORT_CALL | hid_darwin_get_location_id (hid_device *dev, uint32_t *location_id) |
| Get the location ID for a HID device. | |
| void HID_API_EXPORT_CALL | hid_darwin_set_open_exclusive (int open_exclusive) |
| Changes the behavior of all further calls to hid_open or hid_open_path. | |
| int HID_API_EXPORT_CALL | hid_darwin_get_open_exclusive (void) |
| Getter for option set by hid_darwin_set_open_exclusive. | |
| int HID_API_EXPORT_CALL | hid_darwin_is_device_open_exclusive (hid_device *dev) |
| Check how the device was opened. | |
| int HID_API_EXPORT_CALL | hid_winapi_get_container_id (hid_device *dev, GUID *container_id) |
| Get the container ID for a HID device. | |
| int HID_API_EXPORT_CALL | hid_winapi_descriptor_reconstruct_pp_data (void *hidp_preparsed_data, unsigned char *buf, size_t buf_size) |
| Reconstructs a HID Report Descriptor from a Win32 HIDP_PREPARSED_DATA structure. This reconstructed report descriptor is logical identical to the real report descriptor, but not byte wise identical. | |
| void HID_API_EXPORT_CALL | hid_winapi_set_write_timeout (hid_device *dev, unsigned long timeout) |
| Sets the timeout for hid_write operation. | |
Since version 0.11.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 11, 0).
Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
| #define HID_API_MAKE_VERSION | ( | mj, | |
| mn, | |||
| p ) |
Coverts a version as Major/Minor/Patch into a number: <8 bit major><16 bit minor><8 bit patch>.
This macro was added in version 0.12.0.
Convenient function to be used for compile-time checks, like:
| #define HID_API_MAX_REPORT_DESCRIPTOR_SIZE 4096 |
Maximum expected HID Report descriptor size in bytes.
Since version 0.13.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 13, 0)
| #define HID_API_VERSION HID_API_MAKE_VERSION(HID_API_VERSION_MAJOR, HID_API_VERSION_MINOR, HID_API_VERSION_PATCH) |
Static/compile-time version of the library.
This macro was added in version 0.12.0.
| enum hid_bus_type |
HID underlying bus types.
| Enumerator | |
|---|---|
| HID_API_BUS_UNKNOWN | Unknown bus type |
| HID_API_BUS_USB | USB bus Specifications: https://usb.org/hid |
| HID_API_BUS_BLUETOOTH | Bluetooth or Bluetooth LE bus Specifications: https://www.bluetooth.com/specifications/specs/human-interface-device-profile-1-1-1/ https://www.bluetooth.com/specifications/specs/hid-service-1-0/ https://www.bluetooth.com/specifications/specs/hid-over-gatt-profile-1-0/ |
| HID_API_BUS_I2C | I2C bus Specifications: https://docs.microsoft.com/previous-versions/windows/hardware/design/dn642101(v=vs.85) |
| HID_API_BUS_SPI | SPI bus Specifications: https://www.microsoft.com/download/details.aspx?id=103325 |
| HID_API_BUS_VIRTUAL | Virtual device E.g.: https://elixir.bootlin.com/linux/v4.0/source/include/uapi/linux/input.h#L955 Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0) |
| void HID_API_EXPORT HID_API_CALL hid_close | ( | hid_device * | dev | ) |
| int HID_API_EXPORT_CALL hid_darwin_get_location_id | ( | hid_device * | dev, |
| uint32_t * | location_id ) |
Get the location ID for a HID device.
Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
| dev | A device handle returned from hid_open(). |
| location_id | The device's location ID on return. |
| int HID_API_EXPORT_CALL hid_darwin_get_open_exclusive | ( | void | ) |
Getter for option set by hid_darwin_set_open_exclusive.
Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
| int HID_API_EXPORT_CALL hid_darwin_is_device_open_exclusive | ( | hid_device * | dev | ) |
Check how the device was opened.
Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
| dev | A device to get property from. |
| void HID_API_EXPORT_CALL hid_darwin_set_open_exclusive | ( | int | open_exclusive | ) |
Changes the behavior of all further calls to hid_open or hid_open_path.
By default on Darwin platform all devices opened by HIDAPI with hid_open or hid_open_path are opened in exclusive mode (see kIOHIDOptionsTypeSeizeDevice).
Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
| open_exclusive | When set to 0 - all further devices will be opened in non-exclusive mode. Otherwise - all further devices will be opened in exclusive mode. |
| struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_enumerate | ( | unsigned short | vendor_id, |
| unsigned short | product_id ) |
Enumerate the HID Devices.
This function returns a linked list of all the HID devices attached to the system which match vendor_id and product_id. If vendor_id is set to 0 then any vendor matches. If product_id is set to 0 then any product matches. If vendor_id and product_id are both set to 0, then all HID devices will be returned.
| vendor_id | The Vendor ID (VID) of the types of device to open. |
| product_id | The Product ID (PID) of the types of device to open. |
| HID_API_EXPORT const wchar_t *HID_API_CALL hid_error | ( | hid_device * | dev | ) |
Get a string describing the last error which occurred.
This function is intended for logging/debugging purposes.
This function guarantees to never return NULL. If there was no error in the last function call - the returned string clearly indicates that.
Any HIDAPI function that can explicitly indicate an execution failure (e.g. by an error code, or by returning NULL) - may set the error string, to be returned by this function.
Strings returned from hid_error() must not be freed by the user, i.e. owned by HIDAPI library. Device-specific error string may remain allocated at most until hid_close() is called. Global error string may remain allocated at most until hid_exit() is called.
| dev | A device handle returned from hid_open(), or NULL to get the last non-device-specific error (e.g. for errors in hid_open() or hid_enumerate()). |
| int HID_API_EXPORT HID_API_CALL hid_exit | ( | void | ) |
Finalize the HIDAPI library.
This function frees all of the static data associated with HIDAPI. It should be called at the end of execution to avoid memory leaks.
| void HID_API_EXPORT HID_API_CALL hid_free_enumeration | ( | struct hid_device_info * | devs | ) |
Free an enumeration Linked List.
This function frees a linked list created by hid_enumerate().
| devs | Pointer to a list of struct_device returned from hid_enumerate(). |
| struct hid_device_info HID_API_EXPORT *HID_API_CALL hid_get_device_info | ( | hid_device * | dev | ) |
Get The struct hid_device_info from a HID device.
Since version 0.13.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 13, 0)
| dev | A device handle returned from hid_open(). |
dev, and SHOULD NOT be freed by the user. | int HID_API_EXPORT HID_API_CALL hid_get_feature_report | ( | hid_device * | dev, |
| unsigned char * | data, | ||
| size_t | length ) |
Get a feature report from a HID device.
Set the first byte of data[] to the Report ID of the report to be read. Make sure to allow space for this extra byte in data[]. Upon return, the first byte will still contain the Report ID, and the report data will start in data[1].
| dev | A device handle returned from hid_open(). |
| data | A buffer to put the read data into, including the Report ID. Set the first byte of data[] to the Report ID of the report to be read, or set it to zero if your device does not use numbered reports. |
| length | The number of bytes to read, including an extra byte for the report ID. The buffer can be longer than the actual report. |
| int HID_API_EXPORT_CALL hid_get_indexed_string | ( | hid_device * | dev, |
| int | string_index, | ||
| wchar_t * | string, | ||
| size_t | maxlen ) |
Get a string from a HID device, based on its string index.
| dev | A device handle returned from hid_open(). |
| string_index | The index of the string to get. |
| string | A wide string buffer to put the data into. |
| maxlen | The length of the buffer in multiples of wchar_t. |
| int HID_API_EXPORT HID_API_CALL hid_get_input_report | ( | hid_device * | dev, |
| unsigned char * | data, | ||
| size_t | length ) |
Get a input report from a HID device.
Since version 0.10.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 10, 0)
Set the first byte of data[] to the Report ID of the report to be read. Make sure to allow space for this extra byte in data[]. Upon return, the first byte will still contain the Report ID, and the report data will start in data[1].
| dev | A device handle returned from hid_open(). |
| data | A buffer to put the read data into, including the Report ID. Set the first byte of data[] to the Report ID of the report to be read, or set it to zero if your device does not use numbered reports. |
| length | The number of bytes to read, including an extra byte for the report ID. The buffer can be longer than the actual report. |
| int HID_API_EXPORT_CALL hid_get_manufacturer_string | ( | hid_device * | dev, |
| wchar_t * | string, | ||
| size_t | maxlen ) |
Get The Manufacturer String from a HID device.
| dev | A device handle returned from hid_open(). |
| string | A wide string buffer to put the data into. |
| maxlen | The length of the buffer in multiples of wchar_t. |
| int HID_API_EXPORT_CALL hid_get_product_string | ( | hid_device * | dev, |
| wchar_t * | string, | ||
| size_t | maxlen ) |
Get The Product String from a HID device.
| dev | A device handle returned from hid_open(). |
| string | A wide string buffer to put the data into. |
| maxlen | The length of the buffer in multiples of wchar_t. |
| int HID_API_EXPORT_CALL hid_get_report_descriptor | ( | hid_device * | dev, |
| unsigned char * | buf, | ||
| size_t | buf_size ) |
Get a report descriptor from a HID device.
Since version 0.14.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 14, 0)
User has to provide a preallocated buffer where descriptor will be copied to. The recommended size for preallocated buffer is HID_API_MAX_REPORT_DESCRIPTOR_SIZE bytes.
| dev | A device handle returned from hid_open(). |
| buf | The buffer to copy descriptor into. |
| buf_size | The size of the buffer in bytes. |
| int HID_API_EXPORT_CALL hid_get_serial_number_string | ( | hid_device * | dev, |
| wchar_t * | string, | ||
| size_t | maxlen ) |
Get The Serial Number String from a HID device.
| dev | A device handle returned from hid_open(). |
| string | A wide string buffer to put the data into. |
| maxlen | The length of the buffer in multiples of wchar_t. |
| int HID_API_EXPORT HID_API_CALL hid_init | ( | void | ) |
Initialize the HIDAPI library.
This function initializes the HIDAPI library. Calling it is not strictly necessary, as it will be called automatically by hid_enumerate() and any of the hid_open_*() functions if it is needed. This function should be called at the beginning of execution however, if there is a chance of HIDAPI handles being opened by different threads simultaneously.
| int HID_API_EXPORT HID_API_CALL hid_is_read_interrupted | ( | hid_device * | dev | ) |
Query whether the read pipeline is currently interrupted.
Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)
Returns the current interrupt state set by hid_read_interrupt and hid_read_clear_interrupt. This function may run alongside device I/O, but not hid_close() or hid_exit(). The device handle must remain valid until the call returns.
Calls to hid_read_interrupt(), hid_is_read_interrupted(), and hid_read_clear_interrupt() on the same device must be serialized against one another, including calls to the same function. Otherwise, behavior is undefined.
| dev | A device handle returned from hid_open(). |
| HID_API_EXPORT int HID_API_CALL hid_libusb_error | ( | hid_device * | dev | ) |
Similar to hid_error but gives a libusb error code.
Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)
If the error occurred is not immediately caused by a libusb function call, the returned value is 1. hid_error would still contain a valid and meaningful error message.
| dev | A device handle returned from hid_open(), or NULL to get the last non-device-specific error (e.g. for errors in hid_open() or hid_enumerate()). |
| HID_API_EXPORT void HID_API_CALL hid_libusb_set_send_feature_report_timeout | ( | hid_device * | dev, |
| unsigned int | timeout ) |
Sets the timeout for hid_send_feature_report operation.
Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)
The default timeout is 1sec for libusb backend.
In case if 1sec is not enough, on in case of multi-platform development, the recommended value is 5sec, e.g. to match (unconfigurable) 5sec timeout set for hidraw (linux kernel) implementation.
When the timeout is set to 0, the function will not exit, until the write operation is performed or an error occurred.
| dev | A device handle returned from hid_open(). |
| timeout | New timeout value in milliseconds. |
| HID_API_EXPORT void HID_API_CALL hid_libusb_set_send_output_report_timeout | ( | hid_device * | dev, |
| unsigned int | timeout ) |
Sets the timeout for hid_send_output_report operation.
Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)
The default timeout is 1sec for libusb backend.
In case if 1sec is not enough, on in case of multi-platform development, the recommended value is 5sec, e.g. to match (unconfigurable) 5sec timeout set for hidraw (linux kernel) implementation.
When the timeout is set to 0, the function will not exit, until the write operation is performed or an error occurred.
| dev | A device handle returned from hid_open(). |
| timeout | New timeout value in milliseconds. |
| HID_API_EXPORT void HID_API_CALL hid_libusb_set_write_timeout | ( | hid_device * | dev, |
| unsigned int | timeout ) |
Sets the timeout for hid_write operation.
Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)
The default timeout is 1sec for libusb backend.
In case if 1sec is not enough, on in case of multi-platform development, the recommended value is 5sec, e.g. to match (unconfigurable) 5sec timeout set for hidraw (linux kernel) implementation.
When the timeout is set to 0, the function will not exit, until the write operation is performed or an error occurred.
| dev | A device handle returned from hid_open(). |
| timeout | New timeout value in milliseconds. |
| HID_API_EXPORT hid_device *HID_API_CALL hid_libusb_wrap_sys_device | ( | intptr_t | sys_dev, |
| int | interface_num ) |
Open a HID device using libusb_wrap_sys_device. See https://libusb.sourceforge.io/api-1.0/group__libusb__dev.html#ga98f783e115ceff4eaf88a60e6439563c, for details on libusb_wrap_sys_device.
| sys_dev | Platform-specific file descriptor that can be recognized by libusb. |
| interface_num | USB interface number of the device to be used as HID interface. Pass -1 to select first HID interface of the device. |
| HID_API_EXPORT hid_device *HID_API_CALL hid_open | ( | unsigned short | vendor_id, |
| unsigned short | product_id, | ||
| const wchar_t * | serial_number ) |
Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number.
If serial_number is NULL, the first device with the specified VID and PID is opened.
| vendor_id | The Vendor ID (VID) of the device to open. |
| product_id | The Product ID (PID) of the device to open. |
| serial_number | The Serial Number of the device to open (Optionally NULL). |
| HID_API_EXPORT hid_device *HID_API_CALL hid_open_path | ( | const char * | path | ) |
Open a HID device by its path name.
The path name be determined by calling hid_enumerate(), or a platform-specific path name can be used (eg: /dev/hidraw0 on Linux).
| path | The path name of the device to open |
| int HID_API_EXPORT HID_API_CALL hid_read | ( | hid_device * | dev, |
| unsigned char * | data, | ||
| size_t | length ) |
Read an Input report from a HID device.
Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number if the device uses numbered reports.
| dev | A device handle returned from hid_open(). |
| data | A buffer to put the read data into. |
| length | The number of bytes to read. For devices with multiple reports, make sure to read an extra byte for the report number. |
| void HID_API_EXPORT HID_API_CALL hid_read_clear_interrupt | ( | hid_device * | dev | ) |
Clear the read-interrupt state, allowing reads to resume.
Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)
Clears the sticky state set by hid_read_interrupt. After this function returns, hid_is_read_interrupted() returns 0 and reads entered afterwards operate normally. Calling it when the read pipeline is not interrupted has no effect.
Any data that the device buffered during the interrupted period may be returned by subsequent reads, subject to a (backend-specific) buffer capacity. For a fresh-start behavior, the caller may drain the buffered data with a loop of hid_read_timeout(dev, ..., 0) calls before resuming the normal read loop.
Recommended use: call only when no hid_read*() call is in flight on dev. A read that is in flight while this function runs may either fail as interrupted or continue normally; which of the two happens is indeterminate. Do not overlap this function with hid_close() or hid_exit(); the device handle must remain valid until the call returns.
Calls to hid_read_interrupt(), hid_is_read_interrupted(), and hid_read_clear_interrupt() on the same device must be serialized against one another, including calls to the same function. Otherwise, behavior is undefined.
| dev | A device handle returned from hid_open(). |
| HID_API_EXPORT const wchar_t *HID_API_CALL hid_read_error | ( | hid_device * | dev | ) |
Get a string describing the last error which occurred during hid_read/hid_read_timeout.
Since version 0.15.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 15, 0)
This function is intended for logging/debugging purposes.
This function guarantees to never return NULL for a valid dev. If there was no error in the last call to hid_read/hid_read_error - the returned string clearly indicates that.
Strings returned from hid_read_error() must not be freed by the user, i.e. owned by HIDAPI library. Device-specific error string may remain allocated at most until hid_close() is called.
| dev | A device handle. Shall never be NULL. |
| void HID_API_EXPORT HID_API_CALL hid_read_interrupt | ( | hid_device * | dev | ) |
Asynchronously interrupt blocking hid_read/hid_read_timeout call.
Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)
Puts the read pipeline of dev into a sticky interrupted state and wakes a hid_read()/hid_read_timeout() call that is waiting for input. An interrupted read returns -1; hid_read_error(dev) returns a string indicating the read was interrupted.
A read already in progress when this function is called may still return data that was already buffered or queued. Every read entered after this function returns fails immediately with -1, until hid_read_clear_interrupt is called. Queued input is not discarded; it remains readable after the interrupt is cleared.
Only the read pipeline is affected: hid_write(), hid_get_input_report(), feature/output report functions, and all other operations on dev continue to work normally.
This function may run concurrently with device I/O, including a blocked read, but not with hid_close() or hid_exit(). The device handle must remain valid until all calls using it have returned. The call is idempotent and is safe when no read is in flight.
Calls to hid_read_interrupt(), hid_is_read_interrupted(), and hid_read_clear_interrupt() on the same device must be serialized against one another, including calls to the same function. Otherwise, behavior is undefined.
After this function returns, hid_is_read_interrupted() returns 1 until the interrupt is cleared. This function has no failure result; it relies on the operating system and device driver to support read interruption.
Recommended usage to cleanly shut down a dedicated read thread:
| dev | A device handle returned from hid_open(). |
| int HID_API_EXPORT HID_API_CALL hid_read_timeout | ( | hid_device * | dev, |
| unsigned char * | data, | ||
| size_t | length, | ||
| int | milliseconds ) |
Read an Input report from a HID device with timeout.
Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number if the device uses numbered reports.
| dev | A device handle returned from hid_open(). |
| data | A buffer to put the read data into. |
| length | The number of bytes to read. For devices with multiple reports, make sure to read an extra byte for the report number. |
| milliseconds | timeout in milliseconds or -1 for blocking wait. |
| int HID_API_EXPORT HID_API_CALL hid_send_feature_report | ( | hid_device * | dev, |
| const unsigned char * | data, | ||
| size_t | length ) |
Send a Feature report to the device.
Feature reports are sent over the Control endpoint as a Set_Report transfer. The first byte of data[] must contain the Report ID. For devices which only support a single report, this must be set to 0x0. The remaining bytes contain the report data. Since the Report ID is mandatory, calls to hid_send_feature_report() will always contain one more byte than the report contains. For example, if a hid report is 16 bytes long, 17 bytes must be passed to hid_send_feature_report(): the Report ID (or 0x0, for devices which do not use numbered reports), followed by the report data (16 bytes). In this example, the length passed in would be 17.
| dev | A device handle returned from hid_open(). |
| data | The data to send, including the report number as the first byte. |
| length | The length in bytes of the data to send, including the report number. |
| int HID_API_EXPORT HID_API_CALL hid_send_output_report | ( | hid_device * | dev, |
| const unsigned char * | data, | ||
| size_t | length ) |
Send a Output report to the device.
Since version 0.15.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 15, 0)
Output reports are sent over the Control endpoint as a Set_Report transfer. The first byte of data[] must contain the Report ID. For devices which only support a single report, this must be set to 0x0. The remaining bytes contain the report data. Since the Report ID is mandatory, calls to hid_send_output_report() will always contain one more byte than the report contains. For example, if a hid report is 16 bytes long, 17 bytes must be passed to hid_send_output_report(): the Report ID (or 0x0, for devices which do not use numbered reports), followed by the report data (16 bytes). In this example, the length passed in would be 17.
This function sets the return value of hid_error().
| dev | A device handle returned from hid_open(). |
| data | The data to send, including the report number as the first byte. |
| length | The length in bytes of the data to send, including the report number. |
| int HID_API_EXPORT HID_API_CALL hid_set_nonblocking | ( | hid_device * | dev, |
| int | nonblock ) |
Set the device handle to be non-blocking.
In non-blocking mode calls to hid_read() will return immediately with a value of 0 if there is no data to be read. In blocking mode, hid_read() will wait (block) until there is data to read before returning.
Nonblocking can be turned on and off at any time.
| dev | A device handle returned from hid_open(). |
| nonblock | enable or not the nonblocking reads
|
| int HID_API_EXPORT HID_API_CALL hid_set_num_input_buffers | ( | hid_device * | dev, |
| int | num_buffers ) |
Set the number of input report buffers queued per device.
Since version 0.16.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)
Some HID devices emit input reports in bursts at rates that exceed the default internal queue capacity, causing silent report drops on macOS and the libusb backend. This function allows callers to change how many input report buffers are retained per device.
Call after hid_open() and before the first hid_read() to avoid losing reports buffered at open time.
| dev | A device handle returned from hid_open(). |
| num_buffers | The desired number of input report buffers. Must be in range [1, HID_API_MAX_NUM_INPUT_BUFFERS]. |
| HID_API_EXPORT const struct hid_api_version *HID_API_CALL hid_version | ( | void | ) |
Get a runtime version of the library.
This function is thread-safe.
| HID_API_EXPORT const char *HID_API_CALL hid_version_str | ( | void | ) |
Get a runtime version string of the library.
This function is thread-safe.
| int HID_API_EXPORT_CALL hid_winapi_descriptor_reconstruct_pp_data | ( | void * | hidp_preparsed_data, |
| unsigned char * | buf, | ||
| size_t | buf_size ) |
Reconstructs a HID Report Descriptor from a Win32 HIDP_PREPARSED_DATA structure. This reconstructed report descriptor is logical identical to the real report descriptor, but not byte wise identical.
| [in] | hidp_preparsed_data | Pointer to the HIDP_PREPARSED_DATA to read, i.e.: the value of PHIDP_PREPARSED_DATA, as returned by HidD_GetPreparsedData WinAPI function. |
| buf | Pointer to the buffer where the report descriptor should be stored. | |
| [in] | buf_size | Size of the buffer. The recommended size for the buffer is HID_API_MAX_REPORT_DESCRIPTOR_SIZE bytes. |
| int HID_API_EXPORT_CALL hid_winapi_get_container_id | ( | hid_device * | dev, |
| GUID * | container_id ) |
Get the container ID for a HID device.
Since version 0.12.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
This function returns the `DEVPKEY_Device_ContainerId` property of the given device. This can be used to correlate different interfaces/ports on the same hardware device.
| dev | A device handle returned from hid_open(). |
| container_id | The device's container ID on return. |
| void HID_API_EXPORT_CALL hid_winapi_set_write_timeout | ( | hid_device * | dev, |
| unsigned long | timeout ) |
Sets the timeout for hid_write operation.
Since version 0.15.0, HID_API_VERSION >= HID_API_MAKE_VERSION(0, 15, 0)
The default timeout is 1sec for winapi backend.
In case if 1sec is not enough, on in case of multi-platform development, the recommended value is 5sec, e.g. to match (unconfigurable) 5sec timeout set for hidraw (linux kernel) implementation.
When the timeout is set to 0, hid_write function becomes non-blocking and would exit immediately. When the timeout is set to INFINITE ((DWORD)-1), the function will not exit, until the write operation is performed or an error occurred. See dwMilliseconds parameter documentation of WaitForSingleObject function.
| dev | A device handle returned from hid_open(). |
| timeout | New timeout value in milliseconds. |
| int HID_API_EXPORT HID_API_CALL hid_write | ( | hid_device * | dev, |
| const unsigned char * | data, | ||
| size_t | length ) |
Write an Output report to a HID device.
The first byte of data[] must contain the Report ID. For devices which only support a single report, this must be set to 0x0. The remaining bytes contain the report data. Since the Report ID is mandatory, calls to hid_write() will always contain one more byte than the report contains. For example, if a hid report is 16 bytes long, 17 bytes must be passed to hid_write(), the Report ID (or 0x0, for devices with a single report), followed by the report data (16 bytes). In this example, the length passed in would be 17.
hid_write() will send the data on the first interrupt OUT endpoint, if one exists. If it does not the behaviour is as hid_send_output_report
| dev | A device handle returned from hid_open(). |
| data | The data to send, including the report number as the first byte. |
| length | The length in bytes of the data to send. |