GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_keyboard_entry.c Lines: 14 14 100.0 %
Date: 2024-12-12 17:16:36 Branches: 8 8 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 Keyboard Client                                                 */
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_class_hid_keyboard.h"
30
#include "ux_host_stack.h"
31
32
33
#if defined(UX_HOST_STANDALONE)
34
35
#define UX_HOST_CLASS_HID_KEYBOARD_ENUM_START           (UX_STATE_WAIT)
36
#define UX_HOST_CLASS_HID_KEYBOARD_ENUM_SET_REPORT      (UX_STATE_STACK_STEP + 0)
37
#define UX_HOST_CLASS_HID_KEYBOARD_ENUM_SET_IDLE        (UX_STATE_STACK_STEP + 1)
38
#define UX_HOST_CLASS_HID_KEYBOARD_ENUM_CMD_WAIT        (UX_STATE_STACK_STEP + 2)
39
#define UX_HOST_CLASS_HID_KEYBOARD_ENUM_PERIODIC_START  (UX_STATE_STACK_STEP + 3)
40
#define UX_HOST_CLASS_HID_KEYBOARD_ENUM_DONE            (UX_STATE_STACK_STEP + 4)
41
42
43
static inline UINT  _ux_host_class_hid_keyboard_activate_wait(UX_HOST_CLASS_HID_CLIENT_COMMAND *command);
44
#endif
45
46
47
/**************************************************************************/
48
/*                                                                        */
49
/*  FUNCTION                                               RELEASE        */
50
/*                                                                        */
51
/*    _ux_host_class_hid_keyboard_entry                   PORTABLE C      */
52
/*                                                           6.1.10       */
53
/*  AUTHOR                                                                */
54
/*                                                                        */
55
/*    Chaoqiong Xiao, Microsoft Corporation                               */
56
/*                                                                        */
57
/*  DESCRIPTION                                                           */
58
/*                                                                        */
59
/*    This function is the entry point of the HID keyboard client.        */
60
/*    This function is called by the HID class after it has parsed a new  */
61
/*    HID report descriptor and is searching for a HID client.            */
62
/*                                                                        */
63
/*  INPUT                                                                 */
64
/*                                                                        */
65
/*    command                               Pointer to command            */
66
/*                                                                        */
67
/*  OUTPUT                                                                */
68
/*                                                                        */
69
/*    Completion Status                                                   */
70
/*                                                                        */
71
/*  CALLS                                                                 */
72
/*                                                                        */
73
/*    _ux_host_class_hid_keyboard_activate                                */
74
/*                                              Activate HID keyboard     */
75
/*    _ux_host_class_hid_keyboard_deactivate                              */
76
/*                                              Deactivate HID keyboard   */
77
/*                                                                        */
78
/*  CALLED BY                                                             */
79
/*                                                                        */
80
/*    HID Class                                                           */
81
/*                                                                        */
82
/*  RELEASE HISTORY                                                       */
83
/*                                                                        */
84
/*    DATE              NAME                      DESCRIPTION             */
85
/*                                                                        */
86
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
87
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
88
/*                                            resulting in version 6.1    */
89
/*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
90
/*                                            added standalone support,   */
91
/*                                            resulting in version 6.1.10 */
92
/*                                                                        */
93
/**************************************************************************/
94
420
UINT  _ux_host_class_hid_keyboard_entry(UX_HOST_CLASS_HID_CLIENT_COMMAND *command)
95
{
96
97
UINT        status;
98
99
100
    /* The command request will tell us we need to do here, either a enumeration
101
       query, an activation or a deactivation.  */
102

420
    switch (command -> ux_host_class_hid_client_command_request)
103
    {
104
105
106
180
    case UX_HOST_CLASS_COMMAND_QUERY:
107
108
        /* The query command is used to let the HID class know if we want to own
109
           this device or not */
110
180
        if ((command -> ux_host_class_hid_client_command_page == UX_HOST_CLASS_HID_PAGE_GENERIC_DESKTOP_CONTROLS) &&
111
166
            (command -> ux_host_class_hid_client_command_usage == UX_HOST_CLASS_HID_GENERIC_DESKTOP_KEYBOARD))
112
163
            return(UX_SUCCESS);
113
        else
114
17
            return(UX_NO_CLASS_MATCH);
115
116
117
163
    case UX_HOST_CLASS_COMMAND_ACTIVATE:
118
119
        /* The activate command is used by the HID class to start the HID client.  */
120
163
        status =  _ux_host_class_hid_keyboard_activate(command);
121
122
        /* Return completion status.  */
123
163
        return(status);
124
125
126
#if defined(UX_HOST_STANDALONE)
127
    case UX_HOST_CLASS_COMMAND_ACTIVATE_WAIT:
128
129
        return(_ux_host_class_hid_keyboard_activate_wait(command));
130
#endif
131
132
133
76
    case UX_HOST_CLASS_COMMAND_DEACTIVATE:
134
135
        /* The deactivate command is used by the HID class when it received a deactivate
136
           command from the USBX stack and there was a HID client attached to the HID instance */
137
76
        status =  _ux_host_class_hid_keyboard_deactivate(command);
138
139
        /* Return completion status.  */
140
76
        return(status);
141
    }
142
143
    /* Return error status.  */
144
1
    return(UX_ERROR);
145
}
146
147
#if defined(UX_HOST_STANDALONE)
148
static inline UINT _ux_host_class_hid_keyboard_activate_wait(UX_HOST_CLASS_HID_CLIENT_COMMAND *command)
149
{
150
151
UX_HOST_CLASS_HID                       *hid;
152
UX_HOST_CLASS_HID_CLIENT                *hid_client;
153
UX_HOST_CLASS_HID_KEYBOARD              *keyboard;
154
UINT                                    status;
155
156
    /* Get the instance to the HID class.  */
157
    hid = command -> ux_host_class_hid_client_command_instance;
158
159
    /* And of the HID client.  */
160
    hid_client = hid -> ux_host_class_hid_client;
161
162
    /* And of the keyboard instance.  */
163
    keyboard = (UX_HOST_CLASS_HID_KEYBOARD *) hid_client -> ux_host_class_hid_client_local_instance;
164
165
    /* Run states.  */
166
    switch(keyboard -> ux_host_class_hid_keyboard_enum_state)
167
    {
168
    case UX_HOST_CLASS_HID_KEYBOARD_ENUM_START:
169
    /* Fall through.  */
170
    case UX_HOST_CLASS_HID_KEYBOARD_ENUM_SET_REPORT:
171
172
        /* Update LED in task.  */
173
        keyboard -> ux_host_class_hid_keyboard_out_state = UX_STATE_WAIT;
174
        keyboard -> ux_host_class_hid_keyboard_enum_state =
175
                                    UX_HOST_CLASS_HID_KEYBOARD_ENUM_CMD_WAIT;
176
        keyboard -> ux_host_class_hid_keyboard_next_state =
177
                                    UX_HOST_CLASS_HID_KEYBOARD_ENUM_SET_IDLE;
178
        return(UX_STATE_WAIT);
179
180
    case UX_HOST_CLASS_HID_KEYBOARD_ENUM_CMD_WAIT:
181
182
        /* LEDs processed in background task.  */
183
        if (keyboard -> ux_host_class_hid_keyboard_out_state != UX_STATE_WAIT)
184
        {
185
            if (keyboard -> ux_host_class_hid_keyboard_status != UX_SUCCESS)
186
                keyboard -> ux_host_class_hid_keyboard_enum_state =
187
                                        UX_HOST_CLASS_HID_KEYBOARD_ENUM_DONE;
188
            else
189
                keyboard -> ux_host_class_hid_keyboard_enum_state =
190
                            keyboard -> ux_host_class_hid_keyboard_next_state;
191
        }
192
        return(UX_STATE_WAIT);
193
194
    case UX_HOST_CLASS_HID_KEYBOARD_ENUM_SET_IDLE:
195
196
        /* Run SET_IDLE states.  */
197
        status = _ux_host_class_hid_idle_set_run(hid, 0,
198
                            keyboard -> ux_host_class_hid_keyboard_id);
199
        if (status < UX_STATE_WAIT)
200
        {
201
            keyboard -> ux_host_class_hid_keyboard_status =
202
                                hid -> ux_host_class_hid_status;
203
204
            /* Set_Idle is mandatory, if there is error enum fail.  */
205
            if (keyboard -> ux_host_class_hid_keyboard_status == UX_SUCCESS)
206
                keyboard -> ux_host_class_hid_keyboard_enum_state =
207
                                UX_HOST_CLASS_HID_KEYBOARD_ENUM_PERIODIC_START;
208
            else
209
                keyboard -> ux_host_class_hid_keyboard_enum_state =
210
                                UX_HOST_CLASS_HID_KEYBOARD_ENUM_DONE;
211
212
        }
213
        return(UX_STATE_WAIT);
214
215
    case UX_HOST_CLASS_HID_KEYBOARD_ENUM_PERIODIC_START:
216
217
        /* Start the periodic report.  */
218
        status =  _ux_host_class_hid_periodic_report_start(hid);
219
        keyboard -> ux_host_class_hid_keyboard_status = status;
220
221
        /* Fall through.  */
222
    case UX_HOST_CLASS_HID_KEYBOARD_ENUM_DONE:
223
224
        /* Anything failed, Free resources.  */
225
        if (keyboard -> ux_host_class_hid_keyboard_status != UX_SUCCESS)
226
        {
227
228
            /* Detach instance.  */
229
            hid_client -> ux_host_class_hid_client_local_instance = UX_NULL;
230
231
            /* Free usage state.  */
232
            if (keyboard -> ux_host_class_hid_keyboard_key_state)
233
                _ux_utility_memory_free(keyboard -> ux_host_class_hid_keyboard_key_state);
234
235
            /* Free usage array.  */
236
            if (keyboard -> ux_host_class_hid_keyboard_usage_array)
237
                _ux_utility_memory_free(keyboard -> ux_host_class_hid_keyboard_usage_array);
238
239
            /* Free instance.  */
240
            _ux_utility_memory_free(keyboard);
241
242
            return(UX_STATE_ERROR);
243
        }
244
245
        /* Now keyboard instance is live.  */
246
        keyboard -> ux_host_class_hid_keyboard_state =  UX_HOST_CLASS_INSTANCE_LIVE;
247
248
        /* If all is fine and the device is mounted, we may need to inform the application
249
            if a function has been programmed in the system structure.  */
250
        if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
251
        {
252
253
            /* Call system change function.  */
254
            _ux_system_host ->  ux_system_host_change_function(UX_HID_CLIENT_INSERTION, hid -> ux_host_class_hid_class, (VOID *) hid_client);
255
        }
256
257
        /* If trace is enabled, insert this event into the trace buffer.  */
258
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_KEYBOARD_ACTIVATE, hid, keyboard, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
259
260
        /* Enumeration idle.  */
261
        keyboard -> ux_host_class_hid_keyboard_enum_state = UX_STATE_IDLE;
262
        return(UX_STATE_NEXT);
263
264
    default: /* IDLE, Other states.  */
265
        return(UX_STATE_NEXT);
266
    }
267
}
268
#endif