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