GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_field_decompress.c Lines: 25 25 100.0 %
Date: 2024-12-12 17:16:36 Branches: 8 8 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_field_decompress                 PORTABLE C      */
37
/*                                                           6.3.0        */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function will decompress a field and return the usage/value.   */
45
/*                                                                        */
46
/*  INPUT                                                                 */
47
/*                                                                        */
48
/*    hid_field                             Pointer to HID field          */
49
/*    report_buffer                         Pointer to report buffer      */
50
/*    client_report                         Pointer to client report      */
51
/*                                                                        */
52
/*  OUTPUT                                                                */
53
/*                                                                        */
54
/*    Completion Status                                                   */
55
/*                                                                        */
56
/*  CALLS                                                                 */
57
/*                                                                        */
58
/*    None                                                                */
59
/*                                                                        */
60
/*  CALLED BY                                                             */
61
/*                                                                        */
62
/*    HID Class                                                           */
63
/*                                                                        */
64
/*  RELEASE HISTORY                                                       */
65
/*                                                                        */
66
/*    DATE              NAME                      DESCRIPTION             */
67
/*                                                                        */
68
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
69
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
70
/*                                            resulting in version 6.1    */
71
/*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
72
/*                                            improved usage handling,    */
73
/*                                            resulting in version 6.3.0  */
74
/*                                                                        */
75
/**************************************************************************/
76
3164
UINT  _ux_host_class_hid_field_decompress(UX_HOST_CLASS_HID_FIELD *hid_field, UCHAR *report_buffer, UX_HOST_CLASS_HID_CLIENT_REPORT *client_report)
77
{
78
79
ULONG       field_report_count;
80
ULONG       field_report_size;
81
ULONG       data_offset_byte;
82
ULONG       data_offset_bit;
83
ULONG       data_offset_bit_in_report;
84
ULONG       field_value;
85
ULONG       field_value_bit_shifting;
86
ULONG       field_usage;
87
ULONG       report_content;
88
89
90
    /* Calculate the address of the beginning of the field in the report.  */
91
3164
    data_offset_byte =  (hid_field -> ux_host_class_hid_field_report_offset >> 3);
92
93
    /* Calculate the bit start address. Hopefully things will be on a boundary but not necessary.  */
94
3164
    data_offset_bit =  hid_field -> ux_host_class_hid_field_report_offset & 7;
95
96
    /* Each report field has a report_count value. This count is used to extract values from the
97
       incoming report and build each usage/value instance.  */
98
16504
    for (field_report_count = 0; field_report_count < hid_field -> ux_host_class_hid_field_report_count; field_report_count++)
99
    {
100
101
        /* Get the report size in bits.  */
102
13340
        field_report_size =  hid_field -> ux_host_class_hid_field_report_size;
103
104
        /* We use the bit offset for this report and not the generic field bit offset.  */
105
13340
        data_offset_bit_in_report = data_offset_bit;
106
107
        /* Reset the local value.  */
108
13340
        field_value =  0;
109
110
        /* And start with bit 0 in the target value.  */
111
13340
        field_value_bit_shifting =  0;
112
113
        /* Build the value field bit by bit.  */
114
70705
        while (field_report_size-- != 0)
115
        {
116
117
            /* Read the content. This method is redundant if we are not changing byte but it
118
               makes the algorithm much easier.  */
119
57365
            report_content =  (ULONG) *(report_buffer + data_offset_byte + (data_offset_bit_in_report >> 3));
120
121
            /* Shift the current value content to allow space to store the new bit.  */
122
57365
            field_value |=  ((report_content >> (data_offset_bit_in_report & 7)) & 1) << field_value_bit_shifting;
123
124
            /* Move to next bit in the report.  */
125
57365
            data_offset_bit_in_report++;
126
127
            /* And the next bit in the value.  */
128
57365
            field_value_bit_shifting++;
129
        }
130
131
        /* The Usage value will depend if the data is defined as a variable or an array in the HID report.  */
132
13340
        if (hid_field -> ux_host_class_hid_field_value & UX_HOST_CLASS_HID_ITEM_VARIABLE)
133
        {
134
135
            /* Take the usage directly from the usage array (if specified).  */
136
8304
            if (hid_field -> ux_host_class_hid_field_usages)
137
7423
                field_usage =  *(hid_field -> ux_host_class_hid_field_usages + field_report_count);
138
            else
139
881
                field_usage = 0;
140
        }
141
        else
142
        {
143
144
            /* This is an array, so compute the usage from the min value, the report count and the
145
               computed report value.  */
146
5036
            field_usage =  hid_field -> ux_host_class_hid_field_usage_min + (ULONG)((SLONG)field_value - hid_field -> ux_host_class_hid_field_logical_min);
147
148
            /* Also add the usage page.  */
149
5036
            field_usage |=  (hid_field -> ux_host_class_hid_field_usage_page << 16);
150
        }
151
152
        /* Put the value and the usage into the caller's buffer.  */
153
13340
        *(client_report -> ux_host_class_hid_client_report_buffer + client_report -> ux_host_class_hid_client_report_actual_length) =  field_usage;
154
13340
        client_report -> ux_host_class_hid_client_report_actual_length++;
155
13340
        *(client_report -> ux_host_class_hid_client_report_buffer + client_report -> ux_host_class_hid_client_report_actual_length) =  field_value;
156
13340
        client_report -> ux_host_class_hid_client_report_actual_length++;
157
158
        /* Calculate the next address for this field.  */
159
13340
        data_offset_bit +=  hid_field -> ux_host_class_hid_field_report_size;
160
    }
161
162
    /* Return successful completion.  */
163
3164
    return(UX_SUCCESS);
164
}
165