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_remote_control.h"
31 #include "ux_host_stack.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _ux_host_class_hid_remote_control_callback          PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Chaoqiong Xiao, Microsoft Corporation                               */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function is the callback mechanism for a report registration.  */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    callback                              Pointer to callback           */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    None                                                                */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    HID Remote Control Class                                            */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
69 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*                                                                        */
72 /**************************************************************************/
_ux_host_class_hid_remote_control_callback(UX_HOST_CLASS_HID_REPORT_CALLBACK * callback)73 VOID  _ux_host_class_hid_remote_control_callback(UX_HOST_CLASS_HID_REPORT_CALLBACK *callback)
74 {
75 
76 UX_HOST_CLASS_HID_CLIENT            *hid_client;
77 UX_HOST_CLASS_HID_REMOTE_CONTROL    *remote_control_instance;
78 ULONG                               *array_head;
79 ULONG                               *array_tail;
80 ULONG                               *array_end;
81 ULONG                               *array_start;
82 ULONG                               *array_head_next;
83 
84 
85     /* Get the HID client instance that issued the callback.  */
86     hid_client = callback -> ux_host_class_hid_report_callback_client;
87 
88     /* Get the remote control local instance.  */
89     remote_control_instance =  (UX_HOST_CLASS_HID_REMOTE_CONTROL *) hid_client -> ux_host_class_hid_client_local_instance;
90 
91     /* Load the remote control usage/value array info.  */
92     array_start =  remote_control_instance -> ux_host_class_hid_remote_control_usage_array;
93     array_end =    array_start + UX_HOST_CLASS_HID_REMOTE_CONTROL_USAGE_ARRAY_LENGTH;
94     array_head =   remote_control_instance -> ux_host_class_hid_remote_control_usage_array_head;
95     array_tail =   remote_control_instance -> ux_host_class_hid_remote_control_usage_array_tail;
96 
97     /* We have a single usage/value. We have to store it into the array. If the array overflows,
98        there is no mechanism for flow control here so we ignore the usage/value until the
99        applications makes more room in the array.  */
100 
101     /* Get position where next head will be.  */
102     array_head_next =  array_head + 2;
103 
104     /* The head always points to where we will insert the next element. This
105        is the reason for the wrap.  */
106     if (array_head_next == array_end)
107         array_head_next =  array_start;
108 
109     /* Do we have enough space to store the new usage?  */
110     if (array_head_next != array_tail)
111     {
112 
113         /* Yes, we have some space.  */
114         *array_head =        callback -> ux_host_class_hid_report_callback_usage;
115         *(array_head + 1) =  callback -> ux_host_class_hid_report_callback_value;
116 
117         /* Now update the array head.  */
118         remote_control_instance -> ux_host_class_hid_remote_control_usage_array_head =  array_head_next;
119     }
120     else
121     {
122 
123         /* If trace is enabled, insert this event into the trace buffer.  */
124         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_REMOTE_CONTROL_CALLBACK, hid_client, remote_control_instance, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
125 
126         /* Notify application.  */
127         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
128     }
129 
130     /* Return to caller.  */
131     return;
132 }
133 
134