GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_remote_control_activate.c Lines: 36 36 100.0 %
Date: 2026-03-06 18:57:10 Branches: 14 14 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 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_remote_control.h"
31
#include "ux_host_stack.h"
32
33
34
/**************************************************************************/
35
/*                                                                        */
36
/*  FUNCTION                                               RELEASE        */
37
/*                                                                        */
38
/*    _ux_host_class_hid_remote_control_activate          PORTABLE C      */
39
/*                                                           6.1.11       */
40
/*  AUTHOR                                                                */
41
/*                                                                        */
42
/*    Chaoqiong Xiao, Microsoft Corporation                               */
43
/*                                                                        */
44
/*  DESCRIPTION                                                           */
45
/*                                                                        */
46
/*    This function performs the enumeration of a HID Remote Control      */
47
/*    class.                                                              */
48
/*                                                                        */
49
/*  INPUT                                                                 */
50
/*                                                                        */
51
/*    command                               Pointer to command            */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    Completion Status                                                   */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    _ux_host_class_hid_periodic_report_start    Start periodic report   */
60
/*    _ux_host_class_hid_report_callback_register Register callback       */
61
/*    _ux_utility_memory_allocate           Allocate memory block         */
62
/*                                                                        */
63
/*  CALLED BY                                                             */
64
/*                                                                        */
65
/*    HID Remote Control Class                                            */
66
/*                                                                        */
67
/**************************************************************************/
68
22
UINT  _ux_host_class_hid_remote_control_activate(UX_HOST_CLASS_HID_CLIENT_COMMAND *command)
69
{
70
71
UX_HOST_CLASS_HID_REPORT_CALLBACK        call_back;
72
UX_HOST_CLASS_HID                        *hid;
73
UX_HOST_CLASS_HID_CLIENT                 *hid_client;
74
UX_HOST_CLASS_HID_CLIENT_REMOTE_CONTROL  *client_remote_control;
75
UX_HOST_CLASS_HID_REMOTE_CONTROL         *remote_control_instance;
76
22
UINT                                     status = UX_SUCCESS;
77
78
79
    /* Get the instance to the HID class.  */
80
22
    hid =  command -> ux_host_class_hid_client_command_instance;
81
82
    /* Get some memory for both the HID class instance and copy of this client
83
       and for the callback.  */
84
    client_remote_control =  (UX_HOST_CLASS_HID_CLIENT_REMOTE_CONTROL *)
85
22
                    _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY,
86
                                sizeof(UX_HOST_CLASS_HID_CLIENT_REMOTE_CONTROL));
87
22
    if (client_remote_control == UX_NULL)
88
4
        return(UX_MEMORY_INSUFFICIENT);
89
90
    /* Get client instance and client copy.  */
91
18
    remote_control_instance = &client_remote_control -> ux_host_class_hid_client_remote_control_remote_control;
92
18
    hid_client = &client_remote_control -> ux_host_class_hid_client_remote_control_client;
93
18
    _ux_utility_memory_copy(hid_client, hid -> ux_host_class_hid_client, sizeof(UX_HOST_CLASS_HID_CLIENT)); /* Use case of memcpy is verified. */
94
95
    /* Attach the remote control instance to the client instance.  */
96
18
    hid_client -> ux_host_class_hid_client_local_instance =  (VOID *) remote_control_instance;
97
98
    /* Save the HID instance in the client instance.  */
99
18
    remote_control_instance -> ux_host_class_hid_remote_control_hid =  hid;
100
101
    /* The instance is live now.  */
102
18
    remote_control_instance -> ux_host_class_hid_remote_control_state =  UX_HOST_CLASS_INSTANCE_LIVE;
103
104
    /* Allocate the round-robin buffer that the remote control instance will use
105
     * to store the usages as they come in.
106
     * Size calculation overflow is checked near where _USAGE_ARRAY_LENGTH is defined.
107
     */
108
18
    remote_control_instance -> ux_host_class_hid_remote_control_usage_array =  (ULONG *)
109
18
                            _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY,
110
                                                        UX_HOST_CLASS_HID_REMOTE_CONTROL_USAGE_ARRAY_LENGTH*4);
111
112
    /* Check memory pointer. */
113
18
    if (remote_control_instance -> ux_host_class_hid_remote_control_usage_array == UX_NULL)
114
2
        status = (UX_MEMORY_INSUFFICIENT);
115
116
    /* If there is no error, go on.  */
117
18
    if (status == UX_SUCCESS)
118
    {
119
120
        /* Initialize the head and tail of this array.  */
121
16
        remote_control_instance -> ux_host_class_hid_remote_control_usage_array_head =  remote_control_instance -> ux_host_class_hid_remote_control_usage_array;
122
16
        remote_control_instance -> ux_host_class_hid_remote_control_usage_array_tail =  remote_control_instance -> ux_host_class_hid_remote_control_usage_array;
123
124
        /* Initialize the report callback.  */
125
16
        call_back.ux_host_class_hid_report_callback_id =         0;
126
16
        call_back.ux_host_class_hid_report_callback_function =   _ux_host_class_hid_remote_control_callback;
127
16
        call_back.ux_host_class_hid_report_callback_buffer =     UX_NULL;
128
16
        call_back.ux_host_class_hid_report_callback_flags =      UX_HOST_CLASS_HID_REPORT_INDIVIDUAL_USAGE;
129
16
        call_back.ux_host_class_hid_report_callback_length =     0;
130
131
        /* Register the report call back when data comes in on this report.  */
132
16
        status =  _ux_host_class_hid_report_callback_register(hid, &call_back);
133
    }
134
135
    /* If there is no error, go on.  */
136
18
    if (status == UX_SUCCESS)
137
    {
138
139
        /* Start the periodic report.  */
140
16
        status =  _ux_host_class_hid_periodic_report_start(hid);
141
142
        /* If OK, we are done.  */
143
16
        if (status == UX_SUCCESS)
144
        {
145
146
            /* Use out copy of client.  */
147
15
            hid -> ux_host_class_hid_client = hid_client;
148
149
            /* If trace is enabled, insert this event into the trace buffer.  */
150
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_REMOTE_CONTROL_ACTIVATE, hid, remote_control_instance, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
151
152
            /* If all is fine and the device is mounted, we may need to inform the application
153
            if a function has been programmed in the system structure.  */
154
15
            if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
155
            {
156
157
                /* Call system change function.  */
158
6
                _ux_system_host ->  ux_system_host_change_function(UX_HID_CLIENT_INSERTION, hid -> ux_host_class_hid_class, (VOID *) hid_client);
159
            }
160
161
            /* We are done success.  */
162
15
            return (UX_SUCCESS);
163
        }
164
    }
165
166
    /* We are here when there is error.  */
167
168
    /* Free usage array.  */
169
3
    if (remote_control_instance -> ux_host_class_hid_remote_control_usage_array)
170
1
        _ux_utility_memory_free(remote_control_instance -> ux_host_class_hid_remote_control_usage_array);
171
172
    /* Free instance memory.  */
173
3
    _ux_utility_memory_free(remote_control_instance);
174
175
    /* Return completion status.  */
176
3
    return(status);
177
}
178