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