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 
16 #include "ux_test_dcd_sim_slave.h"
17 #include "ux_test_hcd_sim_host.h"
18 #include "ux_test_utility_sim.h"
19 
20 #include "ux_host_stack.h"
21 
22 /* Define constants.  */
23 #define                             UX_DEMO_DEBUG_SIZE  (4096*8)
24 #define                             UX_DEMO_STACK_SIZE  1024
25 #define                             UX_DEMO_BUFFER_SIZE (UX_SLAVE_REQUEST_DATA_MAX_LENGTH + 1)
26 #define                             UX_DEMO_XMIT_BUFFER_SIZE 512
27 #define                             UX_DEMO_RECEPTION_BUFFER_SIZE 512
28 #define                             UX_DEMO_FILE_BUFFER_SIZE 512
29 #define                             UX_DEMO_RECEPTION_BLOCK_SIZE 64
30 #define                             UX_DEMO_MEMORY_SIZE     (64*1024)
31 #define                             UX_DEMO_FILE_SIZE       (128 * 1024)
32 #define                             UX_RAM_DISK_MEMORY      (256 * 1024)
33 
34 /* Define local/extern function prototypes.  */
35 static VOID                                test_thread_entry(ULONG);
36 static TX_THREAD                           tx_test_thread_host_simulation;
37 static TX_THREAD                           tx_test_thread_slave_simulation;
38 static VOID                                tx_test_thread_host_simulation_entry(ULONG);
39 static VOID                                tx_test_thread_slave_simulation_entry(ULONG);
40 static VOID                                test_cdc_instance_activate(VOID  *cdc_instance);
41 static VOID                                test_cdc_instance_deactivate(VOID *cdc_instance);
42 static VOID                                test_cdc_instance_parameter_change(VOID *cdc_instance);
43 
44 /* Define global data structures.  */
45 UCHAR                               usbx_memory[UX_DEMO_MEMORY_SIZE + (UX_DEMO_STACK_SIZE * 2)];
46 UX_HOST_CLASS                       *class_driver;
47 UX_HOST_CLASS_CDC_ACM               *cdc_acm_host_control;
48 UX_HOST_CLASS_CDC_ACM               *cdc_acm_host_data;
49 
50 UX_SLAVE_CLASS_CDC_ACM              *cdc_acm_slave;
51 UCHAR                               cdc_acm_slave_change;
52 UX_SLAVE_CLASS_CDC_ACM_PARAMETER    parameter;
53 
54 ULONG                               error_counter;
55 
56 ULONG                               set_cfg_counter;
57 
58 ULONG                               rsc_mem_free_on_set_cfg;
59 ULONG                               rsc_sem_on_set_cfg;
60 ULONG                               rsc_sem_get_on_set_cfg;
61 ULONG                               rsc_mutex_on_set_cfg;
62 
63 ULONG                               rsc_enum_sem_usage;
64 ULONG                               rsc_enum_sem_get_count;
65 ULONG                               rsc_enum_mutex_usage;
66 ULONG                               rsc_enum_mem_usage;
67 
68 /* Define device framework.  */
69 
70 #define             DEVICE_FRAMEWORK_LENGTH_FULL_SPEED      161
71 #define             DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED      171
72 #define             STRING_FRAMEWORK_LENGTH                 47
73 #define             LANGUAGE_ID_FRAMEWORK_LENGTH            2
74 
75 static unsigned char device_framework_full_speed[] = {
76 
77     /* Device descriptor     18 bytes
78        0x02 bDeviceClass:    CDC class code
79        0x00 bDeviceSubclass: CDC class sub code
80        0x00 bDeviceProtocol: CDC Device protocol
81 
82        idVendor & idProduct - http://www.linux-usb.org/usb.ids
83     */
84     0x12, 0x01, 0x10, 0x01,
85     0xEF, 0x02, 0x01,
86     0x08,
87     0x84, 0x84, 0x00, 0x00,
88     0x00, 0x01,
89     0x01, 0x02, 03,
90     0x02, /* bNumConfigurations */
91 
92     /* Configuration 1 descriptor 9 bytes, total 68 bytes */
93     0x09, 0x02, 0x44, 0x00, /* ,,wTotalLength */
94     0x02, 0x01, 0x00,       /* ,bConfigurationValue, */
95     0x40, 0x00,
96 
97     /* Interface association descriptor. 8 bytes.  */
98     0x08, 0x0b, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,
99 
100     /* Communication Class Interface Descriptor Requirement. 9 bytes.   */
101     0x09, 0x04, 0x00,       /* ,,bInterfaceNumber */
102     0x00,                   /* bAlternateSetting */
103     0x00,                   /* bNumEndpoints */
104     0x02, 0x02, 0x01,       /* bInterfaceClass,SubClass,Protocol */
105     0x00,
106 
107     /* Header Functional Descriptor 5 bytes */
108     0x05, 0x24, 0x00,
109     0x10, 0x01,
110 
111     /* ACM Functional Descriptor 4 bytes */
112     0x04, 0x24, 0x02,
113     0x0f,
114 
115     /* Union Functional Descriptor 5 bytes */
116     0x05, 0x24, 0x06,
117     0x00,                          /* Master interface */
118     0x01,                          /* Slave interface  */
119 
120     /* Call Management Functional Descriptor 5 bytes */
121     0x05, 0x24, 0x01,
122     0x03,
123     0x01,                          /* Data interface   */
124 
125     /* Data Class Interface Descriptor Requirement 9 bytes */
126     0x09, 0x04, 0x01,
127     0x00,
128     0x02,
129     0x0A, 0x00, 0x00,
130     0x00,
131 
132     /* Endpoint 0x81 descriptor 7 bytes */
133     0x07, 0x05, 0x81,
134     0x02,
135     0x40, 0x00,
136     0x00,
137 
138     /* Endpoint 0x02 descriptor 7 bytes */
139     0x07, 0x05, 0x02,
140     0x02,
141     0x40, 0x00,
142     0x00,
143 
144     /* Configuration 2 descriptor 9 bytes, total 75 bytes */
145     0x09, 0x02, 0x4b, 0x00, /* ,,wTotalLength */
146     0x02, 0x02, 0x00,       /* ,bConfigurationValue, */
147     0x40, 0xFA,             /* bmAttributes,bMaxPower () */
148 
149     /* Interface association descriptor. 8 bytes.  */
150     0x08, 0x0b, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,
151 
152     /* Communication Class Interface Descriptor Requirement. 9 bytes.   */
153     0x09, 0x04, 0x00,       /* ,,bInterfaceNumber */
154     0x00,                   /* bAlternateSetting */
155     0x01,                   /* bNumEndpoints */
156     0x02, 0x02, 0x01,       /* bInterfaceClass,SubClass,Protocol */
157     0x00,
158 
159     /* Header Functional Descriptor 5 bytes */
160     0x05, 0x24, 0x00,
161     0x10, 0x01,
162 
163     /* ACM Functional Descriptor 4 bytes */
164     0x04, 0x24, 0x02,
165     0x0f,
166 
167     /* Union Functional Descriptor 5 bytes */
168     0x05, 0x24, 0x06,
169     0x00,                          /* Master interface */
170     0x01,                          /* Slave interface  */
171 
172     /* Call Management Functional Descriptor 5 bytes */
173     0x05, 0x24, 0x01,
174     0x03,
175     0x01,                          /* Data interface   */
176 
177     /* Endpoint 0x83 descriptor 7 bytes */
178     0x07, 0x05, 0x83,          /* ,,bEndpointAddress */
179     0x03,
180     0x08, 0x00,
181     0xFF,
182 
183     /* Data Class Interface Descriptor Requirement 9 bytes */
184     0x09, 0x04, 0x01,
185     0x00,
186     0x02,
187     0x0A, 0x00, 0x00,
188     0x00,
189 
190     /* Endpoint 0x81 descriptor 7 bytes */
191     0x07, 0x05, 0x81,
192     0x02,
193     0x40, 0x00,
194     0x00,
195 
196     /* Endpoint 0x02 descriptor 7 bytes */
197     0x07, 0x05, 0x02,
198     0x02,
199     0x40, 0x00,
200     0x00,
201 };
202 
203 #define DEVICE_FRAMEWORK_EPA_POS_1_FS (DEVICE_FRAMEWORK_LENGTH_FULL_SPEED - 14 + 2)
204 #define DEVICE_FRAMEWORK_EPA_POS_2_FS (DEVICE_FRAMEWORK_LENGTH_FULL_SPEED - 7 + 2)
205 
206 static unsigned char device_framework_high_speed[] = {
207 
208     /* Device descriptor
209        0x02 bDeviceClass:    CDC class code
210        0x00 bDeviceSubclass: CDC class sub code
211        0x00 bDeviceProtocol: CDC Device protocol
212 
213        idVendor & idProduct - http://www.linux-usb.org/usb.ids
214     */
215     0x12, 0x01, 0x00, 0x02,
216     0xEF, 0x02, 0x01,
217     0x40,
218     0x84, 0x84, 0x00, 0x00,
219     0x00, 0x01,
220     0x01, 0x02, 03,
221     0x02, /* bNumConfigurations */
222 
223     /* Device qualifier descriptor */
224     0x0a, 0x06, 0x00, 0x02,
225     0x02, 0x00, 0x00,
226     0x40,
227     0x01,
228     0x00,
229 
230     /* Configuration 1 descriptor 9 bytes, total 68 bytes */
231     0x09, 0x02, 0x44, 0x00, /* ,,wTotalLength */
232     0x02, 0x01, 0x00,       /* ,bConfigurationValue, */
233     0x40, 0x00,
234 
235     /* Interface association descriptor. 8 bytes.  */
236     0x08, 0x0b, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,
237 
238     /* Communication Class Interface Descriptor Requirement. 9 bytes.   */
239     0x09, 0x04, 0x00,       /* ,,bInterfaceNumber */
240     0x00,                   /* bAlternateSetting */
241     0x00,                   /* bNumEndpoints */
242     0x02, 0x02, 0x01,       /* bInterfaceClass,SubClass,Protocol */
243     0x00,
244 
245     /* Header Functional Descriptor 5 bytes */
246     0x05, 0x24, 0x00,
247     0x10, 0x01,
248 
249     /* ACM Functional Descriptor 4 bytes */
250     0x04, 0x24, 0x02,
251     0x0f,
252 
253     /* Union Functional Descriptor 5 bytes */
254     0x05, 0x24, 0x06,
255     0x00,                          /* Master interface */
256     0x01,                          /* Slave interface  */
257 
258     /* Call Management Functional Descriptor 5 bytes */
259     0x05, 0x24, 0x01,
260     0x03,
261     0x01,                          /* Data interface   */
262 
263     /* Data Class Interface Descriptor Requirement 9 bytes */
264     0x09, 0x04, 0x01,
265     0x00,
266     0x02,
267     0x0A, 0x00, 0x00,
268     0x00,
269 
270     /* Endpoint 0x81 descriptor 7 bytes */
271     0x07, 0x05, 0x81,
272     0x02,
273     0x40, 0x00,
274     0x00,
275 
276     /* Endpoint 0x02 descriptor 7 bytes */
277     0x07, 0x05, 0x02,
278     0x02,
279     0x40, 0x00,
280     0x00,
281 
282     /* Configuration 2 descriptor 9 bytes, total 75 bytes */
283     0x09, 0x02, 0x4b, 0x00, /* ,,wTotalLength */
284     0x02, 0x02, 0x00,       /* ,bConfigurationValue, */
285     0x40, 0x00,
286 
287     /* Interface association descriptor. 8 bytes.  */
288     0x08, 0x0b, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,
289 
290     /* Communication Class Interface Descriptor Requirement. 9 bytes.   */
291     0x09, 0x04, 0x00,       /* ,,bInterfaceNumber */
292     0x00,                   /* bAlternateSetting */
293     0x01,                   /* bNumEndpoints */
294     0x02, 0x02, 0x01,       /* bInterfaceClass,SubClass,Protocol */
295     0x00,
296 
297     /* Header Functional Descriptor 5 bytes */
298     0x05, 0x24, 0x00,
299     0x10, 0x01,
300 
301     /* ACM Functional Descriptor 4 bytes */
302     0x04, 0x24, 0x02,
303     0x0f,
304 
305     /* Union Functional Descriptor 5 bytes */
306     0x05, 0x24, 0x06,
307     0x00,                          /* Master interface */
308     0x01,                          /* Slave interface  */
309 
310     /* Call Management Functional Descriptor 5 bytes */
311     0x05, 0x24, 0x01,
312     0x03,
313     0x01,                          /* Data interface   */
314 
315     /* Endpoint 0x83 descriptor 7 bytes */
316     0x07, 0x05, 0x83,          /* ,,bEndpointAddress */
317     0x03,
318     0x08, 0x00,
319     0xFF,
320 
321     /* Data Class Interface Descriptor Requirement 9 bytes */
322     0x09, 0x04, 0x01,
323     0x00,
324     0x02,
325     0x0A, 0x00, 0x00,
326     0x00,
327 
328     /* Endpoint 0x81 descriptor 7 bytes */
329     0x07, 0x05, 0x81,
330     0x02,
331     0x40, 0x00,
332     0x00,
333 
334     /* Endpoint 0x02 descriptor 7 bytes */
335     0x07, 0x05, 0x02,
336     0x02,
337     0x40, 0x00,
338     0x00,
339 };
340 
341 #define DEVICE_FRAMEWORK_EPA_POS_1_HS (DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED - 14 + 2)
342 #define DEVICE_FRAMEWORK_EPA_POS_2_HS (DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED - 7 + 2)
343 
344 static unsigned char string_framework[] = {
345 
346     /* Manufacturer string descriptor : Index 1 - "Express Logic" */
347         0x09, 0x04, 0x01, 0x0c,
348         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
349         0x6f, 0x67, 0x69, 0x63,
350 
351     /* Product string descriptor : Index 2 - "EL Composite device" */
352         0x09, 0x04, 0x02, 0x13,
353         0x45, 0x4c, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x6f,
354         0x73, 0x69, 0x74, 0x65, 0x20, 0x64, 0x65, 0x76,
355         0x69, 0x63, 0x65,
356 
357     /* Serial Number string descriptor : Index 3 - "0001" */
358         0x09, 0x04, 0x03, 0x04,
359         0x30, 0x30, 0x30, 0x31
360 };
361 
362 
363     /* Multiple languages are supported on the device, to add
364        a language besides english, the unicode language code must
365        be appended to the language_id_framework array and the length
366        adjusted accordingly. */
367 static unsigned char language_id_framework[] = {
368 
369     /* English. */
370         0x09, 0x04
371 };
372 
373 static UX_TEST_SETUP _SetConfigure = UX_TEST_SETUP_SetConfigure;
374 
375 static UX_TEST_HCD_SIM_ACTION error_on_SetCfg[] = {
376 /* function, request to match,
377    port action, port status,
378    request action, request EP, request data, request actual length, request status,
379    status, additional callback,
380    no_return */
381 {   UX_HCD_TRANSFER_REQUEST, &_SetConfigure,
382         UX_FALSE, UX_TEST_PORT_STATUS_DISC,
383         UX_TEST_SETUP_MATCH_REQ, 0, UX_NULL, 0, 0,
384         UX_ERROR, UX_NULL}, /* Error */
385 {   0   }
386 };
387 
388 static UX_CONFIGURATION *cfg_2_modify;
ux_test_hcd_entry_interaction_set_cfg(UX_TEST_ACTION * action,VOID * _params)389 static VOID ux_test_hcd_entry_interaction_set_cfg(UX_TEST_ACTION *action, VOID *_params)
390 {
391 
392     if (cfg_2_modify)
393         cfg_2_modify -> ux_configuration_handle = 0;
394 }
395 
396 static UX_TEST_HCD_SIM_ACTION corrupt_configuration_on_SetCfg[] = {
397 /* function, request to match,
398    port action, port status,
399    request action, request EP, request data, request actual length, request status,
400    status, additional callback,
401    no_return */
402 {   UX_HCD_TRANSFER_REQUEST, &_SetConfigure,
403         UX_FALSE, UX_TEST_PORT_STATUS_DISC,
404         UX_TEST_SETUP_MATCH_REQ, 0, UX_NULL, 0, 0,
405         UX_SUCCESS, ux_test_hcd_entry_interaction_set_cfg,
406         UX_TRUE}, /* Go on */
407 {   0   }
408 };
409 
410 /* Define the ISR dispatch.  */
411 
412 extern VOID    (*test_isr_dispatch)(void);
413 
414 
415 /* Prototype for test control return.  */
416 
417 void  test_control_return(UINT status);
418 
419 
420 /* Define the ISR dispatch routine.  */
421 
test_isr(void)422 static void    test_isr(void)
423 {
424 
425     /* For further expansion of interrupt-level testing.  */
426 }
427 
test_host_change_function(ULONG event,UX_HOST_CLASS * cls,VOID * inst)428 static UINT test_host_change_function(ULONG event, UX_HOST_CLASS *cls, VOID *inst)
429 {
430 
431 UX_HOST_CLASS_CDC_ACM *cdc_acm = (UX_HOST_CLASS_CDC_ACM *) inst;
432 
433     switch(event)
434     {
435 
436         case UX_DEVICE_INSERTION:
437 
438             if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
439                 cdc_acm_host_control = cdc_acm;
440             else
441                 cdc_acm_host_data = cdc_acm;
442             break;
443 
444         case UX_DEVICE_REMOVAL:
445 
446             if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
447                 cdc_acm_host_control = UX_NULL;
448             else
449                 cdc_acm_host_data = UX_NULL;
450             break;
451 
452         default:
453             break;
454     }
455     return 0;
456 }
457 
test_cdc_instance_activate(VOID * cdc_instance)458 static VOID    test_cdc_instance_activate(VOID *cdc_instance)
459 {
460 
461     /* Save the CDC instance.  */
462     cdc_acm_slave = (UX_SLAVE_CLASS_CDC_ACM *) cdc_instance;
463 }
test_cdc_instance_deactivate(VOID * cdc_instance)464 static VOID    test_cdc_instance_deactivate(VOID *cdc_instance)
465 {
466 
467     /* Reset the CDC instance.  */
468     cdc_acm_slave = UX_NULL;
469 }
470 
test_cdc_instance_parameter_change(VOID * cdc_instance)471 static VOID test_cdc_instance_parameter_change(VOID *cdc_instance)
472 {
473 
474     /* Set CDC parameter change flag. */
475     cdc_acm_slave_change = UX_TRUE;
476 }
477 
test_ux_error_callback(UINT system_level,UINT system_context,UINT error_code)478 static VOID test_ux_error_callback(UINT system_level, UINT system_context, UINT error_code)
479 {
480 }
481 
482 /* Define what the initial system looks like.  */
483 
484 #ifdef CTEST
test_application_define(void * first_unused_memory)485 void test_application_define(void *first_unused_memory)
486 #else
487 void usbx_cdc_acm_configure_test_application_define(void *first_unused_memory)
488 #endif
489 {
490 
491 UINT                    status;
492 CHAR *                  stack_pointer;
493 CHAR *                  memory_pointer;
494 
495     printf("Running CDC ACM Configure Test...................................... ");
496 
497     /* Reset testing counts. */
498     ux_test_utility_sim_mutex_create_count_reset();
499     ux_test_utility_sim_sem_create_count_reset();
500     ux_test_utility_sim_sem_get_count_reset();
501     /* Reset error generations */
502     ux_test_utility_sim_sem_error_generation_stop();
503     ux_test_utility_sim_mutex_error_generation_stop();
504     ux_test_utility_sim_sem_get_error_generation_stop();
505 
506     /* Initialize the free memory pointer */
507     stack_pointer = (CHAR *) usbx_memory;
508     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
509 
510 
511     /* Initialize USBX Memory */
512     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL, 0);
513     /* Check for error.  */
514     if (status != UX_SUCCESS)
515     {
516 
517         printf("ERROR #%d\n", __LINE__);
518         test_control_return(1);
519     }
520 
521     /* Register the error callback. */
522     _ux_utility_error_callback_register(test_ux_error_callback);
523 
524     /* The code below is required for installing the host portion of USBX */
525     status =  ux_host_stack_initialize(test_host_change_function);
526     if (status != UX_SUCCESS)
527     {
528 
529         printf("ERROR #%d\n", __LINE__);
530         test_control_return(1);
531     }
532 
533     /* Register CDC ACM class */
534     status =  ux_host_stack_class_register(_ux_system_host_class_cdc_acm_name, ux_host_class_cdc_acm_entry);
535     if (status != UX_SUCCESS)
536     {
537 
538         printf("ERROR #%d\n", __LINE__);
539         test_control_return(1);
540     }
541 
542     /* The code below is required for installing the device portion of USBX. No call back for
543        device status change in this example. */
544     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
545                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
546                                        string_framework, STRING_FRAMEWORK_LENGTH,
547                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
548     if(status!=UX_SUCCESS)
549     {
550 
551         printf("ERROR #%d\n", __LINE__);
552         test_control_return(1);
553     }
554 
555     /* Set the parameters for callback when insertion/extraction of a CDC device.  */
556     parameter.ux_slave_class_cdc_acm_instance_activate   =  test_cdc_instance_activate;
557     parameter.ux_slave_class_cdc_acm_instance_deactivate =  test_cdc_instance_deactivate;
558     parameter.ux_slave_class_cdc_acm_parameter_change    =  test_cdc_instance_parameter_change;
559 
560     /* Initialize the device cdc class. This class owns both interfaces starting with 0. */
561     status =  ux_device_stack_class_register(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry,
562                                              1,0,  &parameter);
563 
564     if(status!=UX_SUCCESS)
565     {
566 
567         printf("ERROR #%d\n", __LINE__);
568         test_control_return(1);
569     }
570 
571     /* Initialize the simulated device controller.  */
572     status =  _ux_test_dcd_sim_slave_initialize();
573 
574     /* Check for error.  */
575     if (status != TX_SUCCESS)
576     {
577 
578         printf("ERROR #%d\n", __LINE__);
579         test_control_return(1);
580     }
581 
582     /* Register HCD for test */
583     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
584     if (status != UX_SUCCESS)
585     {
586 
587         printf("ERROR #%d\n", __LINE__);
588         test_control_return(1);
589     }
590 
591     /* Create the main host simulation thread.  */
592     status =  tx_thread_create(&tx_test_thread_host_simulation, "tx test host simulation", tx_test_thread_host_simulation_entry, 0,
593             stack_pointer, UX_DEMO_STACK_SIZE,
594             20, 20, 1, TX_AUTO_START);
595 
596     /* Check for error.  */
597     if (status != TX_SUCCESS)
598     {
599 
600         printf("ERROR #%d\n", __LINE__);
601         test_control_return(1);
602     }
603 
604     /* Create the main slave simulation  thread.  */
605     status =  tx_thread_create(&tx_test_thread_slave_simulation, "tx test slave simulation", tx_test_thread_slave_simulation_entry, 0,
606             stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
607             20, 20, 1, TX_AUTO_START);
608 
609     /* Check for error.  */
610     if (status != TX_SUCCESS)
611     {
612 
613         printf("ERROR #%d\n", __LINE__);
614         test_control_return(1);
615     }
616 }
617 
tx_test_thread_host_simulation_entry(ULONG arg)618 void  tx_test_thread_host_simulation_entry(ULONG arg)
619 {
620 
621 UINT                                                status;
622 ULONG                                               test_n;
623 ULONG                                               mem_free;
624 UX_HOST_CLASS_COMMAND                               class_command;
625 UX_CONFIGURATION                                   *configuration;
626 UX_HOST_CLASS_CDC_ACM                              *cdc_control;
627 UX_HOST_CLASS_CDC_ACM                              *cdc_data;
628 UX_DEVICE                                          *device;
629 UX_DEVICE                                          *parent_device;
630 UX_INTERFACE                                       *interface;
631 
632     stepinfo("\n");
633 
634     /* Test connect. */
635     stepinfo(">>>>>>>>>>>>>>>> Test connect\n");
636     ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
637     tx_thread_sleep(100);
638     if (cdc_acm_host_control == UX_NULL || cdc_acm_host_data == UX_NULL || cdc_acm_slave == UX_NULL)
639     {
640 
641         printf("ERROR #%d: connection not detected\n", __LINE__);
642         test_control_return(1);
643     }
644     mem_free = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available;
645     cdc_control = cdc_acm_host_control;
646     cdc_data = cdc_acm_host_data;
647 
648     /* Test disconnect. */
649     stepinfo(">>>>>>>>>>>>>>>> Test disconnect\n");
650     ux_test_dcd_sim_slave_disconnect();
651     ux_test_hcd_sim_host_disconnect();
652     test_n = 10;
653     while((cdc_acm_host_control || cdc_acm_host_data || cdc_acm_slave) && test_n --)
654         tx_thread_sleep(10);
655 
656     if (cdc_acm_host_control || cdc_acm_host_data || cdc_acm_slave)
657     {
658 
659         printf("ERROR #%d: instance not removed when disconnect, %p %p %p\n", __LINE__, cdc_acm_host_control, cdc_acm_host_data, cdc_acm_slave);
660         test_control_return(1);
661     }
662     if (_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available <= mem_free)
663     {
664 
665         printf("ERROR #%d: memory not freed when disconnect\n", __LINE__);
666         test_control_return(1);
667     }
668 
669     /* Test configure function */
670     stepinfo(">>>>>>>>>>>>>>>> Test Configure\n");
671     ux_test_dcd_sim_slave_connect(UX_FULL_SPEED_DEVICE);
672     /* Now connect, configuration stopped because power issue */
673     ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
674     tx_thread_sleep(100);
675     /* Find device */
676     status = _ux_host_stack_device_get(0, &device);
677     if (status != UX_SUCCESS)
678     {
679 
680         printf("ERROR #%d\n", __LINE__);
681         test_control_return(1);
682     }
683     /* Reset configuration */
684     status = ux_host_stack_device_configuration_reset(device);
685     if (status != UX_SUCCESS)
686     {
687 
688         printf("ERROR #%d\n", __LINE__);
689         test_control_return(1);
690     }
691     /* Find configuration */
692     status = _ux_host_stack_device_configuration_get(device, 0, &configuration);
693     if (status != UX_SUCCESS)
694     {
695 
696         printf("ERROR #%d\n", __LINE__);
697         test_control_return(1);
698     }
699     /* Force power source */
700     device -> ux_device_power_source =  UX_DEVICE_BUS_POWERED;
701     /* Activate interfaces */
702     class_command.ux_host_class_command_request   = UX_HOST_CLASS_COMMAND_ACTIVATE;
703     /* Control interface */
704     interface = configuration -> ux_configuration_first_interface;
705     class_command.ux_host_class_command_container = (VOID *)interface;
706     class_command.ux_host_class_command_class_ptr = interface->ux_interface_class;
707     status = interface->ux_interface_class->ux_host_class_entry_function(&class_command);
708     if (status != UX_SUCCESS)
709     {
710 
711         printf("ERROR #%d\n", __LINE__);
712         test_control_return(1);
713     }
714     /* Data interface */
715     interface = configuration -> ux_configuration_first_interface->ux_interface_next_interface;
716     class_command.ux_host_class_command_container = (VOID *)interface;
717     class_command.ux_host_class_command_class_ptr = interface->ux_interface_class;
718     status = interface->ux_interface_class->ux_host_class_entry_function(&class_command);
719     if (status != UX_SUCCESS)
720     {
721 
722         printf("ERROR #%d\n", __LINE__);
723         test_control_return(1);
724     }
725 
726     stepinfo(">>>>>>>>>>>>>>>> Test Configure ERROR\n");
727     ux_test_dcd_sim_slave_connect(UX_FULL_SPEED_DEVICE);
728     /* Now connect, configuration stopped because power issue */
729     ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
730     tx_thread_sleep(100);
731     /* Find device */
732     status = _ux_host_stack_device_get(0, &device);
733     if (status != UX_SUCCESS)
734     {
735 
736         printf("ERROR #%d\n", __LINE__);
737         test_control_return(1);
738     }
739     /* Reset configuration */
740     status = ux_host_stack_device_configuration_reset(device);
741     if (status != UX_SUCCESS)
742     {
743 
744         printf("ERROR #%d\n", __LINE__);
745         test_control_return(1);
746     }
747     /* Find configuration */
748     status = _ux_host_stack_device_configuration_get(device, 0, &configuration);
749     if (status != UX_SUCCESS)
750     {
751 
752         printf("ERROR #%d\n", __LINE__);
753         test_control_return(1);
754     }
755 #if UX_MAX_DEVICES > 1
756     stepinfo(">>>>>>>>>>>>>>>> Test Configure ERROR - Parent power fail\n");
757     /* Get a device */
758     parent_device = _ux_host_stack_new_device_get();
759     /* Use this device as parent */
760     parent_device -> ux_device_power_source = UX_DEVICE_BUS_POWERED;
761     device -> ux_device_parent = parent_device;
762     /* Force power source */
763     device -> ux_device_power_source =  UX_DEVICE_BUS_POWERED;
764     /* Activate interfaces */
765     class_command.ux_host_class_command_request   = UX_HOST_CLASS_COMMAND_ACTIVATE;
766     /* Control interface */
767     interface = configuration -> ux_configuration_first_interface;
768     class_command.ux_host_class_command_container = (VOID *)interface;
769     class_command.ux_host_class_command_class_ptr = interface->ux_interface_class;
770     status = interface->ux_interface_class->ux_host_class_entry_function(&class_command);
771     if (status != UX_CONNECTION_INCOMPATIBLE)
772     {
773 
774         printf("ERROR #%d\n", __LINE__);
775         test_control_return(1);
776     }
777 
778     stepinfo(">>>>>>>>>>>>>>>> Test Configure ERROR - Configuration handler\n");
779     device -> ux_device_handle = 0;
780     status = interface->ux_interface_class->ux_host_class_entry_function(&class_command);
781     if (status != UX_CONFIGURATION_HANDLE_UNKNOWN)
782     {
783 
784         printf("ERROR %d: expect UX_CONFIGURATION_HANDLE_UNKNOWN but got 0x%x\n", __LINE__, status);
785         test_control_return(1);
786     }
787     device -> ux_device_handle = (ULONG) (ALIGN_TYPE) device;
788 
789     stepinfo(">>>>>>>>>>>>>>>> Test Configure ERROR - Parent power OK but SetConfigure error\n");
790     parent_device -> ux_device_power_source = UX_DEVICE_SELF_POWERED;
791     ux_test_hcd_sim_host_set_actions(error_on_SetCfg);
792     status = interface->ux_interface_class->ux_host_class_entry_function(&class_command);
793     if (status == UX_SUCCESS)
794     {
795 
796         printf("ERROR %d: expect fail\n", __LINE__);
797         test_control_return(1);
798     }
799 #endif
800     stepinfo(">>>>>>>>>>>>>>>> Test Configure ERROR - Device power OK but SetConfigure error\n");
801     device -> ux_device_power_source = UX_DEVICE_SELF_POWERED;
802     ux_test_hcd_sim_host_set_actions(error_on_SetCfg);
803     status = interface->ux_interface_class->ux_host_class_entry_function(&class_command);
804     if (status == UX_SUCCESS)
805     {
806 
807         printf("ERROR %d: expect fail\n", __LINE__);
808         test_control_return(1);
809     }
810 
811     stepinfo(">>>>>>>>>>>>>>>> Test Configure ERROR - SetConfigure\n");
812     UX_DEVICE_PARENT_SET(device, UX_NULL);
813     ux_test_hcd_sim_host_set_actions(error_on_SetCfg);
814     status = interface->ux_interface_class->ux_host_class_entry_function(&class_command);
815     if (status == UX_SUCCESS)
816     {
817 
818         printf("ERROR #%d\n", __LINE__);
819         test_control_return(1);
820     }
821     stepinfo(">>>>>>>>>>>>>>>> Test Configure ERROR - Interface get\n");
822     /* Break the configuration */
823     cfg_2_modify = configuration;
824     ux_test_hcd_sim_host_set_actions(corrupt_configuration_on_SetCfg);
825     status = interface->ux_interface_class->ux_host_class_entry_function(&class_command);
826     if (status == UX_SUCCESS)
827     {
828 
829         printf("ERROR #%d: %x\n", __LINE__, status);
830         test_control_return(1);
831     }
832     /* Restore */
833     cfg_2_modify = UX_NULL;
834     configuration->ux_configuration_handle = (ULONG)(ALIGN_TYPE)configuration;
835 
836     /* Deinitialize the class.  */
837     status =  ux_device_stack_class_unregister(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry);
838 
839     /* Deinitialize the device side of usbx.  */
840     _ux_device_stack_uninitialize();
841 
842     /* And finally the usbx system resources.  */
843     _ux_system_uninitialize();
844 
845     /* Successful test.  */
846     printf("SUCCESS!\n");
847     test_control_return(0);
848 
849 }
850 
tx_test_thread_slave_simulation_entry(ULONG arg)851 void  tx_test_thread_slave_simulation_entry(ULONG arg)
852 {
853 
854     while(1)
855     {
856 
857         /* Sleep so ThreadX on Win32 will delete this thread. */
858         tx_thread_sleep(10);
859     }
860 }
861