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