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