1 #include "usbx_test_common_hid.h"
2 #include "ux_host_class_hid_keyboard.h"
3 
4 
5 #define DUMMY_USBX_MEMORY_SIZE (64*1024)
6 static UCHAR dummy_usbx_memory[DUMMY_USBX_MEMORY_SIZE];
7 
8 static UCHAR hid_report_descriptor[] = {
9 
10     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
11     0x09, 0x06,                    // USAGE (Keyboard)
12     0xa1, 0x01,                    // COLLECTION (Application)
13     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
14     0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
15     0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
16     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
17     0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
18     0x75, 0x01,                    //   REPORT_SIZE (1)
19     0x95, 0x08,                    //   REPORT_COUNT (8)
20     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
21     0x95, 0x01,                    //   REPORT_COUNT (1)
22     0x75, 0x08,                    //   REPORT_SIZE (8)
23     0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
24     0x95, 0x05,                    //   REPORT_COUNT (5)
25     0x75, 0x01,                    //   REPORT_SIZE (1)
26     0x05, 0x08,                    //   USAGE_PAGE (LEDs)
27     0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
28     0x29, 0x05,                    //   USAGE_MAXIMUM (Kana)
29     0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
30     0x95, 0x01,                    //   REPORT_COUNT (1)
31     0x75, 0x03,                    //   REPORT_SIZE (3)
32     0x91, 0x03,                    //   OUTPUT (Cnst,Var,Abs)
33     0x95, 0x06,                    //   REPORT_COUNT (6)
34     0x75, 0x08,                    //   REPORT_SIZE (8)
35     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
36     0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
37     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
38     0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
39     0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
40     0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
41     0xc0                           // END_COLLECTION
42 };
43 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
44 
45 
46 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
47 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
48 
49     /* Device descriptor */
50         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
51         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
52         0x00, 0x01,
53 
54     /* Configuration descriptor */
55         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
56         0x32,
57 
58     /* Interface descriptor */
59         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
60         0x00,
61 
62     /* HID descriptor */
63         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
64         MSB(HID_REPORT_LENGTH),
65 
66     /* Endpoint descriptor (Interrupt) */
67         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
68 
69     };
70 
71 
72 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
73 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
74 
75     /* Device descriptor */
76         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
77         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
78         0x03, 0x01,
79 
80     /* Device qualifier descriptor */
81         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
82         0x01, 0x00,
83 
84     /* Configuration descriptor */
85         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
86         0x32,
87 
88     /* Interface descriptor */
89         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
90         0x00,
91 
92     /* HID descriptor */
93         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
94         MSB(HID_REPORT_LENGTH),
95 
96     /* Endpoint descriptor (Interrupt) */
97         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
98 
99     };
100 
101 
102     /* String Device Framework :
103      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
104      Byte 2       : Byte containing the index of the descriptor
105      Byte 3       : Byte containing the length of the descriptor string
106     */
107 
108 #define STRING_FRAMEWORK_LENGTH 40
109 static UCHAR string_framework[] = {
110 
111     /* Manufacturer string descriptor : Index 1 */
112         0x09, 0x04, 0x01, 0x0c,
113         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
114         0x6f, 0x67, 0x69, 0x63,
115 
116     /* Product string descriptor : Index 2 */
117         0x09, 0x04, 0x02, 0x0c,
118         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
119         0x6f, 0x61, 0x72, 0x64,
120 
121     /* Serial Number string descriptor : Index 3 */
122         0x09, 0x04, 0x03, 0x04,
123         0x30, 0x30, 0x30, 0x31
124     };
125 
126 
127     /* Multiple languages are supported on the device, to add
128        a language besides english, the unicode language code must
129        be appended to the language_id_framework array and the length
130        adjusted accordingly. */
131 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
132 static UCHAR language_id_framework[] = {
133 
134     /* English. */
135         0x09, 0x04
136     };
137 
138 
139 static UCHAR dummy_configuration_descriptor[] = {
140 
141     /* Configuration descriptor */
142         0x09, 0x02,
143         0xff, 0xff, /* Set the total length to something really big so memory_allocate fails. */
144         0x01, 0x01, 0x00, 0xc0,
145         0x32,
146 
147     };
148 
149 
150 UINT  _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
151 
ux_system_host_change_function(ULONG a,UX_HOST_CLASS * b,VOID * c)152 static UINT ux_system_host_change_function(ULONG a, UX_HOST_CLASS *b, VOID *c)
153 {
154     return 0;
155 }
156 
ux_hcd_sim_host_entry_filter(UX_HCD * hcd,UINT function,VOID * parameter)157 static UINT ux_hcd_sim_host_entry_filter(UX_HCD *hcd, UINT function, VOID *parameter)
158 {
159 
160 UINT            status;
161 UX_TRANSFER    *transfer_request;
162 
163     status = _ux_hcd_sim_host_entry(hcd, function, parameter);
164 
165     switch(function)
166     {
167     case UX_HCD_GET_PORT_STATUS:
168 
169         break;
170 
171     case UX_HCD_TRANSFER_REQUEST:
172 
173         transfer_request = parameter;
174 
175         if (transfer_request -> ux_transfer_request_requested_length == UX_CONFIGURATION_DESCRIPTOR_LENGTH &&
176             transfer_request -> ux_transfer_request_function == UX_GET_DESCRIPTOR &&
177             transfer_request -> ux_transfer_request_type == (UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE) &&
178             transfer_request -> ux_transfer_request_value == UX_CONFIGURATION_DESCRIPTOR_ITEM << 8 &&
179             transfer_request -> ux_transfer_request_index == 2)
180         {
181 
182             ux_utility_memory_copy(transfer_request -> ux_transfer_request_data_pointer, dummy_configuration_descriptor, ARRAY_COUNT(dummy_configuration_descriptor));
183             transfer_request -> ux_transfer_request_actual_length = transfer_request -> ux_transfer_request_requested_length;
184         }
185 
186         break;
187 
188     default:
189 
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 
198     return status;
199 }
200 
error_callback(UINT system_level,UINT system_context,UINT error_code)201 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
202 {
203 
204     if (error_code != UX_MEMORY_INSUFFICIENT)
205     {
206 
207         /* Something went wrong.  */
208         printf("Error on line %d\n", __LINE__);
209         test_control_return(1);
210     }
211 }
212 
213 /* Define what the initial system looks like.  */
214 
215 #ifdef CTEST
test_application_define(void * first_unused_memory)216 void test_application_define(void *first_unused_memory)
217 #else
218 void    usbx_ux_host_class_hid_descriptor_parse_test7_application_define(void *first_unused_memory)
219 #endif
220 {
221 
222 UINT status;
223 CHAR *                          stack_pointer;
224 CHAR *                          memory_pointer;
225 UX_HCD                         *hcd;
226 ALIGN_TYPE                      tmp;
227 
228 
229     /* Inform user.  */
230     printf("Running ux_host_class_hid_descriptor_parse Test 7................... ");
231 
232     /* Initialize the free memory pointer */
233     stack_pointer = (CHAR *) usbx_memory;
234     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
235 
236     /* Initialize USBX. Memory */
237     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
238 
239     /* Check for error.  */
240     if (status != UX_SUCCESS)
241     {
242 
243         printf("Error on line %d, error code: %d\n", __LINE__, status);
244         test_control_return(1);
245     }
246 
247     /* Register the error callback. */
248     _ux_utility_error_callback_register(error_callback);
249 
250     /* The code below is required for installing the host portion of USBX */
251     status =  ux_host_stack_initialize(ux_system_host_change_function);
252     if (status != UX_SUCCESS)
253     {
254 
255         printf("Error on line %d, error code: %d\n", __LINE__, status);
256         test_control_return(1);
257     }
258 
259     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
260     if (status != UX_SUCCESS)
261     {
262 
263         printf("Error on line %d, error code: %d\n", __LINE__, status);
264         test_control_return(1);
265     }
266 
267     /* Register the HID client(s).  */
268     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
269     if (status != UX_SUCCESS)
270     {
271 
272         printf("Error on line %d, error code: %d\n", __LINE__, status);
273         test_control_return(1);
274     }
275 
276     /* The code below is required for installing the device portion of USBX. No call back for
277        device status change in this example. */
278     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
279                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
280                                        string_framework, STRING_FRAMEWORK_LENGTH,
281                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
282     if(status!=UX_SUCCESS)
283     {
284 
285         printf("Error on line %d, error code: %d\n", __LINE__, status);
286         test_control_return(1);
287     }
288 
289     /* Initialize the hid class parameters.  */
290     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
291     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
292     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
293 
294     /* Initilize the device hid class. The class is connected with interface 2 */
295     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
296                                                 1,2, (VOID *)&hid_parameter);
297     if(status!=UX_SUCCESS)
298     {
299 
300         printf("Error on line %d, error code: %d\n", __LINE__, status);
301         test_control_return(1);
302     }
303 
304     /* Initialize the simulated device controller.  */
305     status =  _ux_dcd_sim_slave_initialize();
306 
307     /* Check for error.  */
308     if (status != UX_SUCCESS)
309     {
310 
311         printf("Error on line %d, error code: %d\n", __LINE__, status);
312         test_control_return(1);
313     }
314 
315     /* Register all the USB host controllers available in this system */
316     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
317 
318     /* Check for error.  */
319     if (status != UX_SUCCESS)
320     {
321 
322         printf("Error on line %d, error code: %d\n", __LINE__, status);
323         test_control_return(1);
324     }
325 
326     /**************************************************/
327     /** Test case: descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, total_configuration_length); fails **/
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