GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_hid_event_get.c Lines: 28 32 87.5 %
Date: 2026-03-06 18:57:10 Branches: 10 14 71.4 %

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
/**   Device HID Class                                                    */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
#define UX_SOURCE_CODE
24
25
26
/* Include necessary system files.  */
27
28
#include "ux_api.h"
29
#include "ux_device_class_hid.h"
30
#include "ux_device_stack.h"
31
32
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _ux_device_class_hid_event_check                    PORTABLE C      */
38
/*                                                           6.3.0        */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function checks if there is an event from the application and  */
46
/*    fill a pointer to access the event.                                 */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    hid                                      Address of hid class       */
51
/*    event                                    Pointer to fill address    */
52
/*                                             to access event            */
53
/*                                                                        */
54
/*  OUTPUT                                                                */
55
/*                                                                        */
56
/*    status                                   UX_SUCCESS if there is an  */
57
/*                                             event                      */
58
/*  CALLS                                                                 */
59
/*                                                                        */
60
/*                                                                        */
61
/*  CALLED BY                                                             */
62
/*                                                                        */
63
/*    Device HID Class                                                    */
64
/*                                                                        */
65
/**************************************************************************/
66
2167
UINT  _ux_device_class_hid_event_check(UX_SLAVE_CLASS_HID *hid,
67
                                       UX_DEVICE_CLASS_HID_EVENT **hid_event)
68
{
69
UX_SLAVE_DEVICE                 *device;
70
71
    /* Get the pointer to the device.  */
72
2167
    device =  &_ux_system_slave -> ux_system_slave_device;
73
74
    /* Check the device state.  */
75
2167
    if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
76
5
        return(UX_DEVICE_HANDLE_UNKNOWN);
77
78
    /* Check if the head and the tail of the event array is the same.  */
79
2162
    if (hid -> ux_device_class_hid_event_array_head ==
80
2162
        hid -> ux_device_class_hid_event_array_tail)
81
82
        /* No event to report.  */
83
858
        return(UX_ERROR);
84
85
    /* There is an event to report, get the current pointer to the event.  */
86
1304
    *hid_event =  hid -> ux_device_class_hid_event_array_tail;
87
1304
    return(UX_SUCCESS);
88
}
89
90
91
/**************************************************************************/
92
/*                                                                        */
93
/*  FUNCTION                                               RELEASE        */
94
/*                                                                        */
95
/*    _ux_device_class_hid_event_free                     PORTABLE C      */
96
/*                                                           6.3.0        */
97
/*  AUTHOR                                                                */
98
/*                                                                        */
99
/*    Chaoqiong Xiao, Microsoft Corporation                               */
100
/*                                                                        */
101
/*  DESCRIPTION                                                           */
102
/*                                                                        */
103
/*    This function free the event in queue tail.                         */
104
/*                                                                        */
105
/*  INPUT                                                                 */
106
/*                                                                        */
107
/*    hid                                      Address of hid class       */
108
/*                                                                        */
109
/*  OUTPUT                                                                */
110
/*                                                                        */
111
/*                                                                        */
112
/*  CALLS                                                                 */
113
/*                                                                        */
114
/*                                                                        */
115
/*  CALLED BY                                                             */
116
/*                                                                        */
117
/*    Device HID Class                                                    */
118
/*                                                                        */
119
/**************************************************************************/
120
1298
VOID  _ux_device_class_hid_event_free(UX_SLAVE_CLASS_HID *hid)
121
{
122
UCHAR                           *pos;
123
124
1298
    pos = (UCHAR *) hid -> ux_device_class_hid_event_array_tail;
125
1298
    pos += UX_DEVICE_CLASS_HID_EVENT_QUEUE_ITEM_SIZE(hid);
126
1298
    if (pos >= (UCHAR *) hid -> ux_device_class_hid_event_array_end)
127
78
        pos = (UCHAR *) hid -> ux_device_class_hid_event_array;
128
1298
    hid -> ux_device_class_hid_event_array_tail = (UX_DEVICE_CLASS_HID_EVENT *) pos;
129
1298
}
130
131
132
/**************************************************************************/
133
/*                                                                        */
134
/*  FUNCTION                                               RELEASE        */
135
/*                                                                        */
136
/*    _ux_device_class_hid_event_get                      PORTABLE C      */
137
/*                                                           6.3.0        */
138
/*  AUTHOR                                                                */
139
/*                                                                        */
140
/*    Chaoqiong Xiao, Microsoft Corporation                               */
141
/*                                                                        */
142
/*  DESCRIPTION                                                           */
143
/*                                                                        */
144
/*    This function checks if there is an event from the application      */
145
/*                                                                        */
146
/*  INPUT                                                                 */
147
/*                                                                        */
148
/*    hid                                      Address of hid class       */
149
/*    event                                    Pointer of the event       */
150
/*                                                                        */
151
/*  OUTPUT                                                                */
152
/*                                                                        */
153
/*    status                                   UX_SUCCESS if there is an  */
154
/*                                             event                      */
155
/*  CALLS                                                                 */
156
/*                                                                        */
157
/*    _ux_utility_memory_copy                  Copy memory                */
158
/*                                                                        */
159
/*  CALLED BY                                                             */
160
/*                                                                        */
161
/*    ThreadX                                                             */
162
/*                                                                        */
163
/**************************************************************************/
164
95
UINT  _ux_device_class_hid_event_get(UX_SLAVE_CLASS_HID *hid,
165
                                     UX_SLAVE_CLASS_HID_EVENT *hid_event)
