Debian Codegraph @

3rdparty/hidapi/windows/hidapi_descriptor_reconstruct.c

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