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