1 /* This file tests _ux_host_class_hid_descriptor_parse(). */
2 
3 #include "usbx_test_common_hid.h"
4 
5 #include "ux_host_class_hid_keyboard.h"
6 
7 
8 #define DUMMY_USBX_MEMORY_SIZE (64*1024)
9 static UCHAR dummy_usbx_memory[DUMMY_USBX_MEMORY_SIZE];
10 
11 static UCHAR hid_report_descriptor[] = {
12 
13     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
14     0x09, 0x06,                    // USAGE (Keyboard)
15     0xa1, 0x01,                    // COLLECTION (Application)
16     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
17     0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
18     0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
19     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
20     0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
21     0x75, 0x01,                    //   REPORT_SIZE (1)
22     0x95, 0x08,                    //   REPORT_COUNT (8)
23     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
24     0x95, 0x01,                    //   REPORT_COUNT (1)
25     0x75, 0x08,                    //   REPORT_SIZE (8)
26     0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
27     0x95, 0x05,                    //   REPORT_COUNT (5)
28     0x75, 0x01,                    //   REPORT_SIZE (1)
29     0x05, 0x08,                    //   USAGE_PAGE (LEDs)
30     0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
31     0x29, 0x05,                    //   USAGE_MAXIMUM (Kana)
32     0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
33     0x95, 0x01,                    //   REPORT_COUNT (1)
34     0x75, 0x03,                    //   REPORT_SIZE (3)
35     0x91, 0x03,                    //   OUTPUT (Cnst,Var,Abs)
36     0x95, 0x06,                    //   REPORT_COUNT (6)
37     0x75, 0x08,                    //   REPORT_SIZE (8)
38     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
39     0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
40     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
41     0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
42     0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
43     0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
44     0xc0                           // END_COLLECTION
45 };
46 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
47 
48 
49 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
50 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
51 
52     /* Device descriptor */
53         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
54         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
55         0x00, 0x01,
56 
57     /* Configuration descriptor */
58         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
59         0x32,
60 
61     /* Interface descriptor */
62         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
63         0x00,
64 
65     /* HID descriptor */
66         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
67         MSB(HID_REPORT_LENGTH),
68 
69     /* Endpoint descriptor (Interrupt) */
70         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
71 
72     };
73 
74 
75 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
76 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
77 
78     /* Device descriptor */
79         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
80         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
81         0x03, 0x01,
82 
83     /* Device qualifier descriptor */
84         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
85         0x01, 0x00,
86 
87     /* Configuration descriptor */
88         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
89         0x32,
90 
91     /* Interface descriptor */
92         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
93         0x00,
94 
95     /* HID descriptor */
96         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
97         MSB(HID_REPORT_LENGTH),
98 
99     /* Endpoint descriptor (Interrupt) */
100         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
101 
102     };
103 
104 
105     /* String Device Framework :
106      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
107      Byte 2       : Byte containing the index of the descriptor
108      Byte 3       : Byte containing the length of the descriptor string
109     */
110 
111 #define STRING_FRAMEWORK_LENGTH 40
112 static UCHAR string_framework[] = {
113 
114     /* Manufacturer string descriptor : Index 1 */
115         0x09, 0x04, 0x01, 0x0c,
116         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
117         0x6f, 0x67, 0x69, 0x63,
118 
119     /* Product string descriptor : Index 2 */
120         0x09, 0x04, 0x02, 0x0c,
121         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
122         0x6f, 0x61, 0x72, 0x64,
123 
124     /* Serial Number string descriptor : Index 3 */
125         0x09, 0x04, 0x03, 0x04,
126         0x30, 0x30, 0x30, 0x31
127     };
128 
129 
130     /* Multiple languages are supported on the device, to add
131        a language besides english, the unicode language code must
132        be appended to the language_id_framework array and the length
133        adjusted accordingly. */
134 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
135 static UCHAR language_id_framework[] = {
136 
137     /* English. */
138         0x09, 0x04
139     };
140 
141 
142 static UCHAR dummy_configuration_descriptor[] = {
143 
144     /* Configuration descriptor */
145         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
146         0x32,
147 
148     /* Interface descriptor */
149         0x09, 0x04,
150         0x03,  /* Change the interface number to something different than what the HID instance recorded (0x02). */
151         0x00, 0x01, 0x03, 0x00, 0x00,
152         0x00,
153 
154     /* HID descriptor */
155         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
156         MSB(HID_REPORT_LENGTH),
157 
158     /* Endpoint descriptor (Interrupt) */
159         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
160 
161     };
162 
163 
164 UINT  _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
165 
ux_system_host_change_function(ULONG a,UX_HOST_CLASS * b,VOID * c)166 static UINT ux_system_host_change_function(ULONG a, UX_HOST_CLASS *b, VOID *c)
167 {
168     return 0;
169 }
170 
ux_hcd_sim_host_entry_filter(UX_HCD * hcd,UINT function,VOID * parameter)171 static UINT ux_hcd_sim_host_entry_filter(UX_HCD *hcd, UINT function, VOID *parameter)
172 {
173 
174 UINT            status;
175 UX_TRANSFER    *transfer_request;
176 
177     status = _ux_hcd_sim_host_entry(hcd, function, parameter);
178 
179     switch(function)
180     {
181     case UX_HCD_GET_PORT_STATUS:
182 
183         break;
184 
185     case UX_HCD_TRANSFER_REQUEST:
186 
187         transfer_request = parameter;
188 
189         /* Is this the second transfer request from descriptor_parse()? */
190         if (transfer_request -> ux_transfer_request_requested_length == 34 &&
191             transfer_request -> ux_transfer_request_function == UX_GET_DESCRIPTOR &&
192             transfer_request -> ux_transfer_request_type == (UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE) &&
193             transfer_request -> ux_transfer_request_value == UX_CONFIGURATION_DESCRIPTOR_ITEM << 8 &&
194             transfer_request -> ux_transfer_request_index == 2)
195         {
196 
197             UINT dummy_framework_high_speed_length = ARRAY_COUNT(dummy_configuration_descriptor);
198             if(dummy_framework_high_speed_length != transfer_request -> ux_transfer_request_requested_length)
199             {
200 
201                 printf("Error on line %d, error code: %d\n", __LINE__, status);
202                 test_control_return(1);
203             }
204 
205             ux_utility_memory_copy(transfer_request -> ux_transfer_request_data_pointer, dummy_configuration_descriptor, ARRAY_COUNT(dummy_configuration_descriptor));
206             transfer_request -> ux_transfer_request_actual_length = transfer_request -> ux_transfer_request_requested_length;
207         }
208 
209         break;
210 
211     default:
212 
213         if (status != UX_SUCCESS)
214         {
215 
216             printf("Error on line %d, error code: %d\n", __LINE__, status);
217             test_control_return(1);
218         }
219     }
220 
221     return status;
222 }
223 
error_callback(UINT system_level,UINT system_context,UINT error_code)224 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
225 {
226 
227     if (error_code != UX_DESCRIPTOR_CORRUPTED)
228     {
229 
230         /* Something went wrong.  */
231         printf("Error on line %d\n", __LINE__);
232         test_control_return(1);
233     }
234 }
235 
236 /* Define what the initial system looks like.  */
237 
238 #ifdef CTEST
test_application_define(void * first_unused_memory)239 void test_application_define(void *first_unused_memory)
240 #else
241 void    usbx_ux_host_class_hid_descriptor_parse_test6_application_define(void *first_unused_memory)
242 #endif
243 {
244 
245 UINT status;
246 CHAR *                          stack_pointer;
247 CHAR *                          memory_pointer;
248 UX_HCD                         *hcd;
249 ALIGN_TYPE                      tmp;
250 
251 
252     /* Inform user.  */
253     printf("Running ux_host_class_hid_descriptor_parse Test 6................... ");
254 
255     /* Initialize the free memory pointer */
256     stack_pointer = (CHAR *) usbx_memory;
257     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
258 
259     /* Initialize USBX. Memory */
260     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
261 
262     /* Check for error.  */
263     if (status != UX_SUCCESS)
264     {
265 
266         printf("Error on line %d, error code: %d\n", __LINE__, status);
267         test_control_return(1);
268     }
269 
270     /* Register the error callback. */
271     _ux_utility_error_callback_register(error_callback);
272 
273     /* The code below is required for installing the host portion of USBX */
274     status =  ux_host_stack_initialize(ux_system_host_change_function);
275     if (status != UX_SUCCESS)
276     {
277 
278         printf("Error on line %d, error code: %d\n", __LINE__, status);
279         test_control_return(1);
280     }
281 
282     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
283     if (status != UX_SUCCESS)
284     {
285 
286         printf("Error on line %d, error code: %d\n", __LINE__, status);
287         test_control_return(1);
288     }
289 
290     /* Register the HID client(s).  */
291     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
292     if (status != UX_SUCCESS)
293     {
294 
295         printf("Error on line %d, error code: %d\n", __LINE__, status);
296         test_control_return(1);
297     }
298 
299     /* The code below is required for installing the device portion of USBX. No call back for
300        device status change in this example. */
301     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
302                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
303                                        string_framework, STRING_FRAMEWORK_LENGTH,
304                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
305     if(status!=UX_SUCCESS)
306     {
307 
308         printf("Error on line %d, error code: %d\n", __LINE__, status);
309         test_control_return(1);
310     }
311 
312     /* Initialize the hid class parameters.  */
313     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
314     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
315     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
316 
317     /* Initilize the device hid class. The class is connected with interface 2 */
318     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
319                                                 1,2, (VOID *)&hid_parameter);
320     if(status!=UX_SUCCESS)
321     {
322 
323         printf("Error on line %d, error code: %d\n", __LINE__, status);
324         test_control_return(1);
325     }
326 
327     /* Initialize the simulated device controller.  */
328     status =  _ux_dcd_sim_slave_initialize();
329 
330     /* Check for error.  */
331     if (status != UX_SUCCESS)
332     {
333 
334         printf("Error on line %d, error code: %d\n", __LINE__, status);
335         test_control_return(1);
336     }
337 
338     /* Register all the USB host controllers available in this system */
339     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
340 
341     /* Check for error.  */
342     if (status != UX_SUCCESS)
343     {
344 
345         printf("Error on line %d, error code: %d\n", __LINE__, status);
346         test_control_return(1);
347     }
348 
349     /**************************************************/
350     /** Test case: if (current_interface == hid -> ux_host_class_hid_interface -> ux_interface_descriptor.bInterfaceNumber) fails **/
351     /**************************************************/
352 
353     /* Swap the hcd entry function */
354     hcd = &_ux_system_host -> ux_system_host_hcd_array[0];
355     tmp = (ALIGN_TYPE)hcd -> ux_hcd_entry_function;
356     hcd -> ux_hcd_entry_function = ux_hcd_sim_host_entry_filter;
357 
358     /**************************************************/
359     /** END TEST **/
360     /**************************************************/
361 
362     /* Create the main host simulation thread.  */
363     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
364             stack_pointer, UX_DEMO_STACK_SIZE,
365             20, 20, 1, TX_AUTO_START);
366 
367     /* Check for error.  */
368     if (status != TX_SUCCESS)
369     {
370 
371         printf("Error on line %d, error code: %d\n", __LINE__, status);
372         test_control_return(1);
373     }
374 }
375 
tx_demo_thread_host_simulation_entry(ULONG arg)376 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
377 {
378 
379 UINT status;
380 
381     /* Find the HID class */
382     status = demo_class_hid_get();
383     if (status == UX_SUCCESS)
384     {
385 
386         printf("Error on line %d, error code: %d\n", __LINE__, status);
387         test_control_return(1);
388     }
389 
390     /* Now disconnect the device.  */
391     _ux_device_stack_disconnect();
392 
393     /* And deinitialize the class.  */
394     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
395 
396     /* Deinitialize the device side of usbx.  */
397     _ux_device_stack_uninitialize();
398 
399     /* And finally the usbx system resources.  */
400     _ux_system_uninitialize();
401 
402     /* Successful test.  */
403     printf("SUCCESS!\n");
404     test_control_return(0);
405 }
406 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)407 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
408 {
409     return(UX_SUCCESS);
410 }
411