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