GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_configure.c Lines: 16 16 100.0 %
Date: 2024-12-12 17:16:36 Branches: 10 10 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_configure                        PORTABLE C      */
37
/*                                                           6.1.11       */
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
/*    HID. Once the HID is configured, its interface will be activated    */
46
/*    and all the endpoints enumerated (1 interrupt endpoint in the case  */
47
/*    of the HID).                                                        */
48
/*                                                                        */
49
/*  INPUT                                                                 */
50
/*                                                                        */
51
/*    hid                                   Pointer to HID 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
/*    HID Class                                                           */
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
/*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
77
/*                                            internal clean up,          */
78
/*                                            resulting in version 6.1.11 */
79
/*                                                                        */
80
/**************************************************************************/
81
502
UINT  _ux_host_class_hid_configure(UX_HOST_CLASS_HID *hid)
82
{
83
84
UINT                    status;
85
UX_CONFIGURATION        *configuration;
86
#if UX_MAX_DEVICES > 1
87
UX_DEVICE               *device;
88
UX_DEVICE               *parent_device;
89
#endif
90
91
92
    /* If the device has been configured already, we don't need to do it again. */
93
502
    if (hid -> ux_host_class_hid_device -> ux_device_state == UX_DEVICE_CONFIGURED)
94
497
        return(UX_SUCCESS);
95
96
    /* A HID normally has one configuration. So retrieve the 1st configuration only.  */
97
5
    status =  _ux_host_stack_device_configuration_get(hid -> ux_host_class_hid_device, 0, &configuration);
98
5
    if (status != UX_SUCCESS)
99
    {
100
101
        /* Error trap. */
102
1
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
103
104
        /* If trace is enabled, insert this event into the trace buffer.  */
105
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, hid -> ux_host_class_hid_device, 0, 0, UX_TRACE_ERRORS, 0, 0)
106
107
1
        return(UX_CONFIGURATION_HANDLE_UNKNOWN);
108
    }
109
110
#if UX_MAX_DEVICES > 1
111
    /* Get the device container for this configuration.  */
112
4
    device =  configuration -> ux_configuration_device;
113
114
    /* Get the parent container for this device.  */
115
4
    parent_device =  device -> ux_device_parent;
116
117
    /* Check the HID power source and check the parent power source for
118
       incompatible connections.  */
119
4
    if (hid -> ux_host_class_hid_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED)
120
    {
121
122
        /* If the device is NULL, the parent is the root hid and we don't have to worry.
123
           If the parent is not the root HID, check for its power source.  */
124

3
        if ((parent_device != UX_NULL) && (parent_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED))
125
        {
126
127
            /* Error trap. */
128
1
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONNECTION_INCOMPATIBLE);
129
130
            /* If trace is enabled, insert this event into the trace buffer.  */
131
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONNECTION_INCOMPATIBLE, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
132
133
1
            return(UX_CONNECTION_INCOMPATIBLE);
134
        }
135
    }
136
#endif
137
138
    /* We have the valid configuration. Ask the USBX stack to set this configuration.  */
139
3
    _ux_host_stack_device_configuration_select(configuration);
140
141
    /* If the operation went well, the hid default alternate setting for the HID interface is active
142
       and the interrupt endpoint is now enabled. We have to memorize the first interface since the
143
       interrupt endpoint is hooked to it. */
144
3
    status =  _ux_host_stack_configuration_interface_get(configuration, 0, 0, &hid -> ux_host_class_hid_interface);
145
146
    /* Return completion status.  */
147
3
    return(status);
148
}
149