GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_report_compress.c Lines: 33 33 100.0 %
Date: 2026-03-06 18:57:10 Branches: 22 22 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_report_compress                  PORTABLE C      */
38
/*                                                           6.1          */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function will compress a client report into a report buffer.   */
46
/*                                                                        */
47
/*  INPUT                                                                 */
48
/*                                                                        */
49
/*    hid                                   Pointer to HID class          */
50
/*    client_report                         Pointer to client report      */
51
/*    report_buffer                         Pointer to report buffer      */
52
/*    report_length                         Length of report              */
53
/*                                                                        */
54
/*  OUTPUT                                                                */
55
/*                                                                        */
56
/*    Completion Status                                                   */
57
/*                                                                        */
58
/*  CALLS                                                                 */
59
/*                                                                        */
60
/*    None                                                                */
61
/*                                                                        */
62
/*  CALLED BY                                                             */
63
/*                                                                        */
64
/*    HID Class                                                           */
65
/*                                                                        */
66
/**************************************************************************/
67
9
UINT  _ux_host_class_hid_report_compress(UX_HOST_CLASS_HID *hid, UX_HOST_CLASS_HID_CLIENT_REPORT *client_report,
68
                                                    UCHAR *report_buffer, ULONG report_length)
69
{
70
71
UX_HOST_CLASS_HID_REPORT    *hid_report;
72
UX_HOST_CLASS_HID_FIELD     *hid_field;
73
ULONG                       field_usage;
74
ULONG                       field_report_count;
75
ULONG                       field_report_size;
76
ULONG                       *client_buffer;
77
ULONG                       client_value;
78
UCHAR                       value;
79
ULONG                       data_offset_bit;
80
UCHAR                       is_valid_usage;
81
82
    UX_PARAMETER_NOT_USED(hid);
83
    UX_PARAMETER_NOT_USED(report_length);
84
85
    /* Get the report pointer from the caller.  */
86
9
    hid_report =  client_report -> ux_host_class_hid_client_report;
87
88
    /* Get the pointer to the user buffer.  */
89
9
    client_buffer =  client_report -> ux_host_class_hid_client_report_buffer;
90
91
    /* Get the first field associated with the report.  */
92
9
    hid_field =  hid_report -> ux_host_class_hid_report_field;
93
94
    /* Set data offset bit.  */
95
9
    data_offset_bit =  0;
96
97
    /* We need to compress each field defined in the report.  */
98
26
    while (hid_field != UX_NULL)
99
    {
100
101
        /* Each report field has a report count value. This count is used to extract
102
           values from the incoming report and build each usage/value instance.  */
103
57
        for (field_report_count = 0; field_report_count < hid_field -> ux_host_class_hid_field_report_count; field_report_count++)
104
        {
105
106
            /* Ensure the usage in the client buffer is valid. How we determine
107
               this depends on whether the field is VARIABLE or ARRAY.  */
108
40
            is_valid_usage =  UX_FALSE;
109
40
            if (hid_field -> ux_host_class_hid_field_value & UX_HOST_CLASS_HID_ITEM_VARIABLE)
110
            {
111
112
                /* Go through the usage array to try and find the client's usage.  */
113
64
                for (field_usage = 0; field_usage < hid_field -> ux_host_class_hid_field_number_usage; field_usage++)
114
                {
115
116
                    /* Is this a usage we've recorded?  */
117
61
                    if (*client_buffer == hid_field -> ux_host_class_hid_field_usages[field_usage])
118
                    {
119
120
                        /* Yes, this is a valid usage.  */
121
28
                        is_valid_usage =  UX_TRUE;
122
28
                        break;
123
                    }
124
                }
125
            }
126
            else
127
            {
128
129
                /* Is the usage page valid?  */
130
9
                if (((*client_buffer & 0xffff0000) >> 16) == hid_field -> ux_host_class_hid_field_usage_page)
131
                {
132
133
                    /* Is the usage in the min and max range?  */
134
8
                    if ((*client_buffer & 0xffff) >= hid_field -> ux_host_class_hid_field_usage_min &&
135
7
                        (*client_buffer & 0xffff) <= hid_field -> ux_host_class_hid_field_usage_max)
136
                    {
137
138
                        /* Yes, this is a valid usage.  */
139
6
                        is_valid_usage =  UX_TRUE;
140
                    }
141
                }
142
            }
143
144
            /* Is the usage invalid?  */
145
40
            if (is_valid_usage == UX_FALSE)
146
            {
147
148
                /* Error trap. */
149
6
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_REPORT_ERROR);
150
151
                /* If trace is enabled, insert this event into the trace buffer.  */
152
                UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_REPORT_ERROR, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
153
154
6
                return(UX_HOST_CLASS_HID_REPORT_ERROR);
155
            }
156
157
            /* Skip the usage and point the buffer to the value.  */
158
34
            client_buffer++;
159
160
            /* Read the client value.  */
161
34
            client_value =  *client_buffer++;
162
163
            /* Build the value field in the report buffer bit by bit.  */
164
352
            for (field_report_size = hid_field -> ux_host_class_hid_field_report_size; field_report_size > 0; field_report_size--)
165
            {
166
167
                /* Isolate each bit from the report value.  */
168
318
                value =  (UCHAR) client_value & 1;
169
170
                /* Shift the isolated bit to its right space in the report byte.  */
171
318
                value =  (UCHAR)(value << data_offset_bit);
172
173
                /* Update the report with the bit value.  */
174
318
                *report_buffer |=  value;
175
176
                /* Move to next bit.  */
177
318
                data_offset_bit++;
178
179
                /* Are we on a byte boundary.  */
180
318
                if ((data_offset_bit & 7) == 0)
181
                {
182
183
                    /* If so increment the report address.  */
184
38
                    report_buffer++;
185
186
                    /* Reset offset bit.  */
187
38
                    data_offset_bit =  0;
188
                }
189
190
                /* Move to the next bit.  */
191
318
                client_value =  client_value >> 1;
192
            }
193
        }
194
195
        /* Move to the next field.  */
196
17
        hid_field =  hid_field -> ux_host_class_hid_field_next_field;
197
    }
198
199
    /* Return successful completion.  */
200
3
    return(UX_SUCCESS);
201
}