Experimental libwinhid backend for usbhid-ups (Windows only)#3335
Experimental libwinhid backend for usbhid-ups (Windows only)#3335MysteryDove wants to merge 9 commits intonetworkupstools:masterfrom
Conversation
…, reorder HID parsing index
|
❌ Build nut 2.8.4.4276-master failed (commit 1018d75723 by @MysteryDove) |
|
This looks promising, thanks! The indentation is a bit jumpy when I looked at it in GitHub WebUI (tabs vs spaces?) and the two aliases for Notably, does the resulting program need linking with Also, I wonder if the methods you've added to |
I have make few rounds of refactoring of this patch, beginning with using Thanks for the advice and I will make the code cleaner. |
|
For the second part, my goal is make it acting like a replacement of libusb under windows to provide alternative of transporting data besides libusb for HID devices ONLY (But it has drawback of not getting any raw data from USB interface), I think current code should already be capable for testing with other USB-capable drivers (I'm assuming all USB-capable drivers are a sub-driver of usbhid-ups since I'm not having full context of the NUT project, and also windows hid api obviously works straighfoward with usbhid-ups) |
Well, actually yes, for USB HID that should be the case. Other USB cases include numerous Megatec Qx protocol handlers, mostly under Skimming through sources, I see that |
|
@jimklimov Fixed for typo and indent now, Under assist of Claude Opus 4.6, A major refactor is also done I've walk through For newly added |
|
❌ Build nut 2.8.4.4292-master failed (commit 1fcfada43c by @MysteryDove) |
|
✅ Build nut 2.8.4.4293-master completed (commit 23fb3e32de by @MysteryDove) |
|
For those who interested in this patch and want help testing, here's a build Windows x64 release: https://github.com/MysteryDove/nut/releases/tag/v2.8.4-exphid01 |
…inhid Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
…rt for usbhid-ups [networkupstools#3335] Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
…workupstools#3335] Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
|
@MysteryDove : just in case, to confirm: while the driver may now link against some libusb{0,1} and winhid backends simultaneously, this just provides a run-time choice and libusb code path/methods are never called if winhid is chosen? To make the PR more merge-ready, I've added commits with a NEWS entry and with renaming of the config flag from |
For the first part, the simultaneous linkage just gives a runtime backend choice, maybe some shared warning/debug message/error handle is reused, like this upsdebugx(2, "Initializing an USB-connected UPS with backend %s " \
"(NUT subdriver name='%s' ver='%s')",
transport_backend ? transport_backend : "(unknown)",
comm_driver->name, comm_driver->version );
warn_if_bad_usb_port_filename(device_path);Winhid backend also uses static int winhid_map_winerr_to_libusb(const DWORD err)
{
switch (err) {
case ERROR_SUCCESS:
return 0;
case ERROR_ACCESS_DENIED:
return LIBUSB_ERROR_ACCESS;
case ERROR_SHARING_VIOLATION:
return LIBUSB_ERROR_BUSY;
case ERROR_NOT_ENOUGH_MEMORY:
case ERROR_OUTOFMEMORY:
return LIBUSB_ERROR_NO_MEM;
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
return LIBUSB_ERROR_NOT_FOUND;
case ERROR_DEVICE_NOT_CONNECTED:
case ERROR_INVALID_HANDLE:
return LIBUSB_ERROR_NO_DEVICE;
case ERROR_SEM_TIMEOUT:
return LIBUSB_ERROR_TIMEOUT;
case ERROR_INVALID_FUNCTION:
return LIBUSB_ERROR_NOT_SUPPORTED;
default:
return LIBUSB_ERROR_IO;
}
}the libwinhid shares
#if (!WITH_LIBUSB_1_0) && (!WITH_LIBUSB_0_1)
#error "configure script error: Neither WITH_LIBUSB_1_0 nor WITH_LIBUSB_0_1 is set"
#endif
/* Select version-specific libusb header file and define a sort of
* "Compatibility layer" between libusb 0.1 and 1.0
*/
#if WITH_LIBUSB_1_0
# include <libusb.h>
I'm suspecting changing current code with backend-neutral transport ABI/typedefs/device handle type might introduce duplicated code and have impact or risk here. Since I'm not familiar with build system and makefile, I'd rather keep current situation. |
|
Aha, ok, thanks for clarification! Maybe later some |
Proof of concept stage
While working on my own STM32 project to simulate USB HID UPS, I've made a python script to read windows HID API and it works as expected. So I'm looking for some ways to port that so I can use nut on windows without any additional drivers.
I'm working on an experimental, native Windows HID backend (
libwinhid) for theusbhid-upsdriver to try avoid the libusb's problem of fetching raw bytes from Windows system claimed HID UPS device. So that no extra driver likeWinUSBorZadigis needed.For enumeration I'm using setupapi.dll
For HID device:
HidD_GetPreparsedDataandHidP_GetCapsto read collection mappings.HidD_GetFeature / HidD_SetFeatureto handle feature reports.The current implementation is kinda messy, takes Windows capabilities to reconstruct a descriptor, route back to
Parse_ReportDesc()to parse that, And continue the remaining logic as untouched, but few functions are introduced here to deal with improper HID descriptor Parsing. The code is not cleaned and completly tested yet, but it's working now with apchid subdriver with correct data reading now.To use current updated code, pollonly and experimentalhid(newly added) is required:
Looking for test result and idea how to improve this to make the code works better