GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_idle_get.c Lines: 29 33 87.9 %
Date: 2026-03-06 18:57:10 Branches: 12 16 75.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_idle_get                         PORTABLE C      */
38
/*                                                           6.1.10       */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function performs a GET_IDLE to the HID device.                */
46
/*                                                                        */
47
/*  INPUT                                                                 */
48
/*                                                                        */
49
/*    hid                                   Pointer to HID class          */
50
/*    idle_time                             Idle time                     */
51
/*    report_id                             Report ID                     */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    Completion Status                                                   */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    _ux_host_stack_class_instance_verify  Verify instance is valid      */
60
/*    _ux_host_stack_transfer_request       Process transfer request      */
61
/*    _ux_host_semaphore_get                Get protection semaphore      */
62
/*    _ux_host_semaphore_put                Release protection semaphore  */
63
/*                                                                        */
64
/*  CALLED BY                                                             */
65
/*                                                                        */
66
/*    HID Class                                                           */
67
/*                                                                        */
68
/**************************************************************************/
69
12
UINT  _ux_host_class_hid_idle_get(UX_HOST_CLASS_HID *hid, USHORT *idle_time, USHORT report_id)
70
{
71
#if defined(UX_HOST_STANDALONE)
72
UX_INTERRUPT_SAVE_AREA
73
#endif
74
UX_ENDPOINT     *control_endpoint;
75
UX_TRANSFER     *transfer_request;
76
UCHAR           *idle_byte;
77
UINT            status;
78
79
    /* If trace is enabled, insert this event into the trace buffer.  */
80
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_IDLE_GET, hid, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
81
82
    /* Ensure the instance is valid.  */
83
12
    if (_ux_host_stack_class_instance_verify(_ux_system_host_class_hid_name, (VOID *) hid) != UX_SUCCESS)
84
    {
85
86
        /* If trace is enabled, insert this event into the trace buffer.  */
87
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
88
89
1
        return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
90
    }
91
92
    /* We need to get the default control endpoint transfer request pointer.  */
93
11
    control_endpoint =  &hid -> ux_host_class_hid_device -> ux_device_control_endpoint;
94
11
    transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
95
96
    /* Protect thread reentry to this instance.  */
97
#if defined(UX_HOST_STANDALONE)
98
    UX_DISABLE
99
    if (hid -> ux_host_class_hid_flags & UX_HOST_CLASS_HID_FLAG_LOCK)
100
    {
101
        UX_RESTORE
102
        return(UX_BUSY);
103
    }
104
    hid -> ux_host_class_hid_flags &= ~UX_HOST_CLASS_HID_FLAG_LOCK;
105
    UX_RESTORE
106
#else
107
108
11
    status =  _ux_host_semaphore_get(&hid -> ux_host_class_hid_semaphore, UX_WAIT_FOREVER);
109
11
    if (status != UX_SUCCESS)
110
111
        /* Return error.  */
112
1
        return(status);
113
#endif
114
115
    /* Need to allocate memory for the idle byte.  */
116
10
    idle_byte =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, 1);
117
10
    if (idle_byte == UX_NULL)
118
    {
119
120
        /* Unprotect thread reentry to this instance.  */
121
1
        _ux_host_class_hid_unlock(hid);
122
123
        /* Return error status.  */
124
1
        return(UX_MEMORY_INSUFFICIENT);
125
    }
126
127
    /* Protect the control endpoint semaphore here.  It will be unprotected in the
128
       transfer request function.  */
129
#if defined(UX_HOST_STANDALONE)
130
    UX_DISABLE
131
    if (hid -> ux_host_class_hid_device -> ux_device_flags & UX_DEVICE_FLAG_LOCK)
132
    {
133
134
        /* Free allocated return busy.  */
135
        _ux_utility_memory_free(idle_byte);
136
        hid -> ux_host_class_hid_flags &= ~UX_HOST_CLASS_HID_FLAG_LOCK;
137
        UX_RESTORE
138
        return(UX_BUSY);
139
    }
140
    hid -> ux_host_class_hid_device -> ux_device_flags |= UX_DEVICE_FLAG_LOCK;
