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_get                            PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function returns a pointer to the class container. A class     */
45 /*    needs to obtain its container from the USBX stack to search for     */
46 /*    instances when a driver or an application wants to open a device.   */
47 /*                                                                        */
48 /*    Note: The C string of class_name must be NULL-terminated and the    */
49 /*    length of it (without the NULL-terminator itself) must be no larger */
50 /*    than UX_MAX_CLASS_NAME_LENGTH.                                      */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    class_name                            Name of class                 */
55 /*    class                                 Class pointer                 */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    Completion Status                                                   */
60 /*                                                                        */
61 /*  CALLS                                                                 */
62 /*                                                                        */
63 /*    _ux_utility_string_length_check       Check C string and return its */
64 /*                                          length if null-terminated     */
65 /*    _ux_utility_memory_compare            Compare memory blocks         */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application                                                         */
70 /*    USBX Components                                                     */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
77 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            optimized based on compile  */
79 /*                                            definitions,                */
80 /*                                            resulting in version 6.1    */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_stack_class_get(UCHAR * class_name,UX_HOST_CLASS ** host_class)83 UINT  _ux_host_stack_class_get(UCHAR *class_name, UX_HOST_CLASS **host_class)
84 {
85 
86 UX_HOST_CLASS       *class_ptr;
87 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
88 UINT                status;
89 UINT                class_name_length =  0;
90 #endif
91 #if UX_MAX_CLASS_DRIVER > 1
92 ULONG               class_index;
93 #endif
94 
95 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
96     /* Get the length of the class name (exclude null-terminator).  */
97     status =  _ux_utility_string_length_check(class_name, &class_name_length, UX_MAX_CLASS_NAME_LENGTH);
98     if (status)
99         return(status);
100 #endif
101 
102     /* Get first class.  */
103     class_ptr =  _ux_system_host -> ux_system_host_class_array;
104 
105 #if UX_MAX_CLASS_DRIVER > 1
106     /* We need to parse the class driver table.  */
107     for (class_index = 0; class_index < _ux_system_host -> ux_system_host_max_class; class_index++)
108     {
109 #endif
110 
111         /* Check if this class is already being used. */
112         if (class_ptr -> ux_host_class_status == UX_USED)
113         {
114 
115             /* We have found a container. Check if this is the one we need (compare including null-terminator).  */
116             if (ux_utility_name_match(class_ptr -> ux_host_class_name, class_name, class_name_length + 1))
117             {
118 
119                 /* The class container was found. Update the pointer to the class container for the caller.  */
120                 *host_class =  class_ptr;
121 
122                 /* Return success.  */
123                 return(UX_SUCCESS);
124             }
125         }
126 
127 #if UX_MAX_CLASS_DRIVER > 1
128         /* Move to the next class.  */
129         class_ptr++;
130     }
131 #endif
132 
133     /* If trace is enabled, insert this event into the trace buffer.  */
134     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_UNKNOWN, class_name, 0, 0, UX_TRACE_ERRORS, 0, 0)
135 
136     /* This class does not exist, return an error.  */
137     return(UX_HOST_CLASS_UNKNOWN);
138 }
139 
140