summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_hid_keyboard_key_get.c
blob: 41e225b71cb60307c93dc1e23a45cbb70c49820a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   HID Remote Control Class                                            */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_host_class_hid.h"
#include "ux_host_class_hid_keyboard.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_hid_keyboard_key_get                 PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function reads the key and keyboard state from the             */
/*    round-robin buffer.                                                 */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    keyboard_instance                     Pointer to remote control     */
/*    keyboard key                          Pointer to keyboard key       */
/*    keyboard state                        Pointer to keyboard state     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_stack_class_instance_verify  Verify instance               */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    User application                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_class_hid_keyboard_key_get(UX_HOST_CLASS_HID_KEYBOARD *keyboard_instance,
                                            ULONG *keyboard_key, ULONG *keyboard_state)
{

ULONG               *array_head;
ULONG               *array_tail;
ULONG               *array_end;
ULONG               *array_start;
UX_HOST_CLASS_HID   *hid;

    /* Get the HID class associated with the HID client. */
    hid = keyboard_instance -> ux_host_class_hid_keyboard_hid;

    /* Ensure the instance is valid.  */
    if (_ux_host_stack_class_instance_verify(_ux_system_host_class_hid_name, (VOID *) hid) != UX_SUCCESS)
    {

        /* If trace is enabled, insert this event into the trace buffer.  */
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)

        return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
    }

    /* Load the keyboard key and state from the usage array .  */
    array_start =  keyboard_instance -> ux_host_class_hid_keyboard_usage_array;
    array_end =    array_start + UX_HOST_CLASS_HID_KEYBOARD_USAGE_ARRAY_LENGTH;
    array_head =   keyboard_instance -> ux_host_class_hid_keyboard_usage_array_head;
    array_tail =   keyboard_instance -> ux_host_class_hid_keyboard_usage_array_tail;

    /* We want to extract a usage/value from the circular queue of the keyboard instance.  */
    if (array_tail == array_head)
        return(UX_ERROR);

    /* Get the usage/value from the current tail.  */
    *keyboard_key =  *array_tail;
    *keyboard_state =  *(array_tail + 1);

    /* Now we need to update the tail value. Are we at the end of the array?  */
    if ((array_tail+2) >= array_end)
        array_tail =  array_start;
    else
        array_tail+=2;

    /* Set the tail pointer.  */
    keyboard_instance -> ux_host_class_hid_keyboard_usage_array_tail =  array_tail;

    /* The status will tell the application there is something valid in the key/state.  */
    return(UX_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _uxe_host_class_hid_keyboard_key_get                PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks errors in HID key get function call.           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    keyboard_instance                     Pointer to remote control     */
/*    keyboard key                          Pointer to keyboard key       */
/*    keyboard state                        Pointer to keyboard state     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Status                                                              */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_class_hid_keyboard_key_get   Get a keyboard key            */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/**************************************************************************/
UINT  _uxe_host_class_hid_keyboard_key_get(UX_HOST_CLASS_HID_KEYBOARD *keyboard_instance,
                                            ULONG *keyboard_key, ULONG *keyboard_state)
{

    /* Sanity checks.  */
    if ((keyboard_instance == UX_NULL) ||
        (keyboard_key == UX_NULL) || (keyboard_state == UX_NULL) ||
        (keyboard_key == keyboard_state))
        return(UX_INVALID_PARAMETER);

    /* Invoke key get function.  */
    return(_ux_host_class_hid_keyboard_key_get(keyboard_instance, keyboard_key, keyboard_state));
}