1 /* This test is designed to test the ux_device_stack_interface_set.  */
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_stack.h"
10 #include "ux_device_stack.h"
11 
12 #include "ux_device_class_cdc_acm.h"
13 #include "ux_host_class_cdc_acm.h"
14 
15 #include "ux_host_class_dpump.h"
16 #include "ux_device_class_dpump.h"
17 
18 #include "ux_host_class_hid.h"
19 #include "ux_device_class_hid.h"
20 
21 #include "ux_host_class_storage.h"
22 #include "ux_device_class_storage.h"
23 
24 #include "ux_test_dcd_sim_slave.h"
25 #include "ux_test_hcd_sim_host.h"
26 #include "ux_test_utility_sim.h"
27 
28 
29 /* Define USBX test constants.  */
30 
31 #define UX_TEST_STACK_SIZE      4096
32 #define UX_TEST_BUFFER_SIZE     2048
33 #define UX_TEST_RUN             1
34 #define UX_TEST_MEMORY_SIZE     (64*1024)
35 
36 /* HID mouse related descriptors */
37 
38 static UCHAR hid_mouse_report[] = {
39 
40     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
41     0x09, 0x02,                    // USAGE (Mouse)
42     0xa1, 0x01,                    // COLLECTION (Application)
43     0x09, 0x01,                    //   USAGE (Pointer)
44     0xa1, 0x00,                    //   COLLECTION (Physical)
45     0x05, 0x09,                    //     USAGE_PAGE (Button)
46     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
47     0x29, 0x03,                    //     USAGE_MAXIMUM (Button 3)
48     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
49     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
50     0x95, 0x03,                    //     REPORT_COUNT (3)
51     0x75, 0x01,                    //     REPORT_SIZE (1)
52     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
53     0x95, 0x01,                    //     REPORT_COUNT (1)
54     0x75, 0x05,                    //     REPORT_SIZE (5)
55     0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
56     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
57     0x09, 0x30,                    //     USAGE (X)
58     0x09, 0x31,                    //     USAGE (Y)
59     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
60     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
61     0x75, 0x08,                    //     REPORT_SIZE (8)
62     0x95, 0x02,                    //     REPORT_COUNT (2)
63     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
64     0x09, 0x38,                    //     USAGE (Mouse Wheel)
65     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
66     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
67     0x75, 0x08,                    //     REPORT_SIZE (8)
68     0x95, 0x01,                    //     REPORT_COUNT (1)
69     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
70     0xc0,                          //   END_COLLECTION
71     0xc0                           // END_COLLECTION
72 };
73 #define HID_MOUSE_REPORT_LENGTH (sizeof(hid_mouse_report)/sizeof(hid_mouse_report[0]))
74 
75 #define     LSB(x) (x & 0x00ff)
76 #define     MSB(x) ((x & 0xff00) >> 8)
77 
78 /* Configuration descriptor 9 bytes */
79 #define CFG_DESC(wTotalLength, bNumInterfaces, bConfigurationValue)\
80     /* Configuration 1 descriptor 9 bytes */\
81     0x09, 0x02, LSB(wTotalLength), MSB(wTotalLength),\
82     (bNumInterfaces), (bConfigurationValue), 0x00,\
83     0x40, 0x00,
84 #define CFG_DESC_LEN 9
85 
86 /* DPUMP interface descriptors 9+7+7+7+7=37 bytes. */
87 #define DPUMP_IFC_DESC_ALL(ifc, bulk_in_epa, bulk_out_epa, int_in_epa, int_out_epa) \
88     /* Interface descriptor */\
89     0x09, 0x04, (ifc), 0x00, 0x04, 0x99, 0x99, 0x99, 0x00,\
90     /* Endpoint descriptor (Bulk Out) */\
91     0x07, 0x05, (bulk_out_epa), 0x02, 0x40, 0x00, 0x00,\
92     /* Endpoint descriptor (Bulk In) */\
93     0x07, 0x05, (bulk_in_epa), 0x02, 0x40, 0x00, 0x00,\
94     /* Endpoint descriptor (Interrupt Out) */\
95     0x07, 0x05, (int_out_epa), 0x02, 0x40, 0x00, 0x00,\
96     /* Endpoint descriptor (Interrupt In) */\
97     0x07, 0x05, (int_in_epa), 0x02, 0x40, 0x00, 0x00,
98 #define DPUMP_IFC_DESC_ALL_LEN 37
99 
100 /* HID Mouse interface descriptors 9+9=18 bytes */
101 #define HID_MOUSE_IFC0_DESC_ALL(ifc)     \
102     /* Interface descriptor */\
103     0x09, 0x04, (ifc), 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,\
104     /* HID descriptor */\
105     0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_MOUSE_REPORT_LENGTH),\
106     MSB(HID_MOUSE_REPORT_LENGTH),
107 #define HID_MOUSE_IFC0_DESC_ALL_LEN 18
108 
109 /* HID Mouse interface descriptors 9+9+7=25 bytes */
110 #define HID_MOUSE_IFC1_DESC_ALL(ifc, interrupt_epa)     \
111     /* Interface descriptor */\
112     0x09, 0x04, (ifc), 0x01, 0x01, 0x03, 0x00, 0x00, 0x00,\
113     /* HID descriptor */\
114     0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_MOUSE_REPORT_LENGTH),\
115     MSB(HID_MOUSE_REPORT_LENGTH),\
116     /* Endpoint descriptor (Interrupt) */\
117     0x07, 0x05, (interrupt_epa), 0x03, 0x08, 0x00, 0x08,
118 #define HID_MOUSE_IFC1_DESC_ALL_LEN 25
119 
120 /* Define the counters used in the test application...  */
121 
122 static ULONG                           thread_0_counter;
123 static ULONG                           thread_1_counter;
124 static ULONG                           error_counter;
125 
126 static UCHAR                           expect_errors = UX_FALSE;
127 
128 
129 /* Define USBX test global variables.  */
130 
131 static UX_HOST_CLASS                   *class_driver;
132 static UX_HOST_CLASS_DPUMP             *dpump;
133 static UX_SLAVE_CLASS_DPUMP            *dpump_slave = UX_NULL;
134 
135 
136 static UCHAR device_framework_full_speed[] = {
137 
138     /* Device descriptor 18 bytes */
139     0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
140     0xec, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
141     0x00, 0x01,
142 
143     /* Configuration descriptor 9 bytes */
144     CFG_DESC(CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN + HID_MOUSE_IFC0_DESC_ALL_LEN + HID_MOUSE_IFC1_DESC_ALL_LEN, 2, 1)
145     /* DPUMP 9+7+7+7+7  =37 */
146     DPUMP_IFC_DESC_ALL(0, 0x81, 0x02, 0x83, 0x04)
147     /* HID   9+9 + 9+9+7=43 */
148     HID_MOUSE_IFC0_DESC_ALL(1)
149     HID_MOUSE_IFC1_DESC_ALL(1, 0x85)
150 };
151 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
152 
153 static UCHAR device_framework_high_speed[] = {
154 
155     /* Device descriptor */
156     0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
157     0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
158     0x03, 0x01,
159 
160     /* Device qualifier descriptor */
161     0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
162     0x01, 0x00,
163 
164     /* Configuration descriptor */
165     CFG_DESC(CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN + HID_MOUSE_IFC0_DESC_ALL_LEN + HID_MOUSE_IFC1_DESC_ALL_LEN, 2, 1)
166     /* DPUMP 9+7+7+7+7  =37 */
167     DPUMP_IFC_DESC_ALL(0, 0x81, 0x02, 0x83, 0x04)
168     /* HID   9+9 + 9+9+7=43 */
169     HID_MOUSE_IFC0_DESC_ALL(1)
170     HID_MOUSE_IFC1_DESC_ALL(1, 0x85)
171 };
172 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
173 
174 /* String Device Framework :
175     Byte 0 and 1 : Word containing the language ID : 0x0904 for US
176     Byte 2       : Byte containing the index of the descriptor
177     Byte 3       : Byte containing the length of the descriptor string
178 */
179 
180 static UCHAR string_framework[] = {
181 
182     /* Manufacturer string descriptor : Index 1 */
183     0x09, 0x04, 0x01, 0x0c,
184     0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
185     0x6f, 0x67, 0x69, 0x63,
186 
187     /* Product string descriptor : Index 2 */
188     0x09, 0x04, 0x02, 0x0c,
189     0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
190     0x44, 0x65, 0x6d, 0x6f,
191 
192     /* Serial Number string descriptor : Index 3 */
193     0x09, 0x04, 0x03, 0x04,
194     0x30, 0x30, 0x30, 0x31
195 };
196 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
197 
198     /* Multiple languages are supported on the device, to add
199        a language besides English, the unicode language code must
200        be appended to the language_id_framework array and the length
201        adjusted accordingly. */
202 static UCHAR language_id_framework[] = {
203 
204 /* English. */
205     0x09, 0x04
206 };
207 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
208 
209 /* Simulation actions. */
210 
211 static UX_TEST_SIM_ENTRY_ACTION dcd_endpoint_create_fail[] = {
212 /* function, request to match,
213    port action, port status,
214    request action, request EP, request data, request actual length, request status,
215    status, additional callback,
216    no_return */
217 {   UX_DCD_CREATE_ENDPOINT, NULL,
218         UX_FALSE, 0,
219         0         , 0, UX_NULL, 0, 0,
220         UX_SUCCESS , UX_NULL,
221     UX_TRUE},
222 {   UX_DCD_CREATE_ENDPOINT, NULL,
223         UX_FALSE, 0,
224         0         , 0, UX_NULL, 0, 0,
225         UX_ERROR , UX_NULL},
226 {   UX_DCD_CREATE_ENDPOINT, NULL,
227         UX_FALSE, 0,
228         0         , 0, UX_NULL, 0, 0,
229         UX_ERROR , UX_NULL},
230 {   UX_DCD_CREATE_ENDPOINT, NULL,
231         UX_FALSE, 0,
232         0         , 0, UX_NULL, 0, 0,
233         UX_ERROR , UX_NULL},
234 {   UX_DCD_CREATE_ENDPOINT, NULL,
235         UX_FALSE, 0,
236         0         , 0, UX_NULL, 0, 0,
237         UX_ERROR , UX_NULL},
238 {   0   }
239 };
240 
241 
242 /* Define prototypes for external Host Controller's (HCDs), classes and clients.  */
243 
244 static VOID                ux_test_instance_activate(VOID  *dpump_instance);
245 static VOID                ux_test_instance_deactivate(VOID *dpump_instance);
246 
247 UINT                       _ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND *command);
248 UINT                       ux_hcd_sim_initialize(UX_HCD *hcd);
249 UINT                       _ux_host_class_dpump_write(UX_HOST_CLASS_DPUMP *dpump, UCHAR * data_pointer,
250                                     ULONG requested_length, ULONG *actual_length);
251 UINT                       _ux_host_class_dpump_read (UX_HOST_CLASS_DPUMP *dpump, UCHAR *data_pointer,
252                                     ULONG requested_length, ULONG *actual_length);
253 
254 static TX_THREAD           ux_test_thread_simulation_0;
255 static TX_THREAD           ux_test_thread_simulation_1;
256 static void                ux_test_thread_simulation_0_entry(ULONG);
257 static void                ux_test_thread_simulation_1_entry(ULONG);
258 
259 
260 /* Define the ISR dispatch.  */
261 
262 extern VOID    (*test_isr_dispatch)(void);
263 
264 
265 /* Prototype for test control return.  */
266 
267 void  test_control_return(UINT status);
268 
269 
270 /* Define the ISR dispatch routine.  */
271 
test_isr(void)272 static void    test_isr(void)
273 {
274 
275     /* For further expansion of interrupt-level testing.  */
276 }
277 
278 
error_callback(UINT system_level,UINT system_context,UINT error_code)279 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
280 {
281     if (!expect_errors)
282     {
283         if (error_code != UX_DEVICE_HANDLE_UNKNOWN)
284         {
285             /* Failed test.  */
286             printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
287             test_control_return(1);
288         }
289     }
290 }
291 
test_ux_device_class_hid_entry(UX_SLAVE_CLASS_COMMAND * command)292 static UINT test_ux_device_class_hid_entry(UX_SLAVE_CLASS_COMMAND *command)
293 {
294     return UX_SUCCESS;
295     // return _ux_device_class_hid_entry(command);
296 }
297 
298 /* Define what the initial system looks like.  */
299 
300 #ifdef CTEST
test_application_define(void * first_unused_memory)301 void test_application_define(void *first_unused_memory)
302 #else
303 void    usbx_ux_device_stack_interface_set_test_application_define(void *first_unused_memory)
304 #endif
305 {
306 
307 UINT status;
308 CHAR                            *stack_pointer;
309 CHAR                            *memory_pointer;
310 UX_SLAVE_CLASS_DPUMP_PARAMETER  parameter;
311 UX_SLAVE_CLASS_HID_PARAMETER    hid_parameter;
312 
313 #if !(UX_TEST_MULTI_IFC_ON)
314     printf("Running ux_device_stack_interface_set Test.......................... SKIP!");
315     test_control_return(0);
316     return;
317 #endif
318 
319     /* Initialize the free memory pointer.  */
320     stack_pointer = (CHAR *) first_unused_memory;
321     memory_pointer = stack_pointer + (UX_TEST_STACK_SIZE * 2);
322 
323     /* Initialize USBX Memory.  */
324     status =  ux_system_initialize(memory_pointer, UX_TEST_MEMORY_SIZE, UX_NULL, 0);
325 
326     /* Check for error.  */
327     if (status != UX_SUCCESS)
328     {
329 
330         printf("Running ux_device_stack_interface_set Test.......................... ERROR #1\n");
331         test_control_return(1);
332     }
333 
334     /* Register the error callback. */
335     _ux_utility_error_callback_register(error_callback);
336 
337     /* The code below is required for installing the host portion of USBX.  */
338     status =  ux_host_stack_initialize(UX_NULL);
339 
340     /* Check for error.  */
341     if (status != UX_SUCCESS)
342     {
343 
344         printf("Running ux_device_stack_interface_set Test.......................... ERROR #2\n");
345         test_control_return(1);
346     }
347 
348     /* Register all the host class drivers for this USBX implementation.  */
349     status =  ux_host_stack_class_register(_ux_system_host_class_dpump_name, ux_host_class_dpump_entry);
350 
351     /* Check for error.  */
352     if (status != UX_SUCCESS)
353     {
354 
355         printf("Running ux_device_stack_interface_set Test.......................... ERROR #3\n");
356         test_control_return(1);
357     }
358 
359     /* The code below is required for installing the device portion of USBX */
360     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
361                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
362                                        string_framework, STRING_FRAMEWORK_LENGTH,
363                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
364 
365     /* Check for error.  */
366     if (status != UX_SUCCESS)
367     {
368 
369         printf("Running ux_device_stack_interface_set Test.......................... ERROR #5\n");
370         test_control_return(1);
371     }
372 
373     /* Set the parameters for callback when insertion/extraction of a Data Pump device.  */
374     parameter.ux_slave_class_dpump_instance_activate   =  ux_test_instance_activate;
375     parameter.ux_slave_class_dpump_instance_deactivate =  ux_test_instance_deactivate;
376 
377     /* Initialize the device dpump class. The class is connected with interface 0 */
378     status =  ux_device_stack_class_register(_ux_system_slave_class_dpump_name, _ux_device_class_dpump_entry,
379                                               1, 0, &parameter);
380 
381     /* Initialize the hid class parameters.  */
382     hid_parameter.ux_device_class_hid_parameter_report_address = hid_mouse_report;
383     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_MOUSE_REPORT_LENGTH;
384     hid_parameter.ux_device_class_hid_parameter_callback       = UX_NULL;
385     hid_parameter.ux_slave_class_hid_instance_activate         = UX_NULL;
386 
387     status |= ux_device_stack_class_register(_ux_system_slave_class_hid_name, test_ux_device_class_hid_entry,
388                                               1, 1, &parameter);
389 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
390     /* Check for error.  */
391     if (status != UX_SUCCESS)
392     {
393 
394         printf("Running ux_device_stack_interface_set Test.......................... ERROR #6\n");
395         test_control_return(1);
396     }
397 #endif
398     /* Initialize the simulated device controller.  */
399     status =  _ux_test_dcd_sim_slave_initialize();
400 
401     /* Check for error.  */
402     if (status != UX_SUCCESS)
403     {
404 
405         printf("Running ux_device_stack_interface_set Test.......................... ERROR #7\n");
406         test_control_return(1);
407     }
408 
409     /* Register all the USB host controllers available in this system */
410     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
411 
412     /* Check for error.  */
413     if (status != UX_SUCCESS)
414     {
415 
416         printf("Running ux_device_stack_interface_set Test.......................... ERROR #4\n");
417         test_control_return(1);
418     }
419 
420     /* Create the main host simulation thread.  */
421     status =  tx_thread_create(&ux_test_thread_simulation_0, "test host simulation", ux_test_thread_simulation_0_entry, 0,
422             stack_pointer, UX_TEST_STACK_SIZE,
423             20, 20, 1, TX_AUTO_START);
424 
425     /* Check for error.  */
426     if (status != TX_SUCCESS)
427     {
428 
429         printf("Running ux_device_stack_interface_set Test.......................... ERROR #8\n");
430         test_control_return(1);
431     }
432 }
433 
434 
ux_test_thread_simulation_0_entry(ULONG arg)435 static void  ux_test_thread_simulation_0_entry(ULONG arg)
436 {
437 
438 UINT                     status;
439 UX_SLAVE_DEVICE         *device;
440 ULONG                    endpoints_pool_number;
441 
442     /* Inform user.  */
443     printf("Running ux_device_stack_interface_set Test.......................... ");
444 
445     ux_test_dcd_sim_slave_connect(UX_HIGH_SPEED_DEVICE);
446     ux_test_hcd_sim_host_connect(UX_HIGH_SPEED_DEVICE);
447 
448     /* Enumeration check. */
449     if (dpump_slave == UX_NULL)
450     {
451 
452         printf("ERROR #%d: Enum fail\n", __LINE__);
453         test_control_return(1);
454     }
455 
456     /* Slave device. */
457     device = &_ux_system_slave->ux_system_slave_device;
458 
459     /* Expect errors so skip them in error handler. */
460     expect_errors = UX_TRUE;
461 
462     /* Unconfigure. */
463     status = _ux_device_stack_configuration_set(0);
464     if (status != UX_SUCCESS)
465     {
466 
467         printf("ERROR #%d: Unconfigure fail\n", __LINE__);
468         test_control_return(1);
469     }
470 
471     /* Limit endpoints pool to generate error. */
472     endpoints_pool_number = device->ux_slave_device_endpoints_pool_number;
473     device->ux_slave_device_endpoints_pool_number = 0;
474 
475     /* Configure. */
476     status = _ux_device_stack_configuration_set(1);
477     if (status != UX_SUCCESS)
478     {
479 
480         printf("ERROR #%d: Configure fail code %x\n", __LINE__, status);
481         test_control_return(1);
482     }
483     device->ux_slave_device_endpoints_pool_number = endpoints_pool_number;
484 
485     /* Unconfigure. */
486     status = _ux_device_stack_configuration_set(0);
487     if (status != UX_SUCCESS)
488     {
489 
490         printf("ERROR #%d: Unconfigure fail\n", __LINE__);
491         test_control_return(1);
492     }
493 
494     /* Endpoint creation fail on other than EP0. */
495     ux_test_dcd_sim_slave_set_actions(dcd_endpoint_create_fail);
496 
497     /* Configure. */
498     status = _ux_device_stack_configuration_set(1);
499     if (status != UX_SUCCESS)
500     {
501 
502         printf("ERROR #%d: Configure fail code %x\n", __LINE__, status);
503         test_control_return(1);
504     }
505 
506     /* No actions. */
507     ux_test_dcd_sim_slave_set_actions(UX_NULL);
508 
509     /* Break on errors. */
510     expect_errors = UX_TRUE;
511 
512     /* Sleep for a tick to make sure everything is complete.  */
513     tx_thread_sleep(1);
514 
515     /* Check for errors from other threads.  */
516     if (error_counter)
517     {
518 
519         /* Test error.  */
520         printf("ERROR #14\n");
521         test_control_return(1);
522     }
523     else
524     {
525 
526         /* Successful test.  */
527         printf("SUCCESS!\n");
528         test_control_return(0);
529     }
530 }
531 
ux_test_instance_activate(VOID * dpump_instance)532 static VOID  ux_test_instance_activate(VOID *dpump_instance)
533 {
534 
535     /* Save the DPUMP instance.  */
536     dpump_slave = (UX_SLAVE_CLASS_DPUMP *) dpump_instance;
537 }
538 
ux_test_instance_deactivate(VOID * dpump_instance)539 static VOID  ux_test_instance_deactivate(VOID *dpump_instance)
540 {
541 
542     /* Reset the DPUMP instance.  */
543     dpump_slave = UX_NULL;
544 }
545 
546