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_send                PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function sends an object information block prior to sending    */
46 /*    a new object.                                                       */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    pima                                       Pointer to pima class    */
51 /*    pima_session                               Pointer to pima session  */
52 /*    storage_id                                 StorageID where to store */
53 /*    parent_object_id                           Parent object id         */
54 /*    object                                     Object info structure    */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_host_class_pima_command               Pima command function     */
63 /*    _ux_utility_memory_allocate               Allocate memory           */
64 /*    _ux_utility_memory_copy                   Copy memory               */
65 /*    _ux_utility_memory_free                   Free allocated memory     */
66 /*    _ux_utility_descriptor_pack               Pack descriptor           */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    USB application                                                     */
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 /*                                            verified memset and memcpy  */
79 /*                                            cases,                      */
80 /*                                            resulting in version 6.1    */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_class_pima_object_info_send(UX_HOST_CLASS_PIMA * pima,UX_HOST_CLASS_PIMA_SESSION * pima_session,ULONG storage_id,ULONG parent_object_id,UX_HOST_CLASS_PIMA_OBJECT * object)83 UINT  _ux_host_class_pima_object_info_send(UX_HOST_CLASS_PIMA *pima,
84                                         UX_HOST_CLASS_PIMA_SESSION *pima_session,
85                                         ULONG storage_id,
86                                         ULONG parent_object_id,
87                                         UX_HOST_CLASS_PIMA_OBJECT *object)
88 {
89 
90 UX_HOST_CLASS_PIMA_COMMAND           command;
91 UCHAR                                *object_buffer;
92 UCHAR                                *object_buffer_end;
93 UCHAR                                *object_pointer;
94 ULONG                                unicode_string_length;
95 ULONG                                object_info_length;
96 UINT                                 status;
97 
98     /* If trace is enabled, insert this event into the trace buffer.  */
99     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_OBJECT_INFO_SEND, pima, object, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
100 
101     /* Check if this session is valid or not.  */
102     if (pima_session -> ux_host_class_pima_session_magic != UX_HOST_CLASS_PIMA_MAGIC_NUMBER)
103         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
104 
105     /* Check if this session is opened or not.  */
106     if (pima_session -> ux_host_class_pima_session_state != UX_HOST_CLASS_PIMA_SESSION_STATE_OPENED)
107         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
108 
109     /* Issue command to set the object info.  2 parameter.  */
110     command.ux_host_class_pima_command_nb_parameters =  2;
111 
112     /* Parameter 1 is the Storage ID.  */
113     command.ux_host_class_pima_command_parameter_1 =  storage_id;
114 
115     /* Parameter 2 is the Parent Object ID.  */
116     command.ux_host_class_pima_command_parameter_2 =  parent_object_id;
117 
118     /* Other parameters unused.  */
119     command.ux_host_class_pima_command_parameter_3 =  0;
120     command.ux_host_class_pima_command_parameter_4 =  0;
121     command.ux_host_class_pima_command_parameter_5 =  0;
122 
123     /* Then set the command to SEND_OBJECT_INFO.  */
124     command.ux_host_class_pima_command_operation_code =  UX_HOST_CLASS_PIMA_OC_SEND_OBJECT_INFO;
125 
126     /* Allocate some DMA safe memory for sending the object info block.  */
127     object_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_PIMA_OBJECT_MAX_LENGTH);
128     if (object == UX_NULL)
129         return(UX_MEMORY_INSUFFICIENT);
130 
131     /* Get the end of the object buffer.  */
132     object_buffer_end =  object_buffer + UX_HOST_CLASS_PIMA_OBJECT_MAX_LENGTH;
133 
134     /* The object info structure coming from the application needs to be packed. */
135     _ux_utility_descriptor_pack((UCHAR *) object,
136                             _ux_system_class_pima_object_structure,
137                             UX_HOST_CLASS_PIMA_OBJECT_ENTRIES,
138                             object_buffer);
139 
140     /* Copy the object filename  field.  Point to the beginning of the object description string.  */
141     object_pointer =  object_buffer + UX_HOST_CLASS_PIMA_OBJECT_VARIABLE_OFFSET;
142 
143     /* Get the unicode string length for the filename.  */
144     unicode_string_length =  ((ULONG) *object -> ux_host_class_pima_object_filename * 2) + 1;
145 
146     /* Is there enough room for this string?  */
147     if (object_pointer + unicode_string_length > object_buffer_end)
148 
149         /* Return error.  */
150         status =  UX_MEMORY_INSUFFICIENT;
151 
152     else
153 
154         /* Continue.  */
155         status =  UX_SUCCESS;
156 
157     /* Is there enough space?  */
158     if (status == UX_SUCCESS)
159     {
160 
161         /* Copy that string into the object description field.  */
162         _ux_utility_memory_copy(object_pointer, object -> ux_host_class_pima_object_filename, unicode_string_length); /* Use case of memcpy is verified. */
163 
164         /* Point to the next field.  */
165         object_pointer += unicode_string_length;
166 
167         /* Get the unicode string length of the capture date.  */
168         unicode_string_length =  ((ULONG) *object -> ux_host_class_pima_object_capture_date * 2) + 1;
169 
170         /* Is there enough room for this string?  */
171         if (object_pointer + unicode_string_length > object_buffer_end)
172 
173             /* Return error.  */
174             status =  UX_MEMORY_INSUFFICIENT;
175     }
176 
177     /* Is there enough space?  */
178     if (status == UX_SUCCESS)
179     {
180 
181         /* Copy that string into the capture date field.  */
182         _ux_utility_memory_copy(object_pointer, object -> ux_host_class_pima_object_capture_date, unicode_string_length); /* Use case of memcpy is verified. */
183 
184         /* Point to the next field.  */
185         object_pointer += unicode_string_length;
186 
187         /* Get the unicode string length.  */
188         unicode_string_length =  ((ULONG) *object -> ux_host_class_pima_object_modification_date * 2) + 1;
189 
190         /* Is there enough room for this string?  */
191         if (object_pointer + unicode_string_length > object_buffer_end)
192 
193             /* Return error.  */
194             status =  UX_MEMORY_INSUFFICIENT;
195     }
196 
197     /* Is there enough space?  */
198     if (status == UX_SUCCESS)
199     {
200 
201         /* Copy that string into the modification date field.  */
202         _ux_utility_memory_copy(object_pointer, object -> ux_host_class_pima_object_modification_date, unicode_string_length); /* Use case of memcpy is verified. */
203 
204         /* Point to the next field.  */
205         object_pointer += unicode_string_length;
206 
207         /* Get the unicode string length.  */
208         unicode_string_length =  ((ULONG) *object -> ux_host_class_pima_object_keywords * 2) + 1;
209 
210         /* Is there enough room for this string?  */
211         if (object_pointer + unicode_string_length > object_buffer_end)
212 
213             /* Return error.  */
214             status =  UX_MEMORY_INSUFFICIENT;
215     }
216 
217     /* Is there enough space?  */
218     if (status == UX_SUCCESS)
219     {
220 
221         /* Copy that string into the keywords field.  */
222         _ux_utility_memory_copy(object_pointer, object -> ux_host_class_pima_object_keywords, unicode_string_length); /* Use case of memcpy is verified. */
223 
224         /* Point to the next field.  */
225         object_pointer += unicode_string_length;
226 
227         /* Calculate the length of the payload. */
228         object_info_length = (ULONG ) (object_pointer - object_buffer);
229 
230         /* Issue the command.  */
231         status = _ux_host_class_pima_command(pima, &command, UX_HOST_CLASS_PIMA_DATA_PHASE_OUT , object_buffer,
232                                             object_info_length, object_info_length);
233 
234         /* If the status is OK, the device sent us the Object handle in the response.  */
235         if (status == UX_SUCCESS)
236 
237             /* Update the object handle.  */
238             object -> ux_host_class_pima_object_handle_id = command.ux_host_class_pima_command_parameter_2;
239     }
240     else
241     {
242 
243         /* If trace is enabled, insert this event into the trace buffer.  */
244         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_MEMORY_INSUFFICIENT, 0, 0, 0, UX_TRACE_ERRORS, 0, 0)
245 
246         /* Report error to application.  */
247         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT);
248     }
249 
250     /* Free the original object info buffer.  */
251     _ux_utility_memory_free(object_buffer);
252 
253     /* Return completion status.  */
254     return(status);
255 }
256 
257