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