GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_cdc_acm_capabilities_get.c Lines: 58 58 100.0 %
Date: 2026-03-06 18:57:10 Branches: 27 27 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
/**   CDC ACM 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_cdc_acm.h"
30
#include "ux_host_stack.h"
31
32
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _ux_host_class_cdc_acm_capabilities_get             PORTABLE C      */
38
/*                                                           6.2.1        */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function obtains the entire cdc_acm configuration descriptors. */
46
/*    This is needed because the cdc_acm class needs to know if commands  */
47
/*    are routed through the comm interface or the data class.            */
48
/*                                                                        */
49
/*  INPUT                                                                 */
50
/*                                                                        */
51
/*    cdc_acm                                 Pointer to cdc_acm class    */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    Completion Status                                                   */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    _ux_host_stack_transfer_request       Process transfer request      */
60
/*    _ux_utility_descriptor_parse          Parse descriptor              */
61
/*    _ux_utility_memory_allocate           Allocate memory block         */
62
/*    _ux_utility_memory_free               Release memory block          */
63
/*                                                                        */
64
/*  CALLED BY                                                             */
65
/*                                                                        */
66
/*    _ux_host_class_cdc_acm_activate                                     */
67
/*                                                                        */
68
/**************************************************************************/
69
80
UINT  _ux_host_class_cdc_acm_capabilities_get(UX_HOST_CLASS_CDC_ACM *cdc_acm)
70
{
71
72
UCHAR                       *descriptor;
73
UCHAR                       *saved_descriptor;
74
UX_ENDPOINT                 *control_endpoint;
75
UX_TRANSFER                 *transfer_request;
76
UX_CONFIGURATION            configuration;
77
UX_INTERFACE_DESCRIPTOR     interface_descriptor;
78
UINT                        status;
79
ULONG                       total_descriptor_length;
80
UCHAR                       descriptor_length;
81
UCHAR                       descriptor_type;
82
UCHAR                       descriptor_subtype;
83
ULONG                       interface_found;
84
85
    /* We need to get the default control endpoint transfer request pointer.  */
86
80
    control_endpoint =  &cdc_acm -> ux_host_class_cdc_acm_device -> ux_device_control_endpoint;
87
80
    transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
88
89
    /* Need to allocate memory for the descriptor.  */
90
80
    descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_CONFIGURATION_DESCRIPTOR_LENGTH);
91
80
    if (descriptor == UX_NULL)
92
1
        return(UX_MEMORY_INSUFFICIENT);
93
94
    /* Save this descriptor address since we need to free it. */
95
79
    saved_descriptor = descriptor;
96
97
    /* Create a transfer request for the GET_DESCRIPTOR request.  */
98
79
    transfer_request -> ux_transfer_request_data_pointer =      descriptor;
99
79
    transfer_request -> ux_transfer_request_requested_length =  UX_CONFIGURATION_DESCRIPTOR_LENGTH;
100
79
    transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
101
79
    transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
102
79
    transfer_request -> ux_transfer_request_value =             UX_CONFIGURATION_DESCRIPTOR_ITEM << 8;
103
79
    transfer_request -> ux_transfer_request_index =             0;
104
105
    /* Send request to HCD layer.  */
106
79
    status =  _ux_host_stack_transfer_request(transfer_request);
107
108
    /* Check for correct transfer and entire descriptor returned.  */
109

79
    if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == UX_CONFIGURATION_DESCRIPTOR_LENGTH))
110
    {
111
112
        /* The descriptor is in a packed format, parse it locally.  */
113
76
        _ux_utility_descriptor_parse(descriptor, _ux_system_configuration_descriptor_structure,
114
                                    UX_CONFIGURATION_DESCRIPTOR_ENTRIES, (UCHAR *) &configuration.ux_configuration_descriptor);
115
116
        /* Now we have the configuration descriptor which will tell us how many
117
           bytes there are in the entire descriptor.  */
118
76
        total_descriptor_length =  configuration.ux_configuration_descriptor.wTotalLength;
119
120
        /* Free the previous descriptor.  */
121
76
        _ux_utility_memory_free(descriptor);
122
123
        /* Allocate enough memory to read all descriptors attached
124
          to this configuration.  */
125
76
        descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, total_descriptor_length);
126
76
        if (descriptor == UX_NULL)
127
1
            return(UX_MEMORY_INSUFFICIENT);
128
129
        /* Save this descriptor address since we need to free it. */
130
75
        saved_descriptor = descriptor;
131
132
        /* Set the length we need to retrieve.  */
