GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_local_item_parse.c Lines: 56 56 100.0 %
Date: 2026-03-06 18:57:10 Branches: 20 20 100.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_local_item_parse                 PORTABLE C      */
38
/*                                                           6.1          */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function parses a local 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
/*                                                                        */
61
/*  CALLED BY                                                             */
62
/*                                                                        */
63
/*    HID Class                                                           */
64
/*                                                                        */
65
/**************************************************************************/
66
3423
UINT  _ux_host_class_hid_local_item_parse(UX_HOST_CLASS_HID *hid, UX_HOST_CLASS_HID_ITEM *item, UCHAR *descriptor)
67
{
68
69
UX_HOST_CLASS_HID_PARSER    *hid_parser;
70
ULONG                       usage;
71
ULONG                       usage_min;
72
ULONG                       usage_max;
73
ULONG                       delimiter_set;
74
75
    /* Get the temporary parser structure pointer.  */
76
3423
    hid_parser =  &hid -> ux_host_class_hid_parser;
77
78
    /* Analyze the tag.  */
79

3423
    switch (item -> ux_host_class_hid_item_report_tag)
80
    {
81
82
1075
    case UX_HOST_CLASS_HID_LOCAL_TAG_USAGE:
83
84
        /* Local usage tag, check if we have an overflow.  */
85
1075
        if (hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage == UX_HOST_CLASS_HID_USAGES)
86
        {
87
88
            /* Error trap. */
89
3
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_USAGE_OVERFLOW);
90
91
            /* If trace is enabled, insert this event into the trace buffer.  */
92
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_USAGE_OVERFLOW, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
93
94
3
            return(UX_HOST_CLASS_HID_USAGE_OVERFLOW);
95
        }
96
97
        /* Obtain the usage from the descriptor.  */
98
1072
        usage =  _ux_host_class_hid_item_data_get(descriptor, item);
99
100
        /* Combine the global usage with the local usage to form a unique usage ID.  */
101
1072
        usage |= (hid_parser -> ux_host_class_hid_parser_global.ux_host_class_hid_global_item_usage_page << 16);
102
103
        /* Add the usage to the local usage table.  */
104
1072
        hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usages[hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage] =  usage;
105
106
        /* We have one more usage now.  */
107
1072
        hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage++;
108
109
1072
        break;
110
111
112
1152
    case UX_HOST_CLASS_HID_LOCAL_TAG_USAGE_MINIMUM:
113
114
        /* Usage Minimum tag.  */
115
1152
        hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_min =
116
1152
                                                (ULONG) _ux_host_class_hid_item_data_get(descriptor, item);
117
118
1152
        break;
119
120
121
1153
    case UX_HOST_CLASS_HID_LOCAL_TAG_USAGE_MAXIMUM:
122
123
        /* Usage Maximum tag.  */
124
1153
        hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_max =
125
1153
                                                (ULONG) _ux_host_class_hid_item_data_get(descriptor, item);
126
127
        /* Check if the maximum value is coherent with the minimum.  */
128
1153
        if (hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_max < hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_min)
129
        {
130
131
            /* Error trap. */
132
3
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_MIN_MAX_ERROR);
133
134
            /* If trace is enabled, insert this event into the trace buffer.  */
135
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_MIN_MAX_ERROR, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
136
137
3
            return(UX_HOST_CLASS_HID_MIN_MAX_ERROR);
138
        }
139
140
        /* Get the boundaries for the usage values which are defined when encountering the USAGE MAX tag.  */
141
1150
        usage_min =  (ULONG)(hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_min);
142
1150
        usage_max =  (ULONG)(hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usage_max);
143
144
35694
        while (usage_min <= usage_max)
145
        {
146
147
            /* Check if we can still add this usage.  */
148
34547
            if (hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage == UX_HOST_CLASS_HID_USAGES)
149
            {
150
151
                /* Error trap. */
152
3
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_USAGE_OVERFLOW);
153
154
                /* If trace is enabled, insert this event into the trace buffer.  */
155
                UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_USAGE_OVERFLOW, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
156
157
3
                return(UX_HOST_CLASS_HID_USAGE_OVERFLOW);
158
159
            }
160
161
            /* Combine the global usage with the local usage to form a unique usage ID.  */
162
34544
            usage =  usage_min | (hid_parser -> ux_host_class_hid_parser_global.ux_host_class_hid_global_item_usage_page << 16);
163
164
            /* Add the usage to the local usage table.  */
165
34544
            hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_usages[hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage] =  usage;
166
167
            /* We have one more usage now.  */
168
34544
            hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_number_usage++;
169
170
            /* Next usage value.  */
171
34544
            usage_min++;
172
        }
173
174
1147
        break;
175
176
40
    case UX_HOST_CLASS_HID_LOCAL_TAG_DELIMITER:
177
178
        /* Obtain the delimiter set from the descriptor.  */
179
40
        delimiter_set =  _ux_host_class_hid_item_data_get(descriptor, item);
180
181
        /* We should have either an open or a close.  */
182
40
        switch (delimiter_set)
183
        {
184
185
23
        case UX_HOST_CLASS_HID_DELIMITER_OPEN:
186
187
            /* Recursive delimiter opens are not supported.  */
188
23
            if (hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_delimiter_level == 1)
189
            {
190
191
                /* Error trap. */
192
3
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_DELIMITER_ERROR);
193
194
3
                return(UX_HOST_CLASS_HID_DELIMITER_ERROR);
195
            }
196
197
            /* Mark the opening of the delimiter.  */
198
20
            hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_delimiter_level =  1;
199
200
20
            break;
201
202
14
        case UX_HOST_CLASS_HID_DELIMITER_CLOSE:
203
204
            /* Ensure we had an open delimiter before.  */
205
14
            if (hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_delimiter_level == 0)
206
            {
207
208
                /* Error trap. */
209
3
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_DELIMITER_ERROR);
210
211
3
                return(UX_HOST_CLASS_HID_DELIMITER_ERROR);
212
            }
213
214
            /* Mark the closing of the delimiter.  */
215
11
            hid_parser -> ux_host_class_hid_parser_local.ux_host_class_hid_local_item_delimiter_level =  0;
216
217
11
            break;
218
219
3
        default:
220
221
            /* Error trap. */
222
3
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_DELIMITER_ERROR);
223
224
            /* We got a wrong delimiter set.  */
225
3
            return(UX_HOST_CLASS_HID_DELIMITER_ERROR);
226
        }
227
31
        break;
228
229
3
    default:
230
231
        /* Error trap. */
232
3
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_TAG_UNSUPPORTED);
233
234
        /* If trace is enabled, insert this event into the trace buffer.  */
235
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_TAG_UNSUPPORTED, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
236
237
        /* This tag is either unknown or unsupported.  */
238
3
        return(UX_HOST_CLASS_HID_TAG_UNSUPPORTED);
239
    }
240
241
    /* Return status. Always SUCCESS if we get here.*/
242
3402
    return(UX_SUCCESS);
243
}
244