GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_report_descriptor_get.c Lines: 47 49 95.9 %
Date: 2026-03-06 18:57:10 Branches: 19 20 95.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
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_report_descriptor_get            PORTABLE C      */
38
/*                                                           6.1          */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function gets the report descriptor and analyzes it.           */
46
/*                                                                        */
47
/*  INPUT                                                                 */
48
/*                                                                        */
49
/*    hid                                   Pointer to HID class          */
50
/*    length                                Length of descriptor          */
51
/*                                                                        */
52
/*  OUTPUT                                                                */
53
/*                                                                        */
54
/*    Completion Status                                                   */
55
/*                                                                        */
56
/*  CALLS                                                                 */
57
/*                                                                        */
58
/*    _ux_host_class_hid_global_item_parse  Parse global item             */
59
/*    _ux_host_class_hid_local_item_parse   Parse local item              */
60
/*    _ux_host_class_hid_report_item_analyse Analyze report               */
61
/*    _ux_host_class_hid_resources_free     Free HID resources            */
62
/*    _ux_host_stack_transfer_request       Process transfer request      */
63
/*    _ux_utility_memory_allocate           Allocate memory block         */
64
/*    _ux_utility_memory_free               Release memory block          */
65
/*                                                                        */
66
/*  CALLED BY                                                             */
67
/*                                                                        */
68
/*    HID Class                                                           */
69
/*                                                                        */
70
/**************************************************************************/
71
490
UINT  _ux_host_class_hid_report_descriptor_get(UX_HOST_CLASS_HID *hid, ULONG length)
72
{
73
74
UX_ENDPOINT             *control_endpoint;
75
UX_TRANSFER             *transfer_request;
76
UCHAR                   *descriptor;
77
UCHAR                   *start_descriptor;
78
UX_HOST_CLASS_HID_ITEM  item;
79
UINT                    status;
80
81
82
    /* We need to get the default control endpoint transfer request pointer.  */
83
490
    control_endpoint =  &hid -> ux_host_class_hid_device -> ux_device_control_endpoint;
84
490
    transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
85
86
    /* Need to allocate memory for the report descriptor.  */
87
490
    descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, length);
88
490
    if (descriptor == UX_NULL)
89
7
        return(UX_MEMORY_INSUFFICIENT);
90
91
    /* We need to save the descriptor starting address.  */
92
483
    start_descriptor =  descriptor;
93
94
    /* Create a transfer_request for the GET_DESCRIPTOR request */
95
483
    transfer_request -> ux_transfer_request_data_pointer =      descriptor;
96
483
    transfer_request -> ux_transfer_request_requested_length =  length;
97
483
    transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
98
483
    transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_INTERFACE;
99
483
    transfer_request -> ux_transfer_request_value =             UX_HOST_CLASS_HID_REPORT_DESCRIPTOR << 8;
100
483
    transfer_request -> ux_transfer_request_index =             hid -> ux_host_class_hid_interface -> ux_interface_descriptor.bInterfaceNumber;
101
102
    /* Send request to HCD layer.  */
103
483
    status =  _ux_host_stack_transfer_request(transfer_request);
104
105
    /* Check for correct transfer and entire descriptor returned.  */
106

483
    if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == length))
107
447
    {
108
        UINT analysis_failure;
109
        /* Parse the report descriptor and build the report items.  */
110
14371
        while (length)
111
        {
112
            /* Get one item from the report and analyze it.  */
113
            /* Make sure this descriptor has at least the minimum length.  */
114
14080
            analysis_failure = _ux_host_class_hid_report_item_analyse(descriptor, length, &item);
115
14080
            if (analysis_failure)
116
            {
117
              /* Error trap. */
118
              _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
119
120
              /* If trace is enabled, insert this event into the trace buffer.  */
121
              UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
122
123
              /* Return error status.  */
124
              status = (UX_DESCRIPTOR_CORRUPTED);
125
            }
126
127
            /* Point the descriptor right after the item identifier.  */
128
14080
            descriptor +=  item.ux_host_class_hid_item_report_format;
129
130
            /* Process relative to the item type.  */
131

14080
            switch (item.ux_host_class_hid_item_report_type)
132
            {
133
134
7382
            case UX_HOST_CLASS_HID_TYPE_GLOBAL:
135
136
                /* This is a global item.  */
137
7382
                status =  _ux_host_class_hid_global_item_parse(hid, &item, descriptor);
138
7382
                break;
139
140
141
3269
            case UX_HOST_CLASS_HID_TYPE_MAIN:
142
143
                /* This is a main item.  */
144
3269
                status =  _ux_host_class_hid_main_item_parse(hid, &item, descriptor);
145
3269
                break;
146
147
148
3423
            case UX_HOST_CLASS_HID_TYPE_LOCAL:
149
150
                /* This is a local item.  */
151
3423
                status =  _ux_host_class_hid_local_item_parse(hid, &item, descriptor);
152
3423
                break;
153
154
6
            default:
155
156
                /* This is a reserved item, meaning it shouldn't be used!  */
157
158
                /* Set status to error. The check after this switch statement
159
                    will handle the rest.  */
160
6
                status =  UX_DESCRIPTOR_CORRUPTED;
161
6
                break;
162
            }
163
164
            /* Recheck the status code.  */
165
14080
            if (status != UX_SUCCESS)
166
            {
167
153
                break;
168
            }
169
170
            /* Jump to the next item.  */
171
13927
            descriptor +=  item.ux_host_class_hid_item_report_length;
172
173
            /* Verify that the report descriptor is not corrupted.  */
174
13927
            if (length < (item.ux_host_class_hid_item_report_length + item.ux_host_class_hid_item_report_format))
175
            {
176
177
                /* Error trap. */
178
3
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
179
180
                /* If trace is enabled, insert this event into the trace buffer.  */
181
                UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
182
183
                /* Return error status.  */
184
3
                status = (UX_DESCRIPTOR_CORRUPTED);
185
3
                break;
186
            }
187
188
            /* Adjust the length.  */
189
13924
            length -=  (ULONG)(item.ux_host_class_hid_item_report_length + item.ux_host_class_hid_item_report_format);
190
        }
191
    }
192
    else
193
194
        /* Descriptor length error!  */
195
36
        status = UX_DESCRIPTOR_CORRUPTED;
196
197
483
    if (status != UX_SUCCESS)
198
    {
199
        /* Error trap. */
200
192
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
201
202
        /* If trace is enabled, insert this event into the trace buffer.  */
203
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
204
205
        /* The descriptor is corrupted!  */
206
192
        _ux_host_class_hid_resources_free(hid);
207
    }
208
209
    /* Free used resources.  */
210
483
    _ux_utility_memory_free(start_descriptor);
211
212
    /* Return completion status.  */
213
483
    return(status);
214
}
215