133
75
        transfer_request -> ux_transfer_request_requested_length =  total_descriptor_length;
134
135
        /* And reprogram the descriptor buffer address.  */
136
75
        transfer_request -> ux_transfer_request_data_pointer =  descriptor;
137
138
        /* Send request to HCD layer.  */
139
75
        status =  _ux_host_stack_transfer_request(transfer_request);
140
141
        /* Check for correct transfer and entire descriptor returned.  */
142

75
        if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == total_descriptor_length))
143
        {
144
145
            /* Default is Interface descriptor not yet found.  */
146
73
            interface_found =  UX_FALSE;
147
148
            /* Scan the descriptor for the CDC Comm interface.  */
149
946
            while (total_descriptor_length)
150
            {
151
152
                /* Gather the length, type and subtype of the descriptor.  */
153
875
                descriptor_length =   *descriptor;
154
875
                descriptor_type =     *(descriptor + 1);
155
875
                descriptor_subtype =  *(descriptor + 2);
156
157
                /* Make sure this descriptor has at least the minimum length.  */
158
875
                if (descriptor_length < 3)
159
                {
160
161
                    /* Error trap. */
162
1
                    _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
163
164
                    /* If trace is enabled, insert this event into the trace buffer.  */
165
                    UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
166
167
                    /* We can free the resource now.  */
168
1
                    _ux_utility_memory_free(saved_descriptor);
169
170
                    /* Descriptor is corrupted.  */
171
1
                    return(UX_DESCRIPTOR_CORRUPTED);
172
                }
173
174
                /* Process relative to descriptor type.  */
175
874
                switch (descriptor_type)
176
                {
177
178
179
159
                case UX_INTERFACE_DESCRIPTOR_ITEM:
180
181
                    /* Parse the interface descriptor and make it machine independent.  */
182
159
                    _ux_utility_descriptor_parse(descriptor, _ux_system_interface_descriptor_structure,
183
                                                    UX_INTERFACE_DESCRIPTOR_ENTRIES, (UCHAR *) &interface_descriptor);
184
185
                    /* Ensure we have the correct interface for CDC control (current interface).  */
186
159
                    if (interface_descriptor.bInterfaceNumber ==
187
159
                                cdc_acm -> ux_host_class_cdc_acm_interface ->
188
159
                                    ux_interface_descriptor.bInterfaceNumber)
189
                    {
190
191
                        /* Mark we have found it.  */
192
72
                        interface_found =  UX_TRUE;
193
194
                    }
195
                    else
196
                    {
197
198
                        /* Haven't found it.  */
199
87
                        interface_found =  UX_FALSE;
200
                    }
201
159
                    break;
202
203
204
319
                case UX_HOST_CLASS_CDC_ACM_CS_INTERFACE:
205
206
                    /* First make sure we have found the correct generic interface descriptor.  */
207

319
                    if ((interface_found == UX_TRUE) && (descriptor_subtype == UX_HOST_CLASS_CDC_ACM_CALL_MANAGEMENT_DESCRIPTOR))
208
                    {
209
210
                        /* Retrieve the bmCapabilities field which indicates how ACM commands are sent to the device.  */
211
70
                        cdc_acm -> ux_host_class_cdc_acm_capabilities  = *(descriptor + UX_HOST_CLASS_CDC_ACM_CALL_MANAGEMENT_CAPABILITIES);
212
213
214
                    }
215
319
                    break;
216
                }
217
218
                /* Verify if the descriptor is still valid.  */
219
874
                if (descriptor_length > total_descriptor_length)
220
                {
221
222
                    /* Error trap. */
223
1
                    _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
224
225
                    /* If trace is enabled, insert this event into the trace buffer.  */
226
                    UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
227
228
                    /* We can free the resource now.  */
229
1
                    _ux_utility_memory_free(saved_descriptor);
230
231
1
                    return(UX_DESCRIPTOR_CORRUPTED);
232
                }
233
234
                /* Jump to the next descriptor if we have not reached the end.  */
235
873
                descriptor +=  descriptor_length;
236
237
                /* And adjust the length left to parse in the descriptor.  */
238
873
                total_descriptor_length -=  descriptor_length;
239
            }
240
241
            /* We can free the resource now.  */
242
71
            _ux_utility_memory_free(saved_descriptor);
243
244
71
            return(UX_SUCCESS);
245
        }
246
    }
247
248
    /* Free all used resources.  */
249
5
    _ux_utility_memory_free(saved_descriptor);
250
251
    /* Return completion status.  */
252
5
    return(status);
253
}
254