1 /* This test ensures that the parser handles the example digit 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, 0x0d,                    // USAGE_PAGE (Digitizers)
11     0x09, 0x01,                    // USAGE (Digitizer)
12     0xa1, 0x01,                    // COLLECTION (Application)
13     0x85, 0x01,                    //   REPORT_ID (1)
14     0x09, 0x21,                    //   USAGE (Puck)
15     0xa1, 0x00,                    //   COLLECTION (Physical)
16     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
17     0x09, 0x30,                    //     USAGE (X)
18     0x09, 0x31,                    //     USAGE (Y)
19     0x75, 0x10,                    //     REPORT_SIZE (16)
20     0x95, 0x02,                    //     REPORT_COUNT (2)
21     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
22     0x26, 0xe0, 0x2e,              //     LOGICAL_MAXIMUM (12000)
23     0x35, 0x00,                    //     PHYSICAL_MINIMUM (0)
24     0x45, 0x0c,                    //     PHYSICAL_MAXIMUM (12)
25     0x65, 0x13,                    //     UNIT (Eng Lin:Distance)
26     0x55, 0x00,                    //     UNIT_EXPONENT (0)
27     0xa4,                          //     PUSH
28     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
29     0x05, 0x0d,                    //     USAGE_PAGE (Digitizers)
30     0x09, 0x32,                    //     USAGE (In Range)
31     0x09, 0x44,                    //     USAGE (Barrel Switch)
32     0x09, 0x42,                    //     USAGE (Tip Switch)
33     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
34     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
35     0x35, 0x00,                    //     PHYSICAL_MINIMUM (0)
36     0x45, 0x01,                    //     PHYSICAL_MAXIMUM (1)
37     0x75, 0x01,                    //     REPORT_SIZE (1)
38     0x95, 0x03,                    //     REPORT_COUNT (3)
39     0x65, 0x00,                    //     UNIT (None)
40     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
41     0x95, 0x01,                    //     REPORT_COUNT (1)
42     0x75, 0x05,                    //     REPORT_SIZE (5)
43     0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
44     0xc0,                          //   END_COLLECTION
45     0x85, 0x02,                    //   REPORT_ID (2)
46     0x09, 0x20,                    //   USAGE (Stylus)
47     0xa1, 0x00,                    //   COLLECTION (Physical)
48     0xb4,                          //     POP
49     0xa4,                          //     PUSH
50     0x09, 0x30,                    //     USAGE (X)
51     0x09, 0x31,                    //     USAGE (Y)
52     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
53     0x05, 0x0d,                    //     USAGE_PAGE (Digitizers)
54     0x09, 0x32,                    //     USAGE (In Range)
55     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
56     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
57     0x35, 0x00,                    //     PHYSICAL_MINIMUM (0)
58     0x45, 0x01,                    //     PHYSICAL_MAXIMUM (1)
59     0x65, 0x00,                    //     UNIT (None)
60     0x75, 0x01,                    //     REPORT_SIZE (1)
61     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
62     0x05, 0x09,                    //     USAGE_PAGE (Button)
63     0x19, 0x00,                    //     USAGE_MINIMUM (No Buttons Pressed)
64     0x29, 0x10,                    //     USAGE_MAXIMUM (Button 16)
65     0x25, 0x10,                    //     LOGICAL_MAXIMUM (16)
66     0x75, 0x05,                    //     REPORT_SIZE (5)
67     0x81, 0x40,                    //     INPUT (Data,Ary,Abs,Null)
68     0x75, 0x02,                    //     REPORT_SIZE (2)
69     0x81, 0x01,                    //     INPUT (Cnst,Ary,Abs)
70     0xc0,                          //   END_COLLECTION
71     0x85, 0x03,                    //   REPORT_ID (3)
72     0x05, 0x0d,                    //   USAGE_PAGE (Digitizers)
73     0x09, 0x20,                    //   USAGE (Stylus)
74     0xa1, 0x00,                    //   COLLECTION (Physical)
75     0xb4,                          //     POP
76     0x09, 0x30,                    //     USAGE (X)
77     0x09, 0x31,                    //     USAGE (Y)
78     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
79     0x05, 0x0d,                    //     USAGE_PAGE (Digitizers)
80     0x09, 0x32,                    //     USAGE (In Range)
81     0x09, 0x44,                    //     USAGE (Barrel Switch)
82     0x75, 0x01,                    //     REPORT_SIZE (1)
83     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
84     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
85     0x35, 0x00,                    //     PHYSICAL_MINIMUM (0)
86     0x45, 0x01,                    //     PHYSICAL_MAXIMUM (1)
87     0x65, 0x00,                    //     UNIT (None)
88     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
89     0x95, 0x06,                    //     REPORT_COUNT (6)
90     0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
91     0x09, 0x30,                    //     USAGE (Tip Pressure)
92     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
93     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
94     0x35, 0x00,                    //     PHYSICAL_MINIMUM (0)
95     0x45, 0x2d,                    //     PHYSICAL_MAXIMUM (45)
96     0x67, 0x11, 0xe1, 0x00, 0x00,  //     UNIT (SI Lin:Force)
97     0x55, 0x04,                    //     UNIT_EXPONENT (4)
98     0x75, 0x08,                    //     REPORT_SIZE (8)
99     0x95, 0x01,                    //     REPORT_COUNT (1)
100     0x81, 0x12,                    //     INPUT (Data,Var,Abs,NLin)
101     0xc0,                          //   END_COLLECTION
102     0xc0                           // END_COLLECTION
103 };
104 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
105 
106 
107 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
108 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
109 
110     /* Device descriptor */
111         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
112         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
113         0x00, 0x01,
114 
115     /* Configuration descriptor */
116         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
117         0x32,
118 
119     /* Interface descriptor */
120         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
121         0x00,
122 
123     /* HID descriptor */
124         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
125         MSB(HID_REPORT_LENGTH),
126 
127     /* Endpoint descriptor (Interrupt) */
128         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
129 
130     };
131 
132 
133 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
134 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
135 
136     /* Device descriptor */
137         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
138         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
139         0x03, 0x01,
140 
141     /* Device qualifier descriptor */
142         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
143         0x01, 0x00,
144 
145     /* Configuration descriptor */
146         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
147         0x32,
148 
149     /* Interface descriptor */
150         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
151         0x00,
152 
153     /* HID descriptor */
154         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
155         MSB(HID_REPORT_LENGTH),
156 
157     /* Endpoint descriptor (Interrupt) */
158         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
159 
160     };
161 
162 
163     /* String Device Framework :
164      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
165      Byte 2       : Byte containing the index of the descriptor
166      Byte 3       : Byte containing the length of the descriptor string
167     */
168 
169 #define STRING_FRAMEWORK_LENGTH 40
170 static UCHAR string_framework[] = {
171 
172     /* Manufacturer string descriptor : Index 1 */
173         0x09, 0x04, 0x01, 0x0c,
174         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
175         0x6f, 0x67, 0x69, 0x63,
176 
177     /* Product string descriptor : Index 2 */
178         0x09, 0x04, 0x02, 0x0c,
179         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
180         0x6f, 0x61, 0x72, 0x64,
181 
182     /* Serial Number string descriptor : Index 3 */
183         0x09, 0x04, 0x03, 0x04,
184         0x30, 0x30, 0x30, 0x31
185     };
186 
187 
188     /* Multiple languages are supported on the device, to add
189        a language besides english, the unicode language code must
190        be appended to the language_id_framework array and the length
191        adjusted accordingly. */
192 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
193 static UCHAR language_id_framework[] = {
194 
195     /* English. */
196         0x09, 0x04
197     };
198 
199 
error_callback(UINT system_level,UINT system_context,UINT error_code)200 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
201 {
202 
203     /* 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. */
204     if (error_code != UX_HOST_CLASS_HID_UNKNOWN)
205     {
206 
207         /* Failed test.  */
208         printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
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_hid_report_descriptor_example_digit_test_application_define(void *first_unused_memory)
219 #endif
220 {
221 
222 UINT                            status;
223 CHAR *                          stack_pointer;
224 CHAR *                          memory_pointer;
225 UINT                            descriptor_size = HID_REPORT_LENGTH;
226 
227 
228     /* Inform user.  */
229     printf("Running HID Report Descriptor Example digit Test.................... ");
230 
231     /* Initialize the free memory pointer */
232     stack_pointer = (CHAR *) usbx_memory;
233     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
234 
235     /* Initialize USBX. Memory */
236     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
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 the error callback. */
247     _ux_utility_error_callback_register(error_callback);
248 
249     /* The code below is required for installing the host portion of USBX */
250     status =  ux_host_stack_initialize(UX_NULL);
251     if (status != UX_SUCCESS)
252     {
253 
254         printf("Error on line %d\n", __LINE__);
255         test_control_return(1);
256     }
257 
258     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
259     if (status != UX_SUCCESS)
260     {
261 
262         printf("Error on line %d\n", __LINE__);
263         test_control_return(1);
264     }
265 
266     /* Register the HID client(s).  */
267     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
268     if (status != UX_SUCCESS)
269     {
270 
271         printf("Error on line %d, error code: %d\n", __LINE__, status);
272         test_control_return(1);
273     }
274 
275     /* The code below is required for installing the device portion of USBX. No call back for
276        device status change in this example. */
277     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
278                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
279                                        string_framework, STRING_FRAMEWORK_LENGTH,
280                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
281     if(status!=UX_SUCCESS)
282     {
283 
284         printf("Error on line %d\n", __LINE__);
285         test_control_return(1);
286     }
287 
288     /* Initialize the hid class parameters.  */
289     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
290     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
291     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
292 
293     /* Initilize the device hid class. The class is connected with interface 2 */
294     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
295                                                 1,2, (VOID *)&hid_parameter);
296     if(status!=UX_SUCCESS)
297     {
298 
299         printf("Error on line %d\n", __LINE__);
300         test_control_return(1);
301     }
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\n", __LINE__);
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\n", __LINE__);
323         test_control_return(1);
324     }
325 
326     /* Create the main host simulation thread.  */
327     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
328             stack_pointer, UX_DEMO_STACK_SIZE,
329             20, 20, 1, TX_AUTO_START);
330 
331     /* Check for error.  */
332     if (status != TX_SUCCESS)
333     {
334 
335         printf("Error on line %d\n", __LINE__);
336         test_control_return(1);
337     }
338 }
339 
340 
tx_demo_thread_host_simulation_entry(ULONG arg)341 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
342 {
343 
344 UINT                                status;
345 UX_HOST_CLASS_HID_REPORT_GET_ID     report_id;
346 
347 
348     /* Find the HID class */
349     status = demo_class_hid_get();
350     if (status != UX_SUCCESS)
351     {
352 
353         printf("Error on line %d\n", __LINE__);
354         test_control_return(1);
355     }
356 
357     /* Get the input report descriptor. */
358     report_id.ux_host_class_hid_report_get_report = UX_NULL;
359     report_id.ux_host_class_hid_report_get_type = UX_HOST_CLASS_HID_REPORT_TYPE_INPUT;
360     status = ux_host_class_hid_report_id_get(hid, &report_id);
361     if (status != UX_SUCCESS)
362     {
363 
364         printf("Error on line %d\n", __LINE__);
365         test_control_return(1);
366     }
367 
368     /* Do minimal error-checking. */
369     if (report_id.ux_host_class_hid_report_get_report == UX_NULL)
370     {
371 
372         printf("Error on line %d\n", __LINE__);
373         test_control_return(1);
374     }
375 
376     /* Now disconnect the device.  */
377     _ux_device_stack_disconnect();
378 
379     /* And deinitialize the class.  */
380     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
381 
382     /* Deinitialize the device side of usbx.  */
383     _ux_device_stack_uninitialize();
384 
385     /* And finally the usbx system resources.  */
386     _ux_system_uninitialize();
387 
388     /* Successful test.  */
389     printf("SUCCESS!\n");
390     test_control_return(0);
391 }
392 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)393 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
394 {
395     return(UX_SUCCESS);
396 }
397