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