GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_keyboard_thread.c Lines: 16 16 100.0 %
Date: 2026-03-06 18:57:10 Branches: 4 4 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
/**   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
/**************************************************************************/
35
/*                                                                        */
36
/*  FUNCTION                                               RELEASE        */
37
/*                                                                        */
38
/*    _ux_host_class_hid_keyboard_thread                  PORTABLE C      */
39
/*                                                           6.1.10       */
40
/*  AUTHOR                                                                */
41
/*                                                                        */
42
/*    Chaoqiong Xiao, Microsoft Corporation                               */
43
/*                                                                        */
44
/*  DESCRIPTION                                                           */
45
/*                                                                        */
46
/*    This file contains the keyboard thread used to process the changes  */
47
/*    in the keyboard LEDs.                                               */
48
/*                                                                        */
49
/*    It's for RTOS mode.                                                 */
50
/*                                                                        */
51
/*  INPUT                                                                 */
52
/*                                                                        */
53
/*    keyboard_instance                     The keyboard instance for     */
54
/*                                          there was a change in LEDs    */
55
/*                                                                        */
56
/*  OUTPUT                                                                */
57
/*                                                                        */
58
/*    None                                                                */
59
/*                                                                        */
60
/*  CALLS                                                                 */
61
/*                                                                        */
62
/*    _ux_host_semaphore_get                Get signal semaphore          */
63
/*    _ux_host_class_hid_report_id_get      Get report ID                 */
64
/*    _ux_host_class_hid_report_set         Do SET_REPORT                 */
65
/*                                                                        */
66
/*  CALLED BY                                                             */
67
/*                                                                        */
68
/*    ThreadX                                                             */
69
/*                                                                        */
70
/**************************************************************************/
71
149
VOID  _ux_host_class_hid_keyboard_thread(ULONG thread_input)
72
{
73
UX_HOST_CLASS_HID                       *hid;
74
UX_HOST_CLASS_HID_CLIENT_REPORT         client_report;
75
UX_HOST_CLASS_HID_REPORT_GET_ID         report_id;
76
UINT                                    status;
77
UX_HOST_CLASS_HID_KEYBOARD              *keyboard_instance;
78
79
    /* Cast the thread_input variable into the proper format.  */
80
149
    UX_THREAD_EXTENSION_PTR_GET(keyboard_instance, UX_HOST_CLASS_HID_KEYBOARD, thread_input)
81
82
    /* Loop forever waiting for changes signaled through the semaphore. */
83
    while (1)
84
    {
85
86
        /* Wait for the semaphore to be put by the root hub or a regular hub.  */
87
159
        status =  _ux_host_semaphore_get(&keyboard_instance -> ux_host_class_hid_keyboard_semaphore, UX_WAIT_FOREVER);
88
89
        /* The semaphore could be awaken because the semaphore was destroyed by the HID client
90
           when the keyboard is removed.  */
91
11
        if (status == UX_SUCCESS)
92
        {
93
94
            /* We got awaken by the keyboard callback which indicates a change on the LEDs has to happen.
95
               We need to build the field for the leds. */
96
10
            keyboard_instance -> ux_host_class_hid_keyboard_led_mask =  keyboard_instance ->  ux_host_class_hid_keyboard_alternate_key_state &
97
                                                                        UX_HID_KEYBOARD_STATE_MASK_LOCK;
98
99
            /* Recall the HID instance for this client.  */
100
10
            hid =  keyboard_instance -> ux_host_class_hid_keyboard_hid;
101
102
            /* We need to find the OUTPUT report for the keyboard LEDs.  */
103
10
            report_id.ux_host_class_hid_report_get_report = UX_NULL;
104
10
            report_id.ux_host_class_hid_report_get_type = UX_HOST_CLASS_HID_REPORT_TYPE_OUTPUT;
105
10
            status =  _ux_host_class_hid_report_id_get(hid, &report_id);
106
107
            /* The report ID should exist.  If there is an error, we do not proceed.  */
108
10
            if (status == UX_SUCCESS)
109
            {
110
111
                /* Memorize the report pointer.  */
112
9
                client_report.ux_host_class_hid_client_report =  report_id.ux_host_class_hid_report_get_report;
113
114
                /* The report set is raw since the LEDs mask is already in the right format.  */
115
9
                client_report.ux_host_class_hid_client_report_flags = UX_HOST_CLASS_HID_REPORT_RAW;
116
117
                /* The length of this report is 1 byte.  */
118
9
                client_report.ux_host_class_hid_client_report_length = 1;
119
120
                /* The output report buffer is the LED mask field.  */
121
9
                client_report.ux_host_class_hid_client_report_buffer = &keyboard_instance -> ux_host_class_hid_keyboard_led_mask;
122
123
                /* The HID class will perform the SET_REPORT command.  */
124
9
                _ux_host_class_hid_report_set(hid, &client_report);
125
            }
126
        }
127
        else
128
129
            /* The thread should terminate upon a semaphore error.  */
130
1
            return;
131
    }
132
}
133