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