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 /**   Pictbridge Application                                              */
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_pictbridge.h"
30 #include "ux_device_class_pima.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_pictbridge_dpsclient_object_info_send           PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    Receive the info data set of a new object to be stored.             */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    pima                                   Pima instance associated     */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    user application                                                    */
61 /*                                                                        */
62 /*  RELEASE HISTORY                                                       */
63 /*                                                                        */
64 /*    DATE              NAME                      DESCRIPTION             */
65 /*                                                                        */
66 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
67 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
68 /*                                            verified memset and memcpy  */
69 /*                                            cases, used UX prefix to    */
70 /*                                            refer to TX symbols instead */
71 /*                                            of using them directly,     */
72 /*                                            resulting in version 6.1    */
73 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            fixed string length check,  */
75 /*                                            used macros for RTOS calls, */
76 /*                                            resulting in version 6.1.12 */
77 /*                                                                        */
78 /**************************************************************************/
_ux_pictbridge_dpsclient_object_info_send(UX_SLAVE_CLASS_PIMA * pima,UX_SLAVE_CLASS_PIMA_OBJECT * object,ULONG storage_id,ULONG parent_object_handle,ULONG * object_handle)79 UINT  _ux_pictbridge_dpsclient_object_info_send(UX_SLAVE_CLASS_PIMA *pima, UX_SLAVE_CLASS_PIMA_OBJECT *object,
80                                                 ULONG storage_id,
81                                                 ULONG parent_object_handle,
82                                                 ULONG *object_handle)
83 {
84 UX_PICTBRIDGE                   *pictbridge;
85 UX_SLAVE_CLASS_PIMA_OBJECT      *object_info;
86 UCHAR                           string_discovery_name[UX_PICTBRIDGE_MAX_FILE_NAME_SIZE + 1]; /* +1 for null-terminator */
87 UINT                            length, length1;
88 
89     UX_PARAMETER_NOT_USED(storage_id);
90     UX_PARAMETER_NOT_USED(parent_object_handle);
91 
92     /* Get the pointer to the Pictbridge instance.  */
93     pictbridge = (UX_PICTBRIDGE *)pima -> ux_device_class_pima_application;
94 
95     /* We only have one object.  */
96     object_info = (UX_SLAVE_CLASS_PIMA_OBJECT *) pictbridge -> ux_pictbridge_object_host;
97 
98      /* Copy the demanded object info set.  */
99     _ux_utility_memory_copy(object_info, object, UX_SLAVE_CLASS_PIMA_OBJECT_DATA_LENGTH); /* Use case of memcpy is verified. */
100 
101     /* Store the object handle.  In Pictbridge we only receive XML scripts so the handle is hardwired to 1.  */
102     object_info -> ux_device_class_pima_object_handle_id = 1;
103     *object_handle =  1;
104 
105     /* Check state machine. If we are in discovery pending mode, check file name of this object.  */
106     if (pictbridge -> ux_pictbridge_discovery_state == UX_PICTBRIDGE_DPSCLIENT_DISCOVERY_PENDING)
107     {
108 
109         /* We are in the discovery mode. Check for file name. It must match HDISCVRY.DPS in Unicode mode.  */
110         /* Check if this is a script.  */
111         if (object_info -> ux_device_class_pima_object_format == UX_DEVICE_CLASS_PIMA_OFC_SCRIPT)
112         {
113 
114             /* Yes this is a script. We need to search for the HDISCVRY.DPS file name.
115                Get the file name length (with null-terminator).  */
116             length1 = (UINT)(*object_info -> ux_device_class_pima_object_filename);
117 
118             /* Now, compare it to the HDISCVRY.DPS file name.  Check length first.  */
119             if (length1 <= UX_PICTBRIDGE_MAX_FILE_NAME_SIZE)
120             {
121 
122                 /* Invalidate length, on error it's untouched.  */
123                 length = UX_PICTBRIDGE_MAX_FILE_NAME_SIZE + 1;
124                 _ux_utility_string_length_check(_ux_pictbridge_hdiscovery_name, &length, UX_PICTBRIDGE_MAX_FILE_NAME_SIZE);
125                 if ((length + 1) == length1)
126                 {
127 
128                     /* Get the file name in a ascii format (with null-terminator). */
129                     _ux_utility_unicode_to_string(object_info -> ux_device_class_pima_object_filename, string_discovery_name);
130 
131                     /* So far, the length of name of the files are the same.
132                     Compare names now (since length is same just compare without null-terminator). */
133                     if (_ux_utility_memory_compare(_ux_pictbridge_hdiscovery_name, string_discovery_name,
134                                                     length) ==  UX_SUCCESS)
135                     {
136 
137                         /* We are done with discovery of the printer. We can now send notifications when the camera wants to print an object.  */
138                         pictbridge -> ux_pictbridge_discovery_state = UX_PICTBRIDGE_DPSCLIENT_DISCOVERY_COMPLETE;
139 
140                         /* Set an event flag if the application is listening.  */
141                         _ux_system_event_flags_set(&pictbridge -> ux_pictbridge_event_flags_group, UX_PICTBRIDGE_EVENT_FLAG_DISCOVERY, UX_OR);
142 
143                         /* There is no object during the discovery cycle.  */
144                         return(UX_SUCCESS);
145                     }
146                 }
147             }
148         }
149     }
150 
151     /* What cycle are we in ? */
152     if (pictbridge -> ux_pictbridge_host_client_state_machine == UX_PICTBRIDGE_STATE_MACHINE_IDLE)
153 
154         /* Since we are in idle state, we must have received a request from the host.  */
155         pictbridge -> ux_pictbridge_host_client_state_machine = UX_PICTBRIDGE_STATE_MACHINE_HOST_REQUEST;
156 
157 
158     /* We have copied the requested data. Return OK.  */
159     return(UX_SUCCESS);
160 }
161 
162