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