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