summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_hid_report_compress.c
blob: 37a7e7ef99e3451b554df479b9ad1ab6dd397405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   HID Class                                                           */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_host_class_hid.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_hid_report_compress                  PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function will compress a client report into a report buffer.   */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hid                                   Pointer to HID class          */
/*    client_report                         Pointer to client report      */
/*    report_buffer                         Pointer to report buffer      */
/*    report_length                         Length of report              */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    HID Class                                                           */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_class_hid_report_compress(UX_HOST_CLASS_HID *hid, UX_HOST_CLASS_HID_CLIENT_REPORT *client_report,
                                                    UCHAR *report_buffer, ULONG report_length)
{

UX_HOST_CLASS_HID_REPORT    *hid_report;
UX_HOST_CLASS_HID_FIELD     *hid_field;
ULONG                       field_usage;
ULONG                       field_report_count;
ULONG                       field_report_size;
ULONG                       *client_buffer;
ULONG                       client_value;
UCHAR                       value;
ULONG                       data_offset_bit;
UCHAR                       is_valid_usage;

    UX_PARAMETER_NOT_USED(hid);
    UX_PARAMETER_NOT_USED(report_length);

    /* Get the report pointer from the caller.  */
    hid_report =  client_report -> ux_host_class_hid_client_report;

    /* Get the pointer to the user buffer.  */
    client_buffer =  client_report -> ux_host_class_hid_client_report_buffer;

    /* Get the first field associated with the report.  */
    hid_field =  hid_report -> ux_host_class_hid_report_field;

    /* Set data offset bit.  */
    data_offset_bit =  0;

    /* We need to compress each field defined in the report.  */
    while (hid_field != UX_NULL)
    {

        /* Each report field has a report count value. This count is used to extract
           values from the incoming report and build each usage/value instance.  */
        for (field_report_count = 0; field_report_count < hid_field -> ux_host_class_hid_field_report_count; field_report_count++)
        {

            /* Ensure the usage in the client buffer is valid. How we determine
               this depends on whether the field is VARIABLE or ARRAY.  */
            is_valid_usage =  UX_FALSE;
            if (hid_field -> ux_host_class_hid_field_value & UX_HOST_CLASS_HID_ITEM_VARIABLE)
            {

                /* Go through the usage array to try and find the client's usage.  */
                for (field_usage = 0; field_usage < hid_field -> ux_host_class_hid_field_number_usage; field_usage++)
                {

                    /* Is this a usage we've recorded?  */
                    if (*client_buffer == hid_field -> ux_host_class_hid_field_usages[field_usage])
                    {

                        /* Yes, this is a valid usage.  */
                        is_valid_usage =  UX_TRUE;
                        break;
                    }
                }
            }
            else
            {

                /* Is the usage page valid?  */
                if (((*client_buffer & 0xffff0000) >> 16) == hid_field -> ux_host_class_hid_field_usage_page)
                {

                    /* Is the usage in the min and max range?  */
                    if ((*client_buffer & 0xffff) >= hid_field -> ux_host_class_hid_field_usage_min &&
                        (*client_buffer & 0xffff) <= hid_field -> ux_host_class_hid_field_usage_max)
                    {

                        /* Yes, this is a valid usage.  */
                        is_valid_usage =  UX_TRUE;
                    }
                }
            }

            /* Is the usage invalid?  */
            if (is_valid_usage == UX_FALSE)
            {

                /* Error trap. */
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_REPORT_ERROR);

                /* If trace is enabled, insert this event into the trace buffer.  */
                UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_REPORT_ERROR, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)

                return(UX_HOST_CLASS_HID_REPORT_ERROR);
            }

            /* Skip the usage and point the buffer to the value.  */
            client_buffer++;

            /* Read the client value.  */
            client_value =  *client_buffer++;

            /* Build the value field in the report buffer bit by bit.  */
            for (field_report_size = hid_field -> ux_host_class_hid_field_report_size; field_report_size > 0; field_report_size--)
            {

                /* Isolate each bit from the report value.  */
                value =  (UCHAR) client_value & 1;

                /* Shift the isolated bit to its right space in the report byte.  */
                value =  (UCHAR)(value << data_offset_bit);

                /* Update the report with the bit value.  */
                *report_buffer |=  value;

                /* Move to next bit.  */
                data_offset_bit++;

                /* Are we on a byte boundary.  */
                if ((data_offset_bit & 7) == 0)
                {

                    /* If so increment the report address.  */
                    report_buffer++;

                    /* Reset offset bit.  */
                    data_offset_bit =  0;
                }

                /* Move to the next bit.  */
                client_value =  client_value >> 1;
            }
        }

        /* Move to the next field.  */
        hid_field =  hid_field -> ux_host_class_hid_field_next_field;
    }

    /* Return successful completion.  */
    return(UX_SUCCESS);
}