1 #include "usbx_test_common_hid.h"
2 #include "ux_host_class_hid_keyboard.h"
3
4
5 #define DUMMY_USBX_MEMORY_SIZE (64*1024)
6 static UCHAR dummy_usbx_memory[DUMMY_USBX_MEMORY_SIZE];
7
8 static UCHAR hid_report_descriptor[] = {
9
10 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
11 0x09, 0x06, // USAGE (Keyboard)
12 0xa1, 0x01, // COLLECTION (Application)
13 0x05, 0x07, // USAGE_PAGE (Keyboard)
14 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
15 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
16 0x15, 0x00, // LOGICAL_MINIMUM (0)
17 0x25, 0x01, // LOGICAL_MAXIMUM (1)
18 0x75, 0x01, // REPORT_SIZE (1)
19 0x95, 0x08, // REPORT_COUNT (8)
20 0x81, 0x02, // INPUT (Data,Var,Abs)
21 0x95, 0x01, // REPORT_COUNT (1)
22 0x75, 0x08, // REPORT_SIZE (8)
23 0x81, 0x03, // INPUT (Cnst,Var,Abs)
24 0x95, 0x05, // REPORT_COUNT (5)
25 0x75, 0x01, // REPORT_SIZE (1)
26 0x05, 0x08, // USAGE_PAGE (LEDs)
27 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
28 0x29, 0x05, // USAGE_MAXIMUM (Kana)
29 0x91, 0x02, // OUTPUT (Data,Var,Abs)
30 0x95, 0x01, // REPORT_COUNT (1)
31 0x75, 0x03, // REPORT_SIZE (3)
32 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
33 0x95, 0x06, // REPORT_COUNT (6)
34 0x75, 0x08, // REPORT_SIZE (8)
35 0x15, 0x00, // LOGICAL_MINIMUM (0)
36 0x25, 0x65, // LOGICAL_MAXIMUM (101)
37 0x05, 0x07, // USAGE_PAGE (Keyboard)
38 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
39 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
40 0x81, 0x00, // INPUT (Data,Ary,Abs)
41 0xc0 // END_COLLECTION
42 };
43 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
44
45
46 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
47 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
48
49 /* Device descriptor */
50 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
51 0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x01,
53
54 /* Configuration descriptor */
55 0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
56 0x32,
57
58 /* Interface descriptor */
59 0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
60 0x00,
61
62 /* HID descriptor */
63 0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
64 MSB(HID_REPORT_LENGTH),
65
66 /* Endpoint descriptor (Interrupt) */
67 0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
68
69 };
70
71
72 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
73 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
74
75 /* Device descriptor */
76 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
77 0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
78 0x03, 0x01,
79
80 /* Device qualifier descriptor */
81 0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
82 0x01, 0x00,
83
84 /* Configuration descriptor */
85 0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
86 0x32,
87
88 /* Interface descriptor */
89 0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
90 0x00,
91
92 /* HID descriptor */
93 0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
94 MSB(HID_REPORT_LENGTH),
95
96 /* Endpoint descriptor (Interrupt) */
97 0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
98
99 };
100
101
102 /* String Device Framework :
103 Byte 0 and 1 : Word containing the language ID : 0x0904 for US
104 Byte 2 : Byte containing the index of the descriptor
105 Byte 3 : Byte containing the length of the descriptor string
106 */
107
108 #define STRING_FRAMEWORK_LENGTH 40
109 static UCHAR string_framework[] = {
110
111 /* Manufacturer string descriptor : Index 1 */
112 0x09, 0x04, 0x01, 0x0c,
113 0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
114 0x6f, 0x67, 0x69, 0x63,
115
116 /* Product string descriptor : Index 2 */
117 0x09, 0x04, 0x02, 0x0c,
118 0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
119 0x6f, 0x61, 0x72, 0x64,
120
121 /* Serial Number string descriptor : Index 3 */
122 0x09, 0x04, 0x03, 0x04,
123 0x30, 0x30, 0x30, 0x31
124 };
125
126
127 /* Multiple languages are supported on the device, to add
128 a language besides english, the unicode language code must
129 be appended to the language_id_framework array and the length
130 adjusted accordingly. */
131 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
132 static UCHAR language_id_framework[] = {
133
134 /* English. */
135 0x09, 0x04
136 };
137
138
139 UINT _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
140
141
ux_system_host_change_function(ULONG a,UX_HOST_CLASS * b,VOID * c)142 static UINT ux_system_host_change_function(ULONG a, UX_HOST_CLASS *b, VOID *c)
143 {
144 return 0;
145 }
146
error_callback(UINT system_level,UINT system_context,UINT error_code)147 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
148 {
149 }
150
151 /* Define what the initial system looks like. */
152
153 #ifdef CTEST
test_application_define(void * first_unused_memory)154 void test_application_define(void *first_unused_memory)
155 #else
156 void usbx_ux_device_class_hid_initialize_test_application_define(void *first_unused_memory)
157 #endif
158 {
159
160 UINT status;
161 CHAR * stack_pointer;
162 CHAR * memory_pointer;
163
164
165 /* Inform user. */
166 printf("Running ux_device_class_hid_initialize Test......................... ");
167
168 /* Initialize the free memory pointer */
169 stack_pointer = (CHAR *) usbx_memory;
170 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
171
172 /* Initialize USBX. Memory */
173 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
174
175 /* Check for error. */
176 if (status != UX_SUCCESS)
177 {
178
179 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
180 test_control_return(1);
181 }
182
183 /* Register the error callback. */
184 _ux_utility_error_callback_register(error_callback);
185
186 /* The code below is required for installing the host portion of USBX */
187 status = ux_host_stack_initialize(ux_system_host_change_function);
188 if (status != UX_SUCCESS)
189 {
190
191 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
192 test_control_return(1);
193 }
194
195 status = ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
196 if (status != UX_SUCCESS)
197 {
198
199 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
200 test_control_return(1);
201 }
202
203 /* Register the HID client(s). */
204 status = ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
205 if (status != UX_SUCCESS)
206 {
207
208 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
209 test_control_return(1);
210 }
211
212 /* The code below is required for installing the device portion of USBX. No call back for
213 device status change in this example. */
214 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
215 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
216 string_framework, STRING_FRAMEWORK_LENGTH,
217 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
218 if(status!=UX_SUCCESS)
219 {
220
221 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
222 test_control_return(1);
223 }
224
225 /* Initialize the hid class parameters. */
226 hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
227 hid_parameter.ux_device_class_hid_parameter_report_length = HID_REPORT_LENGTH;
228 hid_parameter.ux_device_class_hid_parameter_callback = demo_thread_hid_callback;
229
230 /* Initilize the device hid class. The class is connected with interface 2 */
231 status = ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
232 1,2, (VOID *)&hid_parameter);
233 if(status!=UX_SUCCESS)
234 {
235
236 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
237 test_control_return(1);
238 }
239
240 /* Initialize the simulated device controller. */
241 status = _ux_dcd_sim_slave_initialize();
242
243 /* Check for error. */
244 if (status != UX_SUCCESS)
245 {
246
247 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
248 test_control_return(1);
249 }
250
251 /* Register all the USB host controllers available in this system */
252 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
253
254 /* Check for error. */
255 if (status != UX_SUCCESS)
256 {
257
258 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
259 test_control_return(1);
260 }
261
262 /* Create the main host simulation thread. */
263 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
264 stack_pointer, UX_DEMO_STACK_SIZE,
265 20, 20, 1, TX_AUTO_START);
266
267 /* Check for error. */
268 if (status != TX_SUCCESS)
269 {
270
271 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
272 test_control_return(1);
273 }
274 }
275
tx_demo_thread_host_simulation_entry(ULONG arg)276 static void tx_demo_thread_host_simulation_entry(ULONG arg)
277 {
278
279 UINT status;
280 UX_SLAVE_CLASS_COMMAND command;
281 UX_SLAVE_CLASS class;
282 UX_SLAVE_CLASS_HID_PARAMETER hid_parameter;
283 // UX_MEMORY_BLOCK *dummy_memory_block_first = (UX_MEMORY_BLOCK *)dummy_usbx_memory;
284 // UX_MEMORY_BLOCK *original_regular_memory_block = (UX_MEMORY_BLOCK *)_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_start;
285 // UX_MEMORY_BLOCK *original_cache_safe_memory_block = (UX_MEMORY_BLOCK *)_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE] -> ux_byte_pool_start;
286 UCHAR class_thread_stack[UX_THREAD_STACK_SIZE];
287 UX_SLAVE_CLASS_HID *hid_instance;
288 TX_EVENT_FLAGS_GROUP hid_instance_event_flags_copy;
289
290
291 /* Find the HID class */
292 status = demo_class_hid_get();
293 if (status != UX_SUCCESS)
294 {
295
296 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
297 test_control_return(1);
298 }
299
300 /* Get the HID client */
301 hid_client = hid -> ux_host_class_hid_client;
302
303 /* Check if the instance of the keyboard is live */
304 while (hid_client -> ux_host_class_hid_client_local_instance == UX_NULL)
305 tx_thread_sleep(10);
306
307 command.ux_slave_class_command_parameter = &hid_parameter;
308 command.ux_slave_class_command_class_ptr = &class;
309
310 /* Allocate a hid instance like hid_initialize(). */
311 hid_instance = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_SLAVE_CLASS_HID));
312 if (hid_instance == UX_NULL)
313 {
314
315 printf("Error on line %d\n", __LINE__);
316 test_control_return(1);
317 }
318
319 /**************************************************/
320 /** Test case: status = _ux_utility_event_flags_create(&hid -> ux_device_class_hid_event_flags_group, "ux_device_class_hid_event_flag"); fails **/
321 /**************************************************/
322
323 /* Create the event_flags like hid_initialize(). */
324 status = ux_utility_event_flags_create(&hid_instance -> ux_device_class_hid_event_flags_group, "ux_host_class_hid_keyboard_event_flags");
325 if (status != UX_SUCCESS)
326 {
327
328 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
329 test_control_return(1);
330 }
331
332 /* Make a copy of the event_flags. */
333 hid_instance_event_flags_copy = hid_instance -> ux_device_class_hid_event_flags_group;
334
335 /* Free the memory so hid_initialize() uses the same memory as us, which will cause threadx to detect a event_flags duplicate. */
336 ux_utility_memory_free(hid_instance);
337
338 status = _ux_device_class_hid_initialize(&command);
339 if (status != UX_EVENT_ERROR)
340 {
341
342 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
343 test_control_return(1);
344 }
345
346 /* ux_utility_memory_allocate() zero'd out our hid instance event_flags! Good thing we made a copy! */
347 hid_instance -> ux_device_class_hid_event_flags_group = hid_instance_event_flags_copy;
348
349 /** Restore state for next test. **/
350
351 status = ux_utility_event_flags_delete(&hid_instance -> ux_device_class_hid_event_flags_group);
352 if (status != UX_SUCCESS)
353 {
354
355 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
356 test_control_return(1);
357 }
358
359 /**************************************************/
360 /** Test case: status = _ux_utility_thread_create(&class -> ux_slave_class_thread, "ux_slave_class_thread",
361 _ux_device_class_hid_interrupt_thread,
362 (ULONG) class, (VOID *) class -> ux_slave_class_thread_stack,
363 UX_THREAD_STACK_SIZE, UX_THREAD_PRIORITY_CLASS,
364 UX_THREAD_PRIORITY_CLASS, UX_NO_TIME_SLICE, TX_DONT_START); fails **/
365 /**************************************************/
366
367 /* Create the thread like hid_initialize(). */
368 status = _ux_utility_thread_create(&class.ux_slave_class_thread, "ux_slave_class_thread",
369 _ux_device_class_hid_interrupt_thread,
370 (ULONG) (ALIGN_TYPE) &class, class_thread_stack,
371 UX_THREAD_STACK_SIZE, UX_THREAD_PRIORITY_CLASS,
372 UX_THREAD_PRIORITY_CLASS, UX_NO_TIME_SLICE, TX_DONT_START);
373 UX_THREAD_EXTENSION_PTR_SET(&class.ux_slave_class_thread, &class)
374 if (status != UX_SUCCESS)
375 {
376
377 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
378 test_control_return(1);
379 }
380
381 status = _ux_device_class_hid_initialize(&command);
382 if (status != UX_THREAD_ERROR)
383 {
384
385 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
386 test_control_return(1);
387 }
388
389 /* Restore state for next test. */
390 status = ux_utility_thread_delete(&class.ux_slave_class_thread);
391 if (status != UX_SUCCESS)
392 {
393
394 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
395 test_control_return(1);
396 }
397 #if 0 /* Tested by basic memory tests */
398 /**************************************************/
399 /** Test case: hid = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_SLAVE_CLASS_HID)); fails **/
400 /**************************************************/
401
402 /* Set up the dummy memory block. */
403 dummy_memory_block_first -> ux_memory_block_next = UX_NULL;
404 dummy_memory_block_first -> ux_memory_block_previous = UX_NULL;
405 dummy_memory_block_first -> ux_memory_block_status = UX_MEMORY_UNUSED;
406 dummy_memory_block_first -> ux_memory_block_size = 0;
407 _ux_system -> ux_system_regular_memory_pool_start = dummy_memory_block_first;
408 _ux_system -> ux_system_cache_safe_memory_pool_start = dummy_memory_block_first;
409
410 status = _ux_device_class_hid_initialize(&command);
411 if (status != UX_MEMORY_INSUFFICIENT)
412 {
413
414 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
415 test_control_return(1);
416 }
417
418 /**************************************************/
419 /** Test case: class -> ux_slave_class_thread_stack =
420 _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, UX_THREAD_STACK_SIZE); fails **/
421 /**************************************************/
422
423 command.ux_slave_class_command_class_ptr = &class;
424
425 /* Set up the dummy memory block. */
426 dummy_memory_block_first -> ux_memory_block_next = UX_NULL;
427 dummy_memory_block_first -> ux_memory_block_previous = UX_NULL;
428 dummy_memory_block_first -> ux_memory_block_status = UX_MEMORY_UNUSED;
429 dummy_memory_block_first -> ux_memory_block_size = calculate_final_memory_request_size(1, sizeof(UX_SLAVE_CLASS_HID));
430 _ux_system -> ux_system_regular_memory_pool_start = dummy_memory_block_first;
431 _ux_system -> ux_system_cache_safe_memory_pool_start = dummy_memory_block_first;
432
433 status = _ux_device_class_hid_initialize(&command);
434 if (status != UX_MEMORY_INSUFFICIENT)
435 {
436
437 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
438 test_control_return(1);
439 }
440 #endif
441 #if 0 /* @BUG_FIX_PENDING: returns UX_EVENT_ERROR when it should return UX_MEMORY_INSUFFICIENT */
442 /**************************************************/
443 /** Test case: class -> ux_slave_class_thread_stack =
444 _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, UX_THREAD_STACK_SIZE); fails **/
445 /**************************************************/
446
447 command.ux_slave_class_command_parameter = &hid_parameter;
448 command.ux_slave_class_command_class_ptr = &class;
449
450 /* Set up the dummy memory block. */
451 dummy_memory_block_first -> ux_memory_block_next = UX_NULL;
452 dummy_memory_block_first -> ux_memory_block_previous = UX_NULL;
453 dummy_memory_block_first -> ux_memory_block_status = UX_MEMORY_UNUSED;
454 dummy_memory_block_first -> ux_memory_block_size = calculate_final_memory_request_size(2, sizeof(UX_SLAVE_CLASS_HID), UX_THREAD_STACK_SIZE);
455 _ux_system -> ux_system_regular_memory_pool_start = dummy_memory_block_first;
456 _ux_system -> ux_system_cache_safe_memory_pool_start = dummy_memory_block_first;
457
458 status = _ux_device_class_hid_initialize(&command);
459 if (status != UX_MEMORY_INSUFFICIENT)
460 {
461
462 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
463 test_control_return(1);
464 }
465
466 /* Restore state for next test. */
467 _ux_system -> ux_system_regular_memory_pool_start = original_regular_memory_block;
468 _ux_system -> ux_system_cache_safe_memory_pool_start = original_cache_safe_memory_block;
469
470 status = ux_utility_thread_delete(&class.ux_slave_class_thread);
471 if (status != UX_SUCCESS)
472 {
473
474 printf("Error on line %d, error code 0x%x\n", __LINE__, status);
475 test_control_return(1);
476 }
477 #endif
478
479 /* Now disconnect the device. */
480 _ux_device_stack_disconnect();
481
482 /* And deinitialize the class. */
483 status = ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
484
485 /* Deinitialize the device side of usbx. */
486 _ux_device_stack_uninitialize();
487
488 /* And finally the usbx system resources. */
489 _ux_system_uninitialize();
490
491 /* Successful test. */
492 printf("SUCCESS!\n");
493 test_control_return(0);
494 }
495
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)496 static UINT demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
497 {
498 return(UX_SUCCESS);
499 }
500