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_main_item_parse                  PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function parses a main 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 /*    _ux_host_class_hid_report_add         Add report                    */
61 /*    _ux_utility_memory_set                Memory block set              */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    HID Class                                                           */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            verified memset and memcpy  */
74 /*                                            cases,                      */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_ux_host_class_hid_main_item_parse(UX_HOST_CLASS_HID * hid,UX_HOST_CLASS_HID_ITEM * item,UCHAR * descriptor)78 UINT  _ux_host_class_hid_main_item_parse(UX_HOST_CLASS_HID *hid, UX_HOST_CLASS_HID_ITEM *item, UCHAR *descriptor)
79 {
80 
81 UX_HOST_CLASS_HID_PARSER        *hid_parser;
82 UINT                            status =  UX_ERROR;
83 ULONG                           collection_type;
84 
85 
86     /* Get the temporary parser structure pointer.  */
87     hid_parser =  &hid -> ux_host_class_hid_parser;
88 
89     /* Analyze the tag.  */
90     switch (item -> ux_host_class_hid_item_report_tag)
91     {
92 
93     case UX_HOST_CLASS_HID_MAIN_TAG_COLLECTION:
94 
95         /* We have a new collection to open. If the collection type is application,
96            we have to differentiate the first collection.  */
97         collection_type =  _ux_host_class_hid_item_data_get(descriptor, item);
98 
99         /* Check the collection type.  */
100         if (collection_type == UX_HOST_CLASS_HID_COLLECTION_APPLICATION)
101         {
102 
103             /* We have a collection of type application, check if this is the first one */
104             if ((hid_parser -> ux_host_class_hid_parser_main_page == 0) && (hid_parser -> ux_host_class_hid_parser_main_usage == 0))
105             {
106 
107                 /* It is the first application. Since the main usage and page have not yet
108                    been defined, we use the global page and the current local usage.  */
109                 hid_parser -> ux_host_class_hid_parser_main_page =   hid_parser -> ux_host_class_hid_parser_global.ux_host_class_hid_global_item_usage_page;
110                 hid_parser -> ux_host_class_hid_parser_main_usage =  hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usages[0];
111             }
112 
113             /* Memorize the application.  */
114             hid_parser -> ux_host_class_hid_parser_application =  collection_type;
115 
116             /* Add one collection to this report */
117             hid_parser -> ux_host_class_hid_parser_number_collection++;
118 
119             /* Set the status to success.  */
120             status =  UX_SUCCESS;
121         }
122         else
123         {
124 
125             if (hid_parser -> ux_host_class_hid_parser_number_collection >= UX_HOST_CLASS_HID_MAX_COLLECTION)
126             {
127 
128                 /* Error trap. */
129                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_COLLECTION_OVERFLOW);
130 
131                 /* If trace is enabled, insert this event into the trace buffer.  */
132                 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_COLLECTION_OVERFLOW, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
133 
134                 return(UX_HOST_CLASS_HID_COLLECTION_OVERFLOW);
135             }
136             else
137             {
138 
139                 hid_parser -> ux_host_class_hid_parser_collection[hid_parser -> ux_host_class_hid_parser_number_collection] =  collection_type;
140 
141                 /* Add one collection to this report.  */
142                 hid_parser -> ux_host_class_hid_parser_number_collection++;
143 
144                 /* Set the status to success.  */
145                 status =  UX_SUCCESS;
146             }
147         }
148         break;
149 
150     case UX_HOST_CLASS_HID_MAIN_TAG_END_COLLECTION:
151 
152         /* We need to pop back the last collection.  */
153         if (hid_parser -> ux_host_class_hid_parser_number_collection == 0)
154         {
155 
156             /* Error trap. */
157             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_COLLECTION_OVERFLOW);
158 
159             /* If trace is enabled, insert this event into the trace buffer.  */
160             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_COLLECTION_OVERFLOW, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
161 
162             return(UX_HOST_CLASS_HID_COLLECTION_OVERFLOW);
163         }
164 
165         else
166 
167             hid_parser -> ux_host_class_hid_parser_number_collection--;
168 
169         status =  UX_SUCCESS;
170         break;
171 
172     case UX_HOST_CLASS_HID_MAIN_TAG_INPUT:
173     case UX_HOST_CLASS_HID_MAIN_TAG_OUTPUT:
174     case UX_HOST_CLASS_HID_MAIN_TAG_FEATURE:
175 
176         /* We need to add a report.  */
177         status =  _ux_host_class_hid_report_add(hid, descriptor, item);
178         break;
179     }
180 
181     /* We have a new main item, so the local instances have to be cleaned.  */
182     _ux_utility_memory_set(&hid_parser -> ux_host_class_hid_parser_local, 0, sizeof(UX_HOST_CLASS_HID_LOCAL_ITEM)); /* Use case of memset is verified. */
183 
184     /* Return completion status.  */
185     return(status);
186 }
187 
188