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 UINT  _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
143 
144 
ux_system_host_change_function(ULONG a,UX_HOST_CLASS * b,VOID * c)145 static UINT ux_system_host_change_function(ULONG a, UX_HOST_CLASS *b, VOID *c)
146 {
147     return 0;
148 }
149 
ux_hcd_sim_host_entry_filter(UX_HCD * hcd,UINT function,VOID * parameter)150 static UINT ux_hcd_sim_host_entry_filter(UX_HCD *hcd, UINT function, VOID *parameter)
151 {
152 
153 UINT            status;
154 UX_TRANSFER    *transfer_request;
155 static UINT     get_config_desc_count;
156 
157     status = _ux_hcd_sim_host_entry(hcd, function, parameter);
158 
159     switch(function)
160     {
161     case UX_HCD_GET_PORT_STATUS:
162 
163         break;
164 
165     case UX_HCD_TRANSFER_REQUEST:
166 
167         transfer_request = parameter;
168 
169         /* Is this the transfer request for GetConfigDescriptor? */
170         if (transfer_request -> ux_transfer_request_requested_length == UX_CONFIGURATION_DESCRIPTOR_LENGTH &&
171             transfer_request -> ux_transfer_request_function == UX_GET_DESCRIPTOR &&
172             transfer_request -> ux_transfer_request_type == (UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE) &&
173             transfer_request -> ux_transfer_request_value == UX_CONFIGURATION_DESCRIPTOR_ITEM << 8 &&
174             transfer_request -> ux_transfer_request_index == 0)
175         {
176 
177             /* We're looking for the second time we get the config descriptor (_descriptor_parse is the second time). */
178             get_config_desc_count++;
179             if (get_config_desc_count == 2)
180             {
181                 transfer_request->ux_transfer_request_actual_length = 0;
182             }
183         }
184 
185         break;
186 
187     default:
188 
189         if (status != UX_SUCCESS)
190         {
191 
192             printf("Error on line %d, error code: %d\n", __LINE__, status);
193             test_control_return(1);
194         }
195     }
196 
197     return status;
198 }
199 
error_callback(UINT system_level,UINT system_context,UINT error_code)200 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
201 {
202 
203     if (error_code != UX_DESCRIPTOR_CORRUPTED)
204     {
205 
206         /* Something went wrong.  */
207         printf("Error on line %d\n", __LINE__);
208         test_control_return(1);
209     }
210 }
211 
212 /* Define what the initial system looks like.  */
213 
214 #ifdef CTEST
test_application_define(void * first_unused_memory)215 void test_application_define(void *first_unused_memory)
216 #else
217 void    usbx_ux_host_class_hid_descriptor_parse_test2_application_define(void *first_unused_memory)
218 #endif
219 {
220 
221 UINT status;
222 CHAR *                          stack_pointer;
223 CHAR *                          memory_pointer;
224 UX_HCD                         *hcd;
225 ALIGN_TYPE                      tmp;
226 
227 
228     /* Inform user.  */
229     printf("Running ux_host_class_hid_descriptor_parse Test 2................... ");
230 
231     /* Initialize the free memory pointer */
232     stack_pointer = (CHAR *) usbx_memory;
233     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
234 
235     /* Initialize USBX. Memory */
236     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
237 
238     /* Check for error.  */
239     if (status != UX_SUCCESS)
240     {
241 
242         printf("Error on line %d, error code: %d\n", __LINE__, status);
243         test_control_return(1);
244     }
245 
246     /* Register the error callback. */
247     _ux_utility_error_callback_register(error_callback);
248 
249     /* The code below is required for installing the host portion of USBX */
250     status =  ux_host_stack_initialize(ux_system_host_change_function);
251     if (status != UX_SUCCESS)
252     {
253 
254         printf("Error on line %d, error code: %d\n", __LINE__, status);
255         test_control_return(1);
256     }
257 
258     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
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     /* Register the HID client(s).  */
267     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
268     if (status != UX_SUCCESS)
269     {
270 
271         printf("Error on line %d, error code: %d\n", __LINE__, status);
272         test_control_return(1);
273     }
274 
275     /* The code below is required for installing the device portion of USBX. No call back for
276        device status change in this example. */
277     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
278                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
279                                        string_framework, STRING_FRAMEWORK_LENGTH,
280                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
281     if(status!=UX_SUCCESS)
282     {
283 
284         printf("Error on line %d, error code: %d\n", __LINE__, status);
285         test_control_return(1);
286     }
287 
288     /* Initialize the hid class parameters.  */
289     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
290     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
291     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
292 
293     /* Initilize the device hid class. The class is connected with interface 2 */
294     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
295                                                 1,2, (VOID *)&hid_parameter);
296     if(status!=UX_SUCCESS)
297     {
298 
299         printf("Error on line %d, error code: %d\n", __LINE__, status);
300         test_control_return(1);
301     }
302 
303     /* Initialize the simulated device controller.  */
304     status =  _ux_dcd_sim_slave_initialize();
305 
306     /* Check for error.  */
307     if (status != UX_SUCCESS)
308     {
309 
310         printf("Error on line %d, error code: %d\n", __LINE__, status);
311         test_control_return(1);
312     }
313 
314     /* Register all the USB host controllers available in this system */
315     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
316 
317     /* Check for error.  */
318     if (status != UX_SUCCESS)
319     {
320 
321         printf("Error on line %d, error code: %d\n", __LINE__, status);
322         test_control_return(1);
323     }
324 
325     /**************************************************/
326     /** Test case: transfer_request -> ux_transfer_request_actual_length == UX_CONFIGURATION_DESCRIPTOR_LENGTH fails in **/
327     /** if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == UX_CONFIGURATION_DESCRIPTOR_LENGTH)). **/
328     /**************************************************/
329 
330     /* Swap the hcd entry function. */
331     hcd = &_ux_system_host -> ux_system_host_hcd_array[0];
332     tmp = (ALIGN_TYPE)hcd -> ux_hcd_entry_function;
333     hcd -> ux_hcd_entry_function = ux_hcd_sim_host_entry_filter;
334 
335     /**************************************************/
336     /** END TEST **/
337     /**************************************************/
338 
339     /* Create the main host simulation thread.  */
340     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
341             stack_pointer, UX_DEMO_STACK_SIZE,
342             20, 20, 1, TX_AUTO_START);
343 
344     /* Check for error.  */
345     if (status != TX_SUCCESS)
346     {
347 
348         printf("Error on line %d, error code: %d\n", __LINE__, status);
349         test_control_return(1);
350     }
351 }
352 
tx_demo_thread_host_simulation_entry(ULONG arg)353 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
354 {
355 
356 UINT status;
357 
358     /* Find the HID class */
359     status = demo_class_hid_get();
360     if (status != UX_SUCCESS)
361     {
362 
363         printf("Error on line %d, error code: %d\n", __LINE__, status);
364         test_control_return(1);
365     }
366 
367     /* Now disconnect the device.  */
368     _ux_device_stack_disconnect();
369 
370     /* And deinitialize the class.  */
371     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
372 
373     /* Deinitialize the device side of usbx.  */
374     _ux_device_stack_uninitialize();
375 
376     /* And finally the usbx system resources.  */
377     _ux_system_uninitialize();
378 
379     /* Successful test.  */
380     printf("SUCCESS!\n");
381     test_control_return(0);
382 }
383 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)384 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
385 {
386     return(UX_SUCCESS);
387 }
388