1 /* This test ensures that the parser correctly handles multiple fields (i.e. multiple Input items). */
2 
3 #include "usbx_test_common_hid.h"
4 #include "ux_host_class_hid_keyboard.h"
5 
6 
7 static UCHAR                               raw_output_report_buffer[1024];
8 static SLONG                               decompressed_output_report_buffer[1024];
9 
10 static UCHAR hid_report_descriptor[] = {
11 
12     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
13     0x09, 0x06,                    // USAGE (Keyboard)
14     0xa1, 0x01,                    // COLLECTION (Application)
15     0x19, 0x01,                    //   USAGE_MINIMUM (1)
16     0x29, 0x04,                    //   USAGE_MAXIMUM (4)
17     0x75, 0x04,                    //   REPORT_SIZE (4)
18     0x95, 0x04,                    //   REPORT_COUNT (4)
19     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
20     0x19, 0x01,                    //   USAGE_MINIMUM (1)
21     0x29, 0x03,                    //   USAGE_MAXIMUM (3)
22     0x75, 0x08,                    //   REPORT_SIZE (8)
23     0x95, 0x03,                    //   REPORT_COUNT (3)
24     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
25     0x09, 0x01,                    //   USAGE (1)
26     0x75, 0x10,                    //   REPORT_SIZE (16)
27     0x95, 0x01,                    //   REPORT_COUNT (1)
28     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
29 
30     /* USBX expects keyboards to have at least one output report, otherwise it's an error. */
31     0x95, 0x05,                    //   REPORT_COUNT (5)
32     0x75, 0x01,                    //   REPORT_SIZE (1)
33     0x05, 0x08,                    //   USAGE_PAGE (LEDs)
34     0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
35     0x29, 0x05,                    //   USAGE_MAXIMUM (Kana)
36     0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
37 
38     0xc0,                          // END_COLLECTION
39 };
40 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
41 
42 
43 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
44 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
45 
46     /* Device descriptor */
47         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
48         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
49         0x00, 0x01,
50 
51     /* Configuration descriptor */
52         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
53         0x32,
54 
55     /* Interface descriptor */
56         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
57         0x00,
58 
59     /* HID descriptor */
60         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
61         MSB(HID_REPORT_LENGTH),
62 
63     /* Endpoint descriptor (Interrupt) */
64         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
65 
66     };
67 
68 
69 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
70 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
71 
72     /* Device descriptor */
73         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
74         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
75         0x03, 0x01,
76 
77     /* Device qualifier descriptor */
78         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
79         0x01, 0x00,
80 
81     /* Configuration descriptor */
82         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
83         0x32,
84 
85     /* Interface descriptor */
86         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
87         0x00,
88 
89     /* HID descriptor */
90         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
91         MSB(HID_REPORT_LENGTH),
92 
93     /* Endpoint descriptor (Interrupt) */
94         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
95 
96     };
97 
98 
99     /* String Device Framework :
100      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
101      Byte 2       : Byte containing the index of the descriptor
102      Byte 3       : Byte containing the length of the descriptor string
103     */
104 
105 #define STRING_FRAMEWORK_LENGTH 40
106 static UCHAR string_framework[] = {
107 
108     /* Manufacturer string descriptor : Index 1 */
109         0x09, 0x04, 0x01, 0x0c,
110         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
111         0x6f, 0x67, 0x69, 0x63,
112 
113     /* Product string descriptor : Index 2 */
114         0x09, 0x04, 0x02, 0x0c,
115         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
116         0x6f, 0x61, 0x72, 0x64,
117 
118     /* Serial Number string descriptor : Index 3 */
119         0x09, 0x04, 0x03, 0x04,
120         0x30, 0x30, 0x30, 0x31
121     };
122 
123 
124     /* Multiple languages are supported on the device, to add
125        a language besides english, the unicode language code must
126        be appended to the language_id_framework array and the length
127        adjusted accordingly. */
128 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
129 static UCHAR language_id_framework[] = {
130 
131     /* English. */
132         0x09, 0x04
133     };
134 
135 
error_callback(UINT system_level,UINT system_context,UINT error_code)136 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
137 {
138 
139     /* Failed test.  */
140     printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
141     test_control_return(1);
142 }
143 
144 /* Define what the initial system looks like.  */
145 
146 #ifdef CTEST
test_application_define(void * first_unused_memory)147 void test_application_define(void *first_unused_memory)
148 #else
149 void    usbx_hid_report_descriptor_multiple_fields_test_application_define(void *first_unused_memory)
150 #endif
151 {
152 
153 UINT status;
154 CHAR *                          stack_pointer;
155 CHAR *                          memory_pointer;
156 UINT                            descriptor_size = HID_REPORT_LENGTH;
157 
158 
159     /* Inform user.  */
160     printf("Running HID Report Descriptor Multiple Fields Test.................. ");
161 
162     /* Initialize the free memory pointer */
163     stack_pointer = (CHAR *) usbx_memory;
164     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
165 
166     /* Initialize USBX. Memory */
167     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
168 
169     /* Check for error.  */
170     if (status != UX_SUCCESS)
171     {
172 
173         printf("Error on line %d\n", __LINE__);
174         test_control_return(1);
175     }
176 
177     /* Register the error callback. */
178     _ux_utility_error_callback_register(error_callback);
179 
180     /* The code below is required for installing the host portion of USBX */
181     status =  ux_host_stack_initialize(UX_NULL);
182     if (status != UX_SUCCESS)
183     {
184 
185         printf("Error on line %d\n", __LINE__);
186         test_control_return(1);
187     }
188 
189     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
190     if (status != UX_SUCCESS)
191     {
192 
193         printf("Error on line %d\n", __LINE__);
194         test_control_return(1);
195     }
196 
197     /* Register the HID client(s).  */
198     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
199     if (status != UX_SUCCESS)
200     {
201 
202         printf("Error on line %d, error code: %d\n", __LINE__, status);
203         test_control_return(1);
204     }
205 
206     /* The code below is required for installing the device portion of USBX. No call back for
207        device status change in this example. */
208     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
209                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
210                                        string_framework, STRING_FRAMEWORK_LENGTH,
211                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
212     if(status!=UX_SUCCESS)
213     {
214 
215         printf("Error on line %d\n", __LINE__);
216         test_control_return(1);
217     }
218 
219     /* Initialize the hid class parameters.  */
220     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
221     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
222     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
223 
224     /* Initilize the device hid class. The class is connected with interface 2 */
225     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
226                                                 1,2, (VOID *)&hid_parameter);
227     if(status!=UX_SUCCESS)
228     {
229 
230         printf("Error on line %d\n", __LINE__);
231         test_control_return(1);
232     }
233 
234 
235     /* Initialize the simulated device controller.  */
236     status =  _ux_dcd_sim_slave_initialize();
237 
238     /* Check for error.  */
239     if (status != UX_SUCCESS)
240     {
241 
242         printf("Error on line %d\n", __LINE__);
243         test_control_return(1);
244     }
245 
246     /* Register all the USB host controllers available in this system */
247     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
248 
249     /* Check for error.  */
250     if (status != UX_SUCCESS)
251     {
252 
253         printf("Error on line %d\n", __LINE__);
254         test_control_return(1);
255     }
256 
257     /* Create the main host simulation thread.  */
258     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
259             stack_pointer, UX_DEMO_STACK_SIZE,
260             20, 20, 1, TX_AUTO_START);
261 
262     /* Check for error.  */
263     if (status != TX_SUCCESS)
264     {
265 
266         printf("Error on line %d\n", __LINE__);
267         test_control_return(1);
268     }
269 }
270 
271 
tx_demo_thread_host_simulation_entry(ULONG arg)272 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
273 {
274 
275 UINT status;
276 UX_HOST_CLASS_HID_REPORT_GET_ID     report_id;
277 UX_HOST_CLASS_HID_CLIENT_REPORT     input_report_request;
278 UX_HOST_CLASS_HID_REPORT            *input_report_descriptor;
279 
280 
281     /* Find the HID class */
282     status = demo_class_hid_get();
283     if (status != UX_SUCCESS)
284     {
285 
286         printf("Error on line %d\n", __LINE__);
287         test_control_return(1);
288     }
289 
290     /* Get the input report descriptor. */
291     report_id.ux_host_class_hid_report_get_report = UX_NULL;
292     report_id.ux_host_class_hid_report_get_type = UX_HOST_CLASS_HID_REPORT_TYPE_INPUT;
293     status = ux_host_class_hid_report_id_get(hid, &report_id);
294     if (status != UX_SUCCESS)
295     {
296 
297         printf("Error on line %d\n", __LINE__);
298         test_control_return(1);
299     }
300 
301     /* Get the input report descriptor. */
302     input_report_descriptor = report_id.ux_host_class_hid_report_get_report;
303 
304     /* Fill out the request. */
305     input_report_request.ux_host_class_hid_client_report = input_report_descriptor;
306     input_report_request.ux_host_class_hid_client_report_buffer = decompressed_output_report_buffer;
307     input_report_request.ux_host_class_hid_client_report_length = input_report_descriptor -> ux_host_class_hid_report_byte_length;
308     input_report_request.ux_host_class_hid_client_report_actual_length = 0;
309     input_report_request.ux_host_class_hid_client_report_flags = UX_HOST_CLASS_HID_REPORT_DECOMPRESSED;
310 
311     /* Fill out report to be decompressed. */
312 
313     /* First field. */
314     raw_output_report_buffer[0] = 0x01;
315     raw_output_report_buffer[1] = 0x23;
316 
317     /* Second field. */
318     raw_output_report_buffer[2] = 0x45;
319     raw_output_report_buffer[3] = 0x67;
320     raw_output_report_buffer[4] = 0x89;
321 
322     /* Third field. */
323     raw_output_report_buffer[5] = 0xab;
324     raw_output_report_buffer[6] = 0xcd;
325 
326     /* Request decompression. */
327     status = _ux_host_class_hid_report_decompress(hid, &input_report_request, raw_output_report_buffer, 1);
328 
329     if (status != UX_SUCCESS)
330     {
331 
332         printf("Error on line %d\n", __LINE__);
333         test_control_return(1);
334     }
335 
336     /* Check output of decompression. */
337     if (
338         /* First field. */
339         decompressed_output_report_buffer[0] != 0x00010001 ||
340         decompressed_output_report_buffer[1] != 0x01 ||
341         decompressed_output_report_buffer[2] != 0x00010002 ||
342         decompressed_output_report_buffer[3] != 0x00 ||
343         decompressed_output_report_buffer[4] != 0x00010003 ||
344         decompressed_output_report_buffer[5] != 0x03 ||
345         decompressed_output_report_buffer[6] != 0x00010004 ||
346         decompressed_output_report_buffer[7] != 0x02 ||
347 
348         /* Second field. */
349         decompressed_output_report_buffer[8] != 0x00010001 ||
350         decompressed_output_report_buffer[9] != 0x45 ||
351         decompressed_output_report_buffer[10] != 0x00010002 ||
352         decompressed_output_report_buffer[11] != 0x67 ||
353         decompressed_output_report_buffer[12] != 0x00010003 ||
354         decompressed_output_report_buffer[13] != 0x89 ||
355 
356         /* Third field. */
357         decompressed_output_report_buffer[14] != 0x00010001 ||
358         decompressed_output_report_buffer[15] != 0xcdab
359         )
360     {
361 
362         printf("Error on line %d\n", __LINE__);
363         test_control_return(1);
364     }
365 
366     /* Now disconnect the device.  */
367     _ux_device_stack_disconnect();
368 
369     /* And deinitialize the class.  */
370     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
371 
372     /* Deinitialize the device side of usbx.  */
373     _ux_device_stack_uninitialize();
374 
375     /* And finally the usbx system resources.  */
376     _ux_system_uninitialize();
377 
378     /* Successful test.  */
379     printf("SUCCESS!\n");
380     test_control_return(0);
381 }
382 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)383 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
384 {
385     return(UX_SUCCESS);
386 }
387