1 /* This file tests basic HID functionalities. */
2
3 #include "usbx_test_common_hid.h"
4 #include "ux_host_class_hid_keyboard.h"
5
6 #define DUMMY_USBX_MEMORY_SIZE (64*1024)
7 static UCHAR dummy_usbx_memory[DUMMY_USBX_MEMORY_SIZE];
8
9 static UCHAR hid_keyboard_report[] = {
10
11 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
12 0x09, 0x06, // USAGE (Keyboard)
13 0xa1, 0x01, // COLLECTION (Application)
14
15 0x05, 0x07, // USAGE_PAGE (Keyboard)
16 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
17 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
18 0x15, 0x00, // LOGICAL_MINIMUM (0)
19 0x25, 0x01, // LOGICAL_MAXIMUM (1)
20 0x75, 0x01, // REPORT_SIZE (1)
21 0x95, 0x08, // REPORT_COUNT (8)
22 0x81, 0x02, // INPUT (Data,Var,Abs)
23
24 0x95, 0x01, // REPORT_COUNT (1)
25 0x75, 0x08, // REPORT_SIZE (8)
26 0x81, 0x03, // INPUT (Cnst,Var,Abs)
27
28 0x95, 0x05, // REPORT_COUNT (5)
29 0x75, 0x01, // REPORT_SIZE (1)
30 0x05, 0x08, // USAGE_PAGE (LEDs)
31 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
32 0x29, 0x05, // USAGE_MAXIMUM (Kana)
33 0x91, 0x02, // OUTPUT (Data,Var,Abs)
34
35 0x95, 0x01, // REPORT_COUNT (1)
36 0x75, 0x03, // REPORT_SIZE (3)
37 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
38
39 0x95, 0x06, // REPORT_COUNT (6)
40 0x75, 0x08, // REPORT_SIZE (8)
41 0x15, 0x00, // LOGICAL_MINIMUM (0)
42 0x25, 0x65, // LOGICAL_MAXIMUM (101)
43 0x05, 0x07, // USAGE_PAGE (Keyboard)
44 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
45 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
46 0x81, 0x00, // INPUT (Data,Ary,Abs)
47
48 0xc0 // END_COLLECTION
49 };
50 #define HID_KEYBOARD_REPORT_LENGTH sizeof(hid_keyboard_report)/sizeof(hid_keyboard_report[0])
51
52 static UCHAR hid_mouse_report[] = {
53
54 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
55 0x09, 0x02, // USAGE (Mouse)
56 0xa1, 0x01, // COLLECTION (Application)
57 0x09, 0x01, // USAGE (Pointer)
58 0xa1, 0x00, // COLLECTION (Physical)
59 0x05, 0x09, // USAGE_PAGE (Button)
60 0x19, 0x01, // USAGE_MINIMUM (Button 1)
61 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
62 0x15, 0x00, // LOGICAL_MINIMUM (0)
63 0x25, 0x01, // LOGICAL_MAXIMUM (1)
64 0x95, 0x03, // REPORT_COUNT (3)
65 0x75, 0x01, // REPORT_SIZE (1)
66 0x81, 0x02, // INPUT (Data,Var,Abs)
67 0x95, 0x01, // REPORT_COUNT (1)
68 0x75, 0x05, // REPORT_SIZE (5)
69 0x81, 0x03, // INPUT (Cnst,Var,Abs)
70 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
71 0x09, 0x30, // USAGE (X)
72 0x09, 0x31, // USAGE (Y)
73 0x15, 0x81, // LOGICAL_MINIMUM (-127)
74 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
75 0x75, 0x08, // REPORT_SIZE (8)
76 0x95, 0x02, // REPORT_COUNT (2)
77 0x81, 0x06, // INPUT (Data,Var,Rel)
78 0x09, 0x38, // USAGE (Mouse Wheel)
79 0x15, 0x81, // LOGICAL_MINIMUM (-127)
80 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
81 0x75, 0x08, // REPORT_SIZE (8)
82 0x95, 0x01, // REPORT_COUNT (1)
83 0x81, 0x06, // INPUT (Data,Var,Rel)
84 0xc0, // END_COLLECTION
85 0xc0 // END_COLLECTION
86 };
87 #define HID_MOUSE_REPORT_LENGTH (sizeof(hid_mouse_report)/sizeof(hid_mouse_report[0]))
88
89 #define hid_report_descriptor hid_mouse_report
90 #define HID_REPORT_LENGTH HID_MOUSE_REPORT_LENGTH
91
92 /* Configuration descriptor 9 bytes */
93 #define CFG_DESC(wTotalLength, bNumInterfaces, bConfigurationValue)\
94 /* Configuration 1 descriptor 9 bytes */\
95 0x09, 0x02, LSB(wTotalLength), MSB(wTotalLength),\
96 (bNumInterfaces), (bConfigurationValue), 0x00,\
97 0x40, 0x00,
98 #define CFG_DESC_LEN (9)
99
100
101 /* HID Mouse/Keyboard interface descriptors 9+9+7=25 bytes */
102 #define HID_IFC_DESC_ALL(ifc, report_len, interrupt_epa) \
103 /* Interface descriptor */\
104 0x09, 0x04, (ifc), 0x00, 0x01, 0x03, 0x00, 0x00, 0x00,\
105 /* HID descriptor */\
106 0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(report_len),\
107 MSB(report_len),\
108 /* Endpoint descriptor (Interrupt) */\
109 0x07, 0x05, (interrupt_epa), 0x03, 0x08, 0x00, 0x08,
110 #define HID_IFC_DESC_ALL_LEN (9+9+7)
111
112 static UCHAR device_framework_full_speed[] = {
113
114 /* Device descriptor */
115 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
116 0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
117 0x00, 0x01,
118
119 CFG_DESC(CFG_DESC_LEN+1*HID_IFC_DESC_ALL_LEN, 1, 1)
120 /* Keyboard */
121 HID_IFC_DESC_ALL(0, HID_REPORT_LENGTH, 0x81)
122 };
123 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
124
125 static UCHAR device_framework_high_speed[] = {
126
127 /* Device descriptor */
128 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
129 0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
130 0x03, 0x01,
131
132 /* Device qualifier descriptor */
133 0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
134 0x01, 0x00,
135
136 CFG_DESC(CFG_DESC_LEN+1*HID_IFC_DESC_ALL_LEN, 1, 1)
137 /* Keyboard */
138 HID_IFC_DESC_ALL(0, HID_REPORT_LENGTH, 0x81)
139 };
140 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
141
142
143 /* String Device Framework :
144 Byte 0 and 1 : Word containing the language ID : 0x0904 for US
145 Byte 2 : Byte containing the index of the descriptor
146 Byte 3 : Byte containing the length of the descriptor string
147 */
148 static UCHAR string_framework[] = {
149
150 /* Manufacturer string descriptor : Index 1 */
151 0x09, 0x04, 0x01, 0x0c,
152 0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
153 0x6f, 0x67, 0x69, 0x63,
154
155 /* Product string descriptor : Index 2 */
156 0x09, 0x04, 0x02, 0x0c,
157 0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
158 0x6f, 0x61, 0x72, 0x64,
159
160 /* Serial Number string descriptor : Index 3 */
161 0x09, 0x04, 0x03, 0x04,
162 0x30, 0x30, 0x30, 0x31
163 };
164 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
165
166
167 /* Multiple languages are supported on the device, to add
168 a language besides english, the unicode language code must
169 be appended to the language_id_framework array and the length
170 adjusted accordingly. */
171 static UCHAR language_id_framework[] = {
172
173 /* English. */
174 0x09, 0x04
175 };
176 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
177
178
179 UINT _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
180
181
ux_system_host_change_function(ULONG event,UX_HOST_CLASS * cls,VOID * inst)182 static UINT ux_system_host_change_function(ULONG event, UX_HOST_CLASS *cls, VOID *inst)
183 {
184 switch(event)
185 {
186 case UX_HID_CLIENT_INSERTION:
187 break;
188 case UX_HID_CLIENT_REMOVAL:
189 break;
190 #if defined(UX_HOST_STANDALONE)
191 case UX_STANDALONE_WAIT_BACKGROUND_TASK:
192 /* Let other threads to run. */
193 tx_thread_relinquish();
194 break;
195 #endif
196 default:
197 break;
198 }
199 return 0;
200 }
201
error_callback(UINT system_level,UINT system_context,UINT error_code)202 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
203 {
204 }
205
206 /* Define what the initial system looks like. */
207
208 #ifdef CTEST
test_application_define(void * first_unused_memory)209 void test_application_define(void *first_unused_memory)
210 #else
211 void usbx_class_hid_mouse_basic_test_application_define(void *first_unused_memory)
212 #endif
213 {
214
215 UINT status;
216 CHAR * stack_pointer;
217 CHAR * memory_pointer;
218
219
220 /* Inform user. */
221 printf("Running HID Class Mouse Basic Test............................... ");
222
223 /* Initialize the free memory pointer */
224 stack_pointer = (CHAR *) usbx_memory;
225 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
226
227 /* Initialize USBX. Memory */
228 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
229
230 /* Check for error. */
231 if (status != UX_SUCCESS)
232 {
233
234 printf("Error on line %d, error code: %d\n", __LINE__, status);
235 test_control_return(1);
236 }
237
238 /* Register the error callback. */
239 _ux_utility_error_callback_register(error_callback);
240
241 /* The code below is required for installing the host portion of USBX */
242 status = ux_host_stack_initialize(ux_system_host_change_function);
243 if (status != UX_SUCCESS)
244 {
245
246 printf("Error on line %d, error code: %d\n", __LINE__, status);
247 test_control_return(1);
248 }
249
250 status = ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
251 if (status != UX_SUCCESS)
252 {
253
254 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
255 test_control_return(1);
256 }
257
258 /* Register the HID client(s). */
259 status = ux_host_class_hid_client_register(_ux_system_host_class_hid_client_mouse_name, ux_host_class_hid_mouse_entry);
260 if (status != UX_SUCCESS)
261 {
262
263 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
264 test_control_return(1);
265 }
266
267 /* The code below is required for installing the device portion of USBX. No call back for
268 device status change in this example. */
269 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
270 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
271 string_framework, STRING_FRAMEWORK_LENGTH,
272 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
273 if(status!=UX_SUCCESS)
274 {
275
276 printf("Error on line %d, error code: %d\n", __LINE__, status);
277 test_control_return(1);
278 }
279
280 /* Initialize the hid class parameters. */
281 ux_utility_memory_set(&hid_parameter, 0, sizeof(hid_parameter));
282 hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
283 hid_parameter.ux_device_class_hid_parameter_report_length = HID_REPORT_LENGTH;
284 hid_parameter.ux_device_class_hid_parameter_callback = demo_thread_hid_set_callback;
285 hid_parameter.ux_device_class_hid_parameter_get_callback = demo_thread_hid_get_callback;
286
287 hid_parameter.ux_slave_class_hid_instance_activate = demo_device_hid_instance_activate;
288 hid_parameter.ux_slave_class_hid_instance_deactivate = demo_device_hid_instance_deactivate;
289
290 /* Initialize the device hid class. The class is connected with interface 2 */
291 status = ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
292 1, 0, (VOID *)&hid_parameter);
293 if(status!=UX_SUCCESS)
294 {
295
296 printf("Error on line %d, error code: %d\n", __LINE__, status);
297 test_control_return(1);
298 }
299
300 /* Initialize the simulated device controller. */
301 status = _ux_dcd_sim_slave_initialize();
302
303 /* Check for error. */
304 if (status != UX_SUCCESS)
305 {
306
307 printf("Error on line %d, error code: %d\n", __LINE__, status);
308 test_control_return(1);
309 }
310
311 /* Register all the USB host controllers available in this system */
312 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
313
314 /* Check for error. */
315 if (status != UX_SUCCESS)
316 {
317
318 printf("Error on line %d, error code: %d\n", __LINE__, status);
319 test_control_return(1);
320 }
321
322 /* Create the main device simulation thread. */
323 status = tx_thread_create(&tx_demo_thread_device_simulation, "tx demo device simulation", tx_demo_thread_device_simulation_entry, 0,
324 stack_pointer, UX_DEMO_STACK_SIZE,
325 20, 20, 1, TX_AUTO_START);
326
327 /* Check for error. */
328 if (status != TX_SUCCESS)
329 {
330
331 printf("Error on line %d, error code: %d\n", __LINE__, status);
332 test_control_return(1);
333 }
334 stack_pointer += UX_DEMO_STACK_SIZE;
335
336 /* Create the main host simulation thread. */
337 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
338 stack_pointer, UX_DEMO_STACK_SIZE,
339 20, 20, 1, TX_AUTO_START);
340
341 /* Check for error. */
342 if (status != TX_SUCCESS)
343 {
344
345 printf("Error on line %d, error code: %d\n", __LINE__, status);
346 test_control_return(1);
347 }
348 }
349
tx_demo_thread_device_simulation_entry(ULONG arg)350 static void tx_demo_thread_device_simulation_entry(ULONG arg)
351 {
352 while(1)
353 {
354 #if defined(UX_DEVICE_STANDALONE)
355 ux_system_tasks_run();
356 #else
357 tx_thread_suspend(&tx_demo_thread_device_simulation);
358 #endif
359 }
360 }
361
demo_class_hid_wait(ULONG tick)362 static UINT demo_class_hid_wait(ULONG tick)
363 {
364 return(ux_test_sleep_break_on_success(tick, demo_class_hid_mouse_get));
365 }
366
_wait_mouse_change(UX_HOST_CLASS_HID_MOUSE * mouse,ULONG * buttons,SLONG * x,SLONG * y,SLONG * w)367 static UINT _wait_mouse_change(UX_HOST_CLASS_HID_MOUSE *mouse,
368 ULONG *buttons, SLONG *x, SLONG *y, SLONG *w)
369 {
370 UINT status;
371 UINT i;
372 ULONG buttons_bak = *buttons;
373 SLONG x_bak = *x, y_bak = *y;
374 SLONG w_bak = *w;
375
376 for (i = 0; i < 200; i ++)
377 {
378 #if defined(UX_HOST_STANDALONE)
379 ux_system_tasks_run();
380 #endif
381 ux_host_class_hid_mouse_buttons_get(hid_mouse, buttons);
382 ux_host_class_hid_mouse_position_get(hid_mouse, x, y);
383 ux_host_class_hid_mouse_wheel_get(hid_mouse, w);
384 if (buttons_bak != *buttons ||
385 x_bak != *x ||
386 y_bak != *y ||
387 w_bak != *w)
388 {
389 return(UX_SUCCESS);
390 }
391 tx_thread_sleep(1);
392 }
393 return(UX_ERROR);
394 }
395
test_hid_mouse_events(VOID)396 static void test_hid_mouse_events(VOID)
397 {
398 UINT status;
399 ULONG buttons = 0;
400 SLONG x = 0xFF, y = 0xFF;
401 SLONG wheel = 0;
402
403 /* Initialize mouse event. */
404 device_hid_event.ux_device_class_hid_event_length = 4;
405 device_hid_event.ux_device_class_hid_event_buffer[0] = 0; /* ...M|R|L */
406 device_hid_event.ux_device_class_hid_event_buffer[1] = 0; /* X */
407 device_hid_event.ux_device_class_hid_event_buffer[2] = 0; /* Y */
408 device_hid_event.ux_device_class_hid_event_buffer[3] = 0; /* Wheel */
409
410 status = ux_host_class_hid_mouse_buttons_get(hid_mouse, &buttons);
411 status |= ux_host_class_hid_mouse_position_get(hid_mouse, &x, &y);
412 status |= ux_host_class_hid_mouse_wheel_get(hid_mouse, &wheel);
413 UX_ASSERT(status == UX_SUCCESS);
414 UX_ASSERT(buttons == 0);
415 UX_ASSERT(x == 0);
416 UX_ASSERT(y == 0);
417 UX_ASSERT(wheel == 0);
418
419 /* Set L click. */
420 device_hid_event.ux_device_class_hid_event_buffer[0] = 1; /* ...M|R|L */
421 _ux_device_class_hid_event_set(device_hid, &device_hid_event);
422 status = _wait_mouse_change(hid_mouse, &buttons, &x, &y, &wheel);
423 UX_ASSERT(status == UX_SUCCESS);
424 UX_ASSERT(buttons == 1);
425 UX_ASSERT(x == 0);
426 UX_ASSERT(y == 0);
427 UX_ASSERT(wheel == 0);
428
429 /* Set R click. */
430 device_hid_event.ux_device_class_hid_event_buffer[0] = 3; /* ...M|R|L */
431 _ux_device_class_hid_event_set(device_hid, &device_hid_event);
432 status = _wait_mouse_change(hid_mouse, &buttons, &x, &y, &wheel);
433 UX_ASSERT(status == UX_SUCCESS);
434 UX_ASSERT(buttons == 3);
435 UX_ASSERT(x == 0);
436 UX_ASSERT(y == 0);
437 UX_ASSERT(wheel == 0);
438
439 /* Move X. */
440 device_hid_event.ux_device_class_hid_event_buffer[1] = -1; /* X */
441 _ux_device_class_hid_event_set(device_hid, &device_hid_event);
442 status = _wait_mouse_change(hid_mouse, &buttons, &x, &y, &wheel);
443 UX_ASSERT(status == UX_SUCCESS);
444 UX_ASSERT(buttons == 3);
445 UX_ASSERT(x == -1);
446 UX_ASSERT(y == 0);
447 UX_ASSERT(wheel == 0);
448
449 /* Move X. */
450 device_hid_event.ux_device_class_hid_event_buffer[1] = +8; /* X */
451 _ux_device_class_hid_event_set(device_hid, &device_hid_event);
452 status = _wait_mouse_change(hid_mouse, &buttons, &x, &y, &wheel);
453 UX_ASSERT(status == UX_SUCCESS);
454 UX_ASSERT(buttons == 3);
455 UX_ASSERT(x == +7);
456 UX_ASSERT(y == 0);
457 UX_ASSERT(wheel == 0);
458
459 /* Move Y. */
460 device_hid_event.ux_device_class_hid_event_buffer[2] = +8; /* Y */
461 _ux_device_class_hid_event_set(device_hid, &device_hid_event);
462 status = _wait_mouse_change(hid_mouse, &buttons, &x, &y, &wheel);
463 UX_ASSERT(status == UX_SUCCESS);
464 UX_ASSERT(buttons == 3);
465 UX_ASSERT(x == 15);
466 UX_ASSERT(y == 8);
467 UX_ASSERT(wheel == 0);
468
469 /* Move Wheel. */
470 device_hid_event.ux_device_class_hid_event_buffer[1] = -2; /* X */
471 device_hid_event.ux_device_class_hid_event_buffer[2] = -5; /* Y */
472 device_hid_event.ux_device_class_hid_event_buffer[3] = +10; /* Wheel */
473 _ux_device_class_hid_event_set(device_hid, &device_hid_event);
474 status = _wait_mouse_change(hid_mouse, &buttons, &x, &y, &wheel);
475 UX_ASSERT(status == UX_SUCCESS);
476 UX_ASSERT(buttons == 3);
477 UX_ASSERT(x == 13);
478 UX_ASSERT(y == 3);
479 UX_ASSERT(wheel == 10);
480 }
481
tx_demo_thread_host_simulation_entry(ULONG arg)482 static void tx_demo_thread_host_simulation_entry(ULONG arg)
483 {
484
485 UINT status;
486
487 /* Find the HID class */
488 status = demo_class_hid_wait(100);
489 UX_TEST_ASSERT(status == UX_SUCCESS);
490
491 test_hid_mouse_events();
492
493 /* Now disconnect the device. */
494 _ux_device_stack_disconnect();
495
496 /* And deinitialize the class. */
497 status = ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
498
499 /* Deinitialize the device side of usbx. */
500 _ux_device_stack_uninitialize();
501
502 /* And finally the usbx system resources. */
503 _ux_system_uninitialize();
504
505 /* Successful test. */
506 printf("SUCCESS!\n");
507 test_control_return(0);
508 }
509
demo_thread_hid_set_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)510 static UINT demo_thread_hid_set_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
511 {
512 _ux_utility_memory_copy(&device_hid_event, event, sizeof(UX_SLAVE_CLASS_HID_EVENT));
513 return(UX_SUCCESS);
514 }
demo_thread_hid_get_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)515 static UINT demo_thread_hid_get_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
516 {
517 _ux_utility_memory_copy(event, &device_hid_event, sizeof(UX_SLAVE_CLASS_HID_EVENT));
518 return(UX_SUCCESS);
519 }
520
demo_device_hid_instance_activate(VOID * inst)521 static void demo_device_hid_instance_activate(VOID *inst)
522 {
523 if (device_hid == UX_NULL)
524 device_hid = (UX_SLAVE_CLASS_HID *)inst;
525 }
demo_device_hid_instance_deactivate(VOID * inst)526 static void demo_device_hid_instance_deactivate(VOID *inst)
527 {
528 if (inst == (VOID *)device_hid)
529 device_hid = UX_NULL;
530 }
531