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