GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_cdc_acm_configure.c Lines: 19 19 100.0 %
Date: 2025-07-29 21:51:32 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
/**   CDC ACM 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_cdc_acm.h"
29
#include "ux_host_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_host_class_cdc_acm_configure                    PORTABLE C      */
37
/*                                                           6.1          */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function calls the USBX stack to do a SET_CONFIGURATION to the */
45
/*    cdc_acm. Once the cdc_acm is configured, its interface will be      */
46
/*    activated. The bulk endpoints (1 IN, 1 OUT ) and the optional       */
47
/*    interrupt endpoint are enumerated.                                  */
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_configuration_interface_get  Get interface           */
60
/*    _ux_host_stack_device_configuration_get     Get configuration       */
61
/*    _ux_host_stack_device_configuration_select  Select configuration    */
62
/*                                                                        */
63
/*  CALLED BY                                                             */
64
/*                                                                        */
65
/*    _ux_host_class_cdc_acm_activate        Data Pump class activate     */
66
/*                                                                        */
67
/*  RELEASE HISTORY                                                       */
68
/*                                                                        */
69
/*    DATE              NAME                      DESCRIPTION             */
70
/*                                                                        */
71
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73
/*                                            optimized based on compile  */
74
/*                                            definitions,                */
75
/*                                            resulting in version 6.1    */
76
/*                                                                        */
77
/**************************************************************************/
78
155
UINT  _ux_host_class_cdc_acm_configure(UX_HOST_CLASS_CDC_ACM *cdc_acm)
79
{
80
81
UINT                    status;
82
UX_CONFIGURATION        *configuration;
83
#if UX_MAX_DEVICES > 1
84
UX_DEVICE               *parent_device;
85
#endif
86
87
88
    /* If the device has been configured already, we don't need to do it
89
       again. */
90
155
    if (cdc_acm -> ux_host_class_cdc_acm_device -> ux_device_state == UX_DEVICE_CONFIGURED)
91
148
        return(UX_SUCCESS);
92
93
    /* A cdc_acm normally has one configuration. So retrieve the 1st configuration
94
       only.  */
95
7
    status =  _ux_host_stack_device_configuration_get(cdc_acm -> ux_host_class_cdc_acm_device, 0, &configuration);
96
7
    if (status != UX_SUCCESS)
97
    {
98
99
        /* Error trap. */
100
1
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
101
102
        /* If trace is enabled, insert this event into the trace buffer.  */
103
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, cdc_acm -> ux_host_class_cdc_acm_device, 0, 0, UX_TRACE_ERRORS, 0, 0)
104
105
1
        return(UX_CONFIGURATION_HANDLE_UNKNOWN);
106
    }
107
108
#if UX_MAX_DEVICES > 1
109
    /* Check the cdc_acm power source and check the parent power source for
110
       incompatible connections.  */
111
6
    if (cdc_acm -> ux_host_class_cdc_acm_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED)
112
    {
113
114
        /* Get parent device pointer.  */
115
3
        parent_device =  cdc_acm -> ux_host_class_cdc_acm_device -> ux_device_parent;
116
117
        /* If the device is NULL, the parent is the root cdc_acm and we don't have to worry
118
           if the parent is not the root cdc_acm, check for its power source.  */
119

3
        if ((parent_device != UX_NULL) && (parent_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED))
120
        {
121
122
            /* Error trap. */
123
1
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONNECTION_INCOMPATIBLE);
124
125
            /* If trace is enabled, insert this event into the trace buffer.  */
126
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONNECTION_INCOMPATIBLE, cdc_acm, 0, 0, UX_TRACE_ERRORS, 0, 0)
127
128
1
            return(UX_CONNECTION_INCOMPATIBLE);
129
        }
130
    }
131
#endif
132
133
    /* We have the valid configuration. Ask the USBX stack to set this configuration.  */
134
5
    status =  _ux_host_stack_device_configuration_select(configuration);
135
5
    if (status != UX_SUCCESS)
136
3
        return(status);
137
138
    /* If the operation went well, the cdc_acm default alternate setting for the cdc_acm interface is
139
       active and the interrupt endpoint is now enabled. We have to memorize the first interface since
140
       the interrupt endpoint is hooked to it. */
141
2
    status =  _ux_host_stack_configuration_interface_get(configuration, 0, 0, &cdc_acm -> ux_host_class_cdc_acm_interface);
142
2
    if (status != UX_SUCCESS)
143
    {
144
145
        /* Store the instance in the interface container, this is for the USB stack
146
           when it needs to invoke the class.  */
147
1
        cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_class_instance =  (VOID *) cdc_acm;
148
    }
149
150
    /* Return completion status.  */
151
2
    return(status);
152
}
153