1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   HID Remote Control Class                                            */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_host_class_hid.h"
30 #include "ux_host_class_hid_keyboard.h"
31 #include "ux_host_stack.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _ux_host_class_hid_keyboard_key_get                 PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Chaoqiong Xiao, Microsoft Corporation                               */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function reads the key and keyboard state from the             */
47 /*    round-robin buffer.                                                 */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    keyboard_instance                     Pointer to remote control     */
52 /*    keyboard key                          Pointer to keyboard key       */
53 /*    keyboard state                        Pointer to keyboard state     */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_host_stack_class_instance_verify  Verify instance               */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    User application                                                    */
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 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_ux_host_class_hid_keyboard_key_get(UX_HOST_CLASS_HID_KEYBOARD * keyboard_instance,ULONG * keyboard_key,ULONG * keyboard_state)76 UINT  _ux_host_class_hid_keyboard_key_get(UX_HOST_CLASS_HID_KEYBOARD *keyboard_instance,
77                                             ULONG *keyboard_key, ULONG *keyboard_state)
78 {
79 
80 ULONG               *array_head;
81 ULONG               *array_tail;
82 ULONG               *array_end;
83 ULONG               *array_start;
84 UX_HOST_CLASS_HID   *hid;
85 
86     /* Get the HID class associated with the HID client. */
87     hid = keyboard_instance -> ux_host_class_hid_keyboard_hid;
88 
89     /* Ensure the instance is valid.  */
90     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_hid_name, (VOID *) hid) != UX_SUCCESS)
91     {
92 
93         /* If trace is enabled, insert this event into the trace buffer.  */
94         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
95 
96         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
97     }
98 
99     /* Load the keyboard key and state from the usage array .  */
100     array_start =  keyboard_instance -> ux_host_class_hid_keyboard_usage_array;
101     array_end =    array_start + UX_HOST_CLASS_HID_KEYBOARD_USAGE_ARRAY_LENGTH;
102     array_head =   keyboard_instance -> ux_host_class_hid_keyboard_usage_array_head;
103     array_tail =   keyboard_instance -> ux_host_class_hid_keyboard_usage_array_tail;
104 
105     /* We want to extract a usage/value from the circular queue of the keyboard instance.  */
106     if (array_tail == array_head)
107         return(UX_ERROR);
108 
109     /* Get the usage/value from the current tail.  */
110     *keyboard_key =  *array_tail;
111     *keyboard_state =  *(array_tail + 1);
112 
113     /* Now we need to update the tail value. Are we at the end of the array?  */
114     if ((array_tail+2) >= array_end)
115         array_tail =  array_start;
116     else
117         array_tail+=2;
118 
119     /* Set the tail pointer.  */
120     keyboard_instance -> ux_host_class_hid_keyboard_usage_array_tail =  array_tail;
121 
122     /* The status will tell the application there is something valid in the key/state.  */
123     return(UX_SUCCESS);
124 }
125 
126