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 /**   Host Stack                                                          */
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_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_stack_class_instance_verify                PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function ensures that a given instance exists. An application  */
45 /*    is not responsible for keeping the instance valid pointer. The      */
46 /*    class is responsible for the instance checks if the instance is     */
47 /*    still valid.                                                        */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    class_name                            Name of class                 */
52 /*    class_instance                        Pointer to class instance     */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_utility_string_length_check       Check C string and return its */
61 /*                                          length if null-terminated     */
62 /*    _ux_utility_memory_compare            Compare blocks of memory      */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    USBX Components                                                     */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            optimized based on compile  */
75 /*                                            definitions,                */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_ux_host_stack_class_instance_verify(UCHAR * class_name,VOID * class_instance)79 UINT  _ux_host_stack_class_instance_verify(UCHAR *class_name, VOID *class_instance)
80 {
81 
82 UX_HOST_CLASS   *class_inst;
83 #if UX_MAX_CLASS_DRIVER > 1
84 ULONG           class_index;
85 #endif
86 VOID            **current_class_instance;
87 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
88 UINT            status;
89 UINT            class_name_length =  0;
90 #endif
91 
92 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
93     /* Get the length of the class name (exclude null-terminator).  */
94     status =  _ux_utility_string_length_check(class_name, &class_name_length, UX_MAX_CLASS_NAME_LENGTH);
95     if (status)
96         return(status);
97 #endif
98 
99     /* Get first class.  */
100     class_inst =  _ux_system_host -> ux_system_host_class_array;
101 
102 #if UX_MAX_CLASS_DRIVER > 1
103     /* We need to parse the class table.  */
104     for(class_index = 0; class_index < _ux_system_host -> ux_system_host_max_class; class_index++)
105     {
106 #endif
107 
108         /* Check if this class is already used.  */
109         if (class_inst -> ux_host_class_status == UX_USED)
110         {
111 
112             /* Start with the first class instance attached to the class container.  */
113             current_class_instance =  class_inst -> ux_host_class_first_instance;
114 
115             /* Traverse the list of the class instances until we find the correct instance.  */
116             while (current_class_instance != UX_NULL)
117             {
118 
119                 /* Check the class instance attached to the container with the caller's
120                    instance.  */
121                 if (current_class_instance == class_instance)
122                 {
123 
124                     /* We have found the class container. Check if this is the one we need (compare including null-terminator).  */
125                     if (ux_utility_name_match(class_inst-> ux_host_class_name, class_name, class_name_length + 1))
126                         return(UX_SUCCESS);
127                 }
128 
129                 /* Points to the next class instance.  */
130                 current_class_instance =  *current_class_instance;
131             }
132         }
133 #if UX_MAX_CLASS_DRIVER > 1
134 
135         /* Move to the next class.  */
136         class_inst ++;
137     }
138 #endif
139 
140     /* If trace is enabled, insert this event into the trace buffer.  */
141     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, class_instance, 0, 0, UX_TRACE_ERRORS, 0, 0)
142 
143     /* This class does not exist.  */
144     return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
145 }
146