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