166
{
167
168
UX_DEVICE_CLASS_HID_EVENT       *current_hid_event;
169
UINT                            status;
170
171
    /* If trace is enabled, insert this event into the trace buffer.  */
172
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_HID_EVENT_GET, hid, hid_event, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
173
174
    /* Check and get event pointer.  */
175
95
    status = _ux_device_class_hid_event_check(hid, &current_hid_event);
176
95
    if (status != UX_SUCCESS)
177
13
        return(status);
178
179
    /* Keep the event data length inside buffer area.  */
180
82
    if (current_hid_event -> ux_device_class_hid_event_length > UX_DEVICE_CLASS_HID_EVENT_MAX_LENGTH(hid))
181
1
        current_hid_event -> ux_device_class_hid_event_length = UX_DEVICE_CLASS_HID_EVENT_MAX_LENGTH(hid);
182
183
    /* fill in the event structure from the user.  */
184
82
    hid_event -> ux_device_class_hid_event_length =  current_hid_event -> ux_device_class_hid_event_length;
185
186
    /* Copy the event data into the user buffer.  */
187
82
    _ux_utility_memory_copy(hid_event -> ux_device_class_hid_event_buffer,
188
82
                            UX_DEVICE_CLASS_HID_EVENT_BUFFER(current_hid_event),
189
82
                            current_hid_event -> ux_device_class_hid_event_length); /* Use case of memcpy is verified. */
190
191
    /* Free the tail event.  */
192
82
    _ux_device_class_hid_event_free(hid);
193
194
    /* Return event status to the user.  */
195
82
    return(UX_SUCCESS);
196
}
197
198
199
/**************************************************************************/
200
/*                                                                        */
201
/*  FUNCTION                                               RELEASE        */
202
/*                                                                        */
203
/*    _uxe_device_class_hid_event_get                     PORTABLE C      */
204
/*                                                           6.3.0        */
205
/*  AUTHOR                                                                */
206
/*                                                                        */
207
/*    Chaoqiong Xiao, Microsoft Corporation                               */
208
/*                                                                        */
209
/*  DESCRIPTION                                                           */
210
/*                                                                        */
211
/*    This function checks errors in HID event get function call.         */
212
/*                                                                        */
213
/*  INPUT                                                                 */
214
/*                                                                        */
215
/*    hid                                   Pointer to hid instance       */
216
/*    hid_event                             Pointer to hid event          */
217
/*                                                                        */
218
/*  OUTPUT                                                                */
219
/*                                                                        */
220
/*    None                                                                */
221
/*                                                                        */
222
/*  CALLS                                                                 */
223
/*                                                                        */
224
/*    _ux_device_class_hid_event_get        Get an HID event              */
225
/*                                                                        */
226
/*  CALLED BY                                                             */
227
/*                                                                        */
228
/*    Application                                                         */
229
/*                                                                        */
230
/**************************************************************************/
231
UINT  _uxe_device_class_hid_event_get(UX_SLAVE_CLASS_HID *hid,
232
                                      UX_SLAVE_CLASS_HID_EVENT *hid_event)
233
{
234
235
    /* Sanity checks.  */
236
    if ((hid == UX_NULL) || (hid_event == UX_NULL))
237
        return(UX_INVALID_PARAMETER);
238
239
    /* Invoke function to get event.  */
240
    return(_ux_device_class_hid_event_get(hid, hid_event));
241
}