hid_report_test.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Host-side checks for the HID mouse report-descriptor parser. */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "hid_report.h"
  5. static int failures;
  6. static void check(const char *name, int cond)
  7. {
  8. printf("%s: %s\n", cond ? "ok" : "FAIL", name);
  9. if (!cond)
  10. failures++;
  11. }
  12. /* Standard three-button mouse: buttons, 5 bits padding, X/Y/Wheel. */
  13. static const unsigned char desc_std[] = {
  14. 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01,
  15. 0x09, 0x01, 0xA1, 0x00,
  16. 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01,
  17. 0x95, 0x03, 0x75, 0x01, 0x81, 0x02,
  18. 0x95, 0x01, 0x75, 0x05, 0x81, 0x01,
  19. 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38,
  20. 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06,
  21. 0xC0, 0xC0
  22. };
  23. /* Report ID 2, five buttons, 16-bit X/Y, 8-bit wheel. */
  24. static const unsigned char desc16[] = {
  25. 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01,
  26. 0x85, 0x02,
  27. 0x05, 0x09, 0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 0x25, 0x01,
  28. 0x95, 0x05, 0x75, 0x01, 0x81, 0x02,
  29. 0x95, 0x03, 0x75, 0x01, 0x81, 0x01,
  30. 0x05, 0x01, 0x09, 0x30, 0x09, 0x31,
  31. 0x16, 0x00, 0x80, 0x26, 0xFF, 0x7F,
  32. 0x75, 0x10, 0x95, 0x02, 0x81, 0x06,
  33. 0x09, 0x38, 0x15, 0x81, 0x25, 0x7F,
  34. 0x75, 0x08, 0x95, 0x01, 0x81, 0x06,
  35. 0xC0
  36. };
  37. /* Five buttons, 12-bit X/Y and an 8-bit wheel (a gaming mouse). */
  38. static const unsigned char desc12[] = {
  39. 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01,
  40. 0x05, 0x09, 0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 0x25, 0x01,
  41. 0x95, 0x05, 0x75, 0x01, 0x81, 0x02,
  42. 0x95, 0x03, 0x75, 0x01, 0x81, 0x01,
  43. 0x05, 0x01, 0x09, 0x30, 0x09, 0x31,
  44. 0x15, 0x81, 0x25, 0x7F,
  45. 0x75, 0x0C, 0x95, 0x02, 0x81, 0x06,
  46. 0x09, 0x38, 0x15, 0x81, 0x25, 0x7F,
  47. 0x75, 0x08, 0x95, 0x01, 0x81, 0x06,
  48. 0xC0
  49. };
  50. /* X/Y through Usage Minimum/Maximum, no wheel. */
  51. static const unsigned char desc_minmax[] = {
  52. 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01,
  53. 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01,
  54. 0x95, 0x03, 0x75, 0x01, 0x81, 0x02,
  55. 0x95, 0x01, 0x75, 0x05, 0x81, 0x01,
  56. 0x05, 0x01, 0x15, 0x81, 0x25, 0x7F,
  57. 0x19, 0x30, 0x29, 0x31,
  58. 0x75, 0x08, 0x95, 0x02, 0x81, 0x06,
  59. 0xC0
  60. };
  61. static void test_std(void)
  62. {
  63. struct hid_mouse_layout l;
  64. struct hid_mouse_frame f;
  65. unsigned char rep[4];
  66. memset(&l, 0, sizeof(l));
  67. check("std: parses", hid_parse_mouse(desc_std, sizeof(desc_std), &l));
  68. check("std: no report id", !(l.flags & HID_ML_HAS_ID));
  69. check("std: buttons 1..3",
  70. l.btn_bit[0] == 1 && l.btn_bit[1] == 2 && l.btn_bit[2] == 3);
  71. check("std: button 4 absent", l.btn_bit[3] == 0);
  72. check("std: field offsets",
  73. l.x_off == 8 && l.y_off == 16 && l.wheel_off == 24);
  74. check("std: field sizes",
  75. l.x_size == 8 && l.y_size == 8 && l.wheel_size == 8);
  76. check("std: all signed",
  77. (l.flags & (HID_ML_X_SIGNED | HID_ML_Y_SIGNED |
  78. HID_ML_WHEEL_SIGNED)) ==
  79. (HID_ML_X_SIGNED | HID_ML_Y_SIGNED | HID_ML_WHEEL_SIGNED));
  80. check("std: report is 4 bytes", l.report_bytes == 4);
  81. memset(&f, 0, sizeof(f));
  82. rep[0] = 0x05; /* left + middle */
  83. rep[1] = 0xF6; /* -10 */
  84. rep[2] = 0x14; /* +20 */
  85. rep[3] = 0x01; /* wheel away */
  86. check("std: decodes",
  87. hid_mouse_decode(&l, rep, sizeof(rep), &f));
  88. check("std: frame",
  89. f.buttons == 0x05 && f.dx == -10 && f.dy == 20 && f.wheel == 1);
  90. rep[3] = 0xFF;
  91. check("std: negative wheel",
  92. hid_mouse_decode(&l, rep, sizeof(rep), &f) && f.wheel == -1);
  93. check("std: short report rejected",
  94. !hid_mouse_decode(&l, rep, 3, &f));
  95. }
  96. static void test_report_id(void)
  97. {
  98. struct hid_mouse_layout l;
  99. struct hid_mouse_frame f;
  100. unsigned char rep[7];
  101. memset(&l, 0, sizeof(l));
  102. check("id16: parses", hid_parse_mouse(desc16, sizeof(desc16), &l));
  103. check("id16: report id 2",
  104. (l.flags & HID_ML_HAS_ID) && l.report_id == 2);
  105. check("id16: field offsets",
  106. l.x_off == 8 && l.y_off == 24 && l.wheel_off == 40);
  107. check("id16: field sizes",
  108. l.x_size == 16 && l.y_size == 16 && l.wheel_size == 8);
  109. check("id16: report is 7 bytes", l.report_bytes == 7);
  110. rep[0] = 2;
  111. rep[1] = 0x03;
  112. rep[2] = 0x18; rep[3] = 0xFC; /* -1000 */
  113. rep[4] = 0xE8; rep[5] = 0x03; /* +1000 */
  114. rep[6] = 0xFF; /* -1 */
  115. check("id16: decodes",
  116. hid_mouse_decode(&l, rep, sizeof(rep), &f));
  117. check("id16: frame",
  118. f.buttons == 0x03 && f.dx == -1000 && f.dy == 1000 &&
  119. f.wheel == -1);
  120. rep[0] = 1;
  121. check("id16: other report id ignored",
  122. !hid_mouse_decode(&l, rep, sizeof(rep), &f));
  123. rep[0] = 2;
  124. check("id16: short report rejected",
  125. !hid_mouse_decode(&l, rep, 6, &f));
  126. }
  127. static void test_12bit(void)
  128. {
  129. struct hid_mouse_layout l;
  130. struct hid_mouse_frame f;
  131. unsigned char rep[5];
  132. memset(&l, 0, sizeof(l));
  133. check("12: parses", hid_parse_mouse(desc12, sizeof(desc12), &l));
  134. check("12: field offsets",
  135. l.x_off == 8 && l.y_off == 20 && l.wheel_off == 32);
  136. check("12: field sizes",
  137. l.x_size == 12 && l.y_size == 12 && l.wheel_size == 8);
  138. check("12: report is 5 bytes", l.report_bytes == 5);
  139. check("12: five buttons",
  140. l.btn_bit[0] == 1 && l.btn_bit[1] == 2 &&
  141. l.btn_bit[2] == 3 && l.btn_bit[3] == 4 && l.btn_bit[4] == 5);
  142. rep[0] = 0x05;
  143. rep[1] = 0xFB; /* X low 8 */
  144. rep[2] = 0x4F; /* X high 4, Y low 4 */
  145. rep[3] = 0x06; /* Y high 8 */
  146. rep[4] = 0x01; /* wheel */
  147. check("12: decodes",
  148. hid_mouse_decode(&l, rep, sizeof(rep), &f));
  149. check("12: frame",
  150. f.buttons == 0x05 && f.dx == -5 && f.dy == 100 &&
  151. f.wheel == 1);
  152. }
  153. static void test_no_wheel(void)
  154. {
  155. struct hid_mouse_layout l;
  156. struct hid_mouse_frame f;
  157. unsigned char rep[3];
  158. memset(&l, 0, sizeof(l));
  159. check("minmax: parses", hid_parse_mouse(desc_minmax,
  160. sizeof(desc_minmax), &l));
  161. check("minmax: X/Y", l.x_off == 8 && l.y_off == 16);
  162. check("minmax: no wheel", l.wheel_size == 0);
  163. check("minmax: report is 3 bytes", l.report_bytes == 3);
  164. rep[0] = 0;
  165. rep[1] = 5;
  166. rep[2] = 0xFB;
  167. check("minmax: decodes",
  168. hid_mouse_decode(&l, rep, sizeof(rep), &f));
  169. check("minmax: frame", !f.buttons && f.dx == 5 && f.dy == -5 &&
  170. !f.wheel);
  171. }
  172. static void test_invalid(void)
  173. {
  174. struct hid_mouse_layout l;
  175. struct hid_mouse_frame f;
  176. unsigned char rep[4] = { 0 };
  177. memset(&l, 0, sizeof(l));
  178. check("invalid: empty descriptor", !hid_parse_mouse(rep, 0, &l));
  179. check("invalid: one byte", !hid_parse_mouse(rep, 1, &l));
  180. check("invalid: truncated tail does not crash",
  181. hid_parse_mouse(desc_std, sizeof(desc_std) / 2, &l) == 0 ||
  182. (l.flags & HID_ML_VALID));
  183. memset(&l, 0, sizeof(l));
  184. check("invalid: no layout rejects decode",
  185. !hid_mouse_decode(&l, rep, sizeof(rep), &f));
  186. }
  187. int main(void)
  188. {
  189. test_std();
  190. test_report_id();
  191. test_12bit();
  192. test_no_wheel();
  193. test_invalid();
  194. if (failures) {
  195. printf("%d check(s) failed\n", failures);
  196. return 1;
  197. }
  198. printf("all HID report checks passed\n");
  199. return 0;
  200. }