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_register PORTABLE C */
37 /* 6.1 */
38 /* AUTHOR */
39 /* */
40 /* Chaoqiong Xiao, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function registers a USB class to the USB stack. The Class */
45 /* must specify an entry point for the USB stack to send commands */
46 /* such as: */
47 /* */
48 /* UX_HOST_CLASS_COMMAND_QUERY */
49 /* UX_HOST_CLASS_COMMAND_ACTIVATE */
50 /* UX_HOST_CLASS_COMMAND_DESTROY */
51 /* */
52 /* Note: The C string of class_name must be NULL-terminated and the */
53 /* length of it (without the NULL-terminator itself) must be no larger */
54 /* than UX_MAX_CLASS_NAME_LENGTH. */
55 /* */
56 /* INPUT */
57 /* */
58 /* class_name Name of class */
59 /* class_entry_function Entry function of the class */
60 /* */
61 /* OUTPUT */
62 /* */
63 /* Completion Status */
64 /* */
65 /* CALLS */
66 /* */
67 /* _ux_utility_string_length_check Check C string and return */
68 /* length if null-terminated */
69 /* _ux_utility_memory_copy Copy memory block */
70 /* */
71 /* CALLED BY */
72 /* */
73 /* Application */
74 /* USBX Components */
75 /* */
76 /* RELEASE HISTORY */
77 /* */
78 /* DATE NAME DESCRIPTION */
79 /* */
80 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
81 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
82 /* optimized based on compile */
83 /* definitions, verified */
84 /* memset and memcpy cases, */
85 /* resulting in version 6.1 */
86 /* */
87 /**************************************************************************/
_ux_host_stack_class_register(UCHAR * class_name,UINT (* class_entry_function)(struct UX_HOST_CLASS_COMMAND_STRUCT *))88 UINT _ux_host_stack_class_register(UCHAR *class_name,
89 UINT (*class_entry_function)(struct UX_HOST_CLASS_COMMAND_STRUCT *))
90 {
91
92 UX_HOST_CLASS *class_inst;
93 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
94 UINT status;
95 UINT class_name_length = 0;
96 #endif
97 #if UX_MAX_CLASS_DRIVER > 1
98 ULONG class_index;
99 #endif
100
101 /* If trace is enabled, insert this event into the trace buffer. */
102 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_CLASS_REGISTER, class_name, class_entry_function, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
103
104 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
105 /* Get the length of the class name (exclude null-terminator). */
106 status = _ux_utility_string_length_check(class_name, &class_name_length, UX_MAX_CLASS_NAME_LENGTH);
107 if (status)
108 return(status);
109 #endif
110
111 /* Get first class. */
112 class_inst = _ux_system_host -> ux_system_host_class_array;
113
114 #if UX_MAX_CLASS_DRIVER > 1
115 /* We need to parse the class table to find an empty spot. */
116 for (class_index = 0; class_index < _ux_system_host -> ux_system_host_max_class; class_index++)
117 {
118 #endif
119
120 /* Check if this class is already used. */
121 if (class_inst -> ux_host_class_status == UX_UNUSED)
122 {
123
124 #if defined(UX_NAME_REFERENCED_BY_POINTER)
125 class_inst -> ux_host_class_name = (const UCHAR *) class_name;
126 #else
127
128 /* We have found a free container for the class. Copy the name (with null-terminator). */
129 _ux_utility_memory_copy(class_inst -> ux_host_class_name, class_name, class_name_length + 1); /* Use case of memcpy is verified. */
130 #endif
131
132 /* Memorize the entry function of this class. */
133 class_inst -> ux_host_class_entry_function = class_entry_function;
134
135 /* Mark it as used. */
136 class_inst -> ux_host_class_status = UX_USED;
137
138 /* Return successful completion. */
139 return(UX_SUCCESS);
140 }
141
142 /* Do a sanity check to make sure the class is not already installed by
143 mistake. To verify this, we simple check for the class entry point. */
144 else
145 {
146
147 /* Check for an already installed class entry function. */
148 if(class_inst -> ux_host_class_entry_function == class_entry_function)
149 {
150
151 /* Error trap. */
152 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_INIT, UX_HOST_CLASS_ALREADY_INSTALLED);
153
154 /* If trace is enabled, insert this event into the trace buffer. */
155 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_ALREADY_INSTALLED, class_name, 0, 0, UX_TRACE_ERRORS, 0, 0)
156
157 /* Yes, return an error. */
158 return(UX_HOST_CLASS_ALREADY_INSTALLED);
159 }
160 }
161 #if UX_MAX_CLASS_DRIVER > 1
162 /* Move to the next class. */
163 class_inst ++;
164 }
165 #endif
166
167 /* If trace is enabled, insert this event into the trace buffer. */
168 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_MEMORY_ARRAY_FULL, class_name, 0, 0, UX_TRACE_ERRORS, 0, 0)
169
170 /* Error trap. */
171 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_INIT, UX_MEMORY_ARRAY_FULL);
172
173 /* No more entries in the class table. */
174 return(UX_MEMORY_ARRAY_FULL);
175 }
176