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
9 #include "ux_host_class_dpump.h"
10 #include "ux_device_class_dpump.h"
11
12 #include "ux_host_stack.h"
13
14 #include "ux_test_hcd_sim_host.h"
15 #include "ux_test_utility_sim.h"
16
17 /* Define USBX demo constants. */
18
19 #define UX_DEMO_STACK_SIZE 4096
20 #define UX_DEMO_BUFFER_SIZE 2048
21 #define UX_DEMO_RUN 1
22 #define UX_DEMO_MEMORY_SIZE (64*1024)
23
24
25 /* Define the counters used in the demo application... */
26
27 static ULONG test_error_cases = UX_FALSE;
28 static ULONG test_error_counter = 0;
29
30 static ULONG thread_0_counter;
31 static ULONG thread_1_counter;
32 static ULONG error_counter;
33
34 /* Define USBX demo global variables. */
35
36 static unsigned char host_out_buffer[UX_HOST_CLASS_DPUMP_PACKET_SIZE];
37 static unsigned char host_in_buffer[UX_HOST_CLASS_DPUMP_PACKET_SIZE];
38 static unsigned char slave_buffer[UX_HOST_CLASS_DPUMP_PACKET_SIZE];
39
40 static UX_HOST_CLASS *class_driver;
41 static UX_HOST_CLASS_DPUMP *dpump;
42 static UX_SLAVE_CLASS_DPUMP *dpump_slave;
43
44
45 static UCHAR device_framework_full_speed[] = {
46
47 /* Device descriptor */
48 0x12, 0x01, 0x10, 0x01,
49 0x00, 0x00, 0x00, /* bDeviceClass, bDeviceSubClass, bDeviceProtocol */
50 0x08, /* bMaxPacketSize0 */
51 0xec, 0x08, 0x10, 0x00, /* idVendor, idProduct */
52 0x00, 0x00,
53 0x00, 0x00, 0x00,
54 0x01, /* bNumConfigurations */
55
56 /* Configuration descriptor */
57 0x09, 0x02,
58 0x20, 0x00, /* wTotalLength */
59 0x01, 0x01, /* bNumInterfaces, bConfigurationValue */
60 0x00,
61 0xc0, 0x32, /* bmAttributes, bMaxPower */
62
63 /* Interface descriptor */
64 0x09, 0x04,
65 0x00, 0x00, 0x02, /* bInterfaceNumber, bAlternateSetting, bNumEndpoints */
66 0x99, 0x99, 0x99, /* bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */
67 0x00,
68
69 /* Endpoint descriptor (Bulk Out) */
70 0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00,
71
72 /* Endpoint descriptor (Bulk In) */
73 0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00
74 };
75 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
76
77
78 static UCHAR device_framework_high_speed[] = {
79
80 /* Device descriptor */
81 0x12, 0x01, 0x00, 0x02,
82 0x00, 0x00, 0x00, /* bDeviceClass, bDeviceSubClass, bDeviceProtocol */
83 0x40, /* bMaxPacketSize0 */
84 0x0a, 0x07, /* idVendor */
85 0x25, 0x40, /* idProduct */
86 0x01, 0x00,
87 0x01, 0x02, 0x03,
88 0x01, /* bNumConfigurations */
89
90 /* Device qualifier descriptor */
91 0x0a, 0x06, 0x00, 0x02,
92 0x00, 0x00, 0x00, /* bDeviceClass, bDeviceSubClass, bDeviceProtocol */
93 0x40, /* bMaxPacketSize0 */
94 0x01, /* bNumConfigurations */
95 0x00,
96
97 /* Configuration descriptor */
98 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0xc0,
99 0x32,
100
101 /* Interface descriptor */
102 0x09, 0x04, 0x00, 0x00, 0x02, 0x99, 0x99, 0x99,
103 0x00,
104
105 /* Endpoint descriptor (Bulk Out) */
106 0x07, 0x05, 0x01, 0x02, 0x00, 0x02, 0x00,
107
108 /* Endpoint descriptor (Bulk In) */
109 0x07, 0x05, 0x82, 0x02, 0x00, 0x02, 0x00
110 };
111 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
112
113 /* String Device Framework :
114 Byte 0 and 1 : Word containing the language ID : 0x0904 for US
115 Byte 2 : Byte containing the index of the descriptor
116 Byte 3 : Byte containing the length of the descriptor string
117 */
118 static UCHAR string_framework[] = {
119
120 /* Manufacturer string descriptor : Index 1 */
121 0x09, 0x04, 0x01, 0x0c,
122 0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
123 0x6f, 0x67, 0x69, 0x63,
124
125 /* Product string descriptor : Index 2 */
126 0x09, 0x04, 0x02, 0x0c,
127 0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
128 0x44, 0x65, 0x6d, 0x6f,
129
130 /* Serial Number string descriptor : Index 3 */
131 0x09, 0x04, 0x03, 0x04,
132 0x30, 0x30, 0x30, 0x31
133 };
134 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
135
136 /* Multiple languages are supported on the device, to add
137 a language besides English, the unicode language code must
138 be appended to the language_id_framework array and the length
139 adjusted accordingly. */
140 static UCHAR language_id_framework[] = {
141
142 /* English. */
143 0x09, 0x04
144 };
145 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
146
147
148 /* Define prototypes for external Host Controller's (HCDs), classes and clients. */
149
150 static VOID tx_demo_instance_activate(VOID *dpump_instance);
151 static VOID tx_demo_instance_deactivate(VOID *dpump_instance);
152
153 UINT _ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND *command);
154 UINT ux_hcd_sim_initialize(UX_HCD *hcd);
155 UINT _ux_host_class_dpump_write(UX_HOST_CLASS_DPUMP *dpump, UCHAR * data_pointer,
156 ULONG requested_length, ULONG *actual_length);
157 UINT _ux_host_class_dpump_read (UX_HOST_CLASS_DPUMP *dpump, UCHAR *data_pointer,
158 ULONG requested_length, ULONG *actual_length);
159
160 static TX_THREAD tx_demo_thread_host_simulation;
161 static TX_THREAD tx_demo_thread_slave_simulation;
162 static void tx_demo_thread_host_simulation_entry(ULONG);
163 static void tx_demo_thread_slave_simulation_entry(ULONG);
164
165 /* Define the ISR dispatch. */
166
167 extern VOID (*test_isr_dispatch)(void);
168
169
170 /* Prototype for test control return. */
171
172 void test_control_return(UINT status);
173
174
175 /* Define the ISR dispatch routine. */
176
test_isr(void)177 static void test_isr(void)
178 {
179
180 /* For further expansion of interrupt-level testing. */
181 }
182
183
error_callback(UINT system_level,UINT system_context,UINT error_code)184 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
185 {
186
187 test_error_counter ++;
188
189 /* Failed test. */
190 if (!test_error_cases)
191 {
192
193 printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
194 test_control_return(1);
195 }
196 }
197
198 /* Define what the initial system looks like. */
199
200 #ifdef CTEST
test_application_define(void * first_unused_memory)201 void test_application_define(void *first_unused_memory)
202 #else
203 void usbx_ux_host_stack_interfaces_scan_test_application_define(void *first_unused_memory)
204 #endif
205 {
206
207 UINT status;
208 CHAR *stack_pointer;
209 CHAR *memory_pointer;
210 UX_SLAVE_CLASS_DPUMP_PARAMETER parameter;
211
212
213 /* Initialize the free memory pointer. */
214 stack_pointer = (CHAR *) first_unused_memory;
215 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
216
217 /* Initialize USBX Memory. */
218 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL, 0);
219
220 /* Check for error. */
221 if (status != UX_SUCCESS)
222 {
223
224 printf("Running ux_host_stack_interfaces_scan Test.......................... ERROR #1\n");
225 test_control_return(1);
226 }
227
228 /* Register the error callback. */
229 _ux_utility_error_callback_register(error_callback);
230
231 /* The code below is required for installing the host portion of USBX. */
232 status = ux_host_stack_initialize(UX_NULL);
233
234 /* Check for error. */
235 if (status != UX_SUCCESS)
236 {
237
238 printf("Running ux_host_stack_interfaces_scan Test.......................... ERROR #2\n");
239 test_control_return(1);
240 }
241
242 /* Register all the host class drivers for this USBX implementation. */
243 status = ux_host_stack_class_register(_ux_system_host_class_dpump_name, _ux_host_class_dpump_entry);
244
245 /* Check for error. */
246 if (status != UX_SUCCESS)
247 {
248
249 printf("Running ux_host_stack_interfaces_scan Test.......................... ERROR #3\n");
250 test_control_return(1);
251 }
252
253 /* The code below is required for installing the device portion of USBX */
254 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
255 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
256 string_framework, STRING_FRAMEWORK_LENGTH,
257 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
258
259 /* Check for error. */
260 if (status != UX_SUCCESS)
261 {
262
263 printf("Running ux_host_stack_interfaces_scan Test.......................... ERROR #5\n");
264 test_control_return(1);
265 }
266
267 /* Set the parameters for callback when insertion/extraction of a Data Pump device. */
268 parameter.ux_slave_class_dpump_instance_activate = tx_demo_instance_activate;
269 parameter.ux_slave_class_dpump_instance_deactivate = tx_demo_instance_deactivate;
270
271 /* Initialize the device dpump class. The class is connected with interface 0 */
272 status = ux_device_stack_class_register(_ux_system_slave_class_dpump_name, _ux_device_class_dpump_entry,
273 1, 0, ¶meter);
274
275 /* Check for error. */
276 if (status != UX_SUCCESS)
277 {
278
279 printf("Running ux_host_stack_interfaces_scan Test.......................... ERROR #6\n");
280 test_control_return(1);
281 }
282
283 /* Initialize the simulated device controller. */
284 status = _ux_dcd_sim_slave_initialize();
285
286 /* Check for error. */
287 if (status != UX_SUCCESS)
288 {
289
290 printf("Running ux_host_stack_interfaces_scan Test.......................... ERROR #7\n");
291 test_control_return(1);
292 }
293
294 /* Register all the USB host controllers available in this system */
295 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
296
297 /* Check for error. */
298 if (status != UX_SUCCESS)
299 {
300
301 printf("Running ux_host_stack_interfaces_scan Test.......................... ERROR #4\n");
302 test_control_return(1);
303 }
304
305 /* Create the main host simulation thread. */
306 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
307 stack_pointer, UX_DEMO_STACK_SIZE,
308 20, 20, 1, TX_AUTO_START);
309
310 /* Check for error. */
311 if (status != TX_SUCCESS)
312 {
313
314 printf("Running ux_host_stack_interfaces_scan Test.......................... ERROR #8\n");
315 test_control_return(1);
316 }
317
318 /* Create the main demo thread. */
319 status = tx_thread_create(&tx_demo_thread_slave_simulation, "tx demo slave simulation", tx_demo_thread_slave_simulation_entry, 0,
320 stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
321 20, 20, 1, TX_AUTO_START);
322
323 /* Check for error. */
324 if (status != TX_SUCCESS)
325 {
326
327 printf("Running ux_host_stack_interfaces_scan Test.......................... ERROR #9\n");
328 test_control_return(1);
329 }
330 }
331
332
tx_demo_thread_host_simulation_entry(ULONG arg)333 static void tx_demo_thread_host_simulation_entry(ULONG arg)
334 {
335
336 UINT status;
337 UX_HOST_CLASS *class;
338 UX_DEVICE *device;
339
340 /* Inform user. */
341 printf("Running ux_host_stack_interfaces_scan Test.......................... ");
342
343 /* Find the main data pump container. */
344 status = ux_host_stack_class_get(_ux_system_host_class_dpump_name, &class);
345
346 /* Check for error. */
347 if (status != UX_SUCCESS)
348 {
349
350 /* DPUMP basic test error. */
351 printf("ERROR #10\n");
352 test_control_return(1);
353 }
354
355 /* We get the first instance of the data pump device. */
356 do
357 {
358
359 status = ux_host_stack_class_instance_get(class, 0, (VOID **) &dpump);
360 tx_thread_relinquish();
361 } while (status != UX_SUCCESS);
362
363 /* We still need to wait for the data pump status to be live. */
364 while (dpump -> ux_host_class_dpump_state != UX_HOST_CLASS_INSTANCE_LIVE)
365 {
366
367 tx_thread_relinquish();
368 }
369
370 status = ux_host_stack_device_get(0, &device);
371 if (status != UX_SUCCESS)
372 {
373
374 printf("ERROR #%d: fail to get device instance\n", __LINE__);
375 test_control_return(1);
376 }
377
378 /* Simulate _ux_host_stack_interfaces_scan error cases. */
379
380 /* Disconnect. */
381 ux_test_hcd_sim_host_disconnect();
382
383 /* Test error cases. */
384 test_error_cases = UX_TRUE;
385 test_error_counter = 0;
386
387 /* Modify descriptor to generate errors. */
388 device_framework_full_speed[18] = 0; /* bLength: 9 -> 0 */
389
390 /* Wait enum. */
391 ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
392 tx_thread_sleep(50);
393
394 /* Expect error. */
395 if (!test_error_counter)
396 {
397
398 printf("ERROR #%d: expect configuration descriptor corrupt error\n", __LINE__);
399 error_counter ++;
400 }
401 /* Restore descriptor. */
402 device_framework_full_speed[18] = 9;
403
404 /* Disconnect. */
405 ux_test_hcd_sim_host_disconnect();
406
407 /* Modify descriptor to generate errors. */
408 device_framework_full_speed[18] = 99; /* bLength: 9 -> 99 */
409
410 /* Wait enum. */
411 ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
412 tx_thread_sleep(50);
413
414 /* Expect error. */
415 if (!test_error_counter)
416 {
417
418 printf("ERROR #%d: expect configuration descriptor corrupt error\n", __LINE__);
419 error_counter ++;
420 }
421 /* Restore descriptor. */
422 device_framework_full_speed[18] = 9;
423
424 /* Sleep for a tick to make sure everything is complete. */
425 tx_thread_sleep(1);
426
427 /* Check for errors from other threads. */
428 if (error_counter)
429 {
430
431 /* DPUMP error. */
432 printf("ERROR #14\n");
433 test_control_return(1);
434 }
435 else
436 {
437
438 /* Successful test. */
439 printf("SUCCESS!\n");
440 test_control_return(0);
441 }
442 }
443
444
tx_demo_thread_slave_simulation_entry(ULONG arg)445 static void tx_demo_thread_slave_simulation_entry(ULONG arg)
446 {
447
448 while(1)
449 {
450
451 /* Ensure the dpump class on the device is still alive. */
452 if (dpump_slave != UX_NULL)
453 {
454
455 /* Increment thread counter. */
456 thread_1_counter++;
457 }
458
459 /* Let other thread run. */
460 tx_thread_sleep(10);
461 }
462 }
463
tx_demo_instance_activate(VOID * dpump_instance)464 static VOID tx_demo_instance_activate(VOID *dpump_instance)
465 {
466
467 /* Save the DPUMP instance. */
468 dpump_slave = (UX_SLAVE_CLASS_DPUMP *) dpump_instance;
469 }
470
tx_demo_instance_deactivate(VOID * dpump_instance)471 static VOID tx_demo_instance_deactivate(VOID *dpump_instance)
472 {
473
474 /* Reset the DPUMP instance. */
475 dpump_slave = UX_NULL;
476 }
477
478