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 /**   PIMA 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_pima.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_pima_object_info_get                 PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function gets the current object information block.            */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    pima                                       Pointer to pima class    */
50 /*    pima_session                               Pointer to pima session  */
51 /*    object_handle                              The object handle        */
52 /*    object                                     Object structure to fill */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_host_class_pima_command               Pima command function     */
61 /*    _ux_utility_descriptor_parse              Unpack descriptor         */
62 /*    _ux_utility_memory_allocate               Allocate memory           */
63 /*    _ux_utility_memory_copy                   Copy memory               */
64 /*    _ux_utility_memory_free                   Free allocated memory     */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    USB application                                                     */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
75 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            verified memset and memcpy  */
77 /*                                            cases,                      */
78 /*                                            resulting in version 6.1    */
79 /*                                                                        */
80 /**************************************************************************/
_ux_host_class_pima_object_info_get(UX_HOST_CLASS_PIMA * pima,UX_HOST_CLASS_PIMA_SESSION * pima_session,ULONG object_handle,UX_HOST_CLASS_PIMA_OBJECT * object)81 UINT  _ux_host_class_pima_object_info_get(UX_HOST_CLASS_PIMA *pima,
82                                         UX_HOST_CLASS_PIMA_SESSION *pima_session,
83                                         ULONG object_handle, UX_HOST_CLASS_PIMA_OBJECT *object)
84 {
85 
86 UX_HOST_CLASS_PIMA_COMMAND           command;
87 UCHAR                                *object_buffer;
88 UCHAR                                *object_pointer;
89 ULONG                                unicode_string_length;
90 UINT                                 status;
91 
92     /* If trace is enabled, insert this event into the trace buffer.  */
93     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_OBJECT_INFO_GET, pima, object_handle, object, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
94 
95     /* Check if this session is valid or not.  */
96     if (pima_session -> ux_host_class_pima_session_magic != UX_HOST_CLASS_PIMA_MAGIC_NUMBER)
97         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
98 
99     /* Check if this session is opened or not.  */
100     if (pima_session -> ux_host_class_pima_session_state != UX_HOST_CLASS_PIMA_SESSION_STATE_OPENED)
101         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
102 
103     /* Issue command to get the object info.  1 parameter.  */
104     command.ux_host_class_pima_command_nb_parameters =  1;
105 
106     /* Parameter 1 is the Object Handle.  */
107     command.ux_host_class_pima_command_parameter_1 =  object_handle;
108 
109     /* Other parameters unused.  */
110     command.ux_host_class_pima_command_parameter_2 =  0;
111     command.ux_host_class_pima_command_parameter_3 =  0;
112     command.ux_host_class_pima_command_parameter_4 =  0;
113     command.ux_host_class_pima_command_parameter_5 =  0;
114 
115     /* Then set the command to GET_OBJECT_INFO.  */
116     command.ux_host_class_pima_command_operation_code =  UX_HOST_CLASS_PIMA_OC_GET_OBJECT_INFO;
117 
118     /* Allocate some DMA safe memory for receiving the object info block.  */
119     object_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_PIMA_OBJECT_MAX_LENGTH);
120     if (object == UX_NULL)
121         return(UX_MEMORY_INSUFFICIENT);
122 
123     /* Issue the command.  */
124     status = _ux_host_class_pima_command(pima, &command, UX_HOST_CLASS_PIMA_DATA_PHASE_IN , object_buffer,
125                                         UX_HOST_CLASS_PIMA_OBJECT_MAX_LENGTH, UX_HOST_CLASS_PIMA_OBJECT_MAX_LENGTH);
126 
127     /* Check the result. If the result is OK, the object info block was read properly. */
128     if (status == UX_SUCCESS)
129     {
130         /* Uncompress the object descriptor, at least the fixed part.  */
131         _ux_utility_descriptor_parse(object_buffer,
132                             _ux_system_class_pima_object_structure,
133                             UX_HOST_CLASS_PIMA_OBJECT_ENTRIES,
134                             (UCHAR *) object);
135 
136         /* Copy the object filename  field.  Point to the beginning of the object description string.  */
137         object_pointer =  object_buffer + UX_HOST_CLASS_PIMA_OBJECT_VARIABLE_OFFSET;
138 
139         /* Get the unicode string length.  */
140         unicode_string_length =  ((ULONG) *object_pointer * 2) + 1;
141 
142         /* Check if string can fit in our buffer.  */
143         if (unicode_string_length > UX_HOST_CLASS_PIMA_UNICODE_MAX_LENGTH)
144 
145             /* Return error.  */
146             status =  UX_MEMORY_INSUFFICIENT;
147 
148         /* Is there enough space?  */
149         if (status == UX_SUCCESS)
150         {
151 
152             /* Copy that string into the object description field.  */
153             _ux_utility_memory_copy(object -> ux_host_class_pima_object_filename, object_pointer, unicode_string_length); /* Use case of memcpy is verified. */
154 
155             /* Point to the next field.  */
156             object_pointer += unicode_string_length;
157 
158             /* Get the unicode string length.  */
159             unicode_string_length =  ((ULONG) *object_pointer  * 2) + 1;
160 
161             /* Ensure the string can fit in our buffer.  */
162             if (unicode_string_length > UX_HOST_CLASS_PIMA_DATE_TIME_STRING_MAX_LENGTH)
163 
164                 /* Return error.  */
165                 status =  UX_MEMORY_INSUFFICIENT;
166         }
167 
168         /* Is there enough space?  */
169         if (status == UX_SUCCESS)
170         {
171 
172             /* Copy that string into the capture date field.  */
173             _ux_utility_memory_copy(object -> ux_host_class_pima_object_capture_date, object_pointer, unicode_string_length); /* Use case of memcpy is verified. */
174 
175             /* Point to the next field.  */
176             object_pointer += unicode_string_length;
177 
178             /* Get the unicode string length.  */
179             unicode_string_length =  ((ULONG) *object_pointer  * 2) + 1;
180 
181             /* Ensure the string can fit in our buffer.  */
182             if (unicode_string_length > UX_HOST_CLASS_PIMA_DATE_TIME_STRING_MAX_LENGTH)
183 
184                 /* Return error.  */
185                 status =  UX_MEMORY_INSUFFICIENT;
186         }
187 
188         /* Is there enough space?  */
189         if (status == UX_SUCCESS)
190         {
191 
192             /* Copy that string into the modification date field.  */
193             _ux_utility_memory_copy(object -> ux_host_class_pima_object_modification_date, object_pointer, unicode_string_length); /* Use case of memcpy is verified. */
194 
195             /* Point to the next field.  */
196             object_pointer += unicode_string_length;
197 
198             /* Get the unicode string length.  */
199             unicode_string_length =  ((ULONG) *object_pointer  * 2) + 1;
200 
201             /* Ensure the string can fit in our buffer.  */
202             if (unicode_string_length > UX_HOST_CLASS_PIMA_UNICODE_MAX_LENGTH)
203 
204                 /* Return error.  */
205                 status =  UX_MEMORY_INSUFFICIENT;
206         }
207 
208         /* Is there enough space?  */
209         if (status == UX_SUCCESS)
210         {
211 
212             /* Copy that string into the keywords field.  */
213             _ux_utility_memory_copy(object -> ux_host_class_pima_object_keywords, object_pointer, unicode_string_length); /* Use case of memcpy is verified. */
214 
215             /* Make the object closed.  */
216             object -> ux_host_class_pima_object_state = UX_HOST_CLASS_PIMA_OBJECT_STATE_CLOSED;
217 
218             /* Reset the reading\writing offset  */
219             object -> ux_host_class_pima_object_offset = 0;
220         }
221         else
222         {
223 
224             /* If trace is enabled, insert this event into the trace buffer.  */
225             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_MEMORY_INSUFFICIENT, 0, 0, 0, UX_TRACE_ERRORS, 0, 0)
226 
227             /* Report error to application.  */
228             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT);
229         }
230     }
231 
232     /* Free the original object info buffer.  */
233     _ux_utility_memory_free(object_buffer);
234 
235     /* Return completion status.  */
236     return(status);
237 }
238 
239