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_references_get         PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*   Return object references associated with an object handle.           */
46 /*   The pima class will call the application to get the array of         */
47 /*   references.                                                          */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    pima                                  Pointer to pima class         */
52 /*    object_handle                         Object Handle for which       */
53 /*                                          references are obtained.      */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_device_stack_transfer_request     Transfer request              */
62 /*    _ux_utility_long_put                  Put 32-bit value              */
63 /*    _ux_utility_short_put                 Put 32-bit value              */
64 /*    _ux_utility_memory_copy               Copy memory                   */
65 /*    _ux_device_class_pima_response_send   Send PIMA response            */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Device Pima Class                                                   */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            verified memset and memcpy  */
78 /*                                            cases,                      */
79 /*                                            resulting in version 6.1    */
80 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            updated status handling,    */
82 /*                                            passed max length to app,   */
83 /*                                            resulting in version 6.1.10 */
84 /*                                                                        */
85 /**************************************************************************/
_ux_device_class_pima_object_references_get(UX_SLAVE_CLASS_PIMA * pima,ULONG object_handle)86 UINT  _ux_device_class_pima_object_references_get(UX_SLAVE_CLASS_PIMA *pima,
87                                                     ULONG object_handle)
88 {
89 
90 UINT                    status;
91 UX_SLAVE_TRANSFER       *transfer_request;
92 UCHAR                   *pima_data_buffer;
93 UCHAR                   *pima_data_buffer_end;
94 UCHAR                   *object_references;
95 ULONG                   object_references_length;
96 
97     /* If trace is enabled, insert this event into the trace buffer.  */
98     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_PIMA_GET_OBJECT_REFERENCES, pima, object_handle, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
99 
100     /* Obtain the pointer to the transfer request.  */
101     transfer_request =  &pima -> ux_device_class_pima_bulk_in_endpoint -> ux_slave_endpoint_transfer_request;
102 
103     /* Obtain memory for this object info. Use the transfer request pre-allocated memory.  */
104     pima_data_buffer =  transfer_request -> ux_slave_transfer_request_data_pointer;
105 
106     /* Save end of buffer.  */
107     pima_data_buffer_end =  pima_data_buffer + UX_SLAVE_REQUEST_DATA_MAX_LENGTH;
108 
109     /* Fill in the data container type.  */
110     _ux_utility_short_put(pima_data_buffer + UX_DEVICE_CLASS_PIMA_DATA_HEADER_TYPE,
111                             UX_DEVICE_CLASS_PIMA_CT_DATA_BLOCK);
112 
113     /* Fill in the data code.  */
114     _ux_utility_short_put(pima_data_buffer + UX_DEVICE_CLASS_PIMA_DATA_HEADER_CODE,
115                             UX_DEVICE_CLASS_PIMA_OC_GET_OBJECT_REFERENCES);
116 
117     /* Fill in the Transaction ID.  */
118     _ux_utility_long_put(pima_data_buffer + UX_DEVICE_CLASS_PIMA_DATA_HEADER_TRANSACTION_ID,
119                             pima -> ux_device_class_pima_transaction_id);
120 
121     /* Ask the application to retrieve for us the object references.  */
122     object_references_length = UX_DEVICE_CLASS_PIMA_MAX_PAYLOAD;
123     status =  pima -> ux_device_class_pima_object_references_get(pima, object_handle, &object_references, &object_references_length);
124 
125     /* Result should always be OK, but to be sure .... */
126     if (status != UX_SUCCESS)
127 
128         /* We return an error.  */
129         _ux_device_class_pima_response_send(pima, status, 0, 0, 0, 0);
130 
131     else
132     {
133 
134         /* Check if there enough space in our buffer.  */
135         if (pima_data_buffer + UX_DEVICE_CLASS_PIMA_DATA_HEADER_SIZE + object_references_length <= pima_data_buffer_end)
136         {
137 
138             /* Copy the property dataset into the local buffer.  */
139             _ux_utility_memory_copy(pima_data_buffer + UX_DEVICE_CLASS_PIMA_DATA_HEADER_SIZE, object_references, object_references_length); /* Use case of memcpy is verified. */
140 
141             /* Add the header size to the payload.  */
142             object_references_length += UX_DEVICE_CLASS_PIMA_DATA_HEADER_SIZE;
143 
144             /* Fill in the size of the response header.  */
145             _ux_utility_long_put(pima_data_buffer + UX_DEVICE_CLASS_PIMA_DATA_HEADER_LENGTH,
146                             object_references_length);
147 
148             /* Send a data payload with the object references array.  */
149             status =  _ux_device_stack_transfer_request(transfer_request, object_references_length, 0);
150 
151             /* Now we return a response with success.  */
152             _ux_device_class_pima_response_send(pima, UX_DEVICE_CLASS_PIMA_RC_OK, 0, 0, 0, 0);
153         }
154         else
155         {
156 
157             /* Report the error to the application.  */
158             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT);
159 
160             /* We return an error code to the host.  */
161             _ux_device_class_pima_response_send(pima, UX_DEVICE_CLASS_PIMA_RC_GENERAL_ERROR, 0, 0, 0, 0);
162 
163             /* Return error.  */
164             status =  UX_MEMORY_INSUFFICIENT;
165         }
166     }
167 
168     /* Return completion status.  */
169     return(status);
170 }
171 
172 
173