141
    transfer_request -> ux_transfer_request_flags |= UX_TRANSFER_FLAG_AUTO_DEVICE_UNLOCK;
142
    UX_TRANSFER_STATE_RESET(transfer_request);
143
    UX_RESTORE
144
#else
145
9
    status =  _ux_host_semaphore_get(&hid -> ux_host_class_hid_device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
146
147
    /* Check for status.  */
148
9
    if (status != UX_SUCCESS)
149
    {
150
151
        /* Something went wrong. */
152
153
        /* Free allocated memory.  */
154
1
        _ux_utility_memory_free(idle_byte);
155
156
        /* Unprotect thread reentry to this instance.  */
157
1
        _ux_host_semaphore_put(&hid -> ux_host_class_hid_semaphore);
158
159
1
        return(status);
160
    }
161
#endif
162
163
    /* Create a transfer request for the GET_IDLE request.  */
164
8
    transfer_request -> ux_transfer_request_data_pointer =      idle_byte;
165
8
    transfer_request -> ux_transfer_request_requested_length =  1;
166
8
    transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_HID_GET_IDLE;
167
8
    transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
168
8
    transfer_request -> ux_transfer_request_value =             report_id;
169
8
    transfer_request -> ux_transfer_request_index =             hid -> ux_host_class_hid_interface -> ux_interface_descriptor.bInterfaceNumber;
170
171
    /* Send request to HCD layer.  */
172
8
    status =  _ux_host_stack_transfer_request(transfer_request);
173
174
#if defined(UX_HOST_STANDALONE)
175
    if (!(transfer_request -> ux_transfer_request_flags & UX_TRANSFER_FLAG_AUTO_WAIT))
176
    {
177
        hid -> ux_host_class_hid_allocated = idle_byte;
178
        return(status);
179
    }
180
#endif
181
182
    /* Check for correct transfer and for the entire descriptor returned.  */
183

8
    if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == 1))
184
6
        *idle_time =  (USHORT) *idle_byte;
185
186
    /* Free used resources.  */
187
8
    _ux_utility_memory_free(idle_byte);
188
189
    /* Unprotect thread reentry to this instance.  */
190
#if defined(UX_HOST_STANDALONE)
191
    _ux_host_class_hid_unlock(hid);
192
#else
193
8
    _ux_host_semaphore_put(&hid -> ux_host_class_hid_semaphore);
194
#endif
195
196
    /* Return the completion status.  */
197
8
    return(status);
198
}
199
200
/**************************************************************************/
201
/*                                                                        */
202
/*  FUNCTION                                               RELEASE        */
203
/*                                                                        */
204
/*    _uxe_host_class_hid_report_idle_get                 PORTABLE C      */
205
/*                                                           6.3.0        */
206
/*  AUTHOR                                                                */
207
/*                                                                        */
208
/*    Chaoqiong Xiao, Microsoft Corporation                               */
209
/*                                                                        */
210
/*  DESCRIPTION                                                           */
211
/*                                                                        */
212
/*    This function checks errors in HID idle get function call.          */
213
/*                                                                        */
214
/*  INPUT                                                                 */
215
/*                                                                        */
216
/*    hid                                   Pointer to HID class          */
217
/*    idle_time                             Idle time                     */
218
/*    report_id                             Report ID                     */
219
/*                                                                        */
220
/*  OUTPUT                                                                */
221
/*                                                                        */
222
/*    Status                                                              */
223
/*                                                                        */
224
/*  CALLS                                                                 */
225
/*                                                                        */
226
/*    _ux_host_class_hid_idle_get           Get idle rate                 */
227
/*                                                                        */
228
/*  CALLED BY                                                             */
229
/*                                                                        */
230
/*    Application                                                         */
231
/*                                                                        */
232
/**************************************************************************/
233
UINT  _uxe_host_class_hid_idle_get(UX_HOST_CLASS_HID *hid, USHORT *idle_time, USHORT report_id)
234
{
235
236
    /* Sanity checks.  */
237
    if ((hid == UX_NULL) || (idle_time == UX_NULL))
238
        return(UX_INVALID_PARAMETER);
239
240
    /* Invoke idle get function.  */
241
    return(_ux_host_class_hid_idle_get(hid, idle_time, report_id));
242
}