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