1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   HID Class                                                           */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_host_class_hid.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_hid_local_item_parse                 PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function parses a local item from the report descriptor.       */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    hid                                   Pointer to HID class          */
50 /*    item                                  Pointer to item               */
51 /*    descriptor                            Pointer to descriptor         */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_class_hid_item_data_get      Get data item                 */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    HID Class                                                           */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_ux_host_class_hid_local_item_parse(UX_HOST_CLASS_HID * hid,UX_HOST_CLASS_HID_ITEM * item,UCHAR * descriptor)74 UINT  _ux_host_class_hid_local_item_parse(UX_HOST_CLASS_HID *hid, UX_HOST_CLASS_HID_ITEM *item, UCHAR *descriptor)
75 {
76 
77 UX_HOST_CLASS_HID_PARSER    *hid_parser;
78 ULONG                       usage;
79 ULONG                       usage_min;
80 ULONG                       usage_max;
81 ULONG                       delimiter_set;
82 
83     /* Get the temporary parser structure pointer.  */
84     hid_parser =  &hid -> ux_host_class_hid_parser;
85 
86     /* Analyze the tag.  */
87     switch (item -> ux_host_class_hid_item_report_tag)
88     {
89 
90     case UX_HOST_CLASS_HID_LOCAL_TAG_USAGE:
91 
92         /* Local usage tag, check if we have an overflow.  */
93         if (hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage == UX_HOST_CLASS_HID_USAGES)
94         {
95 
96             /* Error trap. */
97             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_USAGE_OVERFLOW);
98 
99             /* If trace is enabled, insert this event into the trace buffer.  */
100             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_USAGE_OVERFLOW, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
101 
102             return(UX_HOST_CLASS_HID_USAGE_OVERFLOW);
103         }
104 
105         /* Obtain the usage from the descriptor.  */
106         usage =  _ux_host_class_hid_item_data_get(descriptor, item);
107 
108         /* Combine the global usage with the local usage to form a unique usage ID.  */
109         usage |= (hid_parser -> ux_host_class_hid_parser_global.ux_host_class_hid_global_item_usage_page << 16);
110 
111         /* Add the usage to the local usage table.  */
112         hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usages[hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage] =  usage;
113 
114         /* We have one more usage now.  */
115         hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage++;
116 
117         break;
118 
119 
120     case UX_HOST_CLASS_HID_LOCAL_TAG_USAGE_MINIMUM:
121 
122         /* Usage Minimum tag.  */
123         hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_min =
124                                                 (ULONG) _ux_host_class_hid_item_data_get(descriptor, item);
125 
126         break;
127 
128 
129     case UX_HOST_CLASS_HID_LOCAL_TAG_USAGE_MAXIMUM:
130 
131         /* Usage Maximum tag.  */
132         hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_max =
133                                                 (ULONG) _ux_host_class_hid_item_data_get(descriptor, item);
134 
135         /* Check if the maximum value is coherent with the minimum.  */
136         if (hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_max < hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_min)
137         {
138 
139             /* Error trap. */
140             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_MIN_MAX_ERROR);
141 
142             /* If trace is enabled, insert this event into the trace buffer.  */
143             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_MIN_MAX_ERROR, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
144 
145             return(UX_HOST_CLASS_HID_MIN_MAX_ERROR);
146         }
147 
148         /* Get the boundaries for the usage values which are defined when encountering the USAGE MAX tag.  */
149         usage_min =  (ULONG)(hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_min);
150         usage_max =  (ULONG)(hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_max);
151 
152         while (usage_min <= usage_max)
153         {
154 
155             /* Check if we can still add this usage.  */
156             if (hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage == UX_HOST_CLASS_HID_USAGES)
157             {
158 
159                 /* Error trap. */
160                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_USAGE_OVERFLOW);
161 
162                 /* If trace is enabled, insert this event into the trace buffer.  */
163                 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_USAGE_OVERFLOW, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
164 
165                 return(UX_HOST_CLASS_HID_USAGE_OVERFLOW);
166 
167             }
168 
169             /* Combine the global usage with the local usage to form a unique usage ID.  */
170             usage =  usage_min | (hid_parser -> ux_host_class_hid_parser_global.ux_host_class_hid_global_item_usage_page << 16);
171 
172             /* Add the usage to the local usage table.  */
173             hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usages[hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage] =  usage;
174 
175             /* We have one more usage now.  */
176             hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage++;
177 
178             /* Next usage value.  */
179             usage_min++;
180         }
181 
182         break;
183 
184     case UX_HOST_CLASS_HID_LOCAL_TAG_DELIMITER:
185 
186         /* Obtain the delimiter set from the descriptor.  */
187         delimiter_set =  _ux_host_class_hid_item_data_get(descriptor, item);
188 
189         /* We should have either an open or a close.  */
190         switch (delimiter_set)
191         {
192 
193         case UX_HOST_CLASS_HID_DELIMITER_OPEN:
194 
195             /* Recursive delimiter opens are not supported.  */
196             if (hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_delimiter_level == 1)
197             {
198 
199                 /* Error trap. */
200                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_DELIMITER_ERROR);
201 
202                 return(UX_HOST_CLASS_HID_DELIMITER_ERROR);
203             }
204 
205             /* Mark the opening of the delimiter.  */
206             hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_delimiter_level =  1;
207 
208             break;
209 
210         case UX_HOST_CLASS_HID_DELIMITER_CLOSE:
211 
212             /* Ensure we had an open delimiter before.  */
213             if (hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_delimiter_level == 0)
214             {
215 
216                 /* Error trap. */
217                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_DELIMITER_ERROR);
218 
219                 return(UX_HOST_CLASS_HID_DELIMITER_ERROR);
220             }
221 
222             /* Mark the closing of the delimiter.  */
223             hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_delimiter_level =  0;
224 
225             break;
226 
227         default:
228 
229             /* Error trap. */
230             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_DELIMITER_ERROR);
231 
232             /* We got a wrong delimiter set.  */
233             return(UX_HOST_CLASS_HID_DELIMITER_ERROR);
234         }
235         break;
236 
237     default:
238 
239         /* Error trap. */
240         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_TAG_UNSUPPORTED);
241 
242         /* If trace is enabled, insert this event into the trace buffer.  */
243         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_TAG_UNSUPPORTED, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
244 
245         /* This tag is either unknown or unsupported.  */
246         return(UX_HOST_CLASS_HID_TAG_UNSUPPORTED);
247     }
248 
249     /* Return status. Always SUCCESS if we get here.*/
250     return(UX_SUCCESS);
251 }
252 
253