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