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