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