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 "fx_api.h"
10 
11 #include "ux_device_class_cdc_acm.h"
12 #include "ux_device_stack.h"
13 
14 #include "ux_host_class_cdc_acm.h"
15 #include "ux_host_class_hid.h"
16 
17 #include "ux_test_dcd_sim_slave.h"
18 #include "ux_test_hcd_sim_host.h"
19 #include "ux_test_utility_sim.h"
20 
21 #include "ux_host_stack.h"
22 
23 /* Define constants.  */
24 #define                             UX_DEMO_DEBUG_SIZE  (4096*8)
25 #define                             UX_DEMO_STACK_SIZE  1024
26 #define                             UX_DEMO_BUFFER_SIZE (UX_SLAVE_REQUEST_DATA_MAX_LENGTH + 1)
27 #define                             UX_DEMO_XMIT_BUFFER_SIZE 512
28 #define                             UX_DEMO_RECEPTION_BUFFER_SIZE 512
29 #define                             UX_DEMO_FILE_BUFFER_SIZE 512
30 #define                             UX_DEMO_RECEPTION_BLOCK_SIZE 64
31 #define                             UX_DEMO_MEMORY_SIZE     (96*1024)
32 #define                             UX_DEMO_FILE_SIZE       (128 * 1024)
33 #define                             UX_RAM_DISK_MEMORY      (256 * 1024)
34 
35 #define     LSB(x) ( (x) & 0x00ff)
36 #define     MSB(x) (((x) & 0xff00) >> 8)
37 
38 /* Configuration descriptor 9 bytes */
39 #define CFG_DESC(wTotalLength, bNumInterfaces, bmAttributes, bConfigurationValue)\
40     /* Configuration 1 descriptor 9 bytes */\
41     0x09, 0x02, LSB(wTotalLength), MSB(wTotalLength),\
42     (bNumInterfaces), (bConfigurationValue), 0x00,\
43     (bmAttributes), 0x00,
44 #define CFG_DESC_LEN 9
45 
46 #define IAD_DESC(bIfc) \
47     /* Interface association descriptor. 8 bytes.  */\
48     0x08, 0x0b, (bIfc), 0x02, 0x02, 0x02, 0x00, 0x00,
49 #define IAD_DESC_LEN 8
50 
51 #define CDC_IFC_DESC_ALL(bIfc, bIntIn, bBulkIn, bBulkOut)\
52     /* Communication Class Interface Descriptor Requirement. 9 bytes.   */\
53     0x09, 0x04, (bIfc), 0x00, 0x01, 0x02, 0x02, 0x01, 0x00,\
54     /* Header Functional Descriptor 5 bytes */\
55     0x05, 0x24, 0x00, 0x10, 0x01,\
56     /* ACM Functional Descriptor 4 bytes */\
57     0x04, 0x24, 0x02, 0x0f,\
58     /* Union Functional Descriptor 5 bytes */\
59     0x05, 0x24, 0x06, (bIfc), (bIfc + 1),\
60     /* Call Management Functional Descriptor 5 bytes */\
61     0x05, 0x24, 0x01, 0x03, (bIfc + 1),\
62     /* Endpoint interrupt in descriptor 7 bytes */\
63     0x07, 0x05, (bIntIn), 0x03, 0x40, 0x00, 0x10,\
64     /* Data Class Interface Descriptor Requirement 9 bytes */\
65     0x09, 0x04, (bIfc + 1), 0x00, 0x02, 0x0A, 0x00, 0x00, 0x00,\
66     /* Endpoint bulk in descriptor 7 bytes */\
67     0x07, 0x05, (bBulkIn), 0x02, 0x40, 0x00, 0x01,\
68     /* Endpoint bulk out descriptor 7 bytes */\
69     0x07, 0x05, (bBulkOut), 0x02, 0x40, 0x00, 0x01,
70 #define CDC_IFC_DESC_ALL1(bIfc, bIntIn, bBulkIn, bBulkOut)\
71     /* Communication Class Interface Descriptor Requirement. 9 bytes.   */\
72     0x09, 0x04, (bIfc), 0x00, 0x01, 0x02, 0x02, 0x01, 0x00,\
73     /* Header Functional Descriptor 5 bytes */\
74     0x05, 0x24, 0x00, 0x10, 0x01,\
75     /* ACM Functional Descriptor 4 bytes */\
76     0x04, 0x24, 0x02, 0x0f,\
77     /* Union Functional Descriptor 5 bytes */\
78     0x05, 0x24, 0x06, (bIfc), (bIfc + 1),\
79     /* Call Management Functional Descriptor 5 bytes */\
80     0x05, 0x24, 0x01, 0x03, (bIfc + 1),\
81     /* Endpoint interrupt in descriptor 7 bytes */\
82     0x07, 0x05, (bIntIn), 0x03, 0x40, 0x00, 0x10,\
83     /* Data Class Interface Descriptor Requirement 9 bytes */\
84     0x09, 0x04, (bIfc + 1), 0x00, 0x02, 0x0A, 0x00, 0x00, 0x00,\
85     /* Endpoint bulk out descriptor 7 bytes */\
86     0x07, 0x05, (bBulkOut), 0x02, 0x40, 0x00, 0x01,\
87     /* Endpoint bulk in descriptor 7 bytes */\
88     0x07, 0x05, (bBulkIn), 0x02, 0x40, 0x00, 0x01,
89 #define CDC_IFC_DESC_ALL_LEN (9+5+4+5+5+7+ 9+7+7)
90 
91 /* Define local/extern function prototypes.  */
92 static VOID                                test_thread_entry(ULONG);
93 static TX_THREAD                           tx_test_thread_host_simulation;
94 static TX_THREAD                           tx_test_thread_slave_simulation;
95 static VOID                                tx_test_thread_host_simulation_entry(ULONG);
96 static VOID                                tx_test_thread_slave_simulation_entry(ULONG);
97 
98 static VOID                                test_cdc_instance_activate(VOID  *cdc_instance);
99 static VOID                                test_cdc_instance_deactivate(VOID *cdc_instance);
100 static VOID                                test_cdc_instance_parameter_change(VOID *cdc_instance);
101 
102 /* Define global data structures.  */
103 static UCHAR                               usbx_memory[UX_DEMO_MEMORY_SIZE + (UX_DEMO_STACK_SIZE * 2)];
104 
105 static UX_HOST_CLASS_CDC_ACM               *cdc_acm_host_control = UX_NULL;
106 static UX_HOST_CLASS_CDC_ACM               *cdc_acm_host_data = UX_NULL;
107 
108 static UX_HOST_CLASS_CDC_ACM               *cdc_acm_host_control1 = UX_NULL;
109 static UX_HOST_CLASS_CDC_ACM               *cdc_acm_host_data1 = UX_NULL;
110 
111 static UX_SLAVE_CLASS_CDC_ACM              *cdc_acm_slave = UX_NULL;
112 
113 static UX_SLAVE_CLASS_CDC_ACM              *cdc_acm_slave1 = UX_NULL;
114 
115 static UCHAR                               cdc_acm_slave_change;
116 static UX_SLAVE_CLASS_CDC_ACM_PARAMETER    parameter;
117 
118 static ULONG                               error_counter;
119 
120 static ULONG                               error_callback_counter;
121 static UCHAR                               error_callback_ignore;
122 
123 static ULONG                               call_counter;
124 
125 static UCHAR                               buffer[UX_DEMO_BUFFER_SIZE];
126 
127 static UCHAR                               test_slave_code = 0;
128 static UCHAR                               test_slave_state = 0;
129 
130 /* Define device framework.  */
131 
132 static unsigned char device_framework_full_speed[] = {
133 
134     /* Device descriptor     18 bytes
135        0x02 bDeviceClass:    CDC class code
136        0x00 bDeviceSubclass: CDC class sub code
137        0x00 bDeviceProtocol: CDC Device protocol
138 
139        idVendor & idProduct - http://www.linux-usb.org/usb.ids
140     */
141     0x12, 0x01, 0x10, 0x01,
142     0xEF, 0x02, 0x01,
143     0x08,
144     0x84, 0x84, 0x00, 0x00,
145     0x00, 0x01,
146     0x01, 0x02, 03,
147     0x01, /* bNumConfigurations */
148 
149     /* Configuration 1 descriptor 9 bytes, total 75 bytes */
150     CFG_DESC(CFG_DESC_LEN + (IAD_DESC_LEN + CDC_IFC_DESC_ALL_LEN) * 2, 4, 0x40, 1)
151     /* IAD 8 bytes */
152     IAD_DESC(0)
153     /* CDC_ACM interfaces */
154     CDC_IFC_DESC_ALL(0, 0x83, 0x81, 0x02)
155     /* IAD 8 bytes */
156     IAD_DESC(2)
157     /* CDC_ACM interfaces */
158     CDC_IFC_DESC_ALL1(2, 0x86, 0x85, 0x04)
159 };
160 #define             DEVICE_FRAMEWORK_LENGTH_FULL_SPEED      sizeof(device_framework_full_speed)
161 
162 static unsigned char device_framework_high_speed[] = {
163 
164     /* Device descriptor     18 bytes
165        0x02 bDeviceClass:    CDC class code
166        0x00 bDeviceSubclass: CDC class sub code
167        0x00 bDeviceProtocol: CDC Device protocol
168 
169        idVendor & idProduct - http://www.linux-usb.org/usb.ids
170     */
171     0x12, 0x01, 0x00, 0x02,
172     0xEF, 0x02, 0x01,
173     0x40,
174     0x84, 0x84, 0x00, 0x00,
175     0x00, 0x01,
176     0x01, 0x02, 03,
177     0x01, /* bNumConfigurations */
178 
179     /* Device qualifier descriptor 10 bytes */
180     0x0a, 0x06, 0x00, 0x02,
181     0x02, 0x00, 0x00,
182     0x40,
183     0x01,
184     0x00,
185 
186     /* Configuration 1 descriptor 9 bytes, total 75 bytes */
187     CFG_DESC(CFG_DESC_LEN + IAD_DESC_LEN + CDC_IFC_DESC_ALL_LEN, 2, 0x60, 1)
188     /* IAD 8 bytes */
189     IAD_DESC(0)
190     /* CDC_ACM interfaces */
191     CDC_IFC_DESC_ALL1(0, 0x83, 0x81, 0x02)
192     /* IAD 8 bytes */
193     IAD_DESC(2)
194     /* CDC_ACM interfaces */
195     CDC_IFC_DESC_ALL(2, 0x86, 0x85, 0x04)
196 };
197 #define             DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED      sizeof(device_framework_high_speed)
198 
199 static unsigned char string_framework[] = {
200 
201     /* Manufacturer string descriptor : Index 1 - "Express Logic" */
202     0x09, 0x04, 0x01, 0x0c,
203     0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
204     0x6f, 0x67, 0x69, 0x63,
205 
206     /* Product string descriptor : Index 2 - "EL Composite device" */
207     0x09, 0x04, 0x02, 0x13,
208     0x45, 0x4c, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x6f,
209     0x73, 0x69, 0x74, 0x65, 0x20, 0x64, 0x65, 0x76,
210     0x69, 0x63, 0x65,
211 
212     /* Serial Number string descriptor : Index 3 - "0001" */
213     0x09, 0x04, 0x03, 0x04,
214     0x30, 0x30, 0x30, 0x31
215 };
216 #define             STRING_FRAMEWORK_LENGTH                 sizeof(string_framework)
217 
218 /* Multiple languages are supported on the device, to add
219     a language besides english, the unicode language code must
220     be appended to the language_id_framework array and the length
221     adjusted accordingly. */
222 static unsigned char language_id_framework[] = {
223 
224     /* English. */
225     0x09, 0x04
226 };
227 #define             LANGUAGE_ID_FRAMEWORK_LENGTH            sizeof(language_id_framework)
228 
229 /* Simulation actions. */
230 
231 static UX_TEST_SIM_ENTRY_ACTION hcd_transfer_request_fail[] = {
232 /* function, request to match,
233    port action, port status,
234    request action, request EP, request data, request actual length, request status,
235    status, additional callback,
236    no_return */
237 {   UX_HCD_TRANSFER_REQUEST, NULL,
238         UX_FALSE, 0,
239         0         , 0, UX_NULL, 0, 0,
240         UX_ERROR , UX_NULL},
241 {   0   }
242 };
243 
244 /* Define the ISR dispatch.  */
245 
246 extern VOID    (*test_isr_dispatch)(void);
247 
248 
249 /* Prototype for test control return.  */
250 
251 void  test_control_return(UINT status);
252 
253 
254 /* Define the ISR dispatch routine.  */
255 
test_isr(void)256 static void    test_isr(void)
257 {
258 
259     /* For further expansion of interrupt-level testing.  */
260 }
261 
break_on_cdc_acm_all_ready(VOID)262 static UINT break_on_cdc_acm_all_ready(VOID)
263 {
264 
265     if (cdc_acm_host_control == UX_NULL || cdc_acm_host_data == UX_NULL)
266         /* Do not break. */
267         return 0;
268 
269     if (cdc_acm_host_control1 == UX_NULL || cdc_acm_host_data1 == UX_NULL)
270         /* Do not break. */
271         return 0;
272 
273     if (cdc_acm_host_control->ux_host_class_cdc_acm_state != UX_HOST_CLASS_INSTANCE_LIVE)
274         /* Do not break. */
275         return 0;
276 
277     if (cdc_acm_host_data->ux_host_class_cdc_acm_state != UX_HOST_CLASS_INSTANCE_LIVE)
278         /* Do not break. */
279         return 0;
280 
281     if (cdc_acm_host_control1->ux_host_class_cdc_acm_state != UX_HOST_CLASS_INSTANCE_LIVE)
282         /* Do not break. */
283         return 0;
284 
285     if (cdc_acm_host_data1->ux_host_class_cdc_acm_state != UX_HOST_CLASS_INSTANCE_LIVE)
286         /* Do not break. */
287         return 0;
288 
289     if (cdc_acm_slave == UX_NULL || cdc_acm_slave1 == UX_NULL)
290         /* Do not break. */
291         return 0;
292 
293     /* All found, break. */
294     return 1;
295 }
296 
break_on_removal(VOID)297 static UINT break_on_removal(VOID)
298 {
299 
300     if (cdc_acm_host_control != UX_NULL || cdc_acm_host_data != UX_NULL)
301         /* Do not break. */
302         return 0;
303 
304     if (cdc_acm_host_control1 != UX_NULL || cdc_acm_host_data1 != UX_NULL)
305         /* Do not break. */
306         return 0;
307 
308     if (cdc_acm_slave != UX_NULL || cdc_acm_slave1 != UX_NULL)
309         /* Do not break. */
310         return 0;
311 
312     return 1;
313 }
314 
test_host_change_function(ULONG event,UX_HOST_CLASS * cls,VOID * inst)315 static UINT test_host_change_function(ULONG event, UX_HOST_CLASS *cls, VOID *inst)
316 {
317 
318 UX_HOST_CLASS_CDC_ACM *cdc_acm = (UX_HOST_CLASS_CDC_ACM *) inst;
319 
320     switch(event)
321     {
322 
323         case UX_DEVICE_INSERTION:
324 
325             if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
326             {
327                 if (cdc_acm_host_control == UX_NULL)
328                     cdc_acm_host_control = cdc_acm;
329                 else
330                     if (cdc_acm_host_control1 == UX_NULL)
331                         cdc_acm_host_control1 = cdc_acm;
332             }
333             else
334             {
335                 if (cdc_acm_host_data == UX_NULL)
336                     cdc_acm_host_data = cdc_acm;
337                 else
338                     if (cdc_acm_host_data1 == UX_NULL)
339                         cdc_acm_host_data1 = cdc_acm;
340             }
341             break;
342 
343         case UX_DEVICE_REMOVAL:
344 
345             if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
346             {
347                 if (cdc_acm_host_control == cdc_acm)
348                     cdc_acm_host_control = UX_NULL;
349                 if (cdc_acm_host_control1 == cdc_acm)
350                     cdc_acm_host_control1 = UX_NULL;
351             }
352             else
353             {
354                 if (cdc_acm_host_data == cdc_acm)
355                     cdc_acm_host_data = UX_NULL;
356                 if (cdc_acm_host_data1 == cdc_acm)
357                     cdc_acm_host_data1 = UX_NULL;
358             }
359             break;
360 
361         default:
362             break;
363     }
364     return 0;
365 }
366 
test_cdc_instance_activate(VOID * cdc_instance)367 static VOID    test_cdc_instance_activate(VOID *cdc_instance)
368 {
369 
370     /* Save the CDC instance.  */
371     if (cdc_acm_slave == UX_NULL)
372         cdc_acm_slave = (UX_SLAVE_CLASS_CDC_ACM *) cdc_instance;
373     else
374     if (cdc_acm_slave1 == UX_NULL)
375         cdc_acm_slave1 =  (UX_SLAVE_CLASS_CDC_ACM *) cdc_instance;
376 }
test_cdc_instance_deactivate(VOID * cdc_instance)377 static VOID    test_cdc_instance_deactivate(VOID *cdc_instance)
378 {
379 
380     /* Reset the CDC instance.  */
381     if ((VOID *)cdc_acm_slave == cdc_instance)
382         cdc_acm_slave = UX_NULL;
383     if ((VOID *)cdc_acm_slave1 == cdc_instance)
384         cdc_acm_slave1 = UX_NULL;
385 }
386 
test_cdc_instance_parameter_change(VOID * cdc_instance)387 static VOID test_cdc_instance_parameter_change(VOID *cdc_instance)
388 {
389 
390     /* Set CDC parameter change flag. */
391     cdc_acm_slave_change = UX_TRUE;
392 }
393 
test_ux_error_callback(UINT system_level,UINT system_context,UINT error_code)394 static VOID test_ux_error_callback(UINT system_level, UINT system_context, UINT error_code)
395 {
396     error_callback_counter ++;
397 
398     if (!error_callback_ignore)
399     {
400         /* Ignore UX_DEVICE_HANDLE_UNKNOWN.  */
401         if (UX_DEVICE_HANDLE_UNKNOWN == error_code)
402             return;
403         {
404             /* Failed test.  */
405             printf("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
406             // test_control_return(1);
407         }
408     }
409 }
410 
411 /* Define what the initial system looks like.  */
412 
413 #ifdef CTEST
test_application_define(void * first_unused_memory)414 void test_application_define(void *first_unused_memory)
415 #else
416 void usbx_ux_host_stack_endpoint_reset_test_application_define(void *first_unused_memory)
417 #endif
418 {
419 
420 UINT                    status;
421 CHAR *                  stack_pointer;
422 CHAR *                  memory_pointer;
423 
424     printf("Running ux_host_stack_endpoint_reset Test........................... ");
425 
426     stepinfo("\n");
427 
428     /* Initialize the free memory pointer */
429     stack_pointer = (CHAR *) usbx_memory;
430     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
431 
432     stepinfo(">>>>>>>>>>>>>>>> ux_system_initialize\n");
433 
434     /* Initialize USBX Memory */
435     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL, 0);
436     /* Check for error.  */
437     if (status != UX_SUCCESS)
438     {
439 
440         printf("ERROR #%d\n", __LINE__);
441         test_control_return(1);
442     }
443 
444     /* Register the error callback. */
445     _ux_utility_error_callback_register(test_ux_error_callback);
446 
447     stepinfo(">>>>>>>>>>>>>>>> ux_host_stack_initialize\n");
448 
449     /* The code below is required for installing the host portion of USBX */
450     status =  ux_host_stack_initialize(test_host_change_function);
451     if (status != UX_SUCCESS)
452     {
453 
454         printf("ERROR #%d\n", __LINE__);
455         test_control_return(1);
456     }
457 
458     stepinfo(">>>>>>>>>>>>>>>> ux_device_stack_initialize\n");
459 
460     /* The code below is required for installing the device portion of USBX. No call back for
461        device status change in this example. */
462     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
463                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
464                                        string_framework, STRING_FRAMEWORK_LENGTH,
465                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
466     if(status!=UX_SUCCESS)
467     {
468 
469         printf("ERROR #%d\n", __LINE__);
470         test_control_return(1);
471     }
472 
473     stepinfo(">>>>>>>>>>>>>>>> ux_device_stack_class_register\n");
474 
475     /* Set the parameters for callback when insertion/extraction of a CDC device.  */
476     parameter.ux_slave_class_cdc_acm_instance_activate   =  test_cdc_instance_activate;
477     parameter.ux_slave_class_cdc_acm_instance_deactivate =  test_cdc_instance_deactivate;
478     parameter.ux_slave_class_cdc_acm_parameter_change    =  test_cdc_instance_parameter_change;
479 
480     /* Initialize the device cdc class. This class owns both interfaces starting with 0. */
481     status  =  ux_device_stack_class_register(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry,
482                                              1,0,  &parameter);
483     status |=  ux_device_stack_class_register(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry,
484                                              1,2,  &parameter);
485     status |=  ux_device_stack_class_register(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry,
486                                              1,4,  &parameter);
487 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
488     if(status!=UX_SUCCESS)
489     {
490 
491         printf("ERROR #%d\n", __LINE__);
492         test_control_return(1);
493     }
494 #else
495     error_callback_ignore = UX_TRUE;
496 #endif
497     stepinfo(">>>>>>>>>>>>>>>> _ux_test_dcd_sim_slave_initialize\n");
498 
499     /* Initialize the simulated device controller.  */
500     status =  _ux_test_dcd_sim_slave_initialize();
501 
502     /* Check for error.  */
503     if (status != TX_SUCCESS)
504     {
505 
506         printf("ERROR #%d\n", __LINE__);
507         test_control_return(1);
508     }
509 
510     stepinfo(">>>>>>>>>>>>>>>> ux_host_stack_class_register\n");
511 
512 #if UX_MAX_CLASS_DRIVER > 1
513     /* Register HID class */
514     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
515     if (status != UX_SUCCESS)
516     {
517 
518         printf("ERROR #%d\n", __LINE__);
519         test_control_return(1);
520     }
521 #endif
522 
523     /* Register CDC ACM class */
524     status =  ux_host_stack_class_register(_ux_system_host_class_cdc_acm_name, ux_host_class_cdc_acm_entry);
525     if (status != UX_SUCCESS)
526     {
527 
528         printf("ERROR #%d\n", __LINE__);
529         test_control_return(1);
530     }
531 
532     stepinfo(">>>>>>>>>>>>>>>> ux_host_stack_hcd_register\n");
533 
534 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
535     error_callback_ignore = UX_TRUE; /* One of interface no driver.  */
536 #endif
537 
538     /* Register HCD for test */
539     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
540     if (status != UX_SUCCESS)
541     {
542 
543         printf("ERROR #%d\n", __LINE__);
544         test_control_return(1);
545     }
546 
547     stepinfo(">>>>>>>>>>>>>>>> Create main test thread\n");
548 
549     /* Create the main host simulation thread.  */
550     status =  tx_thread_create(&tx_test_thread_host_simulation, "tx test host simulation", tx_test_thread_host_simulation_entry, 0,
551             stack_pointer, UX_DEMO_STACK_SIZE,
552             20, 20, 1, TX_AUTO_START);
553 
554     /* Check for error.  */
555     if (status != TX_SUCCESS)
556     {
557 
558         printf("ERROR #%d\n", __LINE__);
559         test_control_return(1);
560     }
561 
562     stepinfo(">>>>>>>>>>>>>>>> Create test thread\n");
563 
564     /* Create the main slave simulation  thread.  */
565     status =  tx_thread_create(&tx_test_thread_slave_simulation, "tx test slave simulation", tx_test_thread_slave_simulation_entry, 0,
566             stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
567             20, 20, 1, TX_DONT_START);
568 
569     /* Check for error.  */
570     if (status != TX_SUCCESS)
571     {
572 
573         printf("ERROR #%d\n", __LINE__);
574         test_control_return(1);
575     }
576 }
577 
tx_test_thread_host_simulation_entry(ULONG arg)578 void  tx_test_thread_host_simulation_entry(ULONG arg)
579 {
580 
581 UINT                                                status;
582 
583     /* Test connect. */
584     stepinfo(">>>>>>>>>>>>>>>> Test connect (FS)\n");
585     ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
586     ux_test_breakable_sleep(500, break_on_cdc_acm_all_ready);
587     if (!(cdc_acm_host_control
588             && cdc_acm_host_data
589             && cdc_acm_slave
590 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
591             && cdc_acm_host_control1
592             && cdc_acm_host_data1
593             && cdc_acm_slave1
594 #endif
595             ))
596     {
597 
598         printf("ERROR #%d: connect fail\n", __LINE__);
599         test_control_return(1);
600     }
601 
602     error_callback_ignore = UX_TRUE;
603 
604     stepinfo(">>>>>>>>>>>>>>>> Test _ux_host_stack_endpoint_reset - request fail\n");
605 
606     ux_test_hcd_sim_host_set_actions(hcd_transfer_request_fail);
607     status = _ux_host_stack_endpoint_reset(cdc_acm_host_data->ux_host_class_cdc_acm_bulk_in_endpoint);
608     if (status == UX_SUCCESS)
609     {
610         printf("ERROR #%d: expect error\n", __LINE__);
611         error_counter ++;
612     }
613 
614 
615     stepinfo(">>>>>>>>>>>>>>>> Test disconnect\n");
616     ux_test_dcd_sim_slave_disconnect();
617     ux_test_hcd_sim_host_disconnect();
618 
619     ux_test_breakable_sleep(100, break_on_removal);
620 
621     if (cdc_acm_host_control || cdc_acm_host_data || cdc_acm_slave)
622     {
623 
624         printf("ERROR #%d: disconnect fail\n", __LINE__);
625         test_control_return(1);
626     }
627 
628     stepinfo(">>>>>>>>>>>>>>>> Deinitialize\n");
629 
630     /* Deinitialize the class.  */
631     ux_device_stack_class_unregister(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry);
632     ux_device_stack_class_unregister(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry);
633     ux_device_stack_class_unregister(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry);
634 
635     /* Deinitialize the device side of usbx.  */
636     _ux_device_stack_uninitialize();
637 
638     /* And finally the usbx system resources.  */
639     _ux_system_uninitialize();
640 
641     stepinfo(">>>>>>>>>>>>>>>> Dump results\n");
642 
643     if (error_counter > 0)
644     {
645 
646         /* Test error.  */
647         printf("ERROR #%d: total %ld errors\n", __LINE__, error_counter);
648         test_control_return(1);
649     }
650 
651     /* Successful test.  */
652     printf("SUCCESS!\n");
653     test_control_return(0);
654 }
655 
tx_test_thread_slave_simulation_entry(ULONG arg)656 void  tx_test_thread_slave_simulation_entry(ULONG arg)
657 {
658 
659     while(1)
660     {
661         switch (test_slave_code)
662         {
663         default:
664             break;
665         }
666         test_slave_state = 0;
667         tx_thread_suspend(&tx_test_thread_slave_simulation);
668     }
669 }
670