GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_report_get.c Lines: 45 49 91.8 %
Date: 2026-03-06 18:57:10 Branches: 22 26 84.6 %

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_get                       PORTABLE C      */
38
/*                                                           6.1.10       */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function gets a report (input/output/feature) from the device. */
46
/*    The report can be either decompressed by the stack or raw.          */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    hid                                   Pointer to HID class          */
51
/*    client_report                         Pointer to client report      */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    Completion Status                                                   */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    _ux_host_class_hid_report_decompress  Decompress HID report         */
60
/*    _ux_host_stack_class_instance_verify  Verify the instance is valid  */
61
/*    _ux_host_stack_transfer_request       Process transfer request      */
62
/*    _ux_utility_memory_allocate           Allocate memory block         */
63
/*    _ux_utility_memory_copy               Copy memory block             */
64
/*    _ux_utility_memory_free               Release memory block          */
65
/*    _ux_host_semaphore_get                Get protection semaphore      */
66
/*    _ux_host_semaphore_put                Release protection semaphore  */
67
/*                                                                        */
68
/*  CALLED BY                                                             */
69
/*                                                                        */
70
/*    Application                                                         */
71
/*    HID Class                                                           */
72
/*                                                                        */
73
/**************************************************************************/
74
15
UINT  _ux_host_class_hid_report_get(UX_HOST_CLASS_HID *hid, UX_HOST_CLASS_HID_CLIENT_REPORT *client_report)
75
{
76
#if defined(UX_HOST_STANDALONE)
77
UX_INTERRUPT_SAVE_AREA
78
#endif
79
UX_ENDPOINT                     *control_endpoint;
80
UX_TRANSFER                     *transfer_request;
81
UCHAR                           *report_buffer;
82
UX_HOST_CLASS_HID_REPORT        *hid_report;
83
UINT                            status;
84
85
    /* If trace is enabled, insert this event into the trace buffer.  */
86
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_REPORT_GET, hid, client_report, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
87
88
    /* Ensure the instance is valid.  */
89
15
    if (_ux_host_stack_class_instance_verify(_ux_system_host_class_hid_name, (VOID *) hid) != UX_SUCCESS)
90
    {
91
92
        /* Error trap. */
93
1
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
94
95
        /* If trace is enabled, insert this event into the trace buffer.  */
96
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
97
98
1
        return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
99
    }
100
101
    /* Protect thread reentry to this instance.  */
102
14
    _ux_host_class_hid_lock_fail_return(hid);
103
104
    /* Get the report pointer from the caller.  */
105
13
    hid_report =  client_report -> ux_host_class_hid_client_report;
106
107
    /* Ensure this is a INPUT report or FEATURE report.  */
108
13
    if (hid_report -> ux_host_class_hid_report_type != UX_HOST_CLASS_HID_REPORT_TYPE_INPUT &&
109
2
        hid_report -> ux_host_class_hid_report_type != UX_HOST_CLASS_HID_REPORT_TYPE_FEATURE)
110
    {
111
112
        /* Unprotect thread reentry to this instance.  */
113
1
        _ux_host_class_hid_unlock(hid);
114
115
        /* If trace is enabled, insert this event into the trace buffer.  */
116
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_REPORT_ERROR, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
117
118
        /* Return error.  */
119
1
        return(UX_HOST_CLASS_HID_REPORT_ERROR);
120
    }
121
122
    /* Check the report length, if 0, we don't need to do anything.  */
123
12
    if (hid_report -> ux_host_class_hid_report_byte_length == 0)
124
    {
125
126
        /* Unprotect thread reentry to this instance.  */
127
1
        _ux_host_class_hid_unlock(hid);
128
129
        /* Error trap. */
130
1
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_REPORT_ERROR);
131
132
        /* If trace is enabled, insert this event into the trace buffer.  */
133
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_REPORT_ERROR, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
134
135
        /* Return error code.  */
136
1
        return(UX_HOST_CLASS_HID_REPORT_ERROR);
137
    }
138
139
    /* Get some memory for reading the report.  */
140
11
    report_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, hid_report -> ux_host_class_hid_report_byte_length);
141
11
    if (report_buffer == UX_NULL)
142
    {
143
144
        /* Unprotect thread reentry to this instance.  */
145
1
        _ux_host_class_hid_unlock(hid);
146
147
        /* Return error code.  */
148
1
        return(UX_MEMORY_INSUFFICIENT);
149
    }
150
151
    /* We need to get the default control endpoint transfer request pointer.  */
152
10
    control_endpoint =  &hid -> ux_host_class_hid_device -> ux_device_control_endpoint;
153
10
    transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
154
155
    /* Protect the control endpoint semaphore here.  It will be unprotected in the
156
       transfer request function.  */
157
#if defined(UX_HOST_STANDALONE)
158
    UX_DISABLE
159
    if (hid -> ux_host_class_hid_device -> ux_device_flags & UX_DEVICE_FLAG_LOCK)
160
    {
161
        _ux_utility_memory_free(report_buffer);
162
        hid -> ux_host_class_hid_flags &= ~UX_HOST_CLASS_HID_FLAG_LOCK;
163
        UX_RESTORE
164
        return(UX_BUSY);
165
    }
166
    hid -> ux_host_class_hid_device -> ux_device_flags |= UX_DEVICE_FLAG_LOCK;
167
    transfer_request -> ux_transfer_request_flags |= UX_TRANSFER_FLAG_AUTO_DEVICE_UNLOCK;
