1 /* This tests focuses on code coverage of ux_host_class_hid_keyboard_callback. */
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 #include "ux_host_class_hid.h"
9 #include "ux_host_class_hid_keyboard.h"
10 #include "ux_device_class_hid.h"
11 #include "ux_device_stack.h"
12 #include "usbx_test_common_hid.h"
13
14 /* Define constants. */
15 #define UX_DEMO_DEBUG_SIZE (4096*8)
16 #define UX_DEMO_STACK_SIZE 1024
17 #define UX_DEMO_BUFFER_SIZE 2048
18 #define UX_DEMO_RECEPTION_BUFFER_SIZE 512
19 #define UX_DEMO_XMIT_BUFFER_SIZE 512
20 #define UX_DEMO_RECEPTION_BLOCK_SIZE 64
21
22 /* Define local/extern function prototypes. */
23 static void demo_thread_entry(ULONG);
24 static UINT demo_thread_hid_callback(UX_SLAVE_CLASS_HID *, UX_SLAVE_CLASS_HID_EVENT *);
25 static TX_THREAD tx_demo_thread_host_simulation;
26 static TX_THREAD tx_demo_thread_slave_simulation;
27 static void tx_demo_thread_host_simulation_entry(ULONG);
28 static void tx_demo_thread_slave_simulation_entry(ULONG);
29
30
31 /* Define global data structures. */
32 static ULONG error_counter;
33 static TX_THREAD demo_thread;
34 static UX_HOST_CLASS *class_driver;
35 static ULONG class_driver_index;
36 static UX_HOST_CLASS_HID *hid;
37 static UX_HOST_CLASS_HID_CLIENT *hid_client;
38 static UX_HOST_CLASS_HID_KEYBOARD *keyboard;
39 static UINT status;
40 static UINT transfer_completed;
41 static ULONG requested_length;
42 static TX_SEMAPHORE demo_semaphore;
43
44 static ULONG keyboard_char;
45 static ULONG keyboard_state;
46 static UCHAR keyboard_queue[1024];
47 static ULONG keyboard_queue_index;
48
49 static UX_SLAVE_CLASS_HID_PARAMETER hid_parameter;
50
51 static UCHAR hid_keyboard_report[] = {
52
53 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
54 0x09, 0x06, // USAGE (Keyboard)
55 0xa1, 0x01, // COLLECTION (Application)
56 0x05, 0x07, // USAGE_PAGE (Keyboard)
57 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
58 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
59 0x15, 0x00, // LOGICAL_MINIMUM (0)
60 0x25, 0x01, // LOGICAL_MAXIMUM (1)
61 0x75, 0x01, // REPORT_SIZE (1)
62 0x95, 0x08, // REPORT_COUNT (8)
63 0x81, 0x02, // INPUT (Data,Var,Abs)
64 0x09, 0x39, // USAGE (Caps Lock)
65 0x09, 0x53, // USAGE (Num Lock)
66 0x09, 0x47, // USAGE (Scroll Lock)
67 0x75, 0x01, // REPORT_SIZE (1)
68 0x95, 0x03, // REPORT_COUNT (3)
69 0x81, 0x02, // INPUT (Data,Var,Abs)
70 0x95, 0x01, // REPORT_COUNT (1)
71 0x75, 0x05, // REPORT_SIZE (5)
72 0x81, 0x03, // INPUT (Cnst,Var,Abs)
73 0x95, 0x05, // REPORT_COUNT (5)
74 0x75, 0x01, // REPORT_SIZE (1)
75 0x05, 0x08, // USAGE_PAGE (LEDs)
76 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
77 0x29, 0x05, // USAGE_MAXIMUM (Kana)
78 0x91, 0x02, // OUTPUT (Data,Var,Abs)
79 0x95, 0x01, // REPORT_COUNT (1)
80 0x75, 0x03, // REPORT_SIZE (3)
81 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
82 0x95, 0x06, // REPORT_COUNT (6)
83 0x75, 0x08, // REPORT_SIZE (8)
84 0x15, 0x00, // LOGICAL_MINIMUM (0)
85 0x25, 0x65, // LOGICAL_MAXIMUM (101)
86 0x05, 0x07, // USAGE_PAGE (Keyboard)
87 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
88 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
89 0x81, 0x00, // INPUT (Data,Ary,Abs)
90 0xc0 // END_COLLECTION
91 };
92 #define HID_KEYBOARD_REPORT_LENGTH (sizeof(hid_keyboard_report)/sizeof(hid_keyboard_report)[0])
93
94
95 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
96 static UCHAR device_framework_full_speed[] = {
97
98 /* Device descriptor */
99 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
100 0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
101 0x00, 0x01,
102
103 /* Configuration descriptor */
104 0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
105 0x32,
106
107 /* Interface descriptor */
108 0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
109 0x00,
110
111 /* HID descriptor */
112 0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_KEYBOARD_REPORT_LENGTH),
113 MSB(HID_KEYBOARD_REPORT_LENGTH),
114
115 /* Endpoint descriptor (Interrupt) */
116 0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
117
118 };
119
120
121 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
122 static UCHAR device_framework_high_speed[] = {
123
124 /* Device descriptor */
125 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
126 0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
127 0x03, 0x01,
128
129 /* Device qualifier descriptor */
130 0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
131 0x01, 0x00,
132
133 /* Configuration descriptor */
134 0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
135 0x32,
136
137 /* Interface descriptor */
138 0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
139 0x00,
140
141 /* HID descriptor */
142 0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_KEYBOARD_REPORT_LENGTH),
143 MSB(HID_KEYBOARD_REPORT_LENGTH),
144
145 /* Endpoint descriptor (Interrupt) */
146 0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
147
148 };
149
150
151 /* String Device Framework :
152 Byte 0 and 1 : Word containing the language ID : 0x0904 for US
153 Byte 2 : Byte containing the index of the descriptor
154 Byte 3 : Byte containing the length of the descriptor string
155 */
156
157 #define STRING_FRAMEWORK_LENGTH 40
158 static UCHAR string_framework[] = {
159
160 /* Manufacturer string descriptor : Index 1 */
161 0x09, 0x04, 0x01, 0x0c,
162 0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
163 0x6f, 0x67, 0x69, 0x63,
164
165 /* Product string descriptor : Index 2 */
166 0x09, 0x04, 0x02, 0x0c,
167 0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
168 0x6f, 0x61, 0x72, 0x64,
169
170 /* Serial Number string descriptor : Index 3 */
171 0x09, 0x04, 0x03, 0x04,
172 0x30, 0x30, 0x30, 0x31
173 };
174
175
176 /* Multiple languages are supported on the device, to add
177 a language besides english, the unicode language code must
178 be appended to the language_id_framework array and the length
179 adjusted accordingly. */
180 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
181 static UCHAR language_id_framework[] = {
182
183 /* English. */
184 0x09, 0x04
185 };
186
187
188
189 /* Define the ISR dispatch. */
190
191 extern VOID (*test_isr_dispatch)(void);
192
193
194 /* Prototype for test control return. */
195
196 void test_control_return(UINT status);
197
198
199 /* Define the ISR dispatch routine. */
200
test_isr(void)201 static void test_isr(void)
202 {
203
204 /* For further expansion of interrupt-level testing. */
205 }
206
207
208
error_callback(UINT system_level,UINT system_context,UINT error_code)209 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
210 {
211
212 /* @BUG_FIX_PENDING: ux_dcd_sim_slave_function.c doesn't support transfer aborts, which happen during device unregistration of a class. */
213 if (error_code != UX_FUNCTION_NOT_SUPPORTED)
214 {
215
216 /* Failed test. */
217 printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
218 test_control_return(1);
219 }
220 }
221
222 /* Define what the initial system looks like. */
223
224 #ifdef CTEST
test_application_define(void * first_unused_memory)225 void test_application_define(void *first_unused_memory)
226 #else
227 void usbx_hid_keyboard_callback_test_application_define(void *first_unused_memory)
228 #endif
229 {
230
231 UINT status;
232 CHAR * stack_pointer;
233 CHAR * memory_pointer;
234 UINT descriptor_size = HID_KEYBOARD_REPORT_LENGTH;
235
236 /* Inform user. */
237 printf("Running HID Keyboard Callback Test.................................. ");
238
239 /* Initialize the free memory pointer */
240 stack_pointer = (CHAR *) usbx_memory;
241 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
242
243 /* Initialize USBX. Memory */
244 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
245
246 /* Check for error. */
247 if (status != UX_SUCCESS)
248 {
249
250 printf("Error on line %d, error code: %d\n", __LINE__, status);
251 test_control_return(1);
252 }
253
254 /* Register the error callback. */
255 _ux_utility_error_callback_register(error_callback);
256
257 /* The code below is required for installing the host portion of USBX */
258 status = ux_host_stack_initialize(UX_NULL);
259 if (status != UX_SUCCESS)
260 {
261
262 printf("Error on line %d, error code: %d\n", __LINE__, status);
263 test_control_return(1);
264 }
265
266 status = ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
267 if (status != UX_SUCCESS)
268 {
269
270 printf("Error on line %d, error code: %d\n", __LINE__, status);
271 test_control_return(1);
272 }
273
274 /* Register the HID client(s). */
275 status = ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
276 if (status != UX_SUCCESS)
277 {
278
279 printf("Error on line %d, error code: %d\n", __LINE__, status);
280 test_control_return(1);
281 }
282
283 /* The code below is required for installing the device portion of USBX. No call back for
284 device status change in this example. */
285 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
286 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
287 string_framework, STRING_FRAMEWORK_LENGTH,
288 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
289 if(status!=UX_SUCCESS)
290 {
291
292 printf("Error on line %d, error code: %d\n", __LINE__, status);
293 test_control_return(1);
294 }
295
296 /* Initialize the hid class parameters for a keyboard. */
297 hid_parameter.ux_device_class_hid_parameter_report_address = hid_keyboard_report;
298 hid_parameter.ux_device_class_hid_parameter_report_length = HID_KEYBOARD_REPORT_LENGTH;
299 hid_parameter.ux_device_class_hid_parameter_callback = demo_thread_hid_callback;
300
301 /* Initilize the device hid class. The class is connected with interface 2 */
302 status = ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
303 1,2, (VOID *)&hid_parameter);
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
312 /* Initialize the simulated device controller. */
313 status = _ux_dcd_sim_slave_initialize();
314
315 /* Check for error. */
316 if (status != UX_SUCCESS)
317 {
318
319 printf("Error on line %d, error code: %d\n", __LINE__, status);
320 test_control_return(1);
321 }
322
323 /* Register all the USB host controllers available in this system */
324 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
325
326 /* Check for error. */
327 if (status != UX_SUCCESS)
328 {
329
330 printf("Error on line %d, error code: %d\n", __LINE__, status);
331 test_control_return(1);
332 }
333
334 /* Create the main host simulation thread. */
335 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
336 stack_pointer, UX_DEMO_STACK_SIZE,
337 20, 20, 1, TX_AUTO_START);
338
339 /* Check for error. */
340 if (status != TX_SUCCESS)
341 {
342
343 printf("Error on line %d, error code: %d\n", __LINE__, status);
344 test_control_return(1);
345 }
346
347 /* Create the main demo thread. */
348 status = tx_thread_create(&tx_demo_thread_slave_simulation, "tx demo slave simulation", tx_demo_thread_slave_simulation_entry, 0,
349 stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
350 20, 20, 1, TX_AUTO_START);
351
352 /* Check for error. */
353 if (status != TX_SUCCESS)
354 {
355
356 printf("Error on line %d, error code: %d\n", __LINE__, status);
357 test_control_return(1);
358 }
359
360 }
361
362
tx_demo_thread_host_simulation_entry(ULONG arg)363 static void tx_demo_thread_host_simulation_entry(ULONG arg)
364 {
365
366 UINT status;
367 UINT max_hid_loop;
368
369 /* Initialize max loop value. */
370 max_hid_loop = 2*UX_HOST_CLASS_HID_KEYBOARD_USAGE_ARRAY_LENGTH;
371
372 /* Find the HID class */
373 status = demo_class_hid_get();
374 if (status != UX_SUCCESS)
375 {
376
377 printf("Error on line %d, error code: %d\n", __LINE__, status);
378 test_control_return(1);
379 }
380
381 /* Get the HID client */
382 hid_client = hid -> ux_host_class_hid_client;
383
384 /* Check if the instance of the keyboard is live */
385 while (hid_client -> ux_host_class_hid_client_local_instance == UX_NULL)
386 tx_thread_sleep(10);
387
388 /* Get the keyboard instance */
389 keyboard = (UX_HOST_CLASS_HID_KEYBOARD *)hid_client -> ux_host_class_hid_client_local_instance;
390
391 /* Init the keyboard queue index. */
392 keyboard_queue_index = 0;
393
394 while (max_hid_loop--)
395 {
396 /* Get a key/state from the keyboard. */
397 status = ux_host_class_hid_keyboard_key_get(keyboard, &keyboard_char, &keyboard_state);
398
399 /* Check if there is something. */
400 if (status == UX_SUCCESS)
401 {
402 /* We have a character in the queue. */
403 keyboard_queue[keyboard_queue_index] = (UCHAR) keyboard_char;
404
405 /* Can we accept more ? */
406 if(keyboard_queue_index < 1024)
407 keyboard_queue_index++;
408
409 }
410
411 tx_thread_sleep(10);
412 }
413
414 /* Now disconnect the device. */
415 _ux_device_stack_disconnect();
416
417 /* And deinitialize the class. */
418 status = ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
419
420 /* Deinitialize the device side of usbx. */
421 _ux_device_stack_uninitialize();
422
423 /* And finally the usbx system resources. */
424 _ux_system_uninitialize();
425
426 /* Successful test. */
427 printf("SUCCESS!\n");
428 test_control_return(0);
429 }
430
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)431 static UINT demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
432 {
433 return(UX_SUCCESS);
434 }
435
tx_demo_thread_slave_simulation_entry(ULONG arg)436 static void tx_demo_thread_slave_simulation_entry(ULONG arg)
437 {
438
439 UX_SLAVE_DEVICE *device;
440 UX_SLAVE_INTERFACE *interface;
441 UX_SLAVE_CLASS_HID *hid;
442 UX_SLAVE_CLASS_HID_EVENT hid_event;
443 UCHAR key1;
444 UCHAR key2;
445
446 /* Get the pointer to the device. */
447 device = &_ux_system_slave -> ux_system_slave_device;
448
449 /* Set the first key to 'a' which is 04. */
450 key1 = 0x04;
451
452 /* Set the second key to the first keypad key. */
453 key2 = 0x54;
454
455 /* reset the HID event structure. */
456 ux_utility_memory_set(&hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT));
457
458 /* First byte is a modifier byte. */
459 hid_event.ux_device_class_hid_event_buffer[0] = 0xff;
460
461 /* Second byte is part modifier and part padding. */
462 hid_event.ux_device_class_hid_event_buffer[1] = 0xff;
463
464 while(1)
465 {
466
467 /* Is the device configured ? */
468 while (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
469
470 /* Then wait. */
471 tx_thread_sleep(10);
472
473 /* Until the device stays configured. */
474 while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
475 {
476
477 /* Get the interface. We use the first interface, this is a simple device. */
478 interface = device -> ux_slave_device_first_interface;
479
480 /* Form that interface, derive the HID owner. */
481 hid = interface -> ux_slave_interface_class_instance;
482
483 /* Wait for 2 seconds. */
484 ux_utility_thread_sleep(20);
485
486 /* Then insert a key into the keyboard event. Length is fixed to 8. */
487 hid_event.ux_device_class_hid_event_length = 8;
488
489 /* Toggle first and second byte. */
490 hid_event.ux_device_class_hid_event_buffer[0] ^= 0xff;
491 hid_event.ux_device_class_hid_event_buffer[1] ^= 0xff;
492
493 /* The 6 next bytes are keys. We only have two keys here. */
494 hid_event.ux_device_class_hid_event_buffer[2] = key1;
495 hid_event.ux_device_class_hid_event_buffer[3] = key2;
496
497 /* Set the keyboard event. */
498 ux_device_class_hid_event_set(hid, &hid_event);
499
500 /* Next event has the key depressed. */
501 hid_event.ux_device_class_hid_event_buffer[2] = 0;
502
503 /* Length is fixed to 8. */
504 hid_event.ux_device_class_hid_event_length = 8;
505
506 /* Set the keyboard event. */
507 ux_device_class_hid_event_set(hid, &hid_event);
508
509 /* Are we at the end of alphabet? */
510 if (key1 != (0x04 + 26))
511
512 /* Next key. */
513 key1++;
514
515 else
516
517 /* Start over again. */
518 key1 = 0x04;
519
520 /* Are we at the end of keypad keys? */
521 if (key2 != 0x67)
522
523 /* Next key. */
524 key2++;
525
526 else
527
528 /* Start over again. */
529 key2 = 0x54;
530
531 }
532 }
533 }
534
535