1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Host Stack                                                          */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_host_stack_class_instance_verify                PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function ensures that a given instance exists. An application  */
44 /*    is not responsible for keeping the instance valid pointer. The      */
45 /*    class is responsible for the instance checks if the instance is     */
46 /*    still valid.                                                        */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    class_name                            Name of class                 */
51 /*    class_instance                        Pointer to class instance     */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_utility_string_length_check       Check C string and return its */
60 /*                                          length if null-terminated     */
61 /*    _ux_utility_memory_compare            Compare blocks of memory      */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    USBX Components                                                     */
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 /*                                            optimized based on compile  */
74 /*                                            definitions,                */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_ux_host_stack_class_instance_verify(UCHAR * class_name,VOID * class_instance)78 UINT  _ux_host_stack_class_instance_verify(UCHAR *class_name, VOID *class_instance)
79 {
80 
81 UX_HOST_CLASS   *class_inst;
82 #if UX_MAX_CLASS_DRIVER > 1
83 ULONG           class_index;
84 #endif
85 VOID            **current_class_instance;
86 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
87 UINT            status;
88 UINT            class_name_length =  0;
89 #endif
90 
91 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
92     /* Get the length of the class name (exclude null-terminator).  */
93     status =  _ux_utility_string_length_check(class_name, &class_name_length, UX_MAX_CLASS_NAME_LENGTH);
94     if (status)
95         return(status);
96 #endif
97 
98     /* Get first class.  */
99     class_inst =  _ux_system_host -> ux_system_host_class_array;
100 
101 #if UX_MAX_CLASS_DRIVER > 1
102     /* We need to parse the class table.  */
103     for(class_index = 0; class_index < _ux_system_host -> ux_system_host_max_class; class_index++)
104     {
105 #endif
106 
107         /* Check if this class is already used.  */
108         if (class_inst -> ux_host_class_status == UX_USED)
109         {
110 
111             /* Start with the first class instance attached to the class container.  */
112             current_class_instance =  class_inst -> ux_host_class_first_instance;
113 
114             /* Traverse the list of the class instances until we find the correct instance.  */
115             while (current_class_instance != UX_NULL)
116             {
117 
118                 /* Check the class instance attached to the container with the caller's
119                    instance.  */
120                 if (current_class_instance == class_instance)
121                 {
122 
123                     /* We have found the class container. Check if this is the one we need (compare including null-terminator).  */
124                     if (ux_utility_name_match(class_inst-> ux_host_class_name, class_name, class_name_length + 1))
125                         return(UX_SUCCESS);
126                 }
127 
128                 /* Points to the next class instance.  */
129                 current_class_instance =  *current_class_instance;
130             }
131         }
132 #if UX_MAX_CLASS_DRIVER > 1
133 
134         /* Move to the next class.  */
135         class_inst ++;
136     }
137 #endif
138 
139     /* If trace is enabled, insert this event into the trace buffer.  */
140     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, class_instance, 0, 0, UX_TRACE_ERRORS, 0, 0)
141 
142     /* This class does not exist.  */
143     return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
144 }
145