GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_hid_activate.c Lines: 23 23 100.0 %
Date: 2026-03-06 18:57:10 Branches: 10 10 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
/**   Device HID Class                                                    */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
#define UX_SOURCE_CODE
24
25
26
/* Include necessary system files.  */
27
28
#include "ux_api.h"
29
#include "ux_device_class_hid.h"
30
#include "ux_device_stack.h"
31
32
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _ux_device_class_hid_activate                       PORTABLE C      */
38
/*                                                           6.3.0        */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function activates an instance of a HID device.                */
46
/*                                                                        */
47
/*  INPUT                                                                 */
48
/*                                                                        */
49
/*    command                              Pointer to hid command         */
50
/*                                                                        */
51
/*  OUTPUT                                                                */
52
/*                                                                        */
53
/*    Completion Status                                                   */
54
/*                                                                        */
55
/*  CALLS                                                                 */
56
/*                                                                        */
57
/*    _ux_device_thread_resume              Resume thread                 */
58
/*                                                                        */
59
/*  CALLED BY                                                             */
60
/*                                                                        */
61
/*    USBX Source Code                                                    */
62
/*                                                                        */
63
/**************************************************************************/
64
690
UINT  _ux_device_class_hid_activate(UX_SLAVE_CLASS_COMMAND *command)
65
{
66
67
UX_SLAVE_INTERFACE                      *interface_ptr;
68
UX_SLAVE_CLASS                          *class_ptr;
69
UX_SLAVE_CLASS_HID                      *hid;
70
UX_SLAVE_ENDPOINT                       *endpoint_interrupt;
71
690
UX_SLAVE_ENDPOINT                       *endpoint_in = UX_NULL;
72
#if defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
73
UX_SLAVE_ENDPOINT                       *endpoint_out = UX_NULL;
74
UCHAR                                   *pos;
75
#endif
76
77
    /* Get the class container.  */
78
690
    class_ptr =  command -> ux_slave_class_command_class_ptr;
79
80
    /* Get the class instance in the container.  */
81
690
    hid =  (UX_SLAVE_CLASS_HID *) class_ptr -> ux_slave_class_instance;
82
83
    /* Get the interface that owns this instance.  */
84
690
    interface_ptr =  (UX_SLAVE_INTERFACE  *) command -> ux_slave_class_command_interface;
85
86
    /* Store the class instance into the interface.  */
87
690
    interface_ptr -> ux_slave_interface_class_instance =  (VOID *)hid;
88
89
    /* Now the opposite, store the interface in the class instance.  */
90
690
    hid -> ux_slave_class_hid_interface =  interface_ptr;
91
92
    /* Locate the endpoints.  */
93
690
    endpoint_interrupt =  interface_ptr -> ux_slave_interface_first_endpoint;
94
95
    /* Check if interrupt IN endpoint exists.  */
96
701
    while (endpoint_interrupt != UX_NULL)
97
    {
98
691
        if ((endpoint_interrupt -> ux_slave_endpoint_descriptor.bmAttributes &
99
             UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT)
100
        {
101
684
            if ((endpoint_interrupt -> ux_slave_endpoint_descriptor.bEndpointAddress &
102
684
                 UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN)
103
            {
104
105
                /* It's interrupt IN endpoint we need.  */
106
680
                endpoint_in = endpoint_interrupt;
107
108
#if defined(UX_DEVICE_CLASS_HID_OWN_ENDPOINT_BUFFER)
109
110
                /* Set endpoint buffer owner to class instance.  */
111
                endpoint_in -> ux_slave_endpoint_transfer_request.
112
                        ux_slave_transfer_request_data_pointer =
113
                                UX_DEVICE_CLASS_HID_INTERRUPTIN_BUFFER(hid);
114
#endif
115
116
#if defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
117
                if (endpoint_out != UX_NULL)
118
#endif
119
680
                    break;
120
            }
121
#if defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
122
            else
123
            {
124
125
                /* It's optional interrupt OUT endpoint.  */
126
                endpoint_out = endpoint_interrupt;
127
128
#if defined(UX_DEVICE_CLASS_HID_OWN_ENDPOINT_BUFFER)
129
130
                /* Set endpoint buffer owner to class instance.  */
131
                endpoint_out -> ux_slave_endpoint_transfer_request.
132
                        ux_slave_transfer_request_data_pointer =
133
                                UX_DEVICE_CLASS_HID_INTERRUPTOUT_BUFFER(hid);
134
#endif
135
136
                if (endpoint_in != UX_NULL)
137
                    break;
138
            }
139
#endif
140
        }
141
142
        /* Try next endpoint.  */
143
11
        endpoint_interrupt =  endpoint_interrupt -> ux_slave_endpoint_next_endpoint;
144
    }
145
146
    /* Check if we found right endpoint.  */
147
690
    if (endpoint_in == UX_NULL)
148
10
        return (UX_ERROR);
149
150
    /* Validate event buffer size (fits largest transfer payload size).  */
151
    UX_ASSERT(UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH >=
152
              endpoint_in -> ux_slave_endpoint_transfer_request.
153
                            ux_slave_transfer_request_transfer_length);
154
155
    /* Default HID protocol is report protocol.  */
156
680
    hid -> ux_device_class_hid_protocol = UX_DEVICE_CLASS_HID_PROTOCOL_REPORT;
157
158
    /* Save the endpoints in the hid instance.  */
159
680
    hid -> ux_device_class_hid_interrupt_endpoint         = endpoint_in;
160
161
#if defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
162
163
    /* Save endpoint OUT.  */
164
    hid -> ux_device_class_hid_read_endpoint              = endpoint_out;
165
166
    /* Resume receiver thread/task (if present).  */
167
    if (hid -> ux_device_class_hid_receiver && endpoint_out)
168
    {
169
170
        /* Reset events.  */
171
        hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_event_save_pos =
172
            hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_events;
173
        hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_event_read_pos =
174
            hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_events;
175
        for (pos = (UCHAR*)hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_events;
176
             pos < (UCHAR*)hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_events_end;
177
             pos += UX_DEVICE_CLASS_HID_RECEIVED_QUEUE_ITEM_SIZE(hid -> ux_device_class_hid_receiver))
178
        {
179
            ((UX_DEVICE_CLASS_HID_RECEIVED_EVENT*)pos) -> ux_device_class_hid_received_event_length = 0;
180
        }
181
182
#if !defined(UX_DEVICE_STANDALONE)
183
184
        /* Resume thread.  */
185
        _ux_utility_thread_resume(&hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_thread);
186
#else
187
188
        /* Setup read state for receiver.  */
189
        hid -> ux_device_class_hid_read_state = UX_DEVICE_CLASS_HID_RECEIVER_START;
190
#endif
191
    }
192
#endif
193
194
#if !defined(UX_DEVICE_STANDALONE)
195
196
    /* Resume thread.  */
197
680
    _ux_device_thread_resume(&class_ptr -> ux_slave_class_thread);
198
#else
199
200
    /* Reset event buffered for background transfer.  */
201
    _ux_utility_memory_set((VOID *)&hid -> ux_device_class_hid_event, 0,
202
                                            sizeof(UX_DEVICE_CLASS_HID_EVENT)); /* Use case of memset is verified. */
203
    hid -> ux_device_class_hid_event.ux_device_class_hid_event_length =
204
                    endpoint_in -> ux_slave_endpoint_transfer_request.
205
                                    ux_slave_transfer_request_transfer_length;
206
207
    /* Reset event sending state.  */
208
    hid -> ux_device_class_hid_event_state = UX_STATE_RESET;
209
#endif
210
211
212
    /* If there is a activate function call it.  */
213
680
    if (hid -> ux_slave_class_hid_instance_activate != UX_NULL)
214
    {
215
216
        /* Invoke the application.  */
217
509
        hid -> ux_slave_class_hid_instance_activate(hid);
218
    }
219
220
    /* If trace is enabled, insert this event into the trace buffer.  */
221
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_HID_ACTIVATE, hid, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
222
223
    /* If trace is enabled, register this object.  */
224
    UX_TRACE_OBJECT_REGISTER(UX_TRACE_DEVICE_OBJECT_TYPE_INTERFACE, hid, 0, 0, 0)
225
226
    /* Return completion status.  */
227
680
    return(UX_SUCCESS);
228
}