GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_hid_report_get.c Lines: 31 31 100.0 %
Date: 2024-12-12 17:16:36 Branches: 14 14 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
/**   Device HID Class                                                    */
18
/**                                                                       */
19
/**************************************************************************/
20
/**************************************************************************/
21
22
#define UX_SOURCE_CODE
23
24
25
/* Include necessary system files.  */
26
27
#include "ux_api.h"
28
#include "ux_device_class_hid.h"
29
#include "ux_device_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_device_class_hid_report_get                     PORTABLE C      */
37
/*                                                           6.3.0        */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function returns a report to the host.                         */
45
/*                                                                        */
46
/*  INPUT                                                                 */
47
/*                                                                        */
48
/*    hid                                   Pointer to hid instance       */
49
/*    descriptor_type                       Descriptor type               */
50
/*    descriptor_index                      Index of descriptor           */
51
/*    host_length                           Length requested by host      */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    Completion Status                                                   */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    _ux_device_class_hid_event_get        Get HID event                 */
60
/*    _ux_device_stack_transfer_request     Process transfer request      */
61
/*    _ux_utility_memory_set                Set memory                    */
62
/*    _ux_utility_memory_copy               Copy memory                   */
63
/*                                                                        */
64
/*  CALLED BY                                                             */
65
/*                                                                        */
66
/*    Device HID                                                          */
67
/*                                                                        */
68
/*  RELEASE HISTORY                                                       */
69
/*                                                                        */
70
/*    DATE              NAME                      DESCRIPTION             */
71
/*                                                                        */
72
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74
/*                                            verified memset and memcpy  */
75
/*                                            cases,                      */
76
/*                                            resulting in version 6.1    */
77
/*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
78
/*                                            resulting in version 6.3.0  */
79
/*                                                                        */
80
/**************************************************************************/
81
17
UINT  _ux_device_class_hid_report_get(UX_SLAVE_CLASS_HID *hid, ULONG descriptor_type,
82
                                            ULONG request_index, ULONG host_length)
83
{
84
85
UX_SLAVE_DEVICE                 *device;
86
UX_SLAVE_TRANSFER               *transfer_request;
87
UX_SLAVE_ENDPOINT               *endpoint;
88
UCHAR                           report_id;
89
UCHAR                           report_type;
90
UX_SLAVE_CLASS_HID_EVENT        hid_event;
91
ULONG                           hid_event_length;
92
UCHAR                           *buffer;
93
17
UINT                            status =  UX_ERROR;
94
95
    UX_PARAMETER_NOT_USED(descriptor_type);
96
    UX_PARAMETER_NOT_USED(request_index);
97
98
    /* If trace is enabled, insert this event into the trace buffer.  */
99
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_HID_REPORT_GET, hid, descriptor_type, request_index, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
100
101
    /* Get the pointer to the device.  */
102
17
    device =  &_ux_system_slave -> ux_system_slave_device;
103
104
    /* Get the control endpoint associated with the device.  */
105
17
    endpoint =  &device -> ux_slave_device_control_endpoint;
106
107
    /* Get the pointer to the transfer request associated with the endpoint.  */
108
17
    transfer_request =  &endpoint -> ux_slave_endpoint_transfer_request;
109
110
    /* Get report ID (wValue.lower) and report type (wValue.higher).  */
111
17
    report_id   = *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_VALUE + 0);
112
17
    report_type = *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_VALUE + 1);
113
114
    /* Set the direction to OUT.  */
115
17
    transfer_request -> ux_slave_transfer_request_phase =  UX_TRANSFER_PHASE_DATA_OUT;
116
117
    /* Prepare the event data payload from the hid event structure.  Get a pointer to the buffer area.  */
118
17
    buffer =  transfer_request -> ux_slave_transfer_request_data_pointer;
119
120
    /* Initialize event fields.  */
121
17
    hid_event.ux_device_class_hid_event_report_id   = report_id;
122
17
    hid_event.ux_device_class_hid_event_report_type = report_type;
123
17
    hid_event.ux_device_class_hid_event_length      = UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH;
124
125
    /* If it's input report without ID try to get it from event queue head.  */
126
17
    if (report_type == UX_DEVICE_CLASS_HID_REPORT_TYPE_INPUT &&
127
13
        hid -> ux_device_class_hid_report_id != UX_TRUE)
128
129
        /* Check if we have an event to report.  */
130
12
        status = _ux_device_class_hid_event_get(hid, &hid_event);
131
132
    /* Try to get event from application callback.  */
133
    else
134
    {
135
136
        /* Let application fill event.  */
137
5
        if (hid -> ux_device_class_hid_get_callback != UX_NULL)
138
3
            status = hid -> ux_device_class_hid_get_callback(hid, &hid_event);
139
    }
140
141
17
    if (status == UX_SUCCESS)
142
    {
143
144
        /* Get the length to send back to the host.  */
145
7
        if (host_length < hid_event.ux_device_class_hid_event_length)
146
2
            hid_event_length =  host_length;
147
        else
148
5
            hid_event_length =  hid_event.ux_device_class_hid_event_length;
149
7
        if (hid_event_length > UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH)
150
1
            hid_event_length = UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH;
151
152
        /* First reset it.  */
153
7
        _ux_utility_memory_set(buffer, 0, hid_event_length); /* Use case of memset is verified. */
154
155
        /* Copy the event buffer into the target buffer.  */
156
7
        _ux_utility_memory_copy(buffer, hid_event.ux_device_class_hid_event_buffer, hid_event_length); /* Use case of memcpy is verified. */
157
    }
158
    else
159
    {
160
161
        /* There's no event, so send back zero'd memory.  */
162
163
        /* Get the length to send back to the host.  */
164
10
        if (host_length < UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH)
165
9
            hid_event_length =  host_length;
166
        else
167
1
            hid_event_length =  UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH;
168
169
        /* Reset it.  */
170
10
        _ux_utility_memory_set(buffer, 0, hid_event_length); /* Use case of memset is verified. */
171
    }
172
173
    /* We can send the report.  */
174
17
    status =  _ux_device_stack_transfer_request(transfer_request, hid_event_length, host_length);
175
176
    /* Return the status to the caller.  */
177
17
    return(status);
178
}