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 Keyboard Client                                                 */
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 #if defined(UX_HOST_STANDALONE)
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _ux_host_class_hid_keyboard_tasks_run               PORTABLE C      */
40 /*                                                           6.2.0        */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    Chaoqiong Xiao, Microsoft Corporation                               */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This file contains the keyboard tasks used to process the changes   */
48 /*    in the keyboard LEDs.                                               */
49 /*                                                                        */
50 /*    It's for standalone mode.                                           */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    client                                The keyboard client           */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    None                                                                */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_host_class_hid_report_id_get      Get report ID                 */
63 /*    _ux_host_class_hid_report_set         Do SET_REPORT                 */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    USBX Host HID                                                       */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  01-31-2022     Chaoqiong Xiao           Initial Version 6.1.10        */
74 /*  10-31-2022     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            improved HID OUTPUT report  */
76 /*                                            handling in standalone mode,*/
77 /*                                            resulting in version 6.2.0  */
78 /*                                                                        */
79 /**************************************************************************/
_ux_host_class_hid_keyboard_tasks_run(UX_HOST_CLASS_HID_CLIENT * client)80 VOID  _ux_host_class_hid_keyboard_tasks_run(UX_HOST_CLASS_HID_CLIENT *client)
81 {
82 
83 UX_HOST_CLASS_HID_KEYBOARD              *keyboard;
84 UX_HOST_CLASS_HID                       *hid;
85 UX_HOST_CLASS_HID_CLIENT_REPORT         client_report;
86 UX_HOST_CLASS_HID_REPORT_GET_ID         report_id;
87 UINT                                    status;
88 
89 
90     /* Sanity check.  */
91     if (client == UX_NULL)
92         return;
93 
94     /* Get keyboard instance.  */
95     keyboard = (UX_HOST_CLASS_HID_KEYBOARD *)client -> ux_host_class_hid_client_local_instance;
96     if (keyboard == UX_NULL)
97         return;
98 
99     /* Get HID instance.  */
100     hid = keyboard -> ux_host_class_hid_keyboard_hid;
101     if (hid == UX_NULL)
102         return;
103 
104     /* Check if there is pending chnages.  */
105     if (keyboard -> ux_host_class_hid_keyboard_out_state != UX_STATE_WAIT)
106         return;
107 
108     /* We got awaken by the keyboard callback which indicates a change on
109         the LEDs has to happen. We need to build the field for the LEDs. */
110     keyboard -> ux_host_class_hid_keyboard_led_mask =
111                 keyboard ->  ux_host_class_hid_keyboard_alternate_key_state &
112                                             UX_HID_KEYBOARD_STATE_MASK_LOCK;
113 
114     /* We need to find the OUTPUT report for the keyboard LEDs.  */
115     if (keyboard -> ux_host_class_hid_keyboard_out_report == UX_NULL)
116     {
117 
118         report_id.ux_host_class_hid_report_get_report = UX_NULL;
119         report_id.ux_host_class_hid_report_get_type = UX_HOST_CLASS_HID_REPORT_TYPE_OUTPUT;
120         status = _ux_host_class_hid_report_id_get(hid, &report_id);
121         if (status != UX_SUCCESS)
122         {
123             keyboard -> ux_host_class_hid_keyboard_status = status;
124             return;
125         }
126         keyboard -> ux_host_class_hid_keyboard_out_report = report_id.ux_host_class_hid_report_get_report;
127     }
128 
129     /* Build a RAW client report.  */
130     client_report.ux_host_class_hid_client_report = keyboard -> ux_host_class_hid_keyboard_out_report;
131     client_report.ux_host_class_hid_client_report_flags = UX_HOST_CLASS_HID_REPORT_RAW;
132     client_report.ux_host_class_hid_client_report_length = 1;
133     client_report.ux_host_class_hid_client_report_buffer = &keyboard -> ux_host_class_hid_keyboard_led_mask;
134 
135     /* The HID class will perform the SET_REPORT command.  */
136     status = _ux_host_class_hid_report_set_run(hid, &client_report);
137     if (status < UX_STATE_WAIT)
138     {
139 
140         /* Update status.  */
141         keyboard -> ux_host_class_hid_keyboard_status = hid -> ux_host_class_hid_status;
142 
143         /* The change is processed.  */
144         keyboard -> ux_host_class_hid_keyboard_out_state = UX_STATE_IDLE;
145     }
146 }
147 #endif
148