1 /* This tests _ux_host_class_hid_remote_control_callback.  */
2 
3 /**************************************************/
4 /** Test case: if (array_head != array_tail) fails **/
5 /**************************************************/
6 
7 #include "usbx_test_common_hid.h"
8 #include "ux_host_class_hid_keyboard.h"
9 
10 #include "ux_host_class_hid_remote_control.h"
11 
12 
13 static UX_HOST_CLASS_HID_REMOTE_CONTROL *remote_control;
14 
15 
16 static UCHAR hid_remote_control_report[] = {
17 
18     0x05, 0x0c,                    // USAGE_PAGE (Consumer Devices)
19     0x09, 0x01,                    // USAGE (Consumer Control)
20     0xa1, 0x01,                    // COLLECTION (Application)
21     0x09, 0x02,                    //   USAGE (Numeric Key Pad)
22     0xa1, 0x02,                    //   COLLECTION (Logical)
23     0x05, 0x09,                    //     USAGE_PAGE (Button)
24     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
25     0x29, 0x0a,                    //     USAGE_MAXIMUM (Button 10)
26     0x15, 0x01,                    //     LOGICAL_MINIMUM (1)
27     0x25, 0x0a,                    //     LOGICAL_MAXIMUM (10)
28     0x75, 0x04,                    //     REPORT_SIZE (4)
29     0x95, 0x01,                    //     REPORT_COUNT (1)
30     0x81, 0x00,                    //     INPUT (Data,Ary,Abs)
31     0xc0,                          //   END_COLLECTION
32     0x05, 0x0c,                    //   USAGE_PAGE (Consumer Devices)
33     0x09, 0x86,                    //   USAGE (Channel)
34     0x09, 0xe0,                    //   USAGE (Volume)
35     0x15, 0xff,                    //   LOGICAL_MINIMUM (-1)
36     0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
37     0x75, 0x02,                    //   REPORT_SIZE (2)
38     0x95, 0x02,                    //   REPORT_COUNT (2)
39     0x81, 0x46,                    //   INPUT (Data,Var,Rel,Null)
40     0xc0                           // END_COLLECTION
41 };
42 #define HID_REMOTE_CONTROL_REPORT_LENGTH (sizeof(hid_remote_control_report)/sizeof(hid_remote_control_report[0]))
43 
44 
45 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
46 static UCHAR device_framework_full_speed[] = {
47 
48     /* Device descriptor */
49         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
50         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
51         0x00, 0x01,
52 
53     /* Configuration descriptor */
54         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
55         0x32,
56 
57     /* Interface descriptor */
58         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
59         0x00,
60 
61     /* HID descriptor */
62         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REMOTE_CONTROL_REPORT_LENGTH),
63         MSB(HID_REMOTE_CONTROL_REPORT_LENGTH),
64 
65     /* Endpoint descriptor (Interrupt) */
66         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
67 
68     };
69 
70 
71 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
72 static UCHAR device_framework_high_speed[] = {
73 
74     /* Device descriptor */
75         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
76         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
77         0x03, 0x01,
78 
79     /* Device qualifier descriptor */
80         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
81         0x01, 0x00,
82 
83     /* Configuration descriptor */
84         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
85         0x32,
86 
87     /* Interface descriptor */
88         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
89         0x00,
90 
91     /* HID descriptor */
92         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REMOTE_CONTROL_REPORT_LENGTH),
93         MSB(HID_REMOTE_CONTROL_REPORT_LENGTH),
94 
95     /* Endpoint descriptor (Interrupt) */
96         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
97 
98     };
99 
100 
101     /* String Device Framework :
102      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
103      Byte 2       : Byte containing the index of the descriptor
104      Byte 3       : Byte containing the length of the descriptor string
105     */
106 
107 #define STRING_FRAMEWORK_LENGTH 40
108 static UCHAR string_framework[] = {
109 
110     /* Manufacturer string descriptor : Index 1 */
111         0x09, 0x04, 0x01, 0x0c,
112         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
113         0x6f, 0x67, 0x69, 0x63,
114 
115     /* Product string descriptor : Index 2 */
116         0x09, 0x04, 0x02, 0x0c,
117         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
118         0x6f, 0x61, 0x72, 0x64,
119 
120     /* Serial Number string descriptor : Index 3 */
121         0x09, 0x04, 0x03, 0x04,
122         0x30, 0x30, 0x30, 0x31
123     };
124 
125 
126     /* Multiple languages are supported on the device, to add
127        a language besides english, the unicode language code must
128        be appended to the language_id_framework array and the length
129        adjusted accordingly. */
130 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
131 static UCHAR language_id_framework[] = {
132 
133     /* English. */
134         0x09, 0x04
135     };
136 
137 
ux_system_host_change_function(ULONG event,UX_HOST_CLASS * class,VOID * instance)138 static UINT ux_system_host_change_function(ULONG event, UX_HOST_CLASS *class, VOID *instance)
139 {
140     return 0;
141 }
142 
143 
error_callback(UINT system_level,UINT system_context,UINT error_code)144 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
145 {
146 
147     /* @BUG_FIX_PENDING: ux_dcd_sim_slave_function.c doesn't support transfer aborts, which happen during device unregistration of a class. */
148     if (error_code != UX_FUNCTION_NOT_SUPPORTED)
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 
157 /* Define what the initial system looks like.  */
158 
159 #ifdef CTEST
test_application_define(void * first_unused_memory)160 void test_application_define(void *first_unused_memory)
161 #else
162 void    usbx_ux_host_class_hid_remote_control_callback_test_application_define(void *first_unused_memory)
163 #endif
164 {
165 
166 UINT                            status;
167 CHAR *                          stack_pointer;
168 CHAR *                          memory_pointer;
169 
170 
171     /* Inform user.  */
172     printf("Running ux_host_class_hid_remote_control_callback Test.............. ");
173 
174     /* Initialize the free memory pointer */
175     stack_pointer = (CHAR *) usbx_memory;
176     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
177 
178     /* Initialize USBX. Memory */
179     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
180 
181     /* Check for error.  */
182     if (status != UX_SUCCESS)
183     {
184 
185         printf("Error on line %d\n", __LINE__);
186         test_control_return(1);
187     }
188 
189     /* Register the error callback. */
190     _ux_utility_error_callback_register(error_callback);
191 
192     /* The code below is required for installing the host portion of USBX */
193     status =  ux_host_stack_initialize(ux_system_host_change_function);
194     if (status != UX_SUCCESS)
195     {
196 
197         printf("Error on line %d\n", __LINE__);
198         test_control_return(1);
199     }
200 
201     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
202     if (status != UX_SUCCESS)
203     {
204 
205         printf("Error on line %d\n", __LINE__);
206         test_control_return(1);
207     }
208 
209     /* Register the HID client(s).  */
210     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_remote_control_name, ux_host_class_hid_remote_control_entry);
211     if (status != UX_SUCCESS)
212     {
213 
214         printf("Error on line %d\n", __LINE__);
215         test_control_return(1);
216     }
217 
218     /* The code below is required for installing the device portion of USBX. No call back for
219        device status change in this example. */
220     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
221                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
222                                        string_framework, STRING_FRAMEWORK_LENGTH,
223                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
224     if(status!=UX_SUCCESS)
225     {
226 
227         printf("Error on line %d\n", __LINE__);
228         test_control_return(1);
229     }
230 
231     /* Initialize the hid class parameters for a mouse.  */
232     hid_parameter.ux_device_class_hid_parameter_report_address = hid_remote_control_report;
233     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REMOTE_CONTROL_REPORT_LENGTH;
234     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
235 
236     /* Initilize the device hid class. The class is connected with interface 2 */
237     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
238                                                 1,2, (VOID *)&hid_parameter);
239     if(status!=UX_SUCCESS)
240     {
241 
242         printf("Error on line %d\n", __LINE__);
243         test_control_return(1);
244     }
245 
246 
247     /* Initialize the simulated device controller.  */
248     status =  _ux_dcd_sim_slave_initialize();
249 
250     /* Check for error.  */
251     if (status != UX_SUCCESS)
252     {
253 
254         printf("Error on line %d\n", __LINE__);
255         test_control_return(1);
256     }
257 
258     /* Register all the USB host controllers available in this system */
259     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
260 
261     /* Check for error.  */
262     if (status != UX_SUCCESS)
263     {
264 
265         printf("Error on line %d\n", __LINE__);
266         test_control_return(1);
267     }
268 
269     /* Create the main host simulation thread.  */
270     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
271             stack_pointer, UX_DEMO_STACK_SIZE,
272             20, 20, 1, TX_AUTO_START);
273 
274     /* Check for error.  */
275     if (status != TX_SUCCESS)
276     {
277 
278         printf("Error on line %d\n", __LINE__);
279         test_control_return(1);
280     }
281 
282     /* Create the main demo thread.  */
283     status =  tx_thread_create(&tx_demo_thread_slave_simulation, "tx demo slave simulation", tx_demo_thread_slave_simulation_entry, 0,
284             stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
285             20, 20, 1, TX_AUTO_START);
286 
287     /* Check for error.  */
288     if (status != TX_SUCCESS)
289     {
290 
291         printf("Error on line %d\n", __LINE__);
292         test_control_return(1);
293     }
294 
295 }
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 UINT    max_num_loops;
303 
304     /* Initilize max loop value.  */
305     max_num_loops = 16;
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 remote control is live */
320     while (hid_client -> ux_host_class_hid_client_local_instance == UX_NULL)
321         tx_thread_sleep(10);
322 
323     /* Get the remote control instance */
324     remote_control =  (UX_HOST_CLASS_HID_REMOTE_CONTROL *)hid_client -> ux_host_class_hid_client_local_instance;
325 
326     /* Wait for device to send events, and 'remote_control_instance -> ux_host_class_hid_remote_control_usage_array' to overflow */
327     tx_thread_sleep(200);
328 
329     /* Now disconnect the device.  */
330     _ux_device_stack_disconnect();
331 
332     /* And deinitialize the class.  */
333     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
334 
335     /* Deinitialize the device side of usbx.  */
336     _ux_device_stack_uninitialize();
337 
338     /* And finally the usbx system resources.  */
339     _ux_system_uninitialize();
340 
341     /* Successful test.  */
342     printf("SUCCESS!\n");
343     test_control_return(0);
344 }
345 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)346 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
347 {
348     return(UX_SUCCESS);
349 }
350 
tx_demo_thread_slave_simulation_entry(ULONG arg)351 static void  tx_demo_thread_slave_simulation_entry(ULONG arg)
352 {
353 
354 UINT                            status;
355 UX_SLAVE_DEVICE                *device;
356 UX_SLAVE_INTERFACE             *interface;
357 UX_SLAVE_CLASS_HID             *hid;
358 UX_SLAVE_CLASS_HID_EVENT        hid_event;
359 UINT                            i;
360 
361     /* Get the pointer to the device.  */
362     device =  &_ux_system_slave -> ux_system_slave_device;
363 
364     /* reset the HID event structure.  */
365     ux_utility_memory_set(&hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT));
366 
367     /* Set length of event.  */
368     hid_event.ux_device_class_hid_event_length = 1;
369 
370     /* Set initial keypad value. */
371     hid_event.ux_device_class_hid_event_buffer[0] = 0x01;
372 
373     /* Set initial channel value. */
374     hid_event.ux_device_class_hid_event_buffer[0] |= (0x03 << 4);
375 
376     /* Set initial volume value. */
377     hid_event.ux_device_class_hid_event_buffer[0] |= (0x01 << 6);
378 
379     while (1)
380     {
381 
382         /* Is the device configured ? */
383         while (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
384 
385             /* Then wait.  */
386             tx_thread_sleep(10);
387 
388         /* Until the device stays configured.  */
389         while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
390         {
391 
392             for (i = 0; i < UX_HOST_CLASS_HID_REMOTE_CONTROL_USAGE_ARRAY_LENGTH + 1; i++)
393             {
394 
395                 /* Get the interface.  We use the first interface, this is a simple device.  */
396                 interface = device->ux_slave_device_first_interface;
397 
398                 /* From that interface, derive the HID owner.  */
399                 hid = interface -> ux_slave_interface_class_instance;
400 
401                 while (1)
402                 {
403 
404                     status =  ux_device_class_hid_event_set(hid, &hid_event);
405                     if (status == UX_SUCCESS)
406                         break;
407 
408                     /* Wait for events to be sent to host. */
409                     tx_thread_sleep(10);
410                 }
411             }
412         }
413     }
414 }
415 
416