hidapi_descriptor_reconstruct.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /*******************************************************
  2. HIDAPI - Multi-Platform library for
  3. communication with HID devices.
  4. libusb/hidapi Team
  5. Copyright 2022, All Rights Reserved.
  6. At the discretion of the user of this library,
  7. this software may be licensed under the terms of the
  8. GNU General Public License v3, a BSD-Style license, or the
  9. original HIDAPI license as outlined in the LICENSE.txt,
  10. LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
  11. files located at the root of the source distribution.
  12. These files may also be found in the public source
  13. code repository located at:
  14. https://github.com/libusb/hidapi .
  15. ********************************************************/
  16. #include "hidapi_descriptor_reconstruct.h"
  17. /**
  18. * @brief References to report descriptor buffer.
  19. *
  20. */
  21. struct rd_buffer {
  22. unsigned char* buf; /* Pointer to the array which stores the reconstructed descriptor */
  23. size_t buf_size; /* Size of the buffer in bytes */
  24. size_t byte_idx; /* Index of the next report byte to write to buf array */
  25. };
  26. /**
  27. * @brief Function that appends a byte to encoded report descriptor buffer.
  28. *
  29. * @param[in] byte Single byte to append.
  30. * @param rpt_desc Pointer to report descriptor buffer struct.
  31. */
  32. static void rd_append_byte(unsigned char byte, struct rd_buffer* rpt_desc) {
  33. if (rpt_desc->byte_idx < rpt_desc->buf_size) {
  34. rpt_desc->buf[rpt_desc->byte_idx] = byte;
  35. rpt_desc->byte_idx++;
  36. }
  37. }
  38. /**
  39. * @brief Writes a short report descriptor item according USB HID spec 1.11 chapter 6.2.2.2.
  40. *
  41. * @param[in] rd_item Enumeration identifying type (Main, Global, Local) and function (e.g Usage or Report Count) of the item.
  42. * @param[in] data Data (Size depends on rd_item 0,1,2 or 4bytes).
  43. * @param rpt_desc Pointer to report descriptor buffer struct.
  44. *
  45. * @return Returns 0 if successful, -1 for error.
  46. */
  47. static int rd_write_short_item(rd_items rd_item, LONG64 data, struct rd_buffer* rpt_desc) {
  48. if (rd_item & 0x03) {
  49. // Invalid input data, last to bits are reserved for data size
  50. return -1;
  51. }
  52. if (rd_item == rd_main_collection_end) {
  53. // Item without data (1Byte prefix only)
  54. unsigned char oneBytePrefix = (unsigned char) rd_item + 0x00;
  55. rd_append_byte(oneBytePrefix, rpt_desc);
  56. }
  57. else if ((rd_item == rd_global_logical_minimum) ||
  58. (rd_item == rd_global_logical_maximum) ||
  59. (rd_item == rd_global_physical_minimum) ||
  60. (rd_item == rd_global_physical_maximum)) {
  61. // Item with signed integer data
  62. if ((data >= -128) && (data <= 127)) {
  63. // 1Byte prefix + 1Byte data
  64. unsigned char oneBytePrefix = (unsigned char) rd_item + 0x01;
  65. char localData = (char)data;
  66. rd_append_byte(oneBytePrefix, rpt_desc);
  67. rd_append_byte(localData & 0xFF, rpt_desc);
  68. }
  69. else if ((data >= -32768) && (data <= 32767)) {
  70. // 1Byte prefix + 2Byte data
  71. unsigned char oneBytePrefix = (unsigned char) rd_item + 0x02;
  72. INT16 localData = (INT16)data;
  73. rd_append_byte(oneBytePrefix, rpt_desc);
  74. rd_append_byte(localData & 0xFF, rpt_desc);
  75. rd_append_byte(localData >> 8 & 0xFF, rpt_desc);
  76. }
  77. else if ((data >= -2147483648LL) && (data <= 2147483647)) {
  78. // 1Byte prefix + 4Byte data
  79. unsigned char oneBytePrefix = (unsigned char) rd_item + 0x03;
  80. INT32 localData = (INT32)data;
  81. rd_append_byte(oneBytePrefix, rpt_desc);
  82. rd_append_byte(localData & 0xFF, rpt_desc);
  83. rd_append_byte(localData >> 8 & 0xFF, rpt_desc);
  84. rd_append_byte(localData >> 16 & 0xFF, rpt_desc);
  85. rd_append_byte(localData >> 24 & 0xFF, rpt_desc);
  86. }
  87. else {
  88. // Data out of 32 bit signed integer range
  89. return -1;
  90. }
  91. }
  92. else {
  93. // Item with unsigned integer data
  94. if ((data >= 0) && (data <= 0xFF)) {
  95. // 1Byte prefix + 1Byte data
  96. unsigned char oneBytePrefix = (unsigned char) rd_item + 0x01;
  97. unsigned char localData = (unsigned char)data;
  98. rd_append_byte(oneBytePrefix, rpt_desc);
  99. rd_append_byte(localData & 0xFF, rpt_desc);
  100. }
  101. else if ((data >= 0) && (data <= 0xFFFF)) {
  102. // 1Byte prefix + 2Byte data
  103. unsigned char oneBytePrefix = (unsigned char) rd_item + 0x02;
  104. UINT16 localData = (UINT16)data;
  105. rd_append_byte(oneBytePrefix, rpt_desc);
  106. rd_append_byte(localData & 0xFF, rpt_desc);
  107. rd_append_byte(localData >> 8 & 0xFF, rpt_desc);
  108. }
  109. else if ((data >= 0) && (data <= 0xFFFFFFFF)) {
  110. // 1Byte prefix + 4Byte data
  111. unsigned char oneBytePrefix = (unsigned char) rd_item + 0x03;
  112. UINT32 localData = (UINT32)data;
  113. rd_append_byte(oneBytePrefix, rpt_desc);
  114. rd_append_byte(localData & 0xFF, rpt_desc);
  115. rd_append_byte(localData >> 8 & 0xFF, rpt_desc);
  116. rd_append_byte(localData >> 16 & 0xFF, rpt_desc);
  117. rd_append_byte(localData >> 24 & 0xFF, rpt_desc);
  118. }
  119. else {
  120. // Data out of 32 bit unsigned integer range
  121. return -1;
  122. }
  123. }
  124. return 0;
  125. }
  126. static struct rd_main_item_node * rd_append_main_item_node(int first_bit, int last_bit, rd_node_type type_of_node, int caps_index, int collection_index, rd_main_items main_item_type, unsigned char report_id, struct rd_main_item_node **list) {
  127. struct rd_main_item_node *new_list_node;
  128. // Determine last node in the list
  129. while (*list != NULL)
  130. {
  131. list = &(*list)->next;
  132. }
  133. new_list_node = malloc(sizeof(*new_list_node)); // Create new list entry
  134. new_list_node->FirstBit = first_bit;
  135. new_list_node->LastBit = last_bit;
  136. new_list_node->TypeOfNode = type_of_node;
  137. new_list_node->CapsIndex = caps_index;
  138. new_list_node->CollectionIndex = collection_index;
  139. new_list_node->MainItemType = main_item_type;
  140. new_list_node->ReportID = report_id;
  141. new_list_node->next = NULL; // NULL marks last node in the list
  142. *list = new_list_node;
  143. return new_list_node;
  144. }
  145. static struct rd_main_item_node * rd_insert_main_item_node(int first_bit, int last_bit, rd_node_type type_of_node, int caps_index, int collection_index, rd_main_items main_item_type, unsigned char report_id, struct rd_main_item_node **list) {
  146. // Insert item after the main item node referenced by list
  147. struct rd_main_item_node *next_item = (*list)->next;
  148. (*list)->next = NULL;
  149. rd_append_main_item_node(first_bit, last_bit, type_of_node, caps_index, collection_index, main_item_type, report_id, list);
  150. (*list)->next->next = next_item;
  151. return (*list)->next;
  152. }
  153. static struct rd_main_item_node * rd_search_main_item_list_for_bit_position(int search_bit, rd_main_items main_item_type, unsigned char report_id, struct rd_main_item_node **list) {
  154. // Determine first INPUT/OUTPUT/FEATURE main item, where the last bit position is equal or greater than the search bit position
  155. while (((*list)->next->MainItemType != rd_collection) &&
  156. ((*list)->next->MainItemType != rd_collection_end) &&
  157. !(((*list)->next->LastBit >= search_bit) &&
  158. ((*list)->next->ReportID == report_id) &&
  159. ((*list)->next->MainItemType == main_item_type))
  160. )
  161. {
  162. list = &(*list)->next;
  163. }
  164. return *list;
  165. }
  166. int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned char *buf, size_t buf_size)
  167. {
  168. hidp_preparsed_data *pp_data = (hidp_preparsed_data *) preparsed_data;
  169. // Check if MagicKey is correct, to ensure that pp_data points to an valid preparse data structure
  170. if (memcmp(pp_data->MagicKey, "HidP KDR", 8) != 0) {
  171. return -1;
  172. }
  173. struct rd_buffer rpt_desc = {
  174. .buf = buf,
  175. .buf_size = buf_size,
  176. .byte_idx = 0
  177. };
  178. // Set pointer to the first node of link_collection_nodes
  179. phid_pp_link_collection_node link_collection_nodes = (phid_pp_link_collection_node)(((unsigned char*)&pp_data->caps[0]) + pp_data->FirstByteOfLinkCollectionArray);
  180. // ****************************************************************************************************************************
  181. // Create lookup tables for the bit range of each report per collection (position of first bit and last bit in each collection)
  182. // coll_bit_range[COLLECTION_INDEX][REPORT_ID][INPUT/OUTPUT/FEATURE]
  183. // ****************************************************************************************************************************
  184. // Allocate memory and initialize lookup table
  185. rd_bit_range ****coll_bit_range;
  186. coll_bit_range = malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_bit_range));
  187. for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) {
  188. coll_bit_range[collection_node_idx] = malloc(256 * sizeof(*coll_bit_range[0])); // 256 possible report IDs (incl. 0x00)
  189. for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
  190. coll_bit_range[collection_node_idx][reportid_idx] = malloc(NUM_OF_HIDP_REPORT_TYPES * sizeof(*coll_bit_range[0][0]));
  191. for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
  192. coll_bit_range[collection_node_idx][reportid_idx][rt_idx] = malloc(sizeof(rd_bit_range));
  193. coll_bit_range[collection_node_idx][reportid_idx][rt_idx]->FirstBit = -1;
  194. coll_bit_range[collection_node_idx][reportid_idx][rt_idx]->LastBit = -1;
  195. }
  196. }
  197. }
  198. // Fill the lookup table where caps exist
  199. for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
  200. for (USHORT caps_idx = pp_data->caps_info[rt_idx].FirstCap; caps_idx < pp_data->caps_info[rt_idx].LastCap; caps_idx++) {
  201. int first_bit, last_bit;
  202. first_bit = (pp_data->caps[caps_idx].BytePosition - 1) * 8
  203. + pp_data->caps[caps_idx].BitPosition;
  204. last_bit = first_bit + pp_data->caps[caps_idx].ReportSize
  205. * pp_data->caps[caps_idx].ReportCount - 1;
  206. if (coll_bit_range[pp_data->caps[caps_idx].LinkCollection][pp_data->caps[caps_idx].ReportID][rt_idx]->FirstBit == -1 ||
  207. coll_bit_range[pp_data->caps[caps_idx].LinkCollection][pp_data->caps[caps_idx].ReportID][rt_idx]->FirstBit > first_bit) {
  208. coll_bit_range[pp_data->caps[caps_idx].LinkCollection][pp_data->caps[caps_idx].ReportID][rt_idx]->FirstBit = first_bit;
  209. }
  210. if (coll_bit_range[pp_data->caps[caps_idx].LinkCollection][pp_data->caps[caps_idx].ReportID][rt_idx]->LastBit < last_bit) {
  211. coll_bit_range[pp_data->caps[caps_idx].LinkCollection][pp_data->caps[caps_idx].ReportID][rt_idx]->LastBit = last_bit;
  212. }
  213. }
  214. }
  215. // *************************************************************************
  216. // -Determine hierarchy levels of each collections and store it in:
  217. // coll_levels[COLLECTION_INDEX]
  218. // -Determine number of direct childs of each collections and store it in:
  219. // coll_number_of_direct_childs[COLLECTION_INDEX]
  220. // *************************************************************************
  221. int max_coll_level = 0;
  222. int *coll_levels = malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_levels[0]));
  223. int *coll_number_of_direct_childs = malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_number_of_direct_childs[0]));
  224. for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) {
  225. coll_levels[collection_node_idx] = -1;
  226. coll_number_of_direct_childs[collection_node_idx] = 0;
  227. }
  228. {
  229. int actual_coll_level = 0;
  230. USHORT collection_node_idx = 0;
  231. while (actual_coll_level >= 0) {
  232. coll_levels[collection_node_idx] = actual_coll_level;
  233. if ((link_collection_nodes[collection_node_idx].NumberOfChildren > 0) &&
  234. (coll_levels[link_collection_nodes[collection_node_idx].FirstChild] == -1)) {
  235. actual_coll_level++;
  236. coll_levels[collection_node_idx] = actual_coll_level;
  237. if (max_coll_level < actual_coll_level) {
  238. max_coll_level = actual_coll_level;
  239. }
  240. coll_number_of_direct_childs[collection_node_idx]++;
  241. collection_node_idx = link_collection_nodes[collection_node_idx].FirstChild;
  242. }
  243. else if (link_collection_nodes[collection_node_idx].NextSibling != 0) {
  244. coll_number_of_direct_childs[link_collection_nodes[collection_node_idx].Parent]++;
  245. collection_node_idx = link_collection_nodes[collection_node_idx].NextSibling;
  246. }
  247. else {
  248. actual_coll_level--;
  249. if (actual_coll_level >= 0) {
  250. collection_node_idx = link_collection_nodes[collection_node_idx].Parent;
  251. }
  252. }
  253. }
  254. }
  255. // *********************************************************************************
  256. // Propagate the bit range of each report from the child collections to their parent
  257. // and store the merged result for the parent
  258. // *********************************************************************************
  259. for (int actual_coll_level = max_coll_level - 1; actual_coll_level >= 0; actual_coll_level--) {
  260. for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) {
  261. if (coll_levels[collection_node_idx] == actual_coll_level) {
  262. USHORT child_idx = link_collection_nodes[collection_node_idx].FirstChild;
  263. while (child_idx) {
  264. for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
  265. for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
  266. // Merge bit range from childs
  267. if ((coll_bit_range[child_idx][reportid_idx][rt_idx]->FirstBit != -1) &&
  268. (coll_bit_range[collection_node_idx][reportid_idx][rt_idx]->FirstBit > coll_bit_range[child_idx][reportid_idx][rt_idx]->FirstBit)) {
  269. coll_bit_range[collection_node_idx][reportid_idx][rt_idx]->FirstBit = coll_bit_range[child_idx][reportid_idx][rt_idx]->FirstBit;
  270. }
  271. if (coll_bit_range[collection_node_idx][reportid_idx][rt_idx]->LastBit < coll_bit_range[child_idx][reportid_idx][rt_idx]->LastBit) {
  272. coll_bit_range[collection_node_idx][reportid_idx][rt_idx]->LastBit = coll_bit_range[child_idx][reportid_idx][rt_idx]->LastBit;
  273. }
  274. child_idx = link_collection_nodes[child_idx].NextSibling;
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }
  281. // **************************************************************************************************
  282. // Determine child collection order of the whole hierarchy, based on previously determined bit ranges
  283. // and store it this index coll_child_order[COLLECTION_INDEX][DIRECT_CHILD_INDEX]
  284. // **************************************************************************************************
  285. USHORT **coll_child_order;
  286. coll_child_order = malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_child_order));
  287. {
  288. BOOLEAN *coll_parsed_flag;
  289. coll_parsed_flag = malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_parsed_flag[0]));
  290. for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) {
  291. coll_parsed_flag[collection_node_idx] = FALSE;
  292. }
  293. int actual_coll_level = 0;
  294. USHORT collection_node_idx = 0;
  295. while (actual_coll_level >= 0) {
  296. if ((coll_number_of_direct_childs[collection_node_idx] != 0) &&
  297. (coll_parsed_flag[link_collection_nodes[collection_node_idx].FirstChild] == FALSE)) {
  298. coll_parsed_flag[link_collection_nodes[collection_node_idx].FirstChild] = TRUE;
  299. coll_child_order[collection_node_idx] = malloc((coll_number_of_direct_childs[collection_node_idx]) * sizeof(*coll_child_order[0]));
  300. {
  301. // Create list of child collection indices
  302. // sorted reverse to the order returned to HidP_GetLinkCollectionNodeschild
  303. // which seems to match the original order, as long as no bit position needs to be considered
  304. USHORT child_idx = link_collection_nodes[collection_node_idx].FirstChild;
  305. int child_count = coll_number_of_direct_childs[collection_node_idx] - 1;
  306. coll_child_order[collection_node_idx][child_count] = child_idx;
  307. while (link_collection_nodes[child_idx].NextSibling) {
  308. child_count--;
  309. child_idx = link_collection_nodes[child_idx].NextSibling;
  310. coll_child_order[collection_node_idx][child_count] = child_idx;
  311. }
  312. }
  313. if (coll_number_of_direct_childs[collection_node_idx] > 1) {
  314. // Sort child collections indices by bit positions
  315. for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
  316. for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
  317. for (int child_idx = 1; child_idx < coll_number_of_direct_childs[collection_node_idx]; child_idx++) {
  318. // since the coll_bit_range array is not sorted, we need to reference the collection index in
  319. // our sorted coll_child_order array, and look up the corresponding bit ranges for comparing values to sort
  320. int prev_coll_idx = coll_child_order[collection_node_idx][child_idx - 1];
  321. int cur_coll_idx = coll_child_order[collection_node_idx][child_idx];
  322. if ((coll_bit_range[prev_coll_idx][reportid_idx][rt_idx]->FirstBit != -1) &&
  323. (coll_bit_range[cur_coll_idx][reportid_idx][rt_idx]->FirstBit != -1) &&
  324. (coll_bit_range[prev_coll_idx][reportid_idx][rt_idx]->FirstBit > coll_bit_range[cur_coll_idx][reportid_idx][rt_idx]->FirstBit)) {
  325. // Swap position indices of the two compared child collections
  326. USHORT idx_latch = coll_child_order[collection_node_idx][child_idx - 1];
  327. coll_child_order[collection_node_idx][child_idx - 1] = coll_child_order[collection_node_idx][child_idx];
  328. coll_child_order[collection_node_idx][child_idx] = idx_latch;
  329. }
  330. }
  331. }
  332. }
  333. }
  334. actual_coll_level++;
  335. collection_node_idx = link_collection_nodes[collection_node_idx].FirstChild;
  336. }
  337. else if (link_collection_nodes[collection_node_idx].NextSibling != 0) {
  338. collection_node_idx = link_collection_nodes[collection_node_idx].NextSibling;
  339. }
  340. else {
  341. actual_coll_level--;
  342. if (actual_coll_level >= 0) {
  343. collection_node_idx = link_collection_nodes[collection_node_idx].Parent;
  344. }
  345. }
  346. }
  347. free(coll_parsed_flag);
  348. }
  349. // ***************************************************************************************
  350. // Create sorted main_item_list containing all the Collection and CollectionEnd main items
  351. // ***************************************************************************************
  352. struct rd_main_item_node *main_item_list = NULL; // List root
  353. // Lookup table to find the Collection items in the list by index
  354. struct rd_main_item_node **coll_begin_lookup = malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_begin_lookup));
  355. struct rd_main_item_node **coll_end_lookup = malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_end_lookup));
  356. {
  357. int *coll_last_written_child = malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_last_written_child[0]));
  358. for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) {
  359. coll_last_written_child[collection_node_idx] = -1;
  360. }
  361. int actual_coll_level = 0;
  362. USHORT collection_node_idx = 0;
  363. struct rd_main_item_node *firstDelimiterNode = NULL;
  364. struct rd_main_item_node *delimiterCloseNode = NULL;
  365. coll_begin_lookup[0] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_collection, 0, &main_item_list);
  366. while (actual_coll_level >= 0) {
  367. if ((coll_number_of_direct_childs[collection_node_idx] != 0) &&
  368. (coll_last_written_child[collection_node_idx] == -1)) {
  369. // Collection has child collections, but none is written to the list yet
  370. coll_last_written_child[collection_node_idx] = coll_child_order[collection_node_idx][0];
  371. collection_node_idx = coll_child_order[collection_node_idx][0];
  372. // In a HID Report Descriptor, the first usage declared is the most preferred usage for the control.
  373. // While the order in the WIN32 capabiliy strutures is the opposite:
  374. // Here the preferred usage is the last aliased usage in the sequence.
  375. if (link_collection_nodes[collection_node_idx].IsAlias && (firstDelimiterNode == NULL)) {
  376. // Alliased Collection (First node in link_collection_nodes -> Last entry in report descriptor output)
  377. firstDelimiterNode = main_item_list;
  378. coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_usage, 0, &main_item_list);
  379. coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_close, 0, &main_item_list);
  380. delimiterCloseNode = main_item_list;
  381. }
  382. else {
  383. // Normal not aliased collection
  384. coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_collection, 0, &main_item_list);
  385. actual_coll_level++;
  386. }
  387. }
  388. else if ((coll_number_of_direct_childs[collection_node_idx] > 1) &&
  389. (coll_last_written_child[collection_node_idx] != coll_child_order[collection_node_idx][coll_number_of_direct_childs[collection_node_idx] - 1])) {
  390. // Collection has child collections, and this is not the first child
  391. int nextChild = 1;
  392. while (coll_last_written_child[collection_node_idx] != coll_child_order[collection_node_idx][nextChild - 1]) {
  393. nextChild++;
  394. }
  395. coll_last_written_child[collection_node_idx] = coll_child_order[collection_node_idx][nextChild];
  396. collection_node_idx = coll_child_order[collection_node_idx][nextChild];
  397. if (link_collection_nodes[collection_node_idx].IsAlias && (firstDelimiterNode == NULL)) {
  398. // Alliased Collection (First node in link_collection_nodes -> Last entry in report descriptor output)
  399. firstDelimiterNode = main_item_list;
  400. coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_usage, 0, &main_item_list);
  401. coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_close, 0, &main_item_list);
  402. delimiterCloseNode = main_item_list;
  403. }
  404. else if (link_collection_nodes[collection_node_idx].IsAlias && (firstDelimiterNode != NULL)) {
  405. coll_begin_lookup[collection_node_idx] = rd_insert_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_usage, 0, &firstDelimiterNode);
  406. }
  407. else if (!link_collection_nodes[collection_node_idx].IsAlias && (firstDelimiterNode != NULL)) {
  408. coll_begin_lookup[collection_node_idx] = rd_insert_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_usage, 0, &firstDelimiterNode);
  409. coll_begin_lookup[collection_node_idx] = rd_insert_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_open, 0, &firstDelimiterNode);
  410. firstDelimiterNode = NULL;
  411. main_item_list = delimiterCloseNode;
  412. delimiterCloseNode = NULL; // Last entry of alias has .IsAlias == FALSE
  413. }
  414. if (!link_collection_nodes[collection_node_idx].IsAlias) {
  415. coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_collection, 0, &main_item_list);
  416. actual_coll_level++;
  417. }
  418. }
  419. else {
  420. actual_coll_level--;
  421. coll_end_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_collection_end, 0, &main_item_list);
  422. collection_node_idx = link_collection_nodes[collection_node_idx].Parent;
  423. }
  424. }
  425. free(coll_last_written_child);
  426. }
  427. // ****************************************************************
  428. // Inserted Input/Output/Feature main items into the main_item_list
  429. // in order of reconstructed bit positions
  430. // ****************************************************************
  431. for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
  432. // Add all value caps to node list
  433. struct rd_main_item_node *firstDelimiterNode = NULL;
  434. struct rd_main_item_node *delimiterCloseNode = NULL;
  435. for (USHORT caps_idx = pp_data->caps_info[rt_idx].FirstCap; caps_idx < pp_data->caps_info[rt_idx].LastCap; caps_idx++) {
  436. struct rd_main_item_node *coll_begin = coll_begin_lookup[pp_data->caps[caps_idx].LinkCollection];
  437. int first_bit, last_bit;
  438. first_bit = (pp_data->caps[caps_idx].BytePosition - 1) * 8 +
  439. pp_data->caps[caps_idx].BitPosition;
  440. last_bit = first_bit + pp_data->caps[caps_idx].ReportSize *
  441. pp_data->caps[caps_idx].ReportCount - 1;
  442. for (int child_idx = 0; child_idx < coll_number_of_direct_childs[pp_data->caps[caps_idx].LinkCollection]; child_idx++) {
  443. // Determine in which section before/between/after child collection the item should be inserted
  444. if (first_bit < coll_bit_range[coll_child_order[pp_data->caps[caps_idx].LinkCollection][child_idx]][pp_data->caps[caps_idx].ReportID][rt_idx]->FirstBit)
  445. {
  446. // Note, that the default value for undefined coll_bit_range is -1, which can't be greater than the bit position
  447. break;
  448. }
  449. coll_begin = coll_end_lookup[coll_child_order[pp_data->caps[caps_idx].LinkCollection][child_idx]];
  450. }
  451. struct rd_main_item_node *list_node;
  452. list_node = rd_search_main_item_list_for_bit_position(first_bit, (rd_main_items) rt_idx, pp_data->caps[caps_idx].ReportID, &coll_begin);
  453. // In a HID Report Descriptor, the first usage declared is the most preferred usage for the control.
  454. // While the order in the WIN32 capabiliy strutures is the opposite:
  455. // Here the preferred usage is the last aliased usage in the sequence.
  456. if (pp_data->caps[caps_idx].IsAlias && (firstDelimiterNode == NULL)) {
  457. // Alliased Usage (First node in pp_data->caps -> Last entry in report descriptor output)
  458. firstDelimiterNode = list_node;
  459. rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_usage, pp_data->caps[caps_idx].ReportID, &list_node);
  460. rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_close, pp_data->caps[caps_idx].ReportID, &list_node);
  461. delimiterCloseNode = list_node;
  462. } else if (pp_data->caps[caps_idx].IsAlias && (firstDelimiterNode != NULL)) {
  463. rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_usage, pp_data->caps[caps_idx].ReportID, &list_node);
  464. }
  465. else if (!pp_data->caps[caps_idx].IsAlias && (firstDelimiterNode != NULL)) {
  466. // Alliased Collection (Last node in pp_data->caps -> First entry in report descriptor output)
  467. rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_usage, pp_data->caps[caps_idx].ReportID, &list_node);
  468. rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_open, pp_data->caps[caps_idx].ReportID, &list_node);
  469. firstDelimiterNode = NULL;
  470. list_node = delimiterCloseNode;
  471. delimiterCloseNode = NULL; // Last entry of alias has .IsAlias == FALSE
  472. }
  473. if (!pp_data->caps[caps_idx].IsAlias) {
  474. rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, (rd_main_items) rt_idx, pp_data->caps[caps_idx].ReportID, &list_node);
  475. }
  476. }
  477. }
  478. // ***********************************************************
  479. // Add const main items for padding to main_item_list
  480. // -To fill all bit gaps
  481. // -At each report end for 8bit padding
  482. // Note that information about the padding at the report end,
  483. // is not stored in the preparsed data, but in practice all
  484. // report descriptors seem to have it, as assumed here.
  485. // ***********************************************************
  486. {
  487. int *last_bit_position[NUM_OF_HIDP_REPORT_TYPES];
  488. struct rd_main_item_node **last_report_item_lookup[NUM_OF_HIDP_REPORT_TYPES];
  489. for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
  490. last_bit_position[rt_idx] = malloc(256 * sizeof(*last_bit_position[rt_idx]));
  491. last_report_item_lookup[rt_idx] = malloc(256 * sizeof(*last_report_item_lookup[rt_idx]));
  492. for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
  493. last_bit_position[rt_idx][reportid_idx] = -1;
  494. last_report_item_lookup[rt_idx][reportid_idx] = NULL;
  495. }
  496. }
  497. struct rd_main_item_node *list = main_item_list; // List root;
  498. while (list->next != NULL)
  499. {
  500. if ((list->MainItemType >= rd_input) &&
  501. (list->MainItemType <= rd_feature)) {
  502. // INPUT, OUTPUT or FEATURE
  503. if (list->FirstBit != -1) {
  504. if ((last_bit_position[list->MainItemType][list->ReportID] + 1 != list->FirstBit) &&
  505. (last_report_item_lookup[list->MainItemType][list->ReportID] != NULL) &&
  506. (last_report_item_lookup[list->MainItemType][list->ReportID]->FirstBit != list->FirstBit) // Happens in case of IsMultipleItemsForArray for multiple dedicated usages for a multi-button array
  507. ) {
  508. struct rd_main_item_node *list_node = rd_search_main_item_list_for_bit_position(last_bit_position[list->MainItemType][list->ReportID], list->MainItemType, list->ReportID, &last_report_item_lookup[list->MainItemType][list->ReportID]);
  509. rd_insert_main_item_node(last_bit_position[list->MainItemType][list->ReportID] + 1, list->FirstBit - 1, rd_item_node_padding, -1, 0, list->MainItemType, list->ReportID, &list_node);
  510. }
  511. last_bit_position[list->MainItemType][list->ReportID] = list->LastBit;
  512. last_report_item_lookup[list->MainItemType][list->ReportID] = list;
  513. }
  514. }
  515. list = list->next;
  516. }
  517. // Add 8 bit padding at each report end
  518. for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
  519. for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
  520. if (last_bit_position[rt_idx][reportid_idx] != -1) {
  521. int padding = 8 - ((last_bit_position[rt_idx][reportid_idx] + 1) % 8);
  522. if (padding < 8) {
  523. // Insert padding item after item referenced in last_report_item_lookup
  524. rd_insert_main_item_node(last_bit_position[rt_idx][reportid_idx] + 1, last_bit_position[rt_idx][reportid_idx] + padding, rd_item_node_padding, -1, 0, (rd_main_items) rt_idx, (unsigned char) reportid_idx, &last_report_item_lookup[rt_idx][reportid_idx]);
  525. }
  526. }
  527. }
  528. free(last_bit_position[rt_idx]);
  529. free(last_report_item_lookup[rt_idx]);
  530. }
  531. }
  532. // ***********************************
  533. // Encode the report descriptor output
  534. // ***********************************
  535. UCHAR last_report_id = 0;
  536. USAGE last_usage_page = 0;
  537. LONG last_physical_min = 0;// If both, Physical Minimum and Physical Maximum are 0, the logical limits should be taken as physical limits according USB HID spec 1.11 chapter 6.2.2.7
  538. LONG last_physical_max = 0;
  539. ULONG last_unit_exponent = 0; // If Unit Exponent is Undefined it should be considered as 0 according USB HID spec 1.11 chapter 6.2.2.7
  540. ULONG last_unit = 0; // If the first nibble is 7, or second nibble of Unit is 0, the unit is None according USB HID spec 1.11 chapter 6.2.2.7
  541. BOOLEAN inhibit_write_of_usage = FALSE; // Needed in case of delimited usage print, before the normal collection or cap
  542. int report_count = 0;
  543. while (main_item_list != NULL)
  544. {
  545. int rt_idx = main_item_list->MainItemType;
  546. int caps_idx = main_item_list->CapsIndex;
  547. if (main_item_list->MainItemType == rd_collection) {
  548. if (last_usage_page != link_collection_nodes[main_item_list->CollectionIndex].LinkUsagePage) {
  549. // Write "Usage Page" at the begin of a collection - except it refers the same table as wrote last
  550. rd_write_short_item(rd_global_usage_page, link_collection_nodes[main_item_list->CollectionIndex].LinkUsagePage, &rpt_desc);
  551. last_usage_page = link_collection_nodes[main_item_list->CollectionIndex].LinkUsagePage;
  552. }
  553. if (inhibit_write_of_usage) {
  554. // Inhibit only once after DELIMITER statement
  555. inhibit_write_of_usage = FALSE;
  556. }
  557. else {
  558. // Write "Usage" of collection
  559. rd_write_short_item(rd_local_usage, link_collection_nodes[main_item_list->CollectionIndex].LinkUsage, &rpt_desc);
  560. }
  561. // Write begin of "Collection"
  562. rd_write_short_item(rd_main_collection, link_collection_nodes[main_item_list->CollectionIndex].CollectionType, &rpt_desc);
  563. }
  564. else if (main_item_list->MainItemType == rd_collection_end) {
  565. // Write "End Collection"
  566. rd_write_short_item(rd_main_collection_end, 0, &rpt_desc);
  567. }
  568. else if (main_item_list->MainItemType == rd_delimiter_open) {
  569. if (main_item_list->CollectionIndex != -1) {
  570. // Write "Usage Page" inside of a collection delmiter section
  571. if (last_usage_page != link_collection_nodes[main_item_list->CollectionIndex].LinkUsagePage) {
  572. rd_write_short_item(rd_global_usage_page, link_collection_nodes[main_item_list->CollectionIndex].LinkUsagePage, &rpt_desc);
  573. last_usage_page = link_collection_nodes[main_item_list->CollectionIndex].LinkUsagePage;
  574. }
  575. }
  576. else if (main_item_list->CapsIndex != 0) {
  577. // Write "Usage Page" inside of a main item delmiter section
  578. if (pp_data->caps[caps_idx].UsagePage != last_usage_page) {
  579. rd_write_short_item(rd_global_usage_page, pp_data->caps[caps_idx].UsagePage, &rpt_desc);
  580. last_usage_page = pp_data->caps[caps_idx].UsagePage;
  581. }
  582. }
  583. // Write "Delimiter Open"
  584. rd_write_short_item(rd_local_delimiter, 1, &rpt_desc); // 1 = open set of aliased usages
  585. }
  586. else if (main_item_list->MainItemType == rd_delimiter_usage) {
  587. if (main_item_list->CollectionIndex != -1) {
  588. // Write aliased collection "Usage"
  589. rd_write_short_item(rd_local_usage, link_collection_nodes[main_item_list->CollectionIndex].LinkUsage, &rpt_desc);
  590. } if (main_item_list->CapsIndex != 0) {
  591. // Write aliased main item range from "Usage Minimum" to "Usage Maximum"
  592. if (pp_data->caps[caps_idx].IsRange) {
  593. rd_write_short_item(rd_local_usage_minimum, pp_data->caps[caps_idx].Range.UsageMin, &rpt_desc);
  594. rd_write_short_item(rd_local_usage_maximum, pp_data->caps[caps_idx].Range.UsageMax, &rpt_desc);
  595. }
  596. else {
  597. // Write single aliased main item "Usage"
  598. rd_write_short_item(rd_local_usage, pp_data->caps[caps_idx].NotRange.Usage, &rpt_desc);
  599. }
  600. }
  601. }
  602. else if (main_item_list->MainItemType == rd_delimiter_close) {
  603. // Write "Delimiter Close"
  604. rd_write_short_item(rd_local_delimiter, 0, &rpt_desc); // 0 = close set of aliased usages
  605. // Inhibit next usage write
  606. inhibit_write_of_usage = TRUE;
  607. }
  608. else if (main_item_list->TypeOfNode == rd_item_node_padding) {
  609. // Padding
  610. // The preparsed data doesn't contain any information about padding. Therefore all undefined gaps
  611. // in the reports are filled with the same style of constant padding.
  612. // Write "Report Size" with number of padding bits
  613. rd_write_short_item(rd_global_report_size, (main_item_list->LastBit - main_item_list->FirstBit + 1), &rpt_desc);
  614. // Write "Report Count" for padding always as 1
  615. rd_write_short_item(rd_global_report_count, 1, &rpt_desc);
  616. if (rt_idx == HidP_Input) {
  617. // Write "Input" main item - We know it's Constant - We can only guess the other bits, but they don't matter in case of const
  618. rd_write_short_item(rd_main_input, 0x03, &rpt_desc); // Const / Abs
  619. }
  620. else if (rt_idx == HidP_Output) {
  621. // Write "Output" main item - We know it's Constant - We can only guess the other bits, but they don't matter in case of const
  622. rd_write_short_item(rd_main_output, 0x03, &rpt_desc); // Const / Abs
  623. }
  624. else if (rt_idx == HidP_Feature) {
  625. // Write "Feature" main item - We know it's Constant - We can only guess the other bits, but they don't matter in case of const
  626. rd_write_short_item(rd_main_feature, 0x03, &rpt_desc); // Const / Abs
  627. }
  628. report_count = 0;
  629. }
  630. else if (pp_data->caps[caps_idx].IsButtonCap) {
  631. // Button
  632. // (The preparsed data contain different data for 1 bit Button caps, than for parametric Value caps)
  633. if (last_report_id != pp_data->caps[caps_idx].ReportID) {
  634. // Write "Report ID" if changed
  635. rd_write_short_item(rd_global_report_id, pp_data->caps[caps_idx].ReportID, &rpt_desc);
  636. last_report_id = pp_data->caps[caps_idx].ReportID;
  637. }
  638. // Write "Usage Page" when changed
  639. if (pp_data->caps[caps_idx].UsagePage != last_usage_page) {
  640. rd_write_short_item(rd_global_usage_page, pp_data->caps[caps_idx].UsagePage, &rpt_desc);
  641. last_usage_page = pp_data->caps[caps_idx].UsagePage;
  642. }
  643. // Write only local report items for each cap, if ReportCount > 1
  644. if (pp_data->caps[caps_idx].IsRange) {
  645. report_count += (pp_data->caps[caps_idx].Range.DataIndexMax - pp_data->caps[caps_idx].Range.DataIndexMin);
  646. }
  647. if (inhibit_write_of_usage) {
  648. // Inhibit only once after Delimiter - Reset flag
  649. inhibit_write_of_usage = FALSE;
  650. }
  651. else {
  652. if (pp_data->caps[caps_idx].IsRange) {
  653. // Write range from "Usage Minimum" to "Usage Maximum"
  654. rd_write_short_item(rd_local_usage_minimum, pp_data->caps[caps_idx].Range.UsageMin, &rpt_desc);
  655. rd_write_short_item(rd_local_usage_maximum, pp_data->caps[caps_idx].Range.UsageMax, &rpt_desc);
  656. }
  657. else {
  658. // Write single "Usage"
  659. rd_write_short_item(rd_local_usage, pp_data->caps[caps_idx].NotRange.Usage, &rpt_desc);
  660. }
  661. }
  662. if (pp_data->caps[caps_idx].IsDesignatorRange) {
  663. // Write physical descriptor indices range from "Designator Minimum" to "Designator Maximum"
  664. rd_write_short_item(rd_local_designator_minimum, pp_data->caps[caps_idx].Range.DesignatorMin, &rpt_desc);
  665. rd_write_short_item(rd_local_designator_maximum, pp_data->caps[caps_idx].Range.DesignatorMax, &rpt_desc);
  666. }
  667. else if (pp_data->caps[caps_idx].NotRange.DesignatorIndex != 0) {
  668. // Designator set 0 is a special descriptor set (of the HID Physical Descriptor),
  669. // that specifies the number of additional descriptor sets.
  670. // Therefore Designator Index 0 can never be a useful reference for a control and we can inhibit it.
  671. // Write single "Designator Index"
  672. rd_write_short_item(rd_local_designator_index, pp_data->caps[caps_idx].NotRange.DesignatorIndex, &rpt_desc);
  673. }
  674. if (pp_data->caps[caps_idx].IsStringRange) {
  675. // Write range of indices of the USB string descriptor, from "String Minimum" to "String Maximum"
  676. rd_write_short_item(rd_local_string_minimum, pp_data->caps[caps_idx].Range.StringMin, &rpt_desc);
  677. rd_write_short_item(rd_local_string_maximum, pp_data->caps[caps_idx].Range.StringMax, &rpt_desc);
  678. }
  679. else if (pp_data->caps[caps_idx].NotRange.StringIndex != 0) {
  680. // String Index 0 is a special entry of the USB string descriptor, that contains a list of supported languages,
  681. // therefore Designator Index 0 can never be a useful reference for a control and we can inhibit it.
  682. // Write single "String Index"
  683. rd_write_short_item(rd_local_string, pp_data->caps[caps_idx].NotRange.StringIndex, &rpt_desc);
  684. }
  685. if ((main_item_list->next != NULL) &&
  686. ((int)main_item_list->next->MainItemType == rt_idx) &&
  687. (main_item_list->next->TypeOfNode == rd_item_node_cap) &&
  688. (pp_data->caps[main_item_list->next->CapsIndex].IsButtonCap) &&
  689. (!pp_data->caps[caps_idx].IsRange) && // This node in list is no array
  690. (!pp_data->caps[main_item_list->next->CapsIndex].IsRange) && // Next node in list is no array
  691. (pp_data->caps[main_item_list->next->CapsIndex].UsagePage == pp_data->caps[caps_idx].UsagePage) &&
  692. (pp_data->caps[main_item_list->next->CapsIndex].ReportID == pp_data->caps[caps_idx].ReportID) &&
  693. (pp_data->caps[main_item_list->next->CapsIndex].BitField == pp_data->caps[caps_idx].BitField)
  694. ) {
  695. if (main_item_list->next->FirstBit != main_item_list->FirstBit) {
  696. // In case of IsMultipleItemsForArray for multiple dedicated usages for a multi-button array, the report count should be incremented
  697. // Skip global items until any of them changes, than use ReportCount item to write the count of identical report fields
  698. report_count++;
  699. }
  700. }
  701. else {
  702. if ((pp_data->caps[caps_idx].Button.LogicalMin == 0) &&
  703. (pp_data->caps[caps_idx].Button.LogicalMax == 0)) {
  704. // While a HID report descriptor must always contain LogicalMinimum and LogicalMaximum,
  705. // the preparsed data contain both fields set to zero, for the case of simple buttons
  706. // Write "Logical Minimum" set to 0 and "Logical Maximum" set to 1
  707. rd_write_short_item(rd_global_logical_minimum, 0, &rpt_desc);
  708. rd_write_short_item(rd_global_logical_maximum, 1, &rpt_desc);
  709. }
  710. else {
  711. // Write logical range from "Logical Minimum" to "Logical Maximum"
  712. rd_write_short_item(rd_global_logical_minimum, pp_data->caps[caps_idx].Button.LogicalMin, &rpt_desc);
  713. rd_write_short_item(rd_global_logical_maximum, pp_data->caps[caps_idx].Button.LogicalMax, &rpt_desc);
  714. }
  715. // Write "Report Size"
  716. rd_write_short_item(rd_global_report_size, pp_data->caps[caps_idx].ReportSize, &rpt_desc);
  717. // Write "Report Count"
  718. if (!pp_data->caps[caps_idx].IsRange) {
  719. // Variable bit field with one bit per button
  720. // In case of multiple usages with the same items, only "Usage" is written per cap, and "Report Count" is incremented
  721. rd_write_short_item(rd_global_report_count, pp_data->caps[caps_idx].ReportCount + report_count, &rpt_desc);
  722. }
  723. else {
  724. // Button array of "Report Size" x "Report Count
  725. rd_write_short_item(rd_global_report_count, pp_data->caps[caps_idx].ReportCount, &rpt_desc);
  726. }
  727. // Buttons have only 1 bit and therefore no physical limits/units -> Set to undefined state
  728. if (last_physical_min != 0) {
  729. // Write "Physical Minimum", but only if changed
  730. last_physical_min = 0;
  731. rd_write_short_item(rd_global_physical_minimum, last_physical_min, &rpt_desc);
  732. }
  733. if (last_physical_max != 0) {
  734. // Write "Physical Maximum", but only if changed
  735. last_physical_max = 0;
  736. rd_write_short_item(rd_global_physical_maximum, last_physical_max, &rpt_desc);
  737. }
  738. if (last_unit_exponent != 0) {
  739. // Write "Unit Exponent", but only if changed
  740. last_unit_exponent = 0;
  741. rd_write_short_item(rd_global_unit_exponent, last_unit_exponent, &rpt_desc);
  742. }
  743. if (last_unit != 0) {
  744. // Write "Unit",but only if changed
  745. last_unit = 0;
  746. rd_write_short_item(rd_global_unit, last_unit, &rpt_desc);
  747. }
  748. // Write "Input" main item
  749. if (rt_idx == HidP_Input) {
  750. rd_write_short_item(rd_main_input, pp_data->caps[caps_idx].BitField, &rpt_desc);
  751. }
  752. // Write "Output" main item
  753. else if (rt_idx == HidP_Output) {
  754. rd_write_short_item(rd_main_output, pp_data->caps[caps_idx].BitField, &rpt_desc);
  755. }
  756. // Write "Feature" main item
  757. else if (rt_idx == HidP_Feature) {
  758. rd_write_short_item(rd_main_feature, pp_data->caps[caps_idx].BitField, &rpt_desc);
  759. }
  760. report_count = 0;
  761. }
  762. }
  763. else {
  764. if (last_report_id != pp_data->caps[caps_idx].ReportID) {
  765. // Write "Report ID" if changed
  766. rd_write_short_item(rd_global_report_id, pp_data->caps[caps_idx].ReportID, &rpt_desc);
  767. last_report_id = pp_data->caps[caps_idx].ReportID;
  768. }
  769. // Write "Usage Page" if changed
  770. if (pp_data->caps[caps_idx].UsagePage != last_usage_page) {
  771. rd_write_short_item(rd_global_usage_page, pp_data->caps[caps_idx].UsagePage, &rpt_desc);
  772. last_usage_page = pp_data->caps[caps_idx].UsagePage;
  773. }
  774. if (inhibit_write_of_usage) {
  775. // Inhibit only once after Delimiter - Reset flag
  776. inhibit_write_of_usage = FALSE;
  777. }
  778. else {
  779. if (pp_data->caps[caps_idx].IsRange) {
  780. // Write usage range from "Usage Minimum" to "Usage Maximum"
  781. rd_write_short_item(rd_local_usage_minimum, pp_data->caps[caps_idx].Range.UsageMin, &rpt_desc);
  782. rd_write_short_item(rd_local_usage_maximum, pp_data->caps[caps_idx].Range.UsageMax, &rpt_desc);
  783. }
  784. else {
  785. // Write single "Usage"
  786. rd_write_short_item(rd_local_usage, pp_data->caps[caps_idx].NotRange.Usage, &rpt_desc);
  787. }
  788. }
  789. if (pp_data->caps[caps_idx].IsDesignatorRange) {
  790. // Write physical descriptor indices range from "Designator Minimum" to "Designator Maximum"
  791. rd_write_short_item(rd_local_designator_minimum, pp_data->caps[caps_idx].Range.DesignatorMin, &rpt_desc);
  792. rd_write_short_item(rd_local_designator_maximum, pp_data->caps[caps_idx].Range.DesignatorMax, &rpt_desc);
  793. }
  794. else if (pp_data->caps[caps_idx].NotRange.DesignatorIndex != 0) {
  795. // Designator set 0 is a special descriptor set (of the HID Physical Descriptor),
  796. // that specifies the number of additional descriptor sets.
  797. // Therefore Designator Index 0 can never be a useful reference for a control and we can inhibit it.
  798. // Write single "Designator Index"
  799. rd_write_short_item(rd_local_designator_index, pp_data->caps[caps_idx].NotRange.DesignatorIndex, &rpt_desc);
  800. }
  801. if (pp_data->caps[caps_idx].IsStringRange) {
  802. // Write range of indices of the USB string descriptor, from "String Minimum" to "String Maximum"
  803. rd_write_short_item(rd_local_string_minimum, pp_data->caps[caps_idx].Range.StringMin, &rpt_desc);
  804. rd_write_short_item(rd_local_string_maximum, pp_data->caps[caps_idx].Range.StringMax, &rpt_desc);
  805. }
  806. else if (pp_data->caps[caps_idx].NotRange.StringIndex != 0) {
  807. // String Index 0 is a special entry of the USB string descriptor, that contains a list of supported languages,
  808. // therefore Designator Index 0 can never be a useful reference for a control and we can inhibit it.
  809. // Write single "String Index"
  810. rd_write_short_item(rd_local_string, pp_data->caps[caps_idx].NotRange.StringIndex, &rpt_desc);
  811. }
  812. if ((pp_data->caps[caps_idx].BitField & 0x02) != 0x02) {
  813. // In case of an value array overwrite "Report Count"
  814. pp_data->caps[caps_idx].ReportCount = pp_data->caps[caps_idx].Range.DataIndexMax - pp_data->caps[caps_idx].Range.DataIndexMin + 1;
  815. }
  816. // Print only local report items for each cap, if ReportCount > 1
  817. if ((main_item_list->next != NULL) &&
  818. ((int) main_item_list->next->MainItemType == rt_idx) &&
  819. (main_item_list->next->TypeOfNode == rd_item_node_cap) &&
  820. (!pp_data->caps[main_item_list->next->CapsIndex].IsButtonCap) &&
  821. (!pp_data->caps[caps_idx].IsRange) && // This node in list is no array
  822. (!pp_data->caps[main_item_list->next->CapsIndex].IsRange) && // Next node in list is no array
  823. (pp_data->caps[main_item_list->next->CapsIndex].UsagePage == pp_data->caps[caps_idx].UsagePage) &&
  824. (pp_data->caps[main_item_list->next->CapsIndex].NotButton.LogicalMin == pp_data->caps[caps_idx].NotButton.LogicalMin) &&
  825. (pp_data->caps[main_item_list->next->CapsIndex].NotButton.LogicalMax == pp_data->caps[caps_idx].NotButton.LogicalMax) &&
  826. (pp_data->caps[main_item_list->next->CapsIndex].NotButton.PhysicalMin == pp_data->caps[caps_idx].NotButton.PhysicalMin) &&
  827. (pp_data->caps[main_item_list->next->CapsIndex].NotButton.PhysicalMax == pp_data->caps[caps_idx].NotButton.PhysicalMax) &&
  828. (pp_data->caps[main_item_list->next->CapsIndex].UnitsExp == pp_data->caps[caps_idx].UnitsExp) &&
  829. (pp_data->caps[main_item_list->next->CapsIndex].Units == pp_data->caps[caps_idx].Units) &&
  830. (pp_data->caps[main_item_list->next->CapsIndex].ReportSize == pp_data->caps[caps_idx].ReportSize) &&
  831. (pp_data->caps[main_item_list->next->CapsIndex].ReportID == pp_data->caps[caps_idx].ReportID) &&
  832. (pp_data->caps[main_item_list->next->CapsIndex].BitField == pp_data->caps[caps_idx].BitField) &&
  833. (pp_data->caps[main_item_list->next->CapsIndex].ReportCount == 1) &&
  834. (pp_data->caps[caps_idx].ReportCount == 1)
  835. ) {
  836. // Skip global items until any of them changes, than use ReportCount item to write the count of identical report fields
  837. report_count++;
  838. }
  839. else {
  840. // Value
  841. // Write logical range from "Logical Minimum" to "Logical Maximum"
  842. rd_write_short_item(rd_global_logical_minimum, pp_data->caps[caps_idx].NotButton.LogicalMin, &rpt_desc);
  843. rd_write_short_item(rd_global_logical_maximum, pp_data->caps[caps_idx].NotButton.LogicalMax, &rpt_desc);
  844. if ((last_physical_min != pp_data->caps[caps_idx].NotButton.PhysicalMin) ||
  845. (last_physical_max != pp_data->caps[caps_idx].NotButton.PhysicalMax)) {
  846. // Write range from "Physical Minimum" to " Physical Maximum", but only if one of them changed
  847. rd_write_short_item(rd_global_physical_minimum, pp_data->caps[caps_idx].NotButton.PhysicalMin, &rpt_desc);
  848. last_physical_min = pp_data->caps[caps_idx].NotButton.PhysicalMin;
  849. rd_write_short_item(rd_global_physical_maximum, pp_data->caps[caps_idx].NotButton.PhysicalMax, &rpt_desc);
  850. last_physical_max = pp_data->caps[caps_idx].NotButton.PhysicalMax;
  851. }
  852. if (last_unit_exponent != pp_data->caps[caps_idx].UnitsExp) {
  853. // Write "Unit Exponent", but only if changed
  854. rd_write_short_item(rd_global_unit_exponent, pp_data->caps[caps_idx].UnitsExp, &rpt_desc);
  855. last_unit_exponent = pp_data->caps[caps_idx].UnitsExp;
  856. }
  857. if (last_unit != pp_data->caps[caps_idx].Units) {
  858. // Write physical "Unit", but only if changed
  859. rd_write_short_item(rd_global_unit, pp_data->caps[caps_idx].Units, &rpt_desc);
  860. last_unit = pp_data->caps[caps_idx].Units;
  861. }
  862. // Write "Report Size"
  863. rd_write_short_item(rd_global_report_size, pp_data->caps[caps_idx].ReportSize, &rpt_desc);
  864. // Write "Report Count"
  865. rd_write_short_item(rd_global_report_count, pp_data->caps[caps_idx].ReportCount + report_count, &rpt_desc);
  866. if (rt_idx == HidP_Input) {
  867. // Write "Input" main item
  868. rd_write_short_item(rd_main_input, pp_data->caps[caps_idx].BitField, &rpt_desc);
  869. }
  870. else if (rt_idx == HidP_Output) {
  871. // Write "Output" main item
  872. rd_write_short_item(rd_main_output, pp_data->caps[caps_idx].BitField, &rpt_desc);
  873. }
  874. else if (rt_idx == HidP_Feature) {
  875. // Write "Feature" main item
  876. rd_write_short_item(rd_main_feature, pp_data->caps[caps_idx].BitField, &rpt_desc);
  877. }
  878. report_count = 0;
  879. }
  880. }
  881. // Go to next item in main_item_list and free the memory of the actual item
  882. struct rd_main_item_node *main_item_list_prev = main_item_list;
  883. main_item_list = main_item_list->next;
  884. free(main_item_list_prev);
  885. }
  886. // Free multidimensionable array: coll_bit_range[COLLECTION_INDEX][REPORT_ID][INPUT/OUTPUT/FEATURE]
  887. // Free multidimensionable array: coll_child_order[COLLECTION_INDEX][DIRECT_CHILD_INDEX]
  888. for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) {
  889. for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
  890. for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
  891. free(coll_bit_range[collection_node_idx][reportid_idx][rt_idx]);
  892. }
  893. free(coll_bit_range[collection_node_idx][reportid_idx]);
  894. }
  895. free(coll_bit_range[collection_node_idx]);
  896. if (coll_number_of_direct_childs[collection_node_idx] != 0) free(coll_child_order[collection_node_idx]);
  897. }
  898. free(coll_bit_range);
  899. free(coll_child_order);
  900. // Free one dimensional arrays
  901. free(coll_begin_lookup);
  902. free(coll_end_lookup);
  903. free(coll_levels);
  904. free(coll_number_of_direct_childs);
  905. return (int) rpt_desc.byte_idx;
  906. }