1 /* This test is designed to test the simple dpump host/device class operation.  */
2 
3 #include <stdio.h>
4 #include "tx_api.h"
5 #include "ux_api.h"
6 #include "ux_system.h"
7 #include "ux_utility.h"
8 #include "ux_hcd_sim_host.h"
9 
10 #include "fx_api.h"
11 
12 #include "ux_device_class_audio.h"
13 #include "ux_device_stack.h"
14 
15 #include "ux_host_class_audio.h"
16 #include "ux_host_class_dummy.h"
17 
18 #include "ux_test_dcd_sim_slave.h"
19 #include "ux_test_hcd_sim_host.h"
20 #include "ux_test_utility_sim.h"
21 
22 
23 /* Define constants.  */
24 
25 #define                             UX_DEMO_REQUEST_MAX_LENGTH \
26     ((UX_HCD_SIM_HOST_MAX_PAYLOAD) > (UX_SLAVE_REQUEST_DATA_MAX_LENGTH) ? \
27         (UX_HCD_SIM_HOST_MAX_PAYLOAD) : (UX_SLAVE_REQUEST_DATA_MAX_LENGTH))
28 
29 #define                             UX_DEMO_DEBUG_SIZE  (4096*8)
30 #define                             UX_DEMO_STACK_SIZE  1024
31 #define                             UX_DEMO_BUFFER_SIZE (UX_DEMO_REQUEST_MAX_LENGTH + 1)
32 #define                             UX_DEMO_MEMORY_SIZE (128*1024)
33 
34 #define                             UX_TEST_LOG_SIZE    (64)
35 
36 
37 /* Define local/extern function prototypes.  */
38 static void        test_thread_entry(ULONG);
39 static TX_THREAD   tx_test_thread_host_simulation;
40 static TX_THREAD   tx_test_thread_slave_simulation;
41 static void        tx_test_thread_host_simulation_entry(ULONG);
42 static void        tx_test_thread_slave_simulation_entry(ULONG);
43 
44 
45 /* Define global data structures.  */
46 static UCHAR                                    usbx_memory[UX_DEMO_MEMORY_SIZE + (UX_DEMO_STACK_SIZE * 2)];
47 
48 static UX_HOST_CLASS_DUMMY                      *dummy_control;
49 static UX_HOST_CLASS_DUMMY                      *dummy_tx;
50 static UX_HOST_CLASS_DUMMY                      *dummy_rx;
51 
52 static UX_DEVICE_CLASS_AUDIO                    *slave_audio_tx;
53 static UX_DEVICE_CLASS_AUDIO_STREAM             *slave_audio_tx_stream;
54 static UX_DEVICE_CLASS_AUDIO_PARAMETER           slave_audio_tx_parameter;
55 static UX_DEVICE_CLASS_AUDIO_STREAM_PARAMETER    slave_audio_tx_stream_parameter;
56 
57 static UX_DEVICE_CLASS_AUDIO                    *slave_audio_rx;
58 static UX_DEVICE_CLASS_AUDIO_STREAM             *slave_audio_rx_stream;
59 static UX_DEVICE_CLASS_AUDIO_PARAMETER           slave_audio_rx_parameter;
60 static UX_DEVICE_CLASS_AUDIO_STREAM_PARAMETER    slave_audio_rx_stream_parameter;
61 static UX_SLAVE_TRANSFER                        *slave_audio_rx_transfer;
62 
63 static ULONG                                    device_audio_tx_feedback;
64 static UCHAR*                                   device_audio_tx_feedback_u8a = (UCHAR*)&device_audio_tx_feedback;
65 static ULONG                                    device_audio_tx_feedback_count;
66 static ULONG                                    device_audio_rx_feedback;
67 static UCHAR*                                   device_audio_rx_feedback_u8a = (UCHAR*)&device_audio_rx_feedback;
68 static ULONG                                    device_audio_rx_feedback_count;
69 
70 
71 static UX_HOST_CLASS_AUDIO                      *audio;
72 
73 static ULONG                               error_counter;
74 
75 static ULONG                               set_cfg_counter;
76 
77 static ULONG                               rsc_mem_free_on_set_cfg;
78 static ULONG                               rsc_sem_on_set_cfg;
79 static ULONG                               rsc_sem_get_on_set_cfg;
80 static ULONG                               rsc_mutex_on_set_cfg;
81 
82 static ULONG                               rsc_enum_sem_usage;
83 static ULONG                               rsc_enum_sem_get_count;
84 static ULONG                               rsc_enum_mutex_usage;
85 static ULONG                               rsc_enum_mem_usage;
86 
87 static ULONG                               rsc_audio_sem_usage;
88 static ULONG                               rsc_audio_sem_get_count;
89 static ULONG                               rsc_audio_mutex_usage;
90 static ULONG                               rsc_audio_mem_usage;
91 
92 static ULONG                               interaction_count;
93 
94 static UCHAR                               error_callback_ignore = UX_TRUE;
95 static ULONG                               error_callback_counter;
96 
97 static struct BUFFER_LOG_STRUCT {
98     ULONG length;
99     UCHAR data[256];
100 } buffer_log[UX_TEST_LOG_SIZE];
101 static ULONG buffer_log_count = 0;
102 #define SAVE_BUFFER_LOG(buf,siz) do {                                                      \
103     if (buffer_log_count < UX_TEST_LOG_SIZE) {                                             \
104         ULONG __local_size__ = ((siz) > 256) ? 256 : (siz);                                \
105         buffer_log[buffer_log_count].length = (siz);                                       \
106         _ux_utility_memory_copy(buffer_log[buffer_log_count].data, (buf), __local_size__); \
107     }                                                                                      \
108     buffer_log_count ++;                                                                   \
109 } while(0)
110 
111 static ULONG test_tx_ack_count = 0xFFFFFFFF;
112 static ULONG test_tx_ins_count = 0;
113 static ULONG test_tx_ins_way   = 0;
114 
115 static struct CALLBACK_INVOKE_LOG_STRUCT {
116     VOID *func;
117     VOID *param1;
118     VOID *param2;
119     VOID *param3;
120 } callback_invoke_log[UX_TEST_LOG_SIZE];
121 static ULONG callback_invoke_count = 0;
122 
123 #define SAVE_CALLBACK_INVOKE_LOG(f,p1,p2,p3) do {                \
124     if (callback_invoke_count < UX_TEST_LOG_SIZE) {              \
125         callback_invoke_log[callback_invoke_count].func   = (VOID *)(f);  \
126         callback_invoke_log[callback_invoke_count].param1 = (VOID *)(p1); \
127         callback_invoke_log[callback_invoke_count].param2 = (VOID *)(p2); \
128         callback_invoke_log[callback_invoke_count].param3 = (VOID *)(p3); \
129         callback_invoke_count++;                                 \
130     }                                                            \
131 } while(0)
132 #define RESET_CALLBACK_INVOKE_LOG() do { \
133     callback_invoke_count = 0;           \
134 } while(0)
135 
136 /* Define device framework.  */
137 
138 #define D3(d) ((UCHAR)((d) >> 24))
139 #define D2(d) ((UCHAR)((d) >> 16))
140 #define D1(d) ((UCHAR)((d) >> 8))
141 #define D0(d) ((UCHAR)((d) >> 0))
142 
143 static unsigned char device_framework_full_speed[] = {
144 
145 /* --------------------------------------- Device Descriptor */
146 /* 0  bLength, bDescriptorType                               */ 18,   0x01,
147 /* 2  bcdUSB                                                 */ D0(0x200),D1(0x200),
148 /* 4  bDeviceClass, bDeviceSubClass, bDeviceProtocol         */ 0x00, 0x00, 0x00,
149 /* 7  bMaxPacketSize0                                        */ 0x08,
150 /* 8  idVendor, idProduct                                    */ 0x84, 0x84, 0x01, 0x00,
151 /* 12 bcdDevice                                              */ D0(0x100),D1(0x100),
152 /* 14 iManufacturer, iProduct, iSerialNumber                 */ 0,    0,    0,
153 /* 17 bNumConfigurations                                     */ 1,
154 
155 /* -------------------------------- Configuration Descriptor *//* 9+81+52*2=194 */
156 /* 0 bLength, bDescriptorType                                */ 9,    0x02,
157 /* 2 wTotalLength                                            */ D0(194+18),D1(194+18),
158 /* 4 bNumInterfaces, bConfigurationValue                     */ 3,    1,
159 /* 6 iConfiguration                                          */ 0,
160 /* 7 bmAttributes, bMaxPower                                 */ 0x80, 50,
161 
162 /* ------------------------------------ Interface Descriptor *//* 0 Control (9+72=81) */
163 /* 0 bLength, bDescriptorType                                */ 9,    0x04,
164 /* 2 bInterfaceNumber, bAlternateSetting                     */ 0,    0,
165 /* 4 bNumEndpoints                                           */ 0,
166 /* 5 bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */ 0x01, 0x01, 0x00,
167 /* 8 iInterface                                              */ 0,
168 /* ---------------- Audio 1.0 AC Interface Header Descriptor *//* (10+12*2+10*2+9*2=72) */
169 /* 0 bLength, bDescriptorType, bDescriptorSubtype            */ 10,            0x24, 0x01,
170 /* 3 bcdADC                                                  */ 0x00,          0x01,
171 /* 5 wTotalLength, bInCollection                             */ D0(72),D1(72), 2,
172 /* 8 baInterfaceNr(1) ... baInterfaceNr(n)                   */ 1,             2,
173 /* ------------------- Audio 1.0 AC Input Terminal Descriptor */
174 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 12,   0x24,                 0x02,
175 /* 3  bTerminalID, wTerminalType                              */ 0x01, D0(0x0201),D1(0x0201),
176 /* 6  bAssocTerminal,                                         */ 0x00,
177 /* 7  bNrChannels, wChannelConfig                             */ 0x02, D0(0),D1(0),
178 /* 10 iChannelNames, iTerminal                                */ 0,    0,
179 /* --------------------- Audio 1.0 AC Feature Unit Descriptor */
180 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 10,   0x24, 0x06,
181 /* 3  bUnitID, bSourceID                                      */ 0x02, 0x01,
182 /* 5  bControlSize                                            */ 1,
183 /* 6  bmaControls(0) ... bmaControls(...) ...                 */ 0x00, 0x00, 0x00,
184 /* .  iFeature                                                */ 0,
185 /* ------------------ Audio 1.0 AC Output Terminal Descriptor */
186 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 9,    0x24,                 0x03,
187 /* 3  bTerminalID, wTerminalType                              */ 0x03, D0(0x0101),D1(0x0101),
188 /* 6  bAssocTerminal, bSourceID                               */ 0x00, 0x02,
189 /* 8  iTerminal                                               */ 0,
190 /* ------------------- Audio 1.0 AC Input Terminal Descriptor */
191 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 12,   0x24,                 0x02,
192 /* 3  bTerminalID, wTerminalType                              */ 0x04, D0(0x0101),D1(0x0101),
193 /* 6  bAssocTerminal,                                         */ 0x00,
194 /* 7  bNrChannels, wChannelConfig                             */ 0x02, D0(0),D1(0),
195 /* 10 iChannelNames, iTerminal                                */ 0,    0,
196 /* --------------------- Audio 1.0 AC Feature Unit Descriptor */
197 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 10,   0x24, 0x06,
198 /* 3  bUnitID, bSourceID                                      */ 0x05, 0x04,
199 /* 5  bControlSize                                            */ 1,
200 /* 6  bmaControls(0) ... bmaControls(...) ...                 */ 0x00, 0x00, 0x00,
201 /* .  iFeature                                                */ 0,
202 /* ------------------ Audio 1.0 AC Output Terminal Descriptor */
203 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 9,    0x24,                 0x03,
204 /* 3  bTerminalID, wTerminalType                              */ 0x06, D0(0x0301),D1(0x0301),
205 /* 6  bAssocTerminal, bSourceID                               */ 0x00, 0x05,
206 /* 8  iTerminal                                               */ 0,
207 
208 /* ------------------------------------ Interface Descriptor *//* 1 Stream IN (9+9+7+11+9+7=52) */
209 /* 0 bLength, bDescriptorType                                */ 9,    0x04,
210 /* 2 bInterfaceNumber, bAlternateSetting                     */ 1,    0,
211 /* 4 bNumEndpoints                                           */ 0,
212 /* 5 bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */ 0x01, 0x02, 0x00,
213 /* 8 iInterface                                              */ 0,
214 /* ------------------------------------ Interface Descriptor */
215 /* 0 bLength, bDescriptorType                                */ 9,    0x04,
216 /* 2 bInterfaceNumber, bAlternateSetting                     */ 1,    1,
217 /* 4 bNumEndpoints                                           */ 2,
218 /* 5 bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */ 0x01, 0x02, 0x00,
219 /* 8 iInterface                                              */ 0,
220 /* ------------------------ Audio 1.0 AS Interface Descriptor */
221 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 7,    0x24, 0x01,
222 /* 3  bTerminalLink                                           */ 0x03,
223 /* 4  bDelay, wFormatTag                                      */ 0x00, D0(0x0001),D1(0x0001),
224 /* -------------------------- Audio AS Format Type Descriptor */
225 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 11,   0x24, 0x02,
226 /* 3  bFormatType, bNrChannels, bSubframeSize, bBitResolution */ 0x01, 0x02, 0x02, 16,
227 /* 7  bSamFreqType (n), tSamFreq[1] ... tSamFreq[n]           */ 1,    D0(48000),D1(48000),D2(48000),
228 /* --------------------- Audio 1.0 AS ISO Endpoint Descriptor */
229 /* 0  bLength, bDescriptorType                                */ 9,               0x05,
230 /* 2  bEndpointAddress, bmAttributes                          */ 0x81,            0x01,
231 /* 4  wMaxPacketSize, bInterval, bRefresh, bSynchAddress      */ D0(256),D1(256), 1,    0, 0x01,
232 /* ---------- Audio 1.0 AS ISO Audio Data Endpoint Descriptor */
233 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 7,                0x25, 0x01,
234 /* 3  bmAttributes                                            */ 0x00,
235 /* 5  bLockDelayUnits, wLockDelay                             */ 0x00, D0(0),D1(0),
236 /* --------------------- Audio 1.0 AS ISO Feedback Endpoint Descriptor */
237 /* 0  bLength, bDescriptorType                                */ 9,               0x05,
238 /* 2  bEndpointAddress, bmAttributes                          */ 0x01,            0x01,
239 /* 4  wMaxPacketSize, bInterval, bRefresh, bSynchAddress      */ D0(4),D1(4), 1,  4, 0x00,
240 
241 /* ------------------------------------ Interface Descriptor *//* 2 Stream OUT (9+9+7+11+9+7=52) */
242 /* 0 bLength, bDescriptorType                                */ 9,    0x04,
243 /* 2 bInterfaceNumber, bAlternateSetting                     */ 2,    0,
244 /* 4 bNumEndpoints                                           */ 0,
245 /* 5 bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */ 0x01, 0x02, 0x00,
246 /* 8 iInterface                                              */ 0,
247 /* ------------------------------------ Interface Descriptor */
248 /* 0 bLength, bDescriptorType                                */ 9,    0x04,
249 /* 2 bInterfaceNumber, bAlternateSetting                     */ 2,    1,
250 /* 4 bNumEndpoints                                           */ 2,
251 /* 5 bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */ 0x01, 0x02, 0x00,
252 /* 8 iInterface                                              */ 0,
253 /* ------------------------ Audio 1.0 AS Interface Descriptor */
254 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 7,    0x24, 0x01,
255 /* 3  bTerminalLink                                           */ 0x04,
256 /* 4  bDelay, wFormatTag                                      */ 0x00, D0(0x0001),D1(0x0001),
257 /* -------------------------- Audio AS Format Type Descriptor */
258 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 11,   0x24, 0x02,
259 /* 3  bFormatType, bNrChannels, bSubframeSize, bBitResolution */ 0x01, 0x02, 0x02, 16,
260 /* 7  bSamFreqType (n), tSamFreq[1] ... tSamFreq[n]           */ 1,    D0(48000),D1(48000),D2(48000),
261 /* --------------------- Audio 1.0 AS ISO Endpoint Descriptor */
262 /* 0  bLength, bDescriptorType                                */ 9,               0x05,
263 /* 2  bEndpointAddress, bmAttributes                          */ 0x02,            0x01,
264 /* 4  wMaxPacketSize, bInterval, bRefresh, bSynchAddress      */ D0(256),D1(256), 1,    0, 0x82,
265 /* ---------- Audio 1.0 AS ISO Audio Data Endpoint Descriptor */
266 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 7,                0x25, 0x01,
267 /* 3  bmAttributes                                            */ 0x00,
268 /* 5  bLockDelayUnits, wLockDelay                             */ 0x00, D0(0),D1(0),
269 /* --------------------- Audio 1.0 AS ISO Feedback Endpoint Descriptor */
270 /* 0  bLength, bDescriptorType                                */ 9,               0x05,
271 /* 2  bEndpointAddress, bmAttributes                          */ 0x82,            0x01,
272 /* 4  wMaxPacketSize, bInterval, bRefresh, bSynchAddress      */ D0(4),D1(4), 1,  4, 0x00,
273 };
274 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
275 
276 static unsigned char device_framework_high_speed[] = {
277 /* --------------------------------------- Device Descriptor */
278 /* 0  bLength, bDescriptorType                               */ 18,   0x01,
279 /* 2  bcdUSB                                                 */ D0(0x200),D1(0x200),
280 /* 4  bDeviceClass, bDeviceSubClass, bDeviceProtocol         */ 0x00, 0x00, 0x00,
281 /* 7  bMaxPacketSize0                                        */ 8,
282 /* 8  idVendor, idProduct                                    */ 0x84, 0x84, 0x01, 0x00,
283 /* 12 bcdDevice                                              */ D0(0x100),D1(0x100),
284 /* 14 iManufacturer, iProduct, iSerialNumber                 */ 0,    0,    0,
285 /* 17 bNumConfigurations                                     */ 1,
286 
287 /* ----------------------------- Device Qualifier Descriptor */
288 /* 0 bLength, bDescriptorType                                */ 10,                 0x06,
289 /* 2 bcdUSB                                                  */ D0(0x200),D1(0x200),
290 /* 4 bDeviceClass, bDeviceSubClass, bDeviceProtocol          */ 0x00,               0x00, 0x00,
291 /* 7 bMaxPacketSize0                                         */ 8,
292 /* 8 bNumConfigurations                                      */ 1,
293 /* 9 bReserved                                               */ 0,
294 
295 /* -------------------------------- Configuration Descriptor *//* 9+81+52*2=194 */
296 /* 0 bLength, bDescriptorType                                */ 9,    0x02,
297 /* 2 wTotalLength                                            */ D0(194+18),D1(194+18),
298 /* 4 bNumInterfaces, bConfigurationValue                     */ 3,    1,
299 /* 6 iConfiguration                                          */ 0,
300 /* 7 bmAttributes, bMaxPower                                 */ 0x80, 50,
301 
302 /* ------------------------------------ Interface Descriptor *//* 0 Control (9+72=81) */
303 /* 0 bLength, bDescriptorType                                */ 9,    0x04,
304 /* 2 bInterfaceNumber, bAlternateSetting                     */ 0,    0,
305 /* 4 bNumEndpoints                                           */ 0,
306 /* 5 bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */ 0x01, 0x01, 0x00,
307 /* 8 iInterface                                              */ 0,
308 /* ---------------- Audio 1.0 AC Interface Header Descriptor *//* (10+12*2+10*2+9*2=72) */
309 /* 0 bLength, bDescriptorType, bDescriptorSubtype            */ 10,            0x24, 0x01,
310 /* 3 bcdADC                                                  */ 0x00,          0x01,
311 /* 5 wTotalLength, bInCollection                             */ D0(72),D1(72), 2,
312 /* 8 baInterfaceNr(1) ... baInterfaceNr(n)                   */ 1,             2,
313 /* ------------------- Audio 1.0 AC Input Terminal Descriptor */
314 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 12,   0x24,                 0x02,
315 /* 3  bTerminalID, wTerminalType                              */ 0x01, D0(0x0201),D1(0x0201),
316 /* 6  bAssocTerminal,                                         */ 0x00,
317 /* 7  bNrChannels, wChannelConfig                             */ 0x02, D0(0),D1(0),
318 /* 10 iChannelNames, iTerminal                                */ 0,    0,
319 /* --------------------- Audio 1.0 AC Feature Unit Descriptor */
320 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 10,   0x24, 0x06,
321 /* 3  bUnitID, bSourceID                                      */ 0x02, 0x01,
322 /* 5  bControlSize                                            */ 1,
323 /* 6  bmaControls(0) ... bmaControls(...) ...                 */ 0x00, 0x00, 0x00,
324 /* .  iFeature                                                */ 0,
325 /* ------------------ Audio 1.0 AC Output Terminal Descriptor */
326 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 9,    0x24,                 0x03,
327 /* 3  bTerminalID, wTerminalType                              */ 0x03, D0(0x0101),D1(0x0101),
328 /* 6  bAssocTerminal, bSourceID                               */ 0x00, 0x02,
329 /* 8  iTerminal                                               */ 0,
330 /* ------------------- Audio 1.0 AC Input Terminal Descriptor */
331 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 12,   0x24,                 0x02,
332 /* 3  bTerminalID, wTerminalType                              */ 0x04, D0(0x0101),D1(0x0101),
333 /* 6  bAssocTerminal,                                         */ 0x00,
334 /* 7  bNrChannels, wChannelConfig                             */ 0x02, D0(0),D1(0),
335 /* 10 iChannelNames, iTerminal                                */ 0,    0,
336 /* --------------------- Audio 1.0 AC Feature Unit Descriptor */
337 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 10,   0x24, 0x06,
338 /* 3  bUnitID, bSourceID                                      */ 0x05, 0x04,
339 /* 5  bControlSize                                            */ 1,
340 /* 6  bmaControls(0) ... bmaControls(...) ...                 */ 0x00, 0x00, 0x00,
341 /* .  iFeature                                                */ 0,
342 /* ------------------ Audio 1.0 AC Output Terminal Descriptor */
343 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 9,    0x24,                 0x03,
344 /* 3  bTerminalID, wTerminalType                              */ 0x06, D0(0x0301),D1(0x0301),
345 /* 6  bAssocTerminal, bSourceID                               */ 0x00, 0x05,
346 /* 8  iTerminal                                               */ 0,
347 
348 /* ------------------------------------ Interface Descriptor *//* 1 Stream IN (9+9+7+11+9+7=52) */
349 /* 0 bLength, bDescriptorType                                */ 9,    0x04,
350 /* 2 bInterfaceNumber, bAlternateSetting                     */ 1,    0,
351 /* 4 bNumEndpoints                                           */ 0,
352 /* 5 bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */ 0x01, 0x02, 0x00,
353 /* 8 iInterface                                              */ 0,
354 /* ------------------------------------ Interface Descriptor */
355 /* 0 bLength, bDescriptorType                                */ 9,    0x04,
356 /* 2 bInterfaceNumber, bAlternateSetting                     */ 1,    1,
357 /* 4 bNumEndpoints                                           */ 2,
358 /* 5 bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */ 0x01, 0x02, 0x00,
359 /* 8 iInterface                                              */ 0,
360 /* ------------------------ Audio 1.0 AS Interface Descriptor */
361 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 7,    0x24, 0x01,
362 /* 3  bTerminalLink                                           */ 0x03,
363 /* 4  bDelay, wFormatTag                                      */ 0x00, D0(0x0001),D1(0x0001),
364 /* -------------------------- Audio AS Format Type Descriptor */
365 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 11,   0x24, 0x02,
366 /* 3  bFormatType, bNrChannels, bSubframeSize, bBitResolution */ 0x01, 0x02, 0x02, 16,
367 /* 7  bSamFreqType (n), tSamFreq[1] ... tSamFreq[n]           */ 1,    D0(48000),D1(48000),D2(48000),
368 /* --------------------- Audio 1.0 AS ISO Endpoint Descriptor */
369 /* 0  bLength, bDescriptorType                                */ 9,               0x05,
370 /* 2  bEndpointAddress, bmAttributes                          */ 0x81,            0x01,
371 /* 4  wMaxPacketSize, bInterval, bRefresh, bSynchAddress      */ D0(256),D1(256), 4,    0, 0,
372 /* ---------- Audio 1.0 AS ISO Audio Data Endpoint Descriptor */
373 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 7,                0x25, 0x01,
374 /* 3  bmAttributes                                            */ 0x00,
375 /* 5  bLockDelayUnits, wLockDelay                             */ 0x00, D0(0),D1(0),
376 /* --------------------- Audio 1.0 AS ISO Feedback Endpoint Descriptor */
377 /* 0  bLength, bDescriptorType                                */ 9,               0x05,
378 /* 2  bEndpointAddress, bmAttributes                          */ 0x01,            0x01,
379 /* 4  wMaxPacketSize, bInterval, bRefresh, bSynchAddress      */ D0(4),D1(4), 1,  4, 0x00,
380 
381 /* ------------------------------------ Interface Descriptor *//* 2 Stream OUT (9+9+7+11+9+7=52) */
382 /* 0 bLength, bDescriptorType                                */ 9,    0x04,
383 /* 2 bInterfaceNumber, bAlternateSetting                     */ 2,    0,
384 /* 4 bNumEndpoints                                           */ 0,
385 /* 5 bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */ 0x01, 0x02, 0x00,
386 /* 8 iInterface                                              */ 0,
387 /* ------------------------------------ Interface Descriptor */
388 /* 0 bLength, bDescriptorType                                */ 9,    0x04,
389 /* 2 bInterfaceNumber, bAlternateSetting                     */ 2,    1,
390 /* 4 bNumEndpoints                                           */ 2,
391 /* 5 bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */ 0x01, 0x02, 0x00,
392 /* 8 iInterface                                              */ 0,
393 /* ------------------------ Audio 1.0 AS Interface Descriptor */
394 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 7,    0x24, 0x01,
395 /* 3  bTerminalLink                                           */ 0x04,
396 /* 4  bDelay, wFormatTag                                      */ 0x00, D0(0x0001),D1(0x0001),
397 /* -------------------------- Audio AS Format Type Descriptor */
398 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 11,   0x24, 0x02,
399 /* 3  bFormatType, bNrChannels, bSubframeSize, bBitResolution */ 0x01, 0x02, 0x02, 16,
400 /* 7  bSamFreqType (n), tSamFreq[1] ... tSamFreq[n]           */ 1,    D0(48000),D1(48000),D2(48000),
401 /* --------------------- Audio 1.0 AS ISO Endpoint Descriptor */
402 /* 0  bLength, bDescriptorType                                */ 9,               0x05,
403 /* 2  bEndpointAddress, bmAttributes                          */ 0x02,            0x01,
404 /* 4  wMaxPacketSize, bInterval, bRefresh, bSynchAddress      */ D0(256),D1(256), 4,    0, 0,
405 /* ---------- Audio 1.0 AS ISO Audio Data Endpoint Descriptor */
406 /* 0  bLength, bDescriptorType, bDescriptorSubtype            */ 7,                0x25, 0x01,
407 /* 3  bmAttributes                                            */ 0x00,
408 /* 5  bLockDelayUnits, wLockDelay                             */ 0x00, D0(0),D1(0),
409 /* --------------------- Audio 1.0 AS ISO Feedback Endpoint Descriptor */
410 /* 0  bLength, bDescriptorType                                */ 9,               0x05,
411 /* 2  bEndpointAddress, bmAttributes                          */ 0x82,            0x01,
412 /* 4  wMaxPacketSize, bInterval, bRefresh, bSynchAddress      */ D0(4),D1(4), 1,  4, 0x00,
413 };
414 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
415 
416 static unsigned char string_framework[] = {
417 
418 /* Manufacturer string descriptor : Index 1 - "Express Logic" */
419     0x09, 0x04, 0x01, 0x0c,
420     0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
421     0x6f, 0x67, 0x69, 0x63,
422 
423 /* Product string descriptor : Index 2 - "EL Composite device" */
424     0x09, 0x04, 0x02, 0x13,
425     0x45, 0x4c, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x6f,
426     0x73, 0x69, 0x74, 0x65, 0x20, 0x64, 0x65, 0x76,
427     0x69, 0x63, 0x65,
428 
429 /* Serial Number string descriptor : Index 3 - "0001" */
430     0x09, 0x04, 0x03, 0x04,
431     0x30, 0x30, 0x30, 0x31
432 };
433 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
434 
435 
436 /* Multiple languages are supported on the device, to add
437     a language besides English, the Unicode language code must
438     be appended to the language_id_framework array and the length
439     adjusted accordingly. */
440 static unsigned char language_id_framework[] = {
441 
442 /* English. */
443     0x09, 0x04
444 };
445 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
446 
447 /* Setup requests */
448 
449 static UX_TEST_SETUP _SetConfigure = UX_TEST_SETUP_SetConfigure;
450 static UX_TEST_SETUP _GetCfgDescr  = UX_TEST_SETUP_GetCfgDescr;
451 static UX_TEST_SETUP _SetAddress = UX_TEST_SETUP_SetAddress;
452 static UX_TEST_SETUP _GetDeviceDescriptor = UX_TEST_SETUP_GetDevDescr;
453 static UX_TEST_SETUP _GetConfigDescriptor = UX_TEST_SETUP_GetCfgDescr;
454 
455 /* Interaction define */
456 
457 /* Hooks define */
458 
ux_device_class_audio_tx_hook(struct UX_TEST_ACTION_STRUCT * action,VOID * params)459 static VOID ux_device_class_audio_tx_hook(struct UX_TEST_ACTION_STRUCT *action, VOID *params)
460 {
461 
462 UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION_PARAMS *p = (UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION_PARAMS *)params;
463 UX_SLAVE_TRANSFER                                 *transfer = (UX_SLAVE_TRANSFER *)p -> parameter;
464 UCHAR                                              tmp[32] = {'i','n','s','e','r','t','A',0};
465 
466 
467     (void)params;
468     // printf("tTX\n");
469 
470     /* Acknowledge frame sent.  */
471     if (test_tx_ack_count)
472     {
473         SAVE_BUFFER_LOG(transfer -> ux_slave_transfer_request_data_pointer, transfer -> ux_slave_transfer_request_requested_length);
474         transfer -> ux_slave_transfer_request_actual_length = transfer -> ux_slave_transfer_request_requested_length;
475         transfer -> ux_slave_transfer_request_completion_code = UX_SUCCESS;
476         ux_test_dcd_sim_slave_transfer_done(transfer, UX_SUCCESS);
477     }
478     if (test_tx_ack_count != 0xFFFFFFFF && test_tx_ack_count > 0)
479         test_tx_ack_count --;
480 
481     /* Insert frames when sent.  */
482     if (test_tx_ins_count)
483     {
484         tmp[6] = (test_tx_ins_count % 26) + 'A';
485         if (test_tx_ins_way == 0)
486             ux_device_class_audio_frame_write(slave_audio_tx_stream, tmp, 32);
487         else
488         {
489 
490         UCHAR *frame;
491         ULONG frame_length;
492 
493 
494             ux_device_class_audio_write_frame_get(slave_audio_tx_stream, &frame, &frame_length);
495             _ux_utility_memory_copy(frame, tmp, 32);
496             ux_device_class_audio_write_frame_commit(slave_audio_tx_stream, 32);
497         }
498     }
499     if (test_tx_ins_count != 0xFFFFFFFF && test_tx_ins_count > 0)
500         test_tx_ins_count --;
501 }
502 
ux_device_class_audio_rx_hook(struct UX_TEST_ACTION_STRUCT * action,VOID * params)503 static VOID ux_device_class_audio_rx_hook(struct UX_TEST_ACTION_STRUCT *action, VOID *params)
504 {
505 
506 UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION_PARAMS *p = (UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION_PARAMS *)params;
507 UX_SLAVE_TRANSFER                                 *transfer = (UX_SLAVE_TRANSFER *)p -> parameter;
508 
509 
510     (void)action;
511     (void)params;
512     (void)p;
513     (void)transfer;
514     // printf("tRX\n");
515     slave_audio_rx_transfer = transfer;
516 }
517 
ux_device_class_audio_feedback_hook(struct UX_TEST_ACTION_STRUCT * action,VOID * params)518 static VOID ux_device_class_audio_feedback_hook(struct UX_TEST_ACTION_STRUCT *action, VOID *params)
519 {
520 
521 UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION_PARAMS *p = (UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION_PARAMS *)params;
522 UX_SLAVE_TRANSFER                                 *transfer = (UX_SLAVE_TRANSFER *)p -> parameter;
523 ULONG                                             length;
524 
525     (void)action;
526     (void)params;
527     (void)p;
528     (void)transfer;
529     length = UX_MIN(4, transfer->ux_slave_transfer_request_requested_length);
530     if (transfer->ux_slave_transfer_request_endpoint->ux_slave_endpoint_descriptor.bEndpointAddress & 0x80)
531     {
532         device_audio_rx_feedback_count ++;
533         /* IN for OUT(RX/read) feedback.  */
534         device_audio_rx_feedback = 0;
535         _ux_utility_memory_copy(device_audio_rx_feedback_u8a,
536             transfer->ux_slave_transfer_request_data_pointer, length);
537         transfer -> ux_slave_transfer_request_completion_code = UX_SUCCESS;
538         transfer -> ux_slave_transfer_request_actual_length = length;
539     }
540     else
541     {
542         device_audio_tx_feedback_count ++;
543         /* OUT for IN(TX/write) feedback.  */
544         _ux_utility_memory_copy(transfer->ux_slave_transfer_request_data_pointer,
545            device_audio_tx_feedback_u8a, length);
546         transfer -> ux_slave_transfer_request_completion_code = UX_SUCCESS;
547         transfer -> ux_slave_transfer_request_actual_length = length;
548     }
549     ux_test_dcd_sim_slave_transfer_done(transfer, UX_SUCCESS);
550     _ux_utility_thread_sleep(1);
551 }
552 
slave_audio_rx_simulate_one_frame(UCHAR * frame,ULONG frame_length)553 static VOID slave_audio_rx_simulate_one_frame(UCHAR *frame, ULONG frame_length)
554 {
555     UX_TEST_ASSERT(slave_audio_rx_transfer);
556     if (frame_length)
557     {
558         _ux_utility_memory_copy(slave_audio_rx_transfer->ux_slave_transfer_request_data_pointer, frame, frame_length);
559         slave_audio_rx_transfer->ux_slave_transfer_request_actual_length = frame_length;
560         slave_audio_rx_transfer->ux_slave_transfer_request_completion_code = UX_SUCCESS;
561     }
562     ux_test_dcd_sim_slave_transfer_done(slave_audio_rx_transfer, UX_SUCCESS);
563     _ux_utility_delay_ms(100);
564 }
565 
566 static UX_TEST_ACTION ux_device_class_audio_transfer_hook[] =
567 {
568     {
569         .usbx_function = UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION,
570         .function = UX_DCD_TRANSFER_REQUEST,
571         .action_func = ux_device_class_audio_tx_hook,
572         .req_setup = UX_NULL,
573         .req_action = UX_TEST_MATCH_EP,
574         .req_ep_address = 0x81,
575         .do_after = UX_FALSE,
576         .no_return = UX_FALSE,
577     },
578     {
579         .usbx_function = UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION,
580         .function = UX_DCD_TRANSFER_REQUEST,
581         .action_func = ux_device_class_audio_rx_hook,
582         .req_setup = UX_NULL,
583         .req_action = UX_TEST_MATCH_EP,
584         .req_ep_address = 0x02,
585         .do_after = UX_FALSE,
586         .no_return = UX_FALSE,
587     },
588     {
589         .usbx_function = UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION,
590         .function = UX_DCD_TRANSFER_REQUEST,
591         .action_func = ux_device_class_audio_feedback_hook,
592         .req_setup = UX_NULL,
593         .req_action = UX_TEST_MATCH_EP,
594         .req_ep_address = 0x82,
595         .req_status = UX_SUCCESS,
596         .status = UX_SUCCESS,
597         .do_after = UX_FALSE,
598         .no_return = UX_FALSE,
599     },
600     {
601         .usbx_function = UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION,
602         .function = UX_DCD_TRANSFER_REQUEST,
603         .action_func = ux_device_class_audio_feedback_hook,
604         .req_setup = UX_NULL,
605         .req_action = UX_TEST_MATCH_EP,
606         .req_ep_address = 0x01,
607         .req_status = UX_SUCCESS,
608         .status = UX_SUCCESS,
609         .do_after = UX_FALSE,
610         .no_return = UX_FALSE,
611     },
612 { 0 },
613 };
614 
615 /* Define the ISR dispatch.  */
616 
617 extern VOID    (*test_isr_dispatch)(void);
618 
619 
620 /* Prototype for test control return.  */
621 
622 void  test_control_return(UINT status);
623 
error_callback(UINT system_level,UINT system_context,UINT error_code)624 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
625 {
626 
627     error_callback_counter ++;
628     // printf("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
629 
630     if (!error_callback_ignore)
631     {
632         {
633             /* Failed test.  */
634             test_control_return(1);
635         }
636     }
637 }
638 
sleep_break_on_error(VOID)639 static UINT  sleep_break_on_error(VOID)
640 {
641 
642     if (error_callback_counter >= 3)
643         return error_callback_counter;
644 
645     return UX_SUCCESS;
646 }
647 
648 /* Define the ISR dispatch routine.  */
649 
test_isr(void)650 static void    test_isr(void)
651 {
652 
653     /* For further expansion of interrupt-level testing.  */
654 }
655 
slave_audio_tx_activate(VOID * audio_instance)656 static VOID    slave_audio_tx_activate(VOID *audio_instance)
657 {
658     slave_audio_tx = (UX_DEVICE_CLASS_AUDIO *)audio_instance;
659     ux_device_class_audio_stream_get(slave_audio_tx, 0, &slave_audio_tx_stream);
660     // printf("sAUD_tx:%p,%p\n", audio_instance, slave_audio_tx_stream);
661 }
slave_audio_rx_activate(VOID * audio_instance)662 static VOID    slave_audio_rx_activate(VOID *audio_instance)
663 {
664     slave_audio_rx = (UX_DEVICE_CLASS_AUDIO *)audio_instance;
665     ux_device_class_audio_stream_get(slave_audio_rx, 0, &slave_audio_rx_stream);
666     // printf("sAUD_rx:%p,%p\n", audio_instance, slave_audio_rx_stream);
667 }
slave_audio_deactivate(VOID * audio_instance)668 static VOID    slave_audio_deactivate(VOID *audio_instance)
669 {
670     if ((VOID *)slave_audio_rx == audio_instance)
671     {
672         slave_audio_rx = UX_NULL;
673         slave_audio_rx_stream = UX_NULL;
674     }
675     if ((VOID *)slave_audio_tx == audio_instance)
676         slave_audio_tx = UX_NULL;
677         slave_audio_tx_stream = UX_NULL;
678 }
slave_audio_tx_stream_change(UX_DEVICE_CLASS_AUDIO_STREAM * audio,ULONG alt)679 static VOID    slave_audio_tx_stream_change(UX_DEVICE_CLASS_AUDIO_STREAM *audio, ULONG alt)
680 {
681     SAVE_CALLBACK_INVOKE_LOG(slave_audio_tx_stream_change, audio, (ALIGN_TYPE)alt, 0);
682 }
slave_audio_rx_stream_change(UX_DEVICE_CLASS_AUDIO_STREAM * audio,ULONG alt)683 static VOID    slave_audio_rx_stream_change(UX_DEVICE_CLASS_AUDIO_STREAM *audio, ULONG alt)
684 {
685     SAVE_CALLBACK_INVOKE_LOG(slave_audio_rx_stream_change, audio, (ALIGN_TYPE)alt, 0);
686 
687     slave_audio_rx_transfer = UX_NULL;
688 }
slave_audio_control_process(UX_DEVICE_CLASS_AUDIO * audio,UX_SLAVE_TRANSFER * transfer)689 static UINT    slave_audio_control_process(UX_DEVICE_CLASS_AUDIO *audio, UX_SLAVE_TRANSFER *transfer)
690 {
691     SAVE_CALLBACK_INVOKE_LOG(slave_audio_control_process, audio, transfer, 0);
692     return(UX_ERROR);
693 }
slave_audio_tx_done(UX_DEVICE_CLASS_AUDIO_STREAM * audio,ULONG length)694 static VOID    slave_audio_tx_done(UX_DEVICE_CLASS_AUDIO_STREAM *audio, ULONG length)
695 {
696     SAVE_CALLBACK_INVOKE_LOG(slave_audio_tx_done, audio, (ALIGN_TYPE)length, 0);
697 }
slave_audio_rx_done(UX_DEVICE_CLASS_AUDIO_STREAM * audio,ULONG length)698 static VOID    slave_audio_rx_done(UX_DEVICE_CLASS_AUDIO_STREAM *audio, ULONG length)
699 {
700     SAVE_CALLBACK_INVOKE_LOG(slave_audio_rx_done, audio, (ALIGN_TYPE)length, 0);
701 }
702 
test_host_change_function(ULONG event,UX_HOST_CLASS * cls,VOID * inst)703 static UINT test_host_change_function(ULONG event, UX_HOST_CLASS *cls, VOID *inst)
704 {
705 
706 UX_HOST_CLASS_DUMMY *dummy = (UX_HOST_CLASS_DUMMY *) inst;
707 
708 
709     switch(event)
710     {
711 
712         case UX_DEVICE_INSERTION:
713 
714             // printf("hINS:%p,%p:%ld\n", cls, inst, dummy -> ux_host_class_dummy_interface -> ux_interface_descriptor.bInterfaceNumber);
715             switch(dummy -> ux_host_class_dummy_interface -> ux_interface_descriptor.bInterfaceNumber)
716             {
717             case 0: dummy_control = dummy; break;
718             case 1: dummy_rx      = dummy; break;
719             case 2: dummy_tx      = dummy; break;
720             }
721             break;
722 
723         case UX_DEVICE_REMOVAL:
724 
725             // printf("hRMV:%p,%p:%ld\n", cls, inst, dummy -> ux_host_class_dummy_interface -> ux_interface_descriptor.bInterfaceNumber);
726             switch(dummy -> ux_host_class_dummy_interface -> ux_interface_descriptor.bInterfaceNumber)
727             {
728             case 0: dummy_control = UX_NULL; break;
729             case 1: dummy_rx      = UX_NULL; break;
730             case 2: dummy_tx      = UX_NULL; break;
731             }
732             break;
733 
734         default:
735             break;
736     }
737     return 0;
738 }
739 
740 
ux_test_hcd_entry_set_cfg(UX_TEST_ACTION * action,VOID * params)741 static VOID ux_test_hcd_entry_set_cfg(UX_TEST_ACTION *action, VOID *params)
742 {
743 
744     set_cfg_counter ++;
745 
746     rsc_mem_free_on_set_cfg = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available;
747     rsc_sem_on_set_cfg = ux_test_utility_sim_sem_create_count();
748     rsc_mutex_on_set_cfg = ux_test_utility_sim_mutex_create_count();
749 }
750 
751 
752 /* Define what the initial system looks like.  */
753 
754 #ifdef CTEST
test_application_define(void * first_unused_memory)755 void test_application_define(void *first_unused_memory)
756 #else
757 void    usbx_audio10_device_feedback_test_application_define(void *first_unused_memory)
758 #endif
759 {
760 
761 UINT                    status;
762 CHAR *                  stack_pointer;
763 CHAR *                  memory_pointer;
764 
765 
766     /* Inform user.  */
767 #if !UX_TEST_MULTI_IFC_ON || !UX_TEST_MULTI_ALT_ON || !UX_TEST_MULTI_CLS_ON || !UX_TEST_MULTI_EP_OVER(4) || !defined(UX_DEVICE_BIDIRECTIONAL_ENDPOINT_SUPPORT)
768     printf("Running Audio 1.0 Device Feedback Functionality Test................SKIP SUCCESS!\n");
769     test_control_return(0);
770     return;
771 #endif
772 
773     printf("Running Audio 1.0 Device Feedback Functionality Test................ ");
774 
775     stepinfo("\n");
776 
777     /* Reset testing counts. */
778     ux_test_utility_sim_mutex_create_count_reset();
779     ux_test_utility_sim_sem_create_count_reset();
780     ux_test_utility_sim_sem_get_count_reset();
781     /* Reset error generations */
782     ux_test_utility_sim_sem_error_generation_stop();
783     ux_test_utility_sim_mutex_error_generation_stop();
784     ux_test_utility_sim_sem_get_error_generation_stop();
785 
786     /* Initialize the free memory pointer */
787     stack_pointer = (CHAR *) usbx_memory;
788     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
789 
790     /* Initialize USBX Memory */
791     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
792     UX_TEST_CHECK_SUCCESS(status);
793 
794     /* Register the error callback. */
795     _ux_utility_error_callback_register(error_callback);
796 
797     /* The code below is required for installing the host portion of USBX */
798     status =  ux_host_stack_initialize(test_host_change_function);
799     UX_TEST_CHECK_SUCCESS(status);
800 
801     /* Register Audio class.  */
802     status  = ux_host_stack_class_register(_ux_host_class_dummy_name, _ux_host_class_dummy_entry);
803     UX_TEST_CHECK_SUCCESS(status);
804 
805     /* The code below is required for installing the device portion of USBX. No call back for
806        device status change in this example. */
807     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
808                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
809                                        string_framework, STRING_FRAMEWORK_LENGTH,
810                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
811     UX_TEST_CHECK_SUCCESS(status);
812 
813     /* Set the parameters for callback when insertion/extraction of a Audio 1.0 device, no IAD.  */
814     ux_utility_memory_set(&slave_audio_tx_stream_parameter, 0, sizeof(slave_audio_tx_stream_parameter));
815     ux_utility_memory_set(&slave_audio_rx_stream_parameter, 0, sizeof(slave_audio_rx_stream_parameter));
816 #if !defined(UX_DEVICE_STANDALONE)
817     slave_audio_tx_stream_parameter.ux_device_class_audio_stream_parameter_thread_entry = ux_device_class_audio_write_thread_entry;
818 #else
819     slave_audio_tx_stream_parameter.ux_device_class_audio_stream_parameter_task_function = ux_device_class_audio_write_task_function;
820 #endif
821 #if defined(UX_DEVICE_CLASS_AUDIO_FEEDBACK_SUPPORT)
822 #if !defined(UX_DEVICE_STANDALONE)
823     slave_audio_tx_stream_parameter.ux_device_class_audio_stream_parameter_feedback_thread_entry = ux_device_class_audio_feedback_thread_entry;
824 #else
825     slave_audio_tx_stream_parameter.ux_device_class_audio_stream_parameter_feedback_task_function = _ux_device_class_audio_feedback_task_function;
826 #endif
827 #endif
828     slave_audio_tx_stream_parameter.ux_device_class_audio_stream_parameter_callbacks.ux_device_class_audio_stream_change = slave_audio_tx_stream_change;
829     slave_audio_tx_stream_parameter.ux_device_class_audio_stream_parameter_callbacks.ux_device_class_audio_stream_frame_done = slave_audio_tx_done;
830     slave_audio_tx_stream_parameter.ux_device_class_audio_stream_parameter_max_frame_buffer_size = 256;
831     slave_audio_tx_stream_parameter.ux_device_class_audio_stream_parameter_max_frame_buffer_nb   = 8;
832     slave_audio_tx_parameter.ux_device_class_audio_parameter_streams = &slave_audio_tx_stream_parameter;
833     slave_audio_tx_parameter.ux_device_class_audio_parameter_streams_nb = 1;
834     slave_audio_tx_parameter.ux_device_class_audio_parameter_callbacks.ux_slave_class_audio_instance_activate   = slave_audio_tx_activate;
835     slave_audio_tx_parameter.ux_device_class_audio_parameter_callbacks.ux_slave_class_audio_instance_deactivate = slave_audio_deactivate;
836     slave_audio_tx_parameter.ux_device_class_audio_parameter_callbacks.ux_device_class_audio_control_process = slave_audio_control_process;
837     slave_audio_tx_parameter.ux_device_class_audio_parameter_callbacks.ux_device_class_audio_arg             = UX_NULL;
838 
839 #if !defined(UX_DEVICE_STANDALONE)
840     slave_audio_rx_stream_parameter.ux_device_class_audio_stream_parameter_thread_entry = ux_device_class_audio_read_thread_entry;
841 #else
842     slave_audio_rx_stream_parameter.ux_device_class_audio_stream_parameter_task_function = ux_device_class_audio_read_task_function;
843 #endif
844 #if defined(UX_DEVICE_CLASS_AUDIO_FEEDBACK_SUPPORT)
845 #if !defined(UX_DEVICE_STANDALONE)
846     slave_audio_rx_stream_parameter.ux_device_class_audio_stream_parameter_feedback_thread_entry = ux_device_class_audio_feedback_thread_entry;
847 #else
848     slave_audio_rx_stream_parameter.ux_device_class_audio_stream_parameter_feedback_task_function = _ux_device_class_audio_feedback_task_function;
849 #endif
850 #endif
851     slave_audio_rx_stream_parameter.ux_device_class_audio_stream_parameter_callbacks.ux_device_class_audio_stream_change = slave_audio_rx_stream_change;
852     slave_audio_rx_stream_parameter.ux_device_class_audio_stream_parameter_callbacks.ux_device_class_audio_stream_frame_done = slave_audio_rx_done;
853     slave_audio_rx_stream_parameter.ux_device_class_audio_stream_parameter_max_frame_buffer_size = 256;
854     slave_audio_rx_stream_parameter.ux_device_class_audio_stream_parameter_max_frame_buffer_nb   = 8;
855     slave_audio_rx_parameter.ux_device_class_audio_parameter_streams = &slave_audio_rx_stream_parameter;
856     slave_audio_rx_parameter.ux_device_class_audio_parameter_streams_nb = 1;
857     slave_audio_rx_parameter.ux_device_class_audio_parameter_callbacks.ux_slave_class_audio_instance_activate   = slave_audio_rx_activate;
858     slave_audio_rx_parameter.ux_device_class_audio_parameter_callbacks.ux_slave_class_audio_instance_deactivate = slave_audio_deactivate;
859     slave_audio_rx_parameter.ux_device_class_audio_parameter_callbacks.ux_device_class_audio_control_process = slave_audio_control_process;
860     slave_audio_rx_parameter.ux_device_class_audio_parameter_callbacks.ux_device_class_audio_arg             = UX_NULL;
861 
862 #if defined(UX_DEVICE_CLASS_AUDIO_INTERRUPT_SUPPORT)
863     slave_audio_tx_parameter.ux_device_class_audio_parameter_status_queue_size = 2;
864     slave_audio_tx_parameter.ux_device_class_audio_parameter_status_size = 8; /* Actually 6 bytes in length.  */
865     slave_audio_rx_parameter.ux_device_class_audio_parameter_status_queue_size = 2;
866     slave_audio_rx_parameter.ux_device_class_audio_parameter_status_size = 8; /* Actually 6 bytes in length.  */
867 #endif
868 
869     /* Initialize the device Audio class. This class owns interfaces starting with 1, 2. */
870     status  = ux_device_stack_class_register(_ux_system_slave_class_audio_name, ux_device_class_audio_entry,
871                                              1, 1,  &slave_audio_tx_parameter);
872     status |= ux_device_stack_class_register(_ux_system_slave_class_audio_name, ux_device_class_audio_entry,
873                                              1, 2,  &slave_audio_rx_parameter);
874 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
875     UX_TEST_CHECK_SUCCESS(status);
876 #endif
877 
878     /* Initialize the simulated device controller.  */
879     status =  _ux_test_dcd_sim_slave_initialize();
880     UX_TEST_CHECK_SUCCESS(status);
881 
882     /* Register all the USB host controllers available in this system */
883     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
884     UX_TEST_CHECK_SUCCESS(status);
885 
886     /* Create the main host simulation thread.  */
887     status =  tx_thread_create(&tx_test_thread_host_simulation, "tx demo host simulation", tx_test_thread_host_simulation_entry, 0,
888             stack_pointer, UX_DEMO_STACK_SIZE,
889             20, 20, 1, TX_AUTO_START);
890     UX_TEST_CHECK_SUCCESS(status);
891 
892     /* Create the main slave simulation  thread.  */
893     status =  tx_thread_create(&tx_test_thread_slave_simulation, "tx demo slave simulation", tx_test_thread_slave_simulation_entry, 0,
894             stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
895             20, 20, 1, TX_AUTO_START);
896     UX_TEST_CHECK_SUCCESS(status);
897 }
898 
test_wait_until_expected(VOID ** ptr,ULONG loop,VOID * expected)899 static UINT test_wait_until_expected(VOID **ptr, ULONG loop, VOID *expected)
900 {
901     while(loop)
902     {
903         _ux_utility_delay_ms(10);
904         if (*ptr == expected)
905             return UX_SUCCESS;
906     }
907     return UX_ERROR;
908 }
test_wait_until_not_expected(VOID ** ptr,ULONG loop,VOID * expected)909 static UINT test_wait_until_not_expected(VOID **ptr, ULONG loop, VOID *expected)
910 {
911     while(loop)
912     {
913         _ux_utility_delay_ms(10);
914         if (*ptr != expected)
915             return UX_SUCCESS;
916     }
917     return UX_ERROR;
918 }
919 #define test_wait_until_not_null(ptr, loop) test_wait_until_not_expected(ptr, loop, UX_NULL)
920 #define test_wait_until_null(ptr, loop) test_wait_until_expected(ptr, loop, UX_NULL)
921 
tx_test_thread_host_simulation_entry(ULONG arg)922 void  tx_test_thread_host_simulation_entry(ULONG arg)
923 {
924 
925 UINT                                                status;
926 ULONG                                               test_n;
927 UX_DEVICE                                           *device;
928 UX_CONFIGURATION                                    *configuration;
929 UX_INTERFACE                                        *interface;
930 UX_INTERFACE                                        *interface_inst[3][2];
931 UCHAR                                               test_cmp[32];
932 ULONG                                               temp;
933 
934 
935     /* Test connect.  */
936     status  = test_wait_until_not_null((void**)&dummy_control, 100);
937     status |= test_wait_until_not_null((void**)&dummy_tx, 100);
938     status |= test_wait_until_not_null((void**)&dummy_rx, 100);
939     status |= test_wait_until_not_null((void**)&slave_audio_tx, 100);
940     status |= test_wait_until_not_null((void**)&slave_audio_rx, 100);
941     UX_TEST_CHECK_SUCCESS(status);
942 
943     /* Get testing instances.  */
944 
945     status = ux_host_stack_device_get(0, &device);
946     UX_TEST_CHECK_SUCCESS(status);
947 
948     status = ux_host_stack_device_configuration_get(device, 0, &configuration);
949     UX_TEST_CHECK_SUCCESS(status);
950 
951     interface = configuration -> ux_configuration_first_interface;
952     while(interface)
953     {
954         // printf("Interface: %ld.%ld\n", interface -> ux_interface_descriptor.bInterfaceNumber, interface -> ux_interface_descriptor.bAlternateSetting);
955         interface_inst[interface -> ux_interface_descriptor.bInterfaceNumber][interface -> ux_interface_descriptor.bAlternateSetting] = interface;
956         interface = interface -> ux_interface_next_interface;
957     }
958 
959     ux_test_link_hooks_from_array(ux_device_class_audio_transfer_hook);
960 
961     /* Test interface change.  */
962     /* Reset log.  */
963     RESET_CALLBACK_INVOKE_LOG();
964     status  = ux_host_stack_interface_setting_select(interface_inst[1][1]);
965     status |= ux_host_stack_interface_setting_select(interface_inst[1][0]);
966     status |= ux_host_stack_interface_setting_select(interface_inst[1][1]);
967 
968     UX_TEST_CHECK_SUCCESS(status);
969     UX_TEST_ASSERT(callback_invoke_count == 3);
970     UX_TEST_ASSERT(callback_invoke_log[0].func == slave_audio_tx_stream_change);
971     UX_TEST_ASSERT(callback_invoke_log[0].param1 == slave_audio_tx_stream);
972     UX_TEST_ASSERT(callback_invoke_log[0].param2 == (VOID*)1);
973     UX_TEST_ASSERT(callback_invoke_log[1].func == slave_audio_tx_stream_change);
974     UX_TEST_ASSERT(callback_invoke_log[1].param1 == slave_audio_tx_stream);
975     UX_TEST_ASSERT(callback_invoke_log[1].param2 == 0);
976 
977     RESET_CALLBACK_INVOKE_LOG();
978     status  = ux_host_stack_interface_setting_select(interface_inst[2][1]);
979     status |= ux_host_stack_interface_setting_select(interface_inst[2][0]);
980     status |= ux_host_stack_interface_setting_select(interface_inst[2][1]);
981 
982     UX_TEST_CHECK_SUCCESS(status);
983     UX_TEST_ASSERT(callback_invoke_count == 3);
984     UX_TEST_ASSERT(callback_invoke_log[0].func == slave_audio_rx_stream_change);
985     UX_TEST_ASSERT(callback_invoke_log[0].param1 == slave_audio_rx_stream);
986     UX_TEST_ASSERT(callback_invoke_log[0].param2 == (VOID*)1);
987     UX_TEST_ASSERT(callback_invoke_log[1].func == slave_audio_rx_stream_change);
988     UX_TEST_ASSERT(callback_invoke_log[1].param1 == slave_audio_rx_stream);
989     UX_TEST_ASSERT(callback_invoke_log[1].param2 == 0);
990 
991 
992 #if defined(UX_DEVICE_CLASS_AUDIO_FEEDBACK_SUPPORT)
993     /* Test feedbacks.  */
994 
995     /* Read/write feedback test, pass any data of 4 bytes.  */
996     _ux_system_slave->ux_system_slave_speed = UX_HIGH_SPEED_DEVICE;
997     slave_audio_rx_stream->ux_device_class_audio_stream_feedback->ux_slave_endpoint_transfer_request.ux_slave_transfer_request_requested_length = 4;
998     slave_audio_tx_stream->ux_device_class_audio_stream_feedback->ux_slave_endpoint_transfer_request.ux_slave_transfer_request_requested_length = 4;
999     temp = 0x1234567;
1000     ux_device_class_audio_feedback_set(slave_audio_rx_stream, (UCHAR *)&temp);
1001     temp = device_audio_rx_feedback_count;
1002     while(device_audio_rx_feedback_count == temp)
1003         tx_thread_sleep(1);
1004     UX_TEST_ASSERT(device_audio_rx_feedback == 0x1234567);
1005 
1006     device_audio_tx_feedback = 0xabcd1234;
1007     temp = device_audio_tx_feedback_count;
1008     while(device_audio_tx_feedback_count != (temp + 2))
1009         tx_thread_sleep(1);
1010     status = ux_device_class_audio_feedback_get(slave_audio_tx_stream, (UCHAR *)&temp);
1011     UX_TEST_CHECK_SUCCESS(status);
1012     UX_TEST_ASSERT(temp == 0xabcd1234);
1013 
1014     /* Read/write feedback test full speed, pass any data of 3 bytes.  */
1015     _ux_system_slave->ux_system_slave_speed = UX_FULL_SPEED_DEVICE;
1016     slave_audio_rx_stream->ux_device_class_audio_stream_feedback->ux_slave_endpoint_transfer_request.ux_slave_transfer_request_requested_length = 3;
1017     slave_audio_tx_stream->ux_device_class_audio_stream_feedback->ux_slave_endpoint_transfer_request.ux_slave_transfer_request_requested_length = 3;
1018     temp = 0x7654321;
1019     ux_device_class_audio_feedback_set(slave_audio_rx_stream, (UCHAR *)&temp);
1020     temp = device_audio_rx_feedback_count;
1021     while(device_audio_rx_feedback_count == temp)
1022         tx_thread_sleep(1);
1023     UX_TEST_ASSERT(device_audio_rx_feedback == 0x654321);
1024 
1025     device_audio_tx_feedback = 0x4321dcba;
1026     temp = device_audio_tx_feedback_count;
1027     while(device_audio_tx_feedback_count != (temp + 2))
1028         tx_thread_sleep(1);
1029     status = ux_device_class_audio_feedback_get(slave_audio_tx_stream, (UCHAR *)&temp);
1030     UX_TEST_CHECK_SUCCESS(status);
1031     UX_TEST_ASSERT(temp == 0x21dcba);
1032 #endif
1033 
1034 
1035     /* Test data streaming.  */
1036 
1037     /* Wrong interface!  */
1038     status  = ux_device_class_audio_transmission_start(slave_audio_rx_stream);
1039     status |= ux_device_class_audio_reception_start(slave_audio_tx_stream);
1040     UX_TEST_CHECK_NOT_SUCCESS(status);
1041 
1042     /* ------------------ Write test.  */
1043 
1044     status  = ux_device_class_audio_frame_write(slave_audio_tx_stream, "test0", 16);
1045     status |= ux_device_class_audio_frame_write(slave_audio_tx_stream, "test1", 16);
1046     status |= ux_device_class_audio_frame_write(slave_audio_tx_stream, "test2", 16);
1047     status |= ux_device_class_audio_frame_write(slave_audio_tx_stream, "test3", 16);
1048     status |= ux_device_class_audio_frame_write(slave_audio_tx_stream, "test4", 16);
1049     status |= ux_device_class_audio_frame_write(slave_audio_tx_stream, "test5", 16);
1050     status |= ux_device_class_audio_frame_write(slave_audio_tx_stream, "test6", 16);
1051     status |= ux_device_class_audio_frame_write(slave_audio_tx_stream, "test7", 16);
1052     UX_TEST_CHECK_SUCCESS(status);
1053 
1054     error_callback_counter = 0;
1055     status = ux_device_class_audio_frame_write(slave_audio_tx_stream, "test8", 16);
1056     UX_TEST_CHECK_CODE(UX_BUFFER_OVERFLOW, status);
1057 
1058     /*
1059      Keep sending until under-run.
1060      No buffer appended while running.
1061      */
1062 
1063     buffer_log_count = 0;
1064     test_tx_ack_count = 10;
1065     status  = ux_device_class_audio_transmission_start(slave_audio_tx_stream);
1066     UX_TEST_CHECK_SUCCESS(status);
1067 
1068     /* Delay to let thread runs.  */
1069     _ux_utility_delay_ms(500);
1070 
1071     /* Prepare data for checking.  */
1072     _ux_utility_memory_set(test_cmp, 0, 16);
1073     _ux_utility_memory_copy(test_cmp, "test", 4);
1074 
1075     /* 8 frame should be available.  */
1076     for (test_n = 0; test_n < 8; test_n ++)
1077     {
1078         // printf("%ld: %ld: %s\n", test_n, buffer_log[test_n].length, buffer_log[test_n].data);
1079         test_cmp[4] = (UCHAR)(test_n + '0');
1080 
1081         UX_TEST_ASSERT(buffer_log[test_n].length == 16);
1082 
1083         status = _ux_utility_memory_compare(buffer_log[test_n].data, test_cmp, 6);
1084         UX_TEST_CHECK_SUCCESS(status);
1085     }
1086     /* Then under-run, 0 length packets.  */
1087     for(;test_n < buffer_log_count; test_n ++)
1088     {
1089         // printf("%ld: %ld\n", test_n, buffer_log[test_n].length);
1090         UX_TEST_ASSERT(buffer_log[test_n].length == 0);
1091     }
1092 
1093     /*
1094      Keep sending until under-run.
1095      Specific number of buffer appended while running.
1096      */
1097 
1098     for (test_tx_ins_way = 0; test_tx_ins_way < 2; test_tx_ins_way ++)
1099     {
1100 
1101         /* Switch interface to clean up status.  */
1102         status  = ux_host_stack_interface_setting_select(interface_inst[1][0]);
1103         status |= ux_host_stack_interface_setting_select(interface_inst[1][1]);
1104 
1105         status  = ux_device_class_audio_frame_write(slave_audio_tx_stream, "test0", 20);
1106         status |= ux_device_class_audio_frame_write(slave_audio_tx_stream, "test1", 20);
1107         status |= ux_device_class_audio_frame_write(slave_audio_tx_stream, "test2", 20);
1108         status |= ux_device_class_audio_frame_write(slave_audio_tx_stream, "test3", 20);
1109         UX_TEST_CHECK_SUCCESS(status);
1110 
1111         test_tx_ins_count = 10;
1112         test_tx_ack_count = 20;
1113         buffer_log_count  = 0;
1114         status  = ux_device_class_audio_transmission_start(slave_audio_tx_stream);
1115         UX_TEST_CHECK_SUCCESS(status);
1116 
1117         /* Delay to let thread runs.  */
1118         _ux_utility_delay_ms(500);
1119 
1120         /* Prepare data for checking.  */
1121         _ux_utility_memory_set(test_cmp, 0, 32);
1122 
1123         /* 4 frame should be available.  */
1124         _ux_utility_memory_copy(test_cmp, "test", 4);
1125         for (test_n = 0; test_n < 4; test_n ++)
1126         {
1127             // printf("%ld: %ld: %s\n", test_n, buffer_log[test_n].length, buffer_log[test_n].data);
1128             test_cmp[4] = (UCHAR)(test_n + '0');
1129 
1130             UX_TEST_ASSERT(buffer_log[test_n].length == 20);
1131 
1132             status = _ux_utility_memory_compare(buffer_log[test_n].data, test_cmp, 6);
1133             UX_TEST_CHECK_SUCCESS(status);
1134         }
1135         /* 10 more frame should be available.  */
1136         _ux_utility_memory_copy(test_cmp, "insert", 6);
1137         for (; test_n < 14; test_n ++)
1138         {
1139             // printf("%ld: %ld: %s\n", test_n, buffer_log[test_n].length, buffer_log[test_n].data);
1140             test_cmp[6] = (UCHAR)(((10 - (test_n - 4)) % 26) + 'A');
1141 
1142             UX_TEST_ASSERT(buffer_log[test_n].length == 32);
1143 
1144             status = _ux_utility_memory_compare(buffer_log[test_n].data, test_cmp, 8);
1145             UX_TEST_CHECK_SUCCESS(status);
1146         }
1147         /* Then under-run, 0 length packets.  */
1148         for(;test_n < buffer_log_count; test_n ++)
1149         {
1150             // printf("%ld: %ld\n", test_n, buffer_log[test_n].length);
1151             UX_TEST_ASSERT(buffer_log[test_n].length == 0);
1152         }
1153     }
1154 
1155     UX_TEST_ASSERT(slave_audio_tx_stream->ux_device_class_audio_stream_transfer_pos == slave_audio_tx_stream->ux_device_class_audio_stream_access_pos);
1156 
1157     /* ------------------ Read - 8 test.  */
1158 
1159     UX_TEST_ASSERT(slave_audio_rx_transfer == UX_NULL);
1160 
1161     temp = 0;
1162     status = ux_device_class_audio_sample_read8(slave_audio_rx_stream, (UCHAR *)&temp);
1163     UX_TEST_CHECK_CODE(UX_BUFFER_OVERFLOW, status);
1164 
1165     status = ux_device_class_audio_reception_start(slave_audio_rx_stream);
1166     UX_TEST_CHECK_SUCCESS(status);
1167     _ux_utility_thread_sleep(1);
1168 
1169     UX_TEST_ASSERT(slave_audio_rx_transfer != UX_NULL);
1170 
1171     status = ux_device_class_audio_sample_read8(slave_audio_rx_stream, (UCHAR *)&temp);
1172     UX_TEST_CHECK_CODE(UX_BUFFER_OVERFLOW, status);
1173 
1174     RESET_CALLBACK_INVOKE_LOG();
1175 
1176     slave_audio_rx_simulate_one_frame("012345", 6);
1177     for (test_n = 0; test_n < 6; test_n ++)
1178     {
1179         status = ux_device_class_audio_sample_read8(slave_audio_rx_stream, (UCHAR *)&temp);
1180         UX_TEST_CHECK_SUCCESS(status);
1181         UX_TEST_ASSERT(temp == (test_n + '0'));
1182     }
1183 
1184     slave_audio_rx_simulate_one_frame("012345", 6);
1185     slave_audio_rx_simulate_one_frame("67", 2);
1186     slave_audio_rx_simulate_one_frame("89", 2);
1187 
1188     for (test_n = 0; test_n < 10; test_n ++)
1189     {
1190         status = ux_device_class_audio_sample_read8(slave_audio_rx_stream, (UCHAR *)&temp);
1191         UX_TEST_CHECK_SUCCESS(status);
1192         UX_TEST_ASSERT(temp == (test_n + '0'));
1193     }
1194 
1195     UX_TEST_ASSERT(callback_invoke_count == 4);
1196     UX_TEST_ASSERT(callback_invoke_log[0].func == slave_audio_rx_done);
1197     UX_TEST_ASSERT(callback_invoke_log[0].param1 == slave_audio_rx_stream);
1198     UX_TEST_ASSERT(callback_invoke_log[1].func == slave_audio_rx_done);
1199     UX_TEST_ASSERT(callback_invoke_log[1].param1 == slave_audio_rx_stream);
1200     UX_TEST_ASSERT(callback_invoke_log[2].func == slave_audio_rx_done);
1201     UX_TEST_ASSERT(callback_invoke_log[2].param1 == slave_audio_rx_stream);
1202     UX_TEST_ASSERT(callback_invoke_log[3].func == slave_audio_rx_done);
1203     UX_TEST_ASSERT(callback_invoke_log[3].param1 == slave_audio_rx_stream);
1204 
1205     RESET_CALLBACK_INVOKE_LOG();
1206     error_callback_counter = 0;
1207     for (test_n = 0; test_n < 10; test_n ++)
1208     {
1209         test_cmp[0] = (UCHAR)(test_n + '0');
1210         slave_audio_rx_simulate_one_frame(test_cmp, 1);
1211     }
1212 
1213     UX_TEST_ASSERT(callback_invoke_count == 10);
1214     UX_TEST_ASSERT(error_callback_counter == 3);
1215     for (test_n = 0; test_n < 7; test_n ++)
1216     {
1217         status = ux_device_class_audio_sample_read8(slave_audio_rx_stream, (UCHAR *)&temp);
1218         UX_TEST_CHECK_SUCCESS(status);
1219         // printf("(%lx)%c\n", temp, (char)temp);
1220         UX_TEST_ASSERT(temp == (test_n + '0'));
1221     }
1222     UX_TEST_CHECK_SUCCESS(ux_device_class_audio_sample_read8(slave_audio_rx_stream, (UCHAR *)&temp));
1223     UX_TEST_ASSERT(temp == '9');
1224     UX_TEST_CHECK_CODE(UX_BUFFER_OVERFLOW, ux_device_class_audio_sample_read8(slave_audio_rx_stream, (UCHAR *)&temp));
1225 
1226     /* ------------------ Read - 16 test.  */
1227 
1228     status = ux_device_class_audio_sample_read16(slave_audio_rx_stream, (USHORT *)&temp);
1229     UX_TEST_CHECK_CODE(UX_BUFFER_OVERFLOW, status);
1230 
1231     RESET_CALLBACK_INVOKE_LOG();
1232     slave_audio_rx_simulate_one_frame("012345", 6);
1233     slave_audio_rx_simulate_one_frame("67", 2);
1234     UX_TEST_ASSERT(callback_invoke_count == 2);
1235     UX_TEST_ASSERT(callback_invoke_log[0].func == slave_audio_rx_done);
1236     UX_TEST_ASSERT(callback_invoke_log[0].param1 == slave_audio_rx_stream);
1237     UX_TEST_ASSERT(callback_invoke_log[0].param2 == (VOID*)6);
1238     UX_TEST_ASSERT(callback_invoke_log[1].func == slave_audio_rx_done);
1239     UX_TEST_ASSERT(callback_invoke_log[1].param1 == slave_audio_rx_stream);
1240     UX_TEST_ASSERT(callback_invoke_log[1].param2 == (VOID*)2);
1241     for (test_n = 0; test_n < 4; test_n ++)
1242     {
1243         status = ux_device_class_audio_sample_read16(slave_audio_rx_stream, (USHORT *)&temp);
1244         UX_TEST_CHECK_SUCCESS(status);
1245         // printf("%lx\n", temp);
1246         test_cmp[0] = (UCHAR)(test_n * 2 + '0');
1247         test_cmp[1] = (UCHAR)(test_n * 2 + '1');
1248         UX_TEST_CHECK_SUCCESS(_ux_utility_memory_compare(test_cmp, &temp, 2));
1249     }
1250 
1251     /* ------------------ Read - 24 test.  */
1252 
1253     status = ux_device_class_audio_sample_read24(slave_audio_rx_stream, &temp);
1254     UX_TEST_CHECK_CODE(UX_BUFFER_OVERFLOW, status);
1255 
1256     RESET_CALLBACK_INVOKE_LOG();
1257     slave_audio_rx_simulate_one_frame("012345", 6);
1258     slave_audio_rx_simulate_one_frame("678", 3);
1259     UX_TEST_ASSERT(callback_invoke_count == 2);
1260     UX_TEST_ASSERT(callback_invoke_log[0].func == slave_audio_rx_done);
1261     UX_TEST_ASSERT(callback_invoke_log[0].param1 == slave_audio_rx_stream);
1262     UX_TEST_ASSERT(callback_invoke_log[0].param2 == (VOID*)6);
1263     UX_TEST_ASSERT(callback_invoke_log[1].func == slave_audio_rx_done);
1264     UX_TEST_ASSERT(callback_invoke_log[1].param1 == slave_audio_rx_stream);
1265     UX_TEST_ASSERT(callback_invoke_log[1].param2 == (VOID*)3);
1266     for (test_n = 0; test_n < 3; test_n ++)
1267     {
1268         status = ux_device_class_audio_sample_read24(slave_audio_rx_stream, &temp);
1269         UX_TEST_CHECK_SUCCESS(status);
1270         // printf("%lx\n", temp);
1271         test_cmp[0] = (UCHAR)(test_n * 3 + '0');
1272         test_cmp[1] = (UCHAR)(test_n * 3 + '1');
1273         test_cmp[2] = (UCHAR)(test_n * 3 + '2');
1274         UX_TEST_CHECK_SUCCESS(_ux_utility_memory_compare(test_cmp, &temp, 3));
1275     }
1276 
1277     /* ------------------ Read - 32 test.  */
1278 
1279     status = ux_device_class_audio_sample_read32(slave_audio_rx_stream, &temp);
1280     UX_TEST_CHECK_CODE(UX_BUFFER_OVERFLOW, status);
1281 
1282     RESET_CALLBACK_INVOKE_LOG();
1283     slave_audio_rx_simulate_one_frame("01234567", 8);
1284     slave_audio_rx_simulate_one_frame("8901", 4);
1285     UX_TEST_ASSERT(callback_invoke_count == 2);
1286     UX_TEST_ASSERT(callback_invoke_log[0].func == slave_audio_rx_done);
1287     UX_TEST_ASSERT(callback_invoke_log[0].param1 == slave_audio_rx_stream);
1288     UX_TEST_ASSERT(callback_invoke_log[0].param2 == (VOID*)8);
1289     UX_TEST_ASSERT(callback_invoke_log[1].func == slave_audio_rx_done);
1290     UX_TEST_ASSERT(callback_invoke_log[1].param1 == slave_audio_rx_stream);
1291     UX_TEST_ASSERT(callback_invoke_log[1].param2 == (VOID*)4);
1292     for (test_n = 0; test_n < 3; test_n ++)
1293     {
1294         status = ux_device_class_audio_sample_read32(slave_audio_rx_stream, &temp);
1295         UX_TEST_CHECK_SUCCESS(status);
1296         // printf("%lx\n", temp);
1297         test_cmp[0] = ((test_n * 4 + 0) % 10) + '0';
1298         test_cmp[1] = ((test_n * 4 + 1) % 10) + '0';
1299         test_cmp[2] = ((test_n * 4 + 2) % 10) + '0';
1300         test_cmp[3] = ((test_n * 4 + 3) % 10) + '0';
1301         UX_TEST_CHECK_SUCCESS(_ux_utility_memory_compare(test_cmp, &temp, 4));
1302     }
1303 
1304     UX_TEST_ASSERT(slave_audio_tx_stream->ux_device_class_audio_stream_transfer_pos == slave_audio_tx_stream->ux_device_class_audio_stream_access_pos);
1305 
1306 
1307     /* Wait pending threads.  */
1308     _ux_utility_thread_sleep(1);
1309 
1310     /* Finally disconnect the device. */
1311     ux_device_stack_disconnect();
1312 
1313     /* And deinitialize the class.  */
1314     ux_device_stack_class_unregister(_ux_system_slave_class_audio_name, ux_device_class_audio_entry);
1315 
1316     /* Deinitialize the device side of usbx.  */
1317     _ux_device_stack_uninitialize();
1318 
1319     /* And finally the usbx system resources.  */
1320     _ux_system_uninitialize();
1321 
1322     /* Successful test.  */
1323     printf("SUCCESS!\n");
1324     test_control_return(0);
1325 
1326 }
1327 
1328 
1329 
tx_test_thread_slave_simulation_entry(ULONG arg)1330 void  tx_test_thread_slave_simulation_entry(ULONG arg)
1331 {
1332     while(1)
1333     {
1334 
1335 #if defined(UX_DEVICE_STANDALONE)
1336 
1337         /* Standalone background task.  */
1338         ux_system_tasks_run();
1339 #else
1340         /* Sleep so ThreadX on Win32 will delete this thread. */
1341         tx_thread_sleep(10);
1342 #endif
1343     }
1344 }
1345