Skip to content

Commit

Permalink
Detect necessary packet size for the device (#70)
Browse files Browse the repository at this point in the history
* Dynamically determine the packet size for the device via reading the hid descriptor

* Try Ubuntu 24.04 in CI to see if that has the appropriate hidapi version

* Fix function prototype
  • Loading branch information
simon-wh authored Jul 11, 2024
1 parent f68aeac commit be3f8a4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

strategy:
matrix:
os: [macos-latest, ubuntu-latest]
os: [macos-latest, ubuntu-24.04]
include:
- os: windows-latest
target: x64
Expand Down
2 changes: 1 addition & 1 deletion hidapi
Submodule hidapi updated 116 files
50 changes: 49 additions & 1 deletion src/wooting-usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static uint16_t getCrc16ccitt(const uint8_t *buffer, uint16_t size) {
return crc;
}

typedef void (*set_meta_func)();
typedef void (*set_meta_func)(WOOTING_USB_META *device_meta);
void walk_hid_devices(struct hid_device_info *hid_info_walker,
set_meta_func meta_func);

Expand Down Expand Up @@ -433,6 +433,54 @@ void walk_hid_devices(struct hid_device_info *hid_info_walker,
meta_func(&wooting_usb_meta_array[connected_keyboards]);
(&wooting_usb_meta_array[connected_keyboards])->connected = true;

unsigned char buff[HID_API_MAX_REPORT_DESCRIPTOR_SIZE];

int len = hid_get_report_descriptor(keyboard_handle, buff,
HID_API_MAX_REPORT_DESCRIPTOR_SIZE);
if (len > 0) {
#ifdef DEBUG_LOG
printf("Got descriptor with len %d\n", len);
#endif
for (int i = 0; i < len; i++) {
// For this check, we can be a bit basic knowing the descriptors of
// the Wooting devices. In the cases where it's using small packets,
// we'll see the 0x95 byte, indicating the Report size, but with
// only one byte parameter (i.e. 64). For big packet, it's 256, and
// that has to be represented in two bytes, which means we use 0x96
// as the byte to indicate the report size declaration. So a more
// general purpose implementation would read what the value is after
// the Report Size (0x95/6) byte, but it's a bit unnecessary for us
// to do that when we know the descriptors.
if (buff[i] == 0x95) {

(&wooting_usb_meta_array[connected_keyboards])
->uses_small_packets = true;
#ifdef DEBUG_LOG
printf("Determined that device needs small packets from the HID "
"report descriptor\n");
#endif
break;
} else if (buff[i] == 0x96) {

(&wooting_usb_meta_array[connected_keyboards])
->uses_small_packets = false;
#ifdef DEBUG_LOG
printf("Determined that device needs big packets from the HID "
"report descriptor\n");
#endif
break;
}
}
} else {
#ifdef DEBUG_LOG
printf("Failed to get report descriptor (%d) Using default packet "
"size (small = %d)\n",
len,
(&wooting_usb_meta_array[connected_keyboards])
->uses_small_packets);
#endif
}

// Any feature sends need to be done after the meta is set so the
// correct value for v2_interface is set

Expand Down

0 comments on commit be3f8a4

Please sign in to comment.