1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11 /**************************************************************************/
12 /**************************************************************************/
13 /** */
14 /** USBX Component */
15 /** */
16 /** Device Audio Class */
17 /** */
18 /**************************************************************************/
19 /**************************************************************************/
20
21 #define UX_SOURCE_CODE
22
23
24 /* Include necessary system files. */
25
26 #include "ux_api.h"
27 #include "ux_device_class_audio.h"
28 #include "ux_device_stack.h"
29
30
31 /**************************************************************************/
32 /* */
33 /* FUNCTION RELEASE */
34 /* */
35 /* _ux_device_class_audio_sample_read8 PORTABLE C */
36 /* 6.2.1 */
37 /* AUTHOR */
38 /* */
39 /* Chaoqiong Xiao, Microsoft Corporation */
40 /* */
41 /* DESCRIPTION */
42 /* */
43 /* This function reads 8-bit sample from the Audio class. */
44 /* */
45 /* INPUT */
46 /* */
47 /* stream Address of audio stream */
48 /* instance */
49 /* buffer Pointer to buffer to save */
50 /* sample data */
51 /* */
52 /* OUTPUT */
53 /* */
54 /* None */
55 /* */
56 /* CALLS */
57 /* */
58 /* */
59 /* CALLED BY */
60 /* */
61 /* Application */
62 /* */
63 /* RELEASE HISTORY */
64 /* */
65 /* DATE NAME DESCRIPTION */
66 /* */
67 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
68 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
69 /* resulting in version 6.1 */
70 /* 03-08-2023 Chaoqiong Xiao Modified comment(s), */
71 /* resulting in version 6.2.1 */
72 /* */
73 /**************************************************************************/
_ux_device_class_audio_sample_read8(UX_DEVICE_CLASS_AUDIO_STREAM * stream,UCHAR * buffer)74 UINT _ux_device_class_audio_sample_read8(UX_DEVICE_CLASS_AUDIO_STREAM *stream,
75 UCHAR *buffer)
76 {
77
78 UX_SLAVE_ENDPOINT *endpoint;
79 UX_SLAVE_DEVICE *device;
80 UCHAR *sample_ptr;
81 UCHAR *next_frame_buffer;
82 ULONG next_frame_sample;
83
84
85 /* Get the pointer to the device. */
86 device = &_ux_system_slave -> ux_system_slave_device;
87
88 /* As long as the device is in the CONFIGURED state. */
89 if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
90 {
91
92 /* Cannot proceed with command, the interface is down. */
93 return(UX_CONFIGURATION_HANDLE_UNKNOWN);
94 }
95
96 /* Check if endpoint is available. */
97 endpoint = stream -> ux_device_class_audio_stream_endpoint;
98 if (endpoint == UX_NULL)
99 return(UX_ERROR);
100
101 /* Check if endpoint direction is OK. */
102 if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_OUT)
103 return(UX_ERROR);
104
105 /* Underflow!! */
106 if (stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length == 0)
107 {
108 return(UX_BUFFER_OVERFLOW);
109 }
110
111 /* Try to read a sample. */
112 sample_ptr = stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_data +
113 stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos;
114 if (buffer)
115 *buffer = *sample_ptr;
116
117 /* Update sample read state. */
118 next_frame_sample = stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos + 1;
119 if (next_frame_sample >= stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length)
120 {
121
122 /* Set frame length to 0 to indicate no data. */
123 stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length = 0;
124
125 /* Move to next frame buffer. */
126 next_frame_sample = 0;
127
128 /* Move frame if it's not the last one. */
129 if (stream -> ux_device_class_audio_stream_access_pos != stream -> ux_device_class_audio_stream_transfer_pos)
130 {
131 next_frame_buffer = (UCHAR *)stream -> ux_device_class_audio_stream_access_pos;
132 next_frame_buffer += stream -> ux_device_class_audio_stream_frame_buffer_size;
133 if (next_frame_buffer >= stream -> ux_device_class_audio_stream_buffer + stream -> ux_device_class_audio_stream_buffer_size)
134 next_frame_buffer = stream -> ux_device_class_audio_stream_buffer;
135 stream -> ux_device_class_audio_stream_access_pos = (UX_DEVICE_CLASS_AUDIO_FRAME *)next_frame_buffer;
136 }
137 }
138
139 /* Update next sample position. */
140 stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos = next_frame_sample;
141
142 return(UX_SUCCESS);
143 }
144
145 /**************************************************************************/
146 /* */
147 /* FUNCTION RELEASE */
148 /* */
149 /* _uxe_device_class_audio_sample_read8 PORTABLE C */
150 /* 6.2.1 */
151 /* AUTHOR */
152 /* */
153 /* Chaoqiong Xiao, Microsoft Corporation */
154 /* */
155 /* DESCRIPTION */
156 /* */
157 /* This function checks errors in reading 8-bit sample function call. */
158 /* */
159 /* INPUT */
160 /* */
161 /* stream Address of audio stream */
162 /* instance */
163 /* buffer Pointer to buffer to save */
164 /* sample data */
165 /* */
166 /* OUTPUT */
167 /* */
168 /* None */
169 /* */
170 /* CALLS */
171 /* */
172 /* _ux_device_class_audio_sample_read8 Read 8-bit sample */
173 /* */
174 /* CALLED BY */
175 /* */
176 /* Application */
177 /* */
178 /* RELEASE HISTORY */
179 /* */
180 /* DATE NAME DESCRIPTION */
181 /* */
182 /* 03-08-2023 Chaoqiong Xiao Initial Version 6.2.1 */
183 /* */
184 /**************************************************************************/
_uxe_device_class_audio_sample_read8(UX_DEVICE_CLASS_AUDIO_STREAM * stream,UCHAR * buffer)185 UINT _uxe_device_class_audio_sample_read8(UX_DEVICE_CLASS_AUDIO_STREAM *stream,
186 UCHAR *buffer)
187 {
188
189 /* Sanity check. */
190 if (stream == UX_NULL)
191 return(UX_INVALID_PARAMETER);
192
193 /* Read 8-bit sample. */
194 return(_ux_device_class_audio_sample_read8(stream, buffer));
195 }
196