/* Host-side checks for the HID mouse report-descriptor parser. */ #include #include #include "hid_report.h" static int failures; static void check(const char *name, int cond) { printf("%s: %s\n", cond ? "ok" : "FAIL", name); if (!cond) failures++; } /* Standard three-button mouse: buttons, 5 bits padding, X/Y/Wheel. */ static const unsigned char desc_std[] = { 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, 0xA1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06, 0xC0, 0xC0 }; /* Report ID 2, five buttons, 16-bit X/Y, 8-bit wheel. */ static const unsigned char desc16[] = { 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x85, 0x02, 0x05, 0x09, 0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 0x25, 0x01, 0x95, 0x05, 0x75, 0x01, 0x81, 0x02, 0x95, 0x03, 0x75, 0x01, 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x16, 0x00, 0x80, 0x26, 0xFF, 0x7F, 0x75, 0x10, 0x95, 0x02, 0x81, 0x06, 0x09, 0x38, 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x01, 0x81, 0x06, 0xC0 }; /* Five buttons, 12-bit X/Y and an 8-bit wheel (a gaming mouse). */ static const unsigned char desc12[] = { 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 0x25, 0x01, 0x95, 0x05, 0x75, 0x01, 0x81, 0x02, 0x95, 0x03, 0x75, 0x01, 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x15, 0x81, 0x25, 0x7F, 0x75, 0x0C, 0x95, 0x02, 0x81, 0x06, 0x09, 0x38, 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x01, 0x81, 0x06, 0xC0 }; /* X/Y through Usage Minimum/Maximum, no wheel. */ static const unsigned char desc_minmax[] = { 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01, 0x05, 0x01, 0x15, 0x81, 0x25, 0x7F, 0x19, 0x30, 0x29, 0x31, 0x75, 0x08, 0x95, 0x02, 0x81, 0x06, 0xC0 }; static void test_std(void) { struct hid_mouse_layout l; struct hid_mouse_frame f; unsigned char rep[4]; memset(&l, 0, sizeof(l)); check("std: parses", hid_parse_mouse(desc_std, sizeof(desc_std), &l)); check("std: no report id", !(l.flags & HID_ML_HAS_ID)); check("std: buttons 1..3", l.btn_bit[0] == 1 && l.btn_bit[1] == 2 && l.btn_bit[2] == 3); check("std: button 4 absent", l.btn_bit[3] == 0); check("std: field offsets", l.x_off == 8 && l.y_off == 16 && l.wheel_off == 24); check("std: field sizes", l.x_size == 8 && l.y_size == 8 && l.wheel_size == 8); check("std: all signed", (l.flags & (HID_ML_X_SIGNED | HID_ML_Y_SIGNED | HID_ML_WHEEL_SIGNED)) == (HID_ML_X_SIGNED | HID_ML_Y_SIGNED | HID_ML_WHEEL_SIGNED)); check("std: report is 4 bytes", l.report_bytes == 4); memset(&f, 0, sizeof(f)); rep[0] = 0x05; /* left + middle */ rep[1] = 0xF6; /* -10 */ rep[2] = 0x14; /* +20 */ rep[3] = 0x01; /* wheel away */ check("std: decodes", hid_mouse_decode(&l, rep, sizeof(rep), &f)); check("std: frame", f.buttons == 0x05 && f.dx == -10 && f.dy == 20 && f.wheel == 1); rep[3] = 0xFF; check("std: negative wheel", hid_mouse_decode(&l, rep, sizeof(rep), &f) && f.wheel == -1); check("std: short report rejected", !hid_mouse_decode(&l, rep, 3, &f)); } static void test_report_id(void) { struct hid_mouse_layout l; struct hid_mouse_frame f; unsigned char rep[7]; memset(&l, 0, sizeof(l)); check("id16: parses", hid_parse_mouse(desc16, sizeof(desc16), &l)); check("id16: report id 2", (l.flags & HID_ML_HAS_ID) && l.report_id == 2); check("id16: field offsets", l.x_off == 8 && l.y_off == 24 && l.wheel_off == 40); check("id16: field sizes", l.x_size == 16 && l.y_size == 16 && l.wheel_size == 8); check("id16: report is 7 bytes", l.report_bytes == 7); rep[0] = 2; rep[1] = 0x03; rep[2] = 0x18; rep[3] = 0xFC; /* -1000 */ rep[4] = 0xE8; rep[5] = 0x03; /* +1000 */ rep[6] = 0xFF; /* -1 */ check("id16: decodes", hid_mouse_decode(&l, rep, sizeof(rep), &f)); check("id16: frame", f.buttons == 0x03 && f.dx == -1000 && f.dy == 1000 && f.wheel == -1); rep[0] = 1; check("id16: other report id ignored", !hid_mouse_decode(&l, rep, sizeof(rep), &f)); rep[0] = 2; check("id16: short report rejected", !hid_mouse_decode(&l, rep, 6, &f)); } static void test_12bit(void) { struct hid_mouse_layout l; struct hid_mouse_frame f; unsigned char rep[5]; memset(&l, 0, sizeof(l)); check("12: parses", hid_parse_mouse(desc12, sizeof(desc12), &l)); check("12: field offsets", l.x_off == 8 && l.y_off == 20 && l.wheel_off == 32); check("12: field sizes", l.x_size == 12 && l.y_size == 12 && l.wheel_size == 8); check("12: report is 5 bytes", l.report_bytes == 5); check("12: five buttons", l.btn_bit[0] == 1 && l.btn_bit[1] == 2 && l.btn_bit[2] == 3 && l.btn_bit[3] == 4 && l.btn_bit[4] == 5); rep[0] = 0x05; rep[1] = 0xFB; /* X low 8 */ rep[2] = 0x4F; /* X high 4, Y low 4 */ rep[3] = 0x06; /* Y high 8 */ rep[4] = 0x01; /* wheel */ check("12: decodes", hid_mouse_decode(&l, rep, sizeof(rep), &f)); check("12: frame", f.buttons == 0x05 && f.dx == -5 && f.dy == 100 && f.wheel == 1); } static void test_no_wheel(void) { struct hid_mouse_layout l; struct hid_mouse_frame f; unsigned char rep[3]; memset(&l, 0, sizeof(l)); check("minmax: parses", hid_parse_mouse(desc_minmax, sizeof(desc_minmax), &l)); check("minmax: X/Y", l.x_off == 8 && l.y_off == 16); check("minmax: no wheel", l.wheel_size == 0); check("minmax: report is 3 bytes", l.report_bytes == 3); rep[0] = 0; rep[1] = 5; rep[2] = 0xFB; check("minmax: decodes", hid_mouse_decode(&l, rep, sizeof(rep), &f)); check("minmax: frame", !f.buttons && f.dx == 5 && f.dy == -5 && !f.wheel); } static void test_invalid(void) { struct hid_mouse_layout l; struct hid_mouse_frame f; unsigned char rep[4] = { 0 }; memset(&l, 0, sizeof(l)); check("invalid: empty descriptor", !hid_parse_mouse(rep, 0, &l)); check("invalid: one byte", !hid_parse_mouse(rep, 1, &l)); check("invalid: truncated tail does not crash", hid_parse_mouse(desc_std, sizeof(desc_std) / 2, &l) == 0 || (l.flags & HID_ML_VALID)); memset(&l, 0, sizeof(l)); check("invalid: no layout rejects decode", !hid_mouse_decode(&l, rep, sizeof(rep), &f)); } int main(void) { test_std(); test_report_id(); test_12bit(); test_no_wheel(); test_invalid(); if (failures) { printf("%d check(s) failed\n", failures); return 1; } printf("all HID report checks passed\n"); return 0; }