168
    UX_TRANSFER_STATE_RESET(transfer_request);
169
    UX_RESTORE
170
#else
171
10
    status =  _ux_host_semaphore_get(&hid -> ux_host_class_hid_device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
172
173
    /* Check for status.  */
174
10
    if (status != UX_SUCCESS)
175
    {
176
177
        /* Something went wrong. */
178
179
        /* Free all resources.  */
180
1
        _ux_utility_memory_free(report_buffer);
181
182
        /* Unprotect thread reentry to this instance.  */
183
1
        _ux_host_class_hid_unlock(hid);
184
185
1
        return(status);
186
    }
187
#endif
188
189
    /* Create a transfer request for the GET_REPORT request.  */
190
9
    transfer_request -> ux_transfer_request_data_pointer =      report_buffer;
191
9
    transfer_request -> ux_transfer_request_requested_length =  hid_report -> ux_host_class_hid_report_byte_length;
192
9
    transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_HID_GET_REPORT;
193
9
    transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
194
9
    transfer_request -> ux_transfer_request_value =             (UINT)((USHORT) hid_report -> ux_host_class_hid_report_id | (USHORT) hid_report -> ux_host_class_hid_report_type << 8);
195
9
    transfer_request -> ux_transfer_request_index =             hid -> ux_host_class_hid_interface -> ux_interface_descriptor.bInterfaceNumber;
196
197
    /* Send request to HCD layer.  */
198
9
    status =  _ux_host_stack_transfer_request(transfer_request);
199
200
    /* Check for correct transfer and entire descriptor returned.  */
201

9
    if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == hid_report -> ux_host_class_hid_report_byte_length))
202
    {
203
204
        /* The report is now in memory in a raw format. The application may desire to keep it that way!  */
205
7
        if (client_report -> ux_host_class_hid_client_report_flags & UX_HOST_CLASS_HID_REPORT_RAW)
206
        {
207
208
            /* Ensure the user has given us enough memory for the raw buffer.  */
209
5
            if (client_report -> ux_host_class_hid_client_report_length >= transfer_request -> ux_transfer_request_actual_length)
210
            {
211
212
                /* We have enough memory to store the raw buffer.  */
213
4
                _ux_utility_memory_copy(client_report -> ux_host_class_hid_client_report_buffer, report_buffer, transfer_request -> ux_transfer_request_actual_length); /* Use case of memcpy is verified. */
214
215
                /* Set status to success.  */
216
4
                status =  UX_SUCCESS;
217
            }
218
            else
219
            {
220
221
                /* Error trap. */
222
1
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
223
224
                /* If trace is enabled, insert this event into the trace buffer.  */
225
                UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_BUFFER_OVERFLOW, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
226
227
                /* Set overflow status.  */
228
1
                status =  UX_BUFFER_OVERFLOW;
229
            }
230
        }
231
        else
232
        {
233
234
            /* The report buffer must be parsed and decompressed.  */
235
2
            status =  _ux_host_class_hid_report_decompress(hid, client_report, report_buffer, transfer_request -> ux_transfer_request_actual_length);
236
        }
237
    }
238
    else
239
    {
240
241
        /* Error trap. */
242
2
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_REPORT_ERROR);
243
244
        /* If trace is enabled, insert this event into the trace buffer.  */
245
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_REPORT_ERROR, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
246
247
        /* Set report error status.  */
248
2
        status =  UX_HOST_CLASS_HID_REPORT_ERROR;
249
    }
250
251
    /* Free all resources.  */
252
9
    _ux_utility_memory_free(report_buffer);
253
254
    /* Unprotect thread reentry to this instance.  */
255
9
    _ux_host_class_hid_unlock(hid);
256
257
    /* Return the completion status.  */
258
9
    return(status);
259
}
260
261
/**************************************************************************/
262
/*                                                                        */
263
/*  FUNCTION                                               RELEASE        */
264
/*                                                                        */
265
/*    _uxe_host_class_hid_report_get                      PORTABLE C      */
266
/*                                                           6.3.0        */
267
/*  AUTHOR                                                                */
268
/*                                                                        */
269
/*    Chaoqiong Xiao, Microsoft Corporation                               */
270
/*                                                                        */
271
/*  DESCRIPTION                                                           */
272
/*                                                                        */
273
/*    This function checks errors in HID report get function call.        */
274
/*                                                                        */
275
/*  INPUT                                                                 */
276
/*                                                                        */
277
/*    hid                                   Pointer to HID class          */
278
/*    client_report                         Pointer to client report      */
279
/*                                                                        */
280
/*  OUTPUT                                                                */
281
/*                                                                        */
282
/*    Status                                                              */
283
/*                                                                        */
284
/*  CALLS                                                                 */
285
/*                                                                        */
286
/*    _ux_host_class_hid_report_get         get a report                  */
287
/*                                                                        */
288
/*  CALLED BY                                                             */
289
/*                                                                        */
290
/*    Application                                                         */
291
/*                                                                        */
292
/**************************************************************************/
293
UINT  _uxe_host_class_hid_report_get(UX_HOST_CLASS_HID *hid, UX_HOST_CLASS_HID_CLIENT_REPORT *client_report)
294
{
295
296
    /* Sanity checks.  */
297
    if ((hid == UX_NULL) || (client_report == UX_NULL))
298
        return(UX_INVALID_PARAMETER);
299
300
    /* Invoke report ID get function.  */
301
    return(_ux_host_class_hid_report_get(hid, client_report));
302
}