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: 2024-12-12 17:16:36 Branches: 22 22 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 *
4
 * This program and the accompanying materials are made available under the
5
 * terms of the MIT License which is available at
6
 * https://opensource.org/licenses/MIT.
7
 *
8
 * SPDX-License-Identifier: MIT
9
 **************************************************************************/
10
11
12
/**************************************************************************/
13
/**************************************************************************/
14
/**                                                                       */
15
/** USBX Component                                                        */
16
/**                                                                       */
17
/**   HID Class                                                           */
18
/**                                                                       */
19
/**************************************************************************/
20
/**************************************************************************/
21
22
23
/* Include necessary system files.  */
24
25
#define UX_SOURCE_CODE
26
27
#include "ux_api.h"
28
#include "ux_host_class_hid.h"
29
#include "ux_host_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_host_class_hid_report_compress                  PORTABLE C      */
37
/*                                                           6.1          */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function will compress a client report into a report buffer.   */
45
/*                                                                        */
46
/*  INPUT                                                                 */
47
/*                                                                        */
48
/*    hid                                   Pointer to HID class          */
49
/*    client_report                         Pointer to client report      */
50
/*    report_buffer                         Pointer to report buffer      */
51
/*    report_length                         Length of report              */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    Completion Status                                                   */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    None                                                                */
60
/*                                                                        */
61
/*  CALLED BY                                                             */
62
/*                                                                        */
63
/*    HID Class                                                           */
64
/*                                                                        */
65
/*  RELEASE HISTORY                                                       */
66
/*                                                                        */
67
/*    DATE              NAME                      DESCRIPTION             */
68
/*                                                                        */
69
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71
/*                                            resulting in version 6.1    */
72
/*                                                                        */
73
/**************************************************************************/
74
9
UINT  _ux_host_class_hid_report_compress(UX_HOST_CLASS_HID *hid, UX_HOST_CLASS_HID_CLIENT_REPORT *client_report,
75
                                                    UCHAR *report_buffer, ULONG report_length)
76
{
77
78
UX_HOST_CLASS_HID_REPORT    *hid_report;
79
UX_HOST_CLASS_HID_FIELD     *hid_field;
80
ULONG                       field_usage;
81
ULONG                       field_report_count;
82
ULONG                       field_report_size;
83
ULONG                       *client_buffer;
84
ULONG                       client_value;
85
UCHAR                       value;
86
ULONG                       data_offset_bit;
87
UCHAR                       is_valid_usage;
88
89
    UX_PARAMETER_NOT_USED(hid);
90
    UX_PARAMETER_NOT_USED(report_length);
91
92
    /* Get the report pointer from the caller.  */
93
9
    hid_report =  client_report -> ux_host_class_hid_client_report;
94
95
    /* Get the pointer to the user buffer.  */
96
9
    client_buffer =  client_report -> ux_host_class_hid_client_report_buffer;
97
98
    /* Get the first field associated with the report.  */
99
9
    hid_field =  hid_report -> ux_host_class_hid_report_field;
100
101
    /* Set data offset bit.  */
102
9
    data_offset_bit =  0;
103
104
    /* We need to compress each field defined in the report.  */
105
26
    while (hid_field != UX_NULL)
106
    {
107
108
        /* Each report field has a report count value. This count is used to extract
109
           values from the incoming report and build each usage/value instance.  */
110
57
        for (field_report_count = 0; field_report_count < hid_field -> ux_host_class_hid_field_report_count; field_report_count++)
111
        {
112
113
            /* Ensure the usage in the client buffer is valid. How we determine
114
               this depends on whether the field is VARIABLE or ARRAY.  */
115
40
            is_valid_usage =  UX_FALSE;
116
40
            if (hid_field -> ux_host_class_hid_field_value & UX_HOST_CLASS_HID_ITEM_VARIABLE)
117
            {
118
119
                /* Go through the usage array to try and find the client's usage.  */
120
64
                for (field_usage = 0; field_usage < hid_field -> ux_host_class_hid_field_number_usage; field_usage++)
121
                {
122
123
                    /* Is this a usage we've recorded?  */
124
61
                    if (*client_buffer == hid_field -> ux_host_class_hid_field_usages[field_usage])
125
                    {
126
127
                        /* Yes, this is a valid usage.  */
128
28
                        is_valid_usage =  UX_TRUE;
129
28
                        break;
130
                    }
131
                }
132
            }
133
            else
134
            {
135
136
                /* Is the usage page valid?  */
137
9
                if (((*client_buffer & 0xffff0000) >> 16) == hid_field -> ux_host_class_hid_field_usage_page)
138
                {
139
140
                    /* Is the usage in the min and max range?  */
141
8
                    if ((*client_buffer & 0xffff) >= hid_field -> ux_host_class_hid_field_usage_min &&
142
7
                        (*client_buffer & 0xffff) <= hid_field -> ux_host_class_hid_field_usage_max)
143
                    {
144
145
                        /* Yes, this is a valid usage.  */
146
6
                        is_valid_usage =  UX_TRUE;
147
                    }
148
                }
149
            }
150
151
            /* Is the usage invalid?  */
152
40
            if (is_valid_usage == UX_FALSE)
153
            {
154
155
                /* Error trap. */
156
6
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_REPORT_ERROR);
157
158
                /* If trace is enabled, insert this event into the trace buffer.  */
159
                UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_REPORT_ERROR, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
160
161
6
                return(UX_HOST_CLASS_HID_REPORT_ERROR);
162
            }
163
164
            /* Skip the usage and point the buffer to the value.  */
165
34
            client_buffer++;
166
167
            /* Read the client value.  */
168
34
            client_value =  *client_buffer++;
169
170
            /* Build the value field in the report buffer bit by bit.  */
171
352
            for (field_report_size = hid_field -> ux_host_class_hid_field_report_size; field_report_size > 0; field_report_size--)
172
            {
173
174
                /* Isolate each bit from the report value.  */
175
318
                value =  (UCHAR) client_value & 1;
176
177
                /* Shift the isolated bit to its right space in the report byte.  */
178
318
                value =  (UCHAR)(value << data_offset_bit);
179
180
                /* Update the report with the bit value.  */
181
318
                *report_buffer |=  value;
182
183
                /* Move to next bit.  */
184
318
                data_offset_bit++;
185
186
                /* Are we on a byte boundary.  */
187
318
                if ((data_offset_bit & 7) == 0)
188
                {
189
190
                    /* If so increment the report address.  */
191
38
                    report_buffer++;
192
193
                    /* Reset offset bit.  */
194
38
                    data_offset_bit =  0;
195
                }
196
197
                /* Move to the next bit.  */
198
318
                client_value =  client_value >> 1;
199
            }
200
        }
201
202
        /* Move to the next field.  */
203
17
        hid_field =  hid_field -> ux_host_class_hid_field_next_field;
204
    }
205
206
    /* Return successful completion.  */
207
3
    return(UX_SUCCESS);
208
}