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 /**   Device Pima Class                                                   */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define UX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "ux_api.h"
29 #include "ux_device_class_pima.h"
30 #include "ux_device_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_device_class_pima_object_info_get               PORTABLE C      */
38 /*                                                           6.1.11       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function returns the object info structure to the host.        */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    pima                                  Pointer to pima class         */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_device_stack_transfer_request     Transfer request              */
58 /*    _ux_utility_long_put                  Put 32-bit value              */
59 /*    _ux_utility_short_put                 Put 32-bit value              */
60 /*    _ux_utility_memory_copy               Copy memory                   */
61 /*    _ux_utility_descriptor_pack           Pack descriptor               */
62 /*    _ux_device_class_pima_response_send   Send PIMA response            */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Device Storage Class                                                */
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 /*                                            verified memset and memcpy  */
75 /*                                            cases,                      */
76 /*                                            resulting in version 6.1    */
77 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            updated status handling,    */
79 /*                                            resulting in version 6.1.10 */
80 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            internal clean up,          */
82 /*                                            resulting in version 6.1.11 */
83 /*                                                                        */
84 /**************************************************************************/
_ux_device_class_pima_object_info_get(UX_SLAVE_CLASS_PIMA * pima,ULONG object_handle)85 UINT  _ux_device_class_pima_object_info_get(UX_SLAVE_CLASS_PIMA *pima, ULONG object_handle)
86 {
87 
88 UINT                        status;
89 UX_SLAVE_TRANSFER           *transfer_request;
90 UX_SLAVE_CLASS_PIMA_OBJECT  *object;
91 ULONG                       object_info_length;
92 UCHAR                       *object_info;
93 UCHAR                       *object_info_pointer;
94 ULONG                       file_name_length;
95 ULONG                       capture_date_length;
96 ULONG                       modification_date_length;
97 ULONG                       keywords_length;
98 
99 
100     /* If trace is enabled, insert this event into the trace buffer.  */
101     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_PIMA_OBJECT_INFO_GET, pima, object_handle, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
102 
103     /* Obtain the object info from the application.  */
104     status = pima -> ux_device_class_pima_object_info_get(pima, object_handle, &object);
105 
106     /* Check for error.  */
107     if (status != UX_SUCCESS)
108 
109         /* We return an error.  */
110         _ux_device_class_pima_response_send(pima, status, 0, 0, 0, 0);
111 
112     else
113     {
114 
115         /* Length calculation and overflow check.  */
116         file_name_length = ((ULONG) *object -> ux_device_class_pima_object_filename * 2 ) + 1;
117         capture_date_length = ((ULONG) *object -> ux_device_class_pima_object_capture_date *2 ) + 1;
118         modification_date_length = ((ULONG) *object -> ux_device_class_pima_object_modification_date * 2 ) + 1;
119         keywords_length = ((ULONG) *object -> ux_device_class_pima_object_keywords * 2 ) +1;
120         object_info_length = UX_DEVICE_CLASS_PIMA_DATA_HEADER_SIZE +
121                             UX_DEVICE_CLASS_PIMA_OBJECT_VARIABLE_OFFSET +
122                             file_name_length +
123                             capture_date_length +
124                             modification_date_length +
125                             keywords_length;
126 
127         /* Ensure the object info data can fit in the endpoint's data buffer.  */
128         if (object_info_length > UX_SLAVE_REQUEST_DATA_MAX_LENGTH)
129         {
130 
131             /* If trace is enabled, insert this event into the trace buffer.  */
132             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_MEMORY_INSUFFICIENT, 0, 0, 0, UX_TRACE_ERRORS, 0, 0)
133 
134             /* We return an error.  */
135             _ux_device_class_pima_response_send(pima, UX_DEVICE_CLASS_PIMA_RC_GENERAL_ERROR, 0, 0, 0, 0);
136 
137             /* Return overflow error.  */
138             return(UX_MEMORY_INSUFFICIENT);
139         }
140 
141         /* Obtain the pointer to the transfer request.  */
142         transfer_request =  &pima -> ux_device_class_pima_bulk_in_endpoint -> ux_slave_endpoint_transfer_request;
143 
144         /* Obtain memory for this object info. Use the transfer request pre-allocated memory.  */
145         object_info =  transfer_request -> ux_slave_transfer_request_data_pointer;
146 
147         /* Fill in the data container type.  */
148         _ux_utility_short_put(object_info + UX_DEVICE_CLASS_PIMA_DATA_HEADER_TYPE,
149                                 UX_DEVICE_CLASS_PIMA_CT_DATA_BLOCK);
150 
151         /* Fill in the data code.  */
152         _ux_utility_short_put(object_info + UX_DEVICE_CLASS_PIMA_DATA_HEADER_CODE,
153                                 UX_DEVICE_CLASS_PIMA_OC_GET_OBJECT_INFO);
154 
155         /* Fill in the Transaction ID.  */
156         _ux_utility_long_put(object_info + UX_DEVICE_CLASS_PIMA_DATA_HEADER_TRANSACTION_ID,
157                                 pima -> ux_device_class_pima_transaction_id);
158 
159         /* Allocate the device info pointer to the beginning of the dynamic object info field.  */
160         object_info_pointer = object_info + UX_DEVICE_CLASS_PIMA_DATA_HEADER_SIZE;
161 
162         /* The object info structure coming from the application needs to be packed. */
163         _ux_utility_descriptor_pack((UCHAR *) object,
164                             _ux_system_class_pima_object_structure,
165                             UX_DEVICE_CLASS_PIMA_OBJECT_ENTRIES,
166                             object_info_pointer);
167 
168         /* Copy the object filename  field.  Point to the beginning of the object description string.  */
169         object_info_pointer += UX_DEVICE_CLASS_PIMA_OBJECT_VARIABLE_OFFSET;
170 
171         /* Copy that string into the object description field.  */
172         _ux_utility_memory_copy(object_info_pointer, object -> ux_device_class_pima_object_filename, file_name_length); /* Use case of memcpy is verified. */
173 
174         /* Point to the next field.  */
175         object_info_pointer += file_name_length;
176 
177         /* Copy that string into the capture date field.  */
178         _ux_utility_memory_copy(object_info_pointer, object -> ux_device_class_pima_object_capture_date, capture_date_length); /* Use case of memcpy is verified. */
179 
180         /* Point to the next field.  */
181         object_info_pointer += capture_date_length;
182 
183         /* Copy that string into the modification date field.  */
184         _ux_utility_memory_copy(object_info_pointer, object -> ux_device_class_pima_object_modification_date, modification_date_length); /* Use case of memcpy is verified. */
185 
186         /* Point to the next field.  */
187         object_info_pointer += modification_date_length;
188 
189         /* Copy that string into the keywords field.  */
190         _ux_utility_memory_copy(object_info_pointer, object -> ux_device_class_pima_object_keywords, keywords_length); /* Use case of memcpy is verified. */
191 
192         /* Fill in the size of the response header.  */
193         _ux_utility_long_put(object_info + UX_DEVICE_CLASS_PIMA_DATA_HEADER_LENGTH,
194                                 object_info_length);
195 
196         /* Send a data payload with the object info data set.  */
197         status =  _ux_device_stack_transfer_request(transfer_request, object_info_length, 0);
198 
199         /* Now we return a response with success.  */
200         _ux_device_class_pima_response_send(pima, UX_DEVICE_CLASS_PIMA_RC_OK, 0, 0, 0, 0);
201     }
202 
203     /* Return completion status.  */
204     return(status);
205 }
206