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 /**   Video 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_video.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_video_frame_data_get                 PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function finds the frame data within the input terminal.       */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    video                                 Pointer to video class        */
50 /*    frame_data                            Frame request structure       */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_system_error_handler              Log system error              */
59 /*    _ux_utility_descriptor_parse          Parse descriptor              */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Video Class                                                         */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_ux_host_class_video_frame_data_get(UX_HOST_CLASS_VIDEO * video,UX_HOST_CLASS_VIDEO_PARAMETER_FRAME_DATA * frame_parameter)74 UINT  _ux_host_class_video_frame_data_get(UX_HOST_CLASS_VIDEO *video, UX_HOST_CLASS_VIDEO_PARAMETER_FRAME_DATA *frame_parameter)
75 {
76 
77 UCHAR                                           *descriptor;
78 UX_HOST_CLASS_VIDEO_FRAME_DESCRIPTOR            frame_descriptor;
79 ULONG                                           total_descriptor_length;
80 ULONG                                           descriptor_length;
81 ULONG                                           descriptor_type;
82 ULONG                                           descriptor_subtype;
83 
84 
85     /* Get the descriptor to the selected format.  */
86     descriptor =  video -> ux_host_class_video_current_format_address;
87     total_descriptor_length =  video -> ux_host_class_video_length_formats;
88 
89     /* Descriptors are arranged in order. First FORMAT then FRAME.  */
90     while (total_descriptor_length)
91     {
92 
93         /* Gather the length, type and subtype of the descriptor.  */
94         descriptor_length =   *descriptor;
95         descriptor_type =     *(descriptor + 1);
96         descriptor_subtype =  *(descriptor + 2);
97 
98         /* Make sure this descriptor has at least the minimum length.  */
99         if (descriptor_length < 3)
100         {
101 
102             /* Error trap. */
103             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
104 
105             /* If trace is enabled, insert this event into the trace buffer.  */
106             //UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
107 
108             return(UX_DESCRIPTOR_CORRUPTED);
109         }
110 
111         /* Must be CS_INTERFACE.  */
112         if (descriptor_type == UX_HOST_CLASS_VIDEO_CS_INTERFACE)
113         {
114 
115             /* Process relative to descriptor type.  */
116             switch (descriptor_subtype)
117             {
118 
119                 case UX_HOST_CLASS_VIDEO_VS_FRAME_UNCOMPRESSED  :
120                 case UX_HOST_CLASS_VIDEO_VS_FRAME_MJPEG         :
121                 case UX_HOST_CLASS_VIDEO_VS_FRAME_FRAME_BASED   :
122 
123                     /* We found a Frame descriptor.  Is it the right one ? */
124                     if (frame_parameter -> ux_host_class_video_parameter_frame_requested == *(descriptor + 3))
125                     {
126 
127                         /* Make the descriptor machine independent.  */
128                         _ux_utility_descriptor_parse(descriptor, _ux_system_class_video_frame_descriptor_structure,
129                                                      UX_HOST_CLASS_VIDEO_FRAME_DESCRIPTOR_ENTRIES, (UCHAR *) &frame_descriptor);
130 
131 
132                         /* Save the frame subtype.  */
133                         frame_parameter -> ux_host_class_video_parameter_frame_subtype =  descriptor_type;
134 
135                         /* Save useful frame parameters. */
136                         frame_parameter -> ux_host_class_video_parameter_frame_width = frame_descriptor.wWidth;
137                         frame_parameter -> ux_host_class_video_parameter_frame_height = frame_descriptor.wHeight;
138                         frame_parameter -> ux_host_class_video_parameter_default_frame_interval = frame_descriptor.dwDefaultFrameInterval;
139                         frame_parameter -> ux_host_class_video_parameter_frame_interval_type = frame_descriptor.bFrameIntervalType;
140                         video -> ux_host_class_video_current_frame_address = descriptor;
141                         video -> ux_host_class_video_current_frame_interval = frame_descriptor.dwDefaultFrameInterval;
142                         /* We are done here. */
143                         return(UX_SUCCESS);
144                     }
145 
146                     break;
147 
148             }
149         }
150 
151         /* Verify if the descriptor is still valid.  */
152         if (descriptor_length > total_descriptor_length)
153         {
154 
155             /* Error trap. */
156             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
157 
158             /* If trace is enabled, insert this event into the trace buffer.  */
159             // UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
160 
161             return(UX_DESCRIPTOR_CORRUPTED);
162         }
163 
164         /* Jump to the next descriptor if we have not reached the end.  */
165         descriptor +=  descriptor_length;
166 
167         /* And adjust the length left to parse in the descriptor.  */
168         total_descriptor_length -=  descriptor_length;
169     }
170 
171     /* Error trap. */
172     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_VIDEO_WRONG_TYPE);
173 
174     /* We get here when either the report descriptor has a problem or we could
175        not find the right video device.  */
176     return(UX_HOST_CLASS_VIDEO_WRONG_TYPE);
177 }
178 
179