hidapi icon indicating copy to clipboard operation
hidapi copied to clipboard

More precise error reporting on hid_write and others

Open VinDuv opened this issue 7 years ago • 1 comments

Currently, most hidapi functions only return -1 or NULL on failure, without more detailed error codes.

The dolphin-emu project, which uses hidapi internally, discriminates between different possible failures of hid_writeby using errno (see https://github.com/dolphin-emu/dolphin/blob/master/Source/Core/Core/HW/WiimoteReal/IOhidapi.cpp#L29)

This works on the hid-raw Linux port of hidapi because hid_write is implemented as a write syscall which sets errno on failure. On macOS, the IOKit functions don’t set errno so the check is inconsistent.

I’ve proposed a patch to dolphin that modifies hidapi to set errno in hid_write depending on the return value of IOHIDDeviceSetReport (see https://github.com/dolphin-emu/dolphin/pull/6294/files#diff-19b47524bb1e75d2880473dc17f2daa8R769), but I’m not sure if it’s the best way to do it in a cross-platform manner.

One way to do it could be to add an error code enum into hidapi and have the functions return the right error code on failure. This would have some souce compatibility issues, though. I’m also not sure if the errors returned by the low-level HID API on different OSes is consistent enough to be able to return meaningful common error codes.

Another way would be to provide a platform-specific error code somewhere in hidapi (possibly with a global variable/function), that would contain the error code returned by the OS API on failure. I noticed that hidapi provides a hid_error function, but it returns a string (so it isn’t easily usable for error checking purposes) and doesn’t seems implemented on Linux and macOS.

VinDuv avatar Jan 13 '18 14:01 VinDuv

I agree with this. I had to invent a band-aid to get more info. I essentially had to write a flag byte and check on return.

OT: VinDuv, do you work on the dolphin emu much? I make adapters (Bliss-Box) and I'm working with what is called native controller support. It really is more like USb tunneling. Anyways the dolphin bar does this I think. I want to allow Dolphin to work with Bliss-Box in the same manner. That is sent/got via a feature report to the device and get raw data from the controller. Are you aware of which I refer to? Can you help? PM, reply, or Email :)

ulao avatar Jan 31 '18 15:01 ulao