1 /* This test is designed to test the ux_device_stack_get_status.  */
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     0x00, 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 static UCHAR                           buffer[UX_TEST_BUFFER_SIZE];
129 
130 /* Define USBX test global variables.  */
131 
132 static UX_HOST_CLASS                   *class_driver;
133 static UX_HOST_CLASS_DPUMP             *dpump;
134 static UX_SLAVE_CLASS_DPUMP            *dpump_slave = UX_NULL;
135 
136 
137 static UCHAR device_framework_full_speed[] = {
138 
139     /* Device descriptor 18 bytes */
140     0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
141     0xec, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
142     0x00, 0x01,
143 
144     /* Configuration descriptor 9 bytes */
145     CFG_DESC(CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN + HID_MOUSE_IFC0_DESC_ALL_LEN + HID_MOUSE_IFC1_DESC_ALL_LEN, 2, 1)
146     /* DPUMP 9+7+7+7+7  =37 */
147     DPUMP_IFC_DESC_ALL(0, 0x81, 0x02, 0x83, 0x04)
148     /* HID   9+9 + 9+9+7=43 */
149     HID_MOUSE_IFC0_DESC_ALL(1)
150     HID_MOUSE_IFC1_DESC_ALL(1, 0x85)
151 };
152 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
153 
154 static UCHAR device_framework_high_speed[] = {
155 
156     /* Device descriptor */
157     0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
158     0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
159     0x03, 0x01,
160 
161     /* Device qualifier descriptor */
162     0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
163     0x01, 0x00,
164 
165     /* Configuration descriptor */
166     CFG_DESC(CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN + HID_MOUSE_IFC0_DESC_ALL_LEN + HID_MOUSE_IFC1_DESC_ALL_LEN, 2, 1)
167     /* DPUMP 9+7+7+7+7  =37 */
168     DPUMP_IFC_DESC_ALL(0, 0x81, 0x02, 0x83, 0x04)
169     /* HID   9+9 + 9+9+7=43 */
170     HID_MOUSE_IFC0_DESC_ALL(1)
171     HID_MOUSE_IFC1_DESC_ALL(1, 0x85)
172 };
173 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
174 
175 /* String Device Framework :
176     Byte 0 and 1 : Word containing the language ID : 0x0904 for US
177     Byte 2       : Byte containing the index of the descriptor
178     Byte 3       : Byte containing the length of the descriptor string
179 */
180 
181 static UCHAR string_framework[] = {
182 
183     /* Manufacturer string descriptor : Index 1 */
184     0x09, 0x04, 0x01, 0x0c,
185     0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
186     0x6f, 0x67, 0x69, 0x63,
187 
188     /* Product string descriptor : Index 2 */
189     0x09, 0x04, 0x02, 0x0c,
190     0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
191     0x44, 0x65, 0x6d, 0x6f,
192 
193     /* Serial Number string descriptor : Index 3 */
194     0x09, 0x04, 0x03, 0x04,
195     0x30, 0x30, 0x30, 0x31
196 };
197 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
198 
199     /* Multiple languages are supported on the device, to add
200        a language besides English, the unicode language code must
201        be appended to the language_id_framework array and the length
202        adjusted accordingly. */
203 static UCHAR language_id_framework[] = {
204 
205 /* English. */
206     0x09, 0x04
207 };
208 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
209 
210 /* Simulation actions. */
211 
212 static UX_TEST_SIM_ENTRY_ACTION dcd_endpoint_status_fail[] = {
213 /* function, request to match,
214    port action, port status,
215    request action, request EP, request data, request actual length, request status,
216    status, additional callback,
217    no_return */
218 {   UX_DCD_ENDPOINT_STATUS, NULL,
219         UX_FALSE, 0,
220         0         , 0, UX_NULL, 0, 0,
221         UX_ERROR , UX_NULL},
222 {   0   }
223 };
224 
225 /* Define prototypes for external Host Controller's (HCDs), classes and clients.  */
226 
227 static VOID                ux_test_instance_activate(VOID  *dpump_instance);
228 static VOID                ux_test_instance_deactivate(VOID *dpump_instance);
229 
230 UINT                       _ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND *command);
231 UINT                       ux_hcd_sim_initialize(UX_HCD *hcd);
232 UINT                       _ux_host_class_dpump_write(UX_HOST_CLASS_DPUMP *dpump, UCHAR * data_pointer,
233                                     ULONG requested_length, ULONG *actual_length);
234 UINT                       _ux_host_class_dpump_read (UX_HOST_CLASS_DPUMP *dpump, UCHAR *data_pointer,
235                                     ULONG requested_length, ULONG *actual_length);
236 
237 static TX_THREAD           ux_test_thread_simulation_0;
238 static TX_THREAD           ux_test_thread_simulation_1;
239 static void                ux_test_thread_simulation_0_entry(ULONG);
240 static void                ux_test_thread_simulation_1_entry(ULONG);
241 
242 
243 /* Define the ISR dispatch.  */
244 
245 extern VOID    (*test_isr_dispatch)(void);
246 
247 
248 /* Prototype for test control return.  */
249 
250 void  test_control_return(UINT status);
251 
252 
253 /* Define the ISR dispatch routine.  */
254 
test_isr(void)255 static void    test_isr(void)
256 {
257 
258     /* For further expansion of interrupt-level testing.  */
259 }
260 
261 
error_callback(UINT system_level,UINT system_context,UINT error_code)262 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
263 {
264     if (!expect_errors)
265     {
266         if (error_code != UX_DEVICE_HANDLE_UNKNOWN)
267         {
268             /* Failed test.  */
269             printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
270             test_control_return(1);
271         }
272     }
273 }
274 
test_ux_device_class_hid_entry(UX_SLAVE_CLASS_COMMAND * command)275 static UINT test_ux_device_class_hid_entry(UX_SLAVE_CLASS_COMMAND *command)
276 {
277     return UX_SUCCESS;
278     // return _ux_device_class_hid_entry(command);
279 }
280 
281 /* Define what the initial system looks like.  */
282 
283 #ifdef CTEST
test_application_define(void * first_unused_memory)284 void test_application_define(void *first_unused_memory)
285 #else
286 void    usbx_ux_device_stack_get_status_test_application_define(void *first_unused_memory)
287 #endif
288 {
289 
290 UINT status;
291 CHAR                            *stack_pointer;
292 CHAR                            *memory_pointer;
293 UX_SLAVE_CLASS_DPUMP_PARAMETER  parameter;
294 UX_SLAVE_CLASS_HID_PARAMETER    hid_parameter;
295 
296     printf("Running ux_device_stack_get_status Test............................. ");
297 
298     /* Initialize the free memory pointer.  */
299     stack_pointer = (CHAR *) first_unused_memory;
300     memory_pointer = stack_pointer + (UX_TEST_STACK_SIZE * 2);
301 
302     /* Initialize USBX Memory.  */
303     status =  ux_system_initialize(memory_pointer, UX_TEST_MEMORY_SIZE, UX_NULL, 0);
304 
305     /* Check for error.  */
306     if (status != UX_SUCCESS)
307     {
308 
309         printf("ERROR #1\n");
310         test_control_return(1);
311     }
312 
313     /* Register the error callback. */
314     _ux_utility_error_callback_register(error_callback);
315 
316     /* The code below is required for installing the host portion of USBX.  */
317     status =  ux_host_stack_initialize(UX_NULL);
318 
319     /* Check for error.  */
320     if (status != UX_SUCCESS)
321     {
322 
323         printf("ERROR #2\n");
324         test_control_return(1);
325     }
326 
327     /* Register all the host class drivers for this USBX implementation.  */
328     status =  ux_host_stack_class_register(_ux_system_host_class_dpump_name, ux_host_class_dpump_entry);
329 
330     /* Check for error.  */
331     if (status != UX_SUCCESS)
332     {
333 
334         printf("ERROR #3\n");
335         test_control_return(1);
336     }
337 
338     /* The code below is required for installing the device portion of USBX */
339     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
340                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
341                                        string_framework, STRING_FRAMEWORK_LENGTH,
342                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
343 
344     /* Check for error.  */
345     if (status != UX_SUCCESS)
346     {
347 
348         printf("ERROR #5\n");
349         test_control_return(1);
350     }
351 
352     /* Set the parameters for callback when insertion/extraction of a Data Pump device.  */
353     parameter.ux_slave_class_dpump_instance_activate   =  ux_test_instance_activate;
354     parameter.ux_slave_class_dpump_instance_deactivate =  ux_test_instance_deactivate;
355 
356     /* Initialize the device dpump class. The class is connected with interface 0 */
357     status =  ux_device_stack_class_register(_ux_system_slave_class_dpump_name, _ux_device_class_dpump_entry,
358                                               1, 0, &parameter);
359 
360     /* Initialize the hid class parameters.  */
361     hid_parameter.ux_device_class_hid_parameter_report_address = hid_mouse_report;
362     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_MOUSE_REPORT_LENGTH;
363     hid_parameter.ux_device_class_hid_parameter_callback       = UX_NULL;
364     hid_parameter.ux_slave_class_hid_instance_activate         = UX_NULL;
365 
366     status |= ux_device_stack_class_register(_ux_system_slave_class_hid_name, test_ux_device_class_hid_entry,
367                                               1, 1, &parameter);
368 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
369     /* Check for error.  */
370     if (status != UX_SUCCESS)
371     {
372 
373         printf("ERROR #6\n");
374         test_control_return(1);
375     }
376 #endif
377     /* Initialize the simulated device controller.  */
378     status =  _ux_test_dcd_sim_slave_initialize();
379 
380     /* Check for error.  */
381     if (status != UX_SUCCESS)
382     {
383 
384         printf("ERROR #7\n");
385         test_control_return(1);
386     }
387 
388     /* Register all the USB host controllers available in this system */
389     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
390 
391     /* Check for error.  */
392     if (status != UX_SUCCESS)
393     {
394 
395         printf("ERROR #4\n");
396         test_control_return(1);
397     }
398 
399     /* Create the main host simulation thread.  */
400     status =  tx_thread_create(&ux_test_thread_simulation_0, "test host simulation", ux_test_thread_simulation_0_entry, 0,
401             stack_pointer, UX_TEST_STACK_SIZE,
402             20, 20, 1, TX_AUTO_START);
403 
404     /* Check for error.  */
405     if (status != TX_SUCCESS)
406     {
407 
408         printf("ERROR #8\n");
409         test_control_return(1);
410     }
411 }
412 
413 
ux_test_thread_simulation_0_entry(ULONG arg)414 static void  ux_test_thread_simulation_0_entry(ULONG arg)
415 {
416 
417 UINT                     status;
418 UX_DEVICE               *device;
419 UX_ENDPOINT             *control_endpoint;
420 UX_TRANSFER             *transfer_request;
421 
422     /* Inform user.  */
423 
424     ux_test_dcd_sim_slave_connect(UX_HIGH_SPEED_DEVICE);
425     ux_test_hcd_sim_host_connect(UX_HIGH_SPEED_DEVICE);
426 
427     /* Enumeration check. */
428     if (dpump_slave == UX_NULL)
429     {
430 
431         printf("ERROR #%d: Enum fail\n", __LINE__);
432         test_control_return(1);
433     }
434 
435     /* Get device, control endpoint and transfer request. */
436     status = ux_host_stack_device_get(0, &device);
437     if (status != UX_SUCCESS)
438     {
439 
440         printf("ERROR #%d: device_get fail\n", __LINE__);
441         test_control_return(1);
442     }
443     control_endpoint = &device->ux_device_control_endpoint;
444     transfer_request = &control_endpoint->ux_endpoint_transfer_request;
445 
446     /* Issue GetDeviceStatus. */
447     transfer_request -> ux_transfer_request_data_pointer =      buffer;
448     transfer_request -> ux_transfer_request_requested_length =  64;
449     transfer_request -> ux_transfer_request_function =          UX_GET_STATUS;
450     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
451     transfer_request -> ux_transfer_request_value =             0;
452     transfer_request -> ux_transfer_request_index =             0;
453     status = ux_host_stack_transfer_request(transfer_request);
454     if (status != UX_SUCCESS)
455     {
456 
457         printf("ERROR #%d: GetDeviceStatus fail\n", __LINE__);
458         error_counter ++;
459     }
460     if (buffer[0] != 0)
461     {
462 
463         printf("ERROR #%d: GetDeviceStatus %x but expect 0\n", __LINE__, buffer[0]);
464         error_counter ++;
465     }
466 
467     expect_errors = UX_TRUE;
468 
469     /* Issue GetEndpointStatus.  */
470     ux_test_dcd_sim_slave_set_actions(dcd_endpoint_status_fail);
471     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_ENDPOINT;
472     transfer_request -> ux_transfer_request_value =             0;
473     transfer_request -> ux_transfer_request_index =             0x81;
474     status = ux_host_stack_transfer_request(transfer_request);
475     if (status != UX_TRANSFER_STALLED)
476     {
477 
478         printf("ERROR #%d: GetEndpointStatus(0x81) expect UX_TRANSFER_STALLED but got 0x%x\n", __LINE__, status);
479         error_counter ++;
480     }
481 
482     /* Sleep for a tick to make sure everything is complete.  */
483     tx_thread_sleep(1);
484 
485     /* Check for errors from other threads.  */
486     if (error_counter)
487     {
488 
489         /* Test error.  */
490         printf("ERROR #14\n");
491         test_control_return(1);
492     }
493     else
494     {
495 
496         /* Successful test.  */
497         printf("SUCCESS!\n");
498         test_control_return(0);
499     }
500 }
501 
ux_test_instance_activate(VOID * dpump_instance)502 static VOID  ux_test_instance_activate(VOID *dpump_instance)
503 {
504 
505     /* Save the DPUMP instance.  */
506     dpump_slave = (UX_SLAVE_CLASS_DPUMP *) dpump_instance;
507 }
508 
ux_test_instance_deactivate(VOID * dpump_instance)509 static VOID  ux_test_instance_deactivate(VOID *dpump_instance)
510 {
511 
512     /* Reset the DPUMP instance.  */
513     dpump_slave = UX_NULL;
514 }
515 
516