1 /* This test concentrates on _ux_host_class_hid_transfer_request_completed()'s decompressed handling. */
2 
3 #include "usbx_test_common_hid.h"
4 #include "ux_host_class_hid_keyboard.h"
5 #include "ux_host_class_hid_mouse.h"
6 
7 
8 static UX_HOST_CLASS_HID_MOUSE *mouse;
9 static UINT                     callback_error_code;
10 static UINT                     num_callbacks;
11 
12 
13 static UCHAR hid_mouse_report[] = {
14 
15     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
16     0x09, 0x02,                    // USAGE (Mouse)
17     0xa1, 0x01,                    // COLLECTION (Application)
18     0x09, 0x01,                    //   USAGE (Pointer)
19     0xa1, 0x00,                    //   COLLECTION (Physical)
20     0x05, 0x09,                    //     USAGE_PAGE (Button)
21     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
22     0x29, 0x03,                    //     USAGE_MAXIMUM (Button 3)
23     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
24     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
25     0x95, 0x03,                    //     REPORT_COUNT (3)
26     0x75, 0x01,                    //     REPORT_SIZE (1)
27     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
28     0x95, 0x01,                    //     REPORT_COUNT (1)
29     0x75, 0x05,                    //     REPORT_SIZE (5)
30     0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
31     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
32     0x09, 0x30,                    //     USAGE (X)
33     0x09, 0x31,                    //     USAGE (Y)
34     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
35     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
36     0x75, 0x08,                    //     REPORT_SIZE (8)
37     0x95, 0x02,                    //     REPORT_COUNT (2)
38     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
39     0x09, 0x38,                    //     USAGE (Mouse Wheel)
40     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
41     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
42     0x75, 0x08,                    //     REPORT_SIZE (8)
43     0x95, 0x01,                    //     REPORT_COUNT (1)
44     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
45     0xc0,                          //   END_COLLECTION
46     0xc0                           // END_COLLECTION
47 };
48 #define HID_MOUSE_REPORT_LENGTH (sizeof(hid_mouse_report)/sizeof(hid_mouse_report[0]))
49 
50 
51 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
52 static UCHAR device_framework_full_speed[] = {
53 
54     /* Device descriptor */
55         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
56         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
57         0x00, 0x01,
58 
59     /* Configuration descriptor */
60         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
61         0x32,
62 
63     /* Interface descriptor */
64         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
65         0x00,
66 
67     /* HID descriptor */
68         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_MOUSE_REPORT_LENGTH),
69         MSB(HID_MOUSE_REPORT_LENGTH),
70 
71     /* Endpoint descriptor (Interrupt) */
72         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
73 
74     };
75 
76 
77 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
78 static UCHAR device_framework_high_speed[] = {
79 
80     /* Device descriptor */
81         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
82         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
83         0x03, 0x01,
84 
85     /* Device qualifier descriptor */
86         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
87         0x01, 0x00,
88 
89     /* Configuration descriptor */
90         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
91         0x32,
92 
93     /* Interface descriptor */
94         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
95         0x00,
96 
97     /* HID descriptor */
98         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_MOUSE_REPORT_LENGTH),
99         MSB(HID_MOUSE_REPORT_LENGTH),
100 
101     /* Endpoint descriptor (Interrupt) */
102         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
103 
104     };
105 
106 
107     /* String Device Framework :
108      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
109      Byte 2       : Byte containing the index of the descriptor
110      Byte 3       : Byte containing the length of the descriptor string
111     */
112 
113 #define STRING_FRAMEWORK_LENGTH 40
114 static UCHAR string_framework[] = {
115 
116     /* Manufacturer string descriptor : Index 1 */
117         0x09, 0x04, 0x01, 0x0c,
118         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
119         0x6f, 0x67, 0x69, 0x63,
120 
121     /* Product string descriptor : Index 2 */
122         0x09, 0x04, 0x02, 0x0c,
123         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
124         0x6f, 0x61, 0x72, 0x64,
125 
126     /* Serial Number string descriptor : Index 3 */
127         0x09, 0x04, 0x03, 0x04,
128         0x30, 0x30, 0x30, 0x31
129     };
130 
131 
132     /* Multiple languages are supported on the device, to add
133        a language besides english, the unicode language code must
134        be appended to the language_id_framework array and the length
135        adjusted accordingly. */
136 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
137 static UCHAR language_id_framework[] = {
138 
139     /* English. */
140         0x09, 0x04
141     };
142 
143 
144 
error_callback(UINT system_level,UINT system_context,UINT error_code)145 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
146 {
147 
148     /* @BUG_FIX_PENDING: ux_dcd_sim_slave_function.c doesn't support transfer aborts, which happen during device unregistration of a class. */
149     if (error_code != UX_FUNCTION_NOT_SUPPORTED)
150     {
151 
152         /* Failed test.  */
153         printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
154         test_control_return(1);
155     }
156 }
157 
158 /* Define what the initial system looks like.  */
159 
160 #ifdef CTEST
test_application_define(void * first_unused_memory)161 void test_application_define(void *first_unused_memory)
162 #else
163 void    usbx_hid_transfer_request_completed_decompressed_test_application_define(void *first_unused_memory)
164 #endif
165 {
166 
167 UINT status;
168 CHAR *                          stack_pointer;
169 CHAR *                          memory_pointer;
170 
171 
172     /* Inform user.  */
173     printf("Running HID Transfer Request Completed Decompressed Test............ ");
174 
175     /* Initialize the free memory pointer */
176     stack_pointer = (CHAR *) usbx_memory;
177     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
178 
179     /* Initialize USBX. Memory */
180     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
181 
182     /* Check for error.  */
183     if (status != UX_SUCCESS)
184     {
185 
186         printf("Error on line %d\n", __LINE__);
187         test_control_return(1);
188     }
189 
190     /* Register the error callback. */
191     _ux_utility_error_callback_register(error_callback);
192 
193     /* The code below is required for installing the host portion of USBX */
194     status =  ux_host_stack_initialize(UX_NULL);
195     if (status != UX_SUCCESS)
196     {
197 
198         printf("Error on line %d\n", __LINE__);
199         test_control_return(1);
200     }
201 
202     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
203     if (status != UX_SUCCESS)
204     {
205 
206         printf("Error on line %d\n", __LINE__);
207         test_control_return(1);
208     }
209 
210     /* Register the HID client(s).  */
211     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_mouse_name, ux_host_class_hid_mouse_entry);
212     if (status != UX_SUCCESS)
213     {
214 
215         printf("Error on line %d\n", __LINE__);
216         test_control_return(1);
217     }
218 
219     /* The code below is required for installing the device portion of USBX. No call back for
220        device status change in this example. */
221     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
222                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
223                                        string_framework, STRING_FRAMEWORK_LENGTH,
224                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
225     if(status!=UX_SUCCESS)
226     {
227 
228         printf("Error on line %d\n", __LINE__);
229         test_control_return(1);
230     }
231 
232     /* Initialize the hid class parameters for a mouse.  */
233     hid_parameter.ux_device_class_hid_parameter_report_address = hid_mouse_report;
234     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_MOUSE_REPORT_LENGTH;
235     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
236 
237     /* Initilize the device hid class. The class is connected with interface 2 */
238     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
239                                                 1,2, (VOID *)&hid_parameter);
240     if(status!=UX_SUCCESS)
241     {
242 
243         printf("Error on line %d\n", __LINE__);
244         test_control_return(1);
245     }
246 
247 
248     /* Initialize the simulated device controller.  */
249     status =  _ux_dcd_sim_slave_initialize();
250 
251     /* Check for error.  */
252     if (status != UX_SUCCESS)
253     {
254 
255         printf("Error on line %d\n", __LINE__);
256         test_control_return(1);
257     }
258 
259     /* Register all the USB host controllers available in this system */
260     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
261 
262     /* Check for error.  */
263     if (status != UX_SUCCESS)
264     {
265 
266         printf("Error on line %d\n", __LINE__);
267         test_control_return(1);
268     }
269 
270     /* Create the main host simulation thread.  */
271     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
272             stack_pointer, UX_DEMO_STACK_SIZE,
273             20, 20, 1, TX_AUTO_START);
274 
275     /* Check for error.  */
276     if (status != TX_SUCCESS)
277     {
278 
279         printf("Error on line %d\n", __LINE__);
280         test_control_return(1);
281     }
282 
283     /* Create the main demo thread.  */
284     status =  tx_thread_create(&tx_demo_thread_slave_simulation, "tx demo slave simulation", tx_demo_thread_slave_simulation_entry, 0,
285             stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
286             20, 20, 1, TX_AUTO_START);
287 
288     /* Check for error.  */
289     if (status != TX_SUCCESS)
290     {
291 
292         printf("Error on line %d\n", __LINE__);
293         test_control_return(1);
294     }
295 
296 }
297 
298 
dummy_transfer_request_callback(UX_HOST_CLASS_HID_REPORT_CALLBACK * callback)299 VOID  dummy_transfer_request_callback(UX_HOST_CLASS_HID_REPORT_CALLBACK *callback)
300 {
301 
302 static INT  expected_button_value = -1;
303 static INT  expected_x_value = -1;
304 static INT  expected_y_value = -1;
305 static INT  expected_mouse_wheel_value = -1;
306 INT         actual_button_value;
307 INT         actual_x_value;
308 INT         actual_y_value;
309 INT         actual_mouse_wheel_value;
310 ULONG      *buffer;
311 
312 
313     buffer = callback->ux_host_class_hid_report_callback_buffer;
314 
315     actual_button_value = (
316         buffer[1] << 0 |
317         buffer[3] << 1 |
318         buffer[5] << 2
319         );
320 
321     actual_x_value = buffer[9];
322     actual_y_value = buffer[11];
323     actual_mouse_wheel_value = buffer[13];
324 
325     /* If uninitialized, initialize expected_value. */
326     if (expected_button_value == -1)
327     {
328 
329         expected_button_value =         actual_button_value;
330         expected_x_value =              actual_x_value;
331         expected_y_value =              actual_y_value;
332         expected_mouse_wheel_value =    actual_mouse_wheel_value;
333     }
334 
335     if (actual_button_value != expected_button_value ||
336         actual_x_value != expected_x_value ||
337         actual_y_value != expected_y_value ||
338         actual_mouse_wheel_value != expected_mouse_wheel_value)
339     {
340 
341         printf("Error on line %d\n", __LINE__);
342         test_control_return(1);
343     }
344 
345     expected_button_value = (0x7 & (expected_button_value + 1));
346     expected_x_value++;
347     expected_y_value++;
348     expected_mouse_wheel_value++;
349 
350     num_callbacks++;
351 }
352 
353 
tx_demo_thread_host_simulation_entry(ULONG arg)354 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
355 {
356 
357 UINT status;
358 UX_HOST_CLASS_HID_REPORT_CALLBACK   callback;
359 UCHAR                               buffer[1024];
360 
361 
362     /* Find the HID class */
363     status =  demo_class_hid_get();
364     if (status != UX_SUCCESS)
365     {
366 
367         printf("Error on line %d\n", __LINE__);
368         test_control_return(1);
369     }
370 
371     /* Get the HID client */
372     hid_client = hid -> ux_host_class_hid_client;
373 
374     /* Check if the instance of the keyboard is live */
375     while (hid_client -> ux_host_class_hid_client_local_instance == UX_NULL)
376         tx_thread_sleep(10);
377 
378     /* Get the mouse instance */
379     mouse =  (UX_HOST_CLASS_HID_MOUSE *)hid_client -> ux_host_class_hid_client_local_instance;
380 
381     /* Initialize the report callback.  */
382     callback.ux_host_class_hid_report_callback_id =         0;
383     callback.ux_host_class_hid_report_callback_function =   dummy_transfer_request_callback;
384     callback.ux_host_class_hid_report_callback_buffer =     buffer;
385     callback.ux_host_class_hid_report_callback_flags =      UX_HOST_CLASS_HID_REPORT_DECOMPRESSED;
386     callback.ux_host_class_hid_report_callback_length =     sizeof(buffer);
387 
388     status = ux_host_class_hid_report_callback_register(hid, &callback);
389     if (status != UX_SUCCESS)
390     {
391 
392         printf("Error on line %d\n", __LINE__);
393         test_control_return(1);
394     }
395 
396     /* Wait until callback has been invoked at least 16 times. */
397     while(num_callbacks < 16)
398         tx_thread_sleep(10);
399 
400     /* Now disconnect the device.  */
401     _ux_device_stack_disconnect();
402 
403     /* And deinitialize the class.  */
404     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
405 
406     /* Deinitialize the device side of usbx.  */
407     _ux_device_stack_uninitialize();
408 
409     /* And finally the usbx system resources.  */
410     _ux_system_uninitialize();
411 
412     /* Successful test.  */
413     printf("SUCCESS!\n");
414     test_control_return(0);
415 }
416 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)417 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
418 {
419     return(UX_SUCCESS);
420 }
421 
tx_demo_thread_slave_simulation_entry(ULONG arg)422 static void  tx_demo_thread_slave_simulation_entry(ULONG arg)
423 {
424 
425 UX_SLAVE_DEVICE                 *device;
426 UX_SLAVE_INTERFACE              *interface;
427 UX_SLAVE_CLASS_HID              *hid;
428 UX_SLAVE_CLASS_HID_EVENT        hid_event;
429 
430     /* Get the pointer to the device.  */
431     device =  &_ux_system_slave -> ux_system_slave_device;
432 
433     /* reset the HID event structure.  */
434     ux_utility_memory_set(&hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT));
435 
436     /* Set length of event.  */
437     hid_event.ux_device_class_hid_event_length = 4;
438 
439     /* Set initial value for buttons. The first three bits are for buttons, the rest are padding. */
440     hid_event.ux_device_class_hid_event_buffer[0] = 1;
441 
442     /* Set initial value for x and y positions. */
443     hid_event.ux_device_class_hid_event_buffer[1] = 2;
444     hid_event.ux_device_class_hid_event_buffer[2] = 3;
445 
446     /* Set initial value for mouse wheel. */
447     hid_event.ux_device_class_hid_event_buffer[3] = 4;
448 
449     while (1)
450     {
451 
452         /* Is the device configured ? */
453         while (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
454 
455             /* Then wait.  */
456             tx_thread_sleep(10);
457 
458         /* Until the device stays configured.  */
459         while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
460         {
461 
462             /* Get the interface.  We use the first interface, this is a simple device.  */
463             interface =  device -> ux_slave_device_first_interface;
464 
465             /* From that interface, derive the HID owner.  */
466             hid = interface -> ux_slave_interface_class_instance;
467 
468             /* Sleep for 1 second. */
469             ux_utility_thread_sleep(10);
470 
471             /* Set the mouse event.  */
472             ux_device_class_hid_event_set(hid, &hid_event);
473 
474             /* Increment.  */
475             hid_event.ux_device_class_hid_event_buffer[0] = (0x7 & hid_event.ux_device_class_hid_event_buffer[0] + 1);
476             hid_event.ux_device_class_hid_event_buffer[1]++;
477             hid_event.ux_device_class_hid_event_buffer[2]++;
478             hid_event.ux_device_class_hid_event_buffer[3]++;
479         }
480     }
481 }