1 /* This file tests _ux_device_class_hid_interrupt_thread(), which only runs when there
2 is a new event available in the queue. */
3 
4 #include "usbx_test_common_hid.h"
5 #include "ux_host_class_hid_keyboard.h"
6 
7 UINT callback_error_code;
8 
9 #define DUMMY_USBX_MEMORY_SIZE (64*1024)
10 static UCHAR dummy_usbx_memory[DUMMY_USBX_MEMORY_SIZE];
11 
12 static UCHAR hid_report_descriptor[] = {
13 
14     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
15     0x09, 0x06,                    // USAGE (Keyboard)
16     0xa1, 0x01,                    // COLLECTION (Application)
17     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
18     0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
19     0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
20     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
21     0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
22     0x75, 0x01,                    //   REPORT_SIZE (1)
23     0x95, 0x08,                    //   REPORT_COUNT (8)
24     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
25     0x95, 0x01,                    //   REPORT_COUNT (1)
26     0x75, 0x08,                    //   REPORT_SIZE (8)
27     0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
28     0x95, 0x05,                    //   REPORT_COUNT (5)
29     0x75, 0x01,                    //   REPORT_SIZE (1)
30     0x05, 0x08,                    //   USAGE_PAGE (LEDs)
31     0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
32     0x29, 0x05,                    //   USAGE_MAXIMUM (Kana)
33     0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
34     0x95, 0x01,                    //   REPORT_COUNT (1)
35     0x75, 0x03,                    //   REPORT_SIZE (3)
36     0x91, 0x03,                    //   OUTPUT (Cnst,Var,Abs)
37     0x95, 0x06,                    //   REPORT_COUNT (6)
38     0x75, 0x08,                    //   REPORT_SIZE (8)
39     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
40     0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
41     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
42     0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
43     0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
44     0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
45     0xc0                           // END_COLLECTION
46 };
47 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
48 
49 
50 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
51 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
52 
53     /* Device descriptor */
54         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
55         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
56         0x00, 0x01,
57 
58     /* Configuration descriptor */
59         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
60         0x32,
61 
62     /* Interface descriptor */
63         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
64         0x00,
65 
66     /* HID descriptor */
67         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
68         MSB(HID_REPORT_LENGTH),
69 
70     /* Endpoint descriptor (Interrupt) */
71         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
72 
73     };
74 
75 
76 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
77 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
78 
79     /* Device descriptor */
80         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
81         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
82         0x03, 0x01,
83 
84     /* Device qualifier descriptor */
85         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
86         0x01, 0x00,
87 
88     /* Configuration descriptor */
89         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
90         0x32,
91 
92     /* Interface descriptor */
93         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
94         0x00,
95 
96     /* HID descriptor */
97         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
98         MSB(HID_REPORT_LENGTH),
99 
100     /* Endpoint descriptor (Interrupt) */
101         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
102 
103     };
104 
105 
106     /* String Device Framework :
107      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
108      Byte 2       : Byte containing the index of the descriptor
109      Byte 3       : Byte containing the length of the descriptor string
110     */
111 
112 #define STRING_FRAMEWORK_LENGTH 40
113 static UCHAR string_framework[] = {
114 
115     /* Manufacturer string descriptor : Index 1 */
116         0x09, 0x04, 0x01, 0x0c,
117         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
118         0x6f, 0x67, 0x69, 0x63,
119 
120     /* Product string descriptor : Index 2 */
121         0x09, 0x04, 0x02, 0x0c,
122         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
123         0x6f, 0x61, 0x72, 0x64,
124 
125     /* Serial Number string descriptor : Index 3 */
126         0x09, 0x04, 0x03, 0x04,
127         0x30, 0x30, 0x30, 0x31
128     };
129 
130 
131     /* Multiple languages are supported on the device, to add
132        a language besides english, the unicode language code must
133        be appended to the language_id_framework array and the length
134        adjusted accordingly. */
135 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
136 static UCHAR language_id_framework[] = {
137 
138     /* English. */
139         0x09, 0x04
140     };
141 
142 
143 UINT  _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
144 
145 
ux_system_host_change_function(ULONG a,UX_HOST_CLASS * b,VOID * c)146 static UINT ux_system_host_change_function(ULONG a, UX_HOST_CLASS *b, VOID *c)
147 {
148     return 0;
149 }
150 
error_callback(UINT system_level,UINT system_context,UINT error_code)151 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
152 {
153 
154     callback_error_code = error_code;
155 }
156 
157 static UCHAR count = 0;
158 UINT _ux_dcd_sim_slave_function(UX_SLAVE_DCD *dcd , UINT function, VOID *parameter);
159 
my_ux_dcd_sim_slave_function(UX_SLAVE_DCD * dcd,UINT function,VOID * parameter)160 static UINT my_ux_dcd_sim_slave_function(UX_SLAVE_DCD *dcd , UINT function, VOID *parameter)
161 {
162     if (count == 0)
163     {
164         count++;
165         return UX_ERROR;
166     }
167     else if (count == 1)
168     {
169         count++;
170         return UX_TRANSFER_BUS_RESET;
171     }
172     else if (count == 2)
173     {
174         count++;
175         return UX_TRANSFER_ERROR;
176     }
177     else
178     {
179         count++;
180         return UX_SUCCESS;
181     }
182 }
183 
184 /* Define what the initial system looks like.  */
185 
186 #ifdef CTEST
test_application_define(void * first_unused_memory)187 void test_application_define(void *first_unused_memory)
188 #else
189 void    usbx_ux_device_class_hid_interrupt_thread_test2_application_define(void *first_unused_memory)
190 #endif
191 {
192 
193 UINT                            status;
194 CHAR *                          stack_pointer;
195 CHAR *                          memory_pointer;
196 
197 
198     /* Inform user.  */
199     printf("Running ux_device_class_hid_interrupt_thread Test 2................. ");
200 
201     /* Initialize the free memory pointer */
202     stack_pointer = (CHAR *) usbx_memory;
203     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
204 
205     /* Initialize USBX. Memory */
206     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
207 
208     /* Check for error.  */
209     if (status != UX_SUCCESS)
210     {
211 
212         printf("Error on line %d, error code: %d\n", __LINE__, status);
213         test_control_return(1);
214     }
215 
216     /* Register the error callback. */
217     _ux_utility_error_callback_register(error_callback);
218 
219     /* The code below is required for installing the host portion of USBX */
220     status =  ux_host_stack_initialize(ux_system_host_change_function);
221     if (status != UX_SUCCESS)
222     {
223 
224         printf("Error on line %d, error code: %d\n", __LINE__, status);
225         test_control_return(1);
226     }
227 
228     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
229     if (status != UX_SUCCESS)
230     {
231 
232         printf("Error on line %d, error code: %d\n", __LINE__, status);
233         test_control_return(1);
234     }
235 
236     /* Register the HID client(s).  */
237     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
238     if (status != UX_SUCCESS)
239     {
240 
241         printf("Error on line %d, error code: %d\n", __LINE__, status);
242         test_control_return(1);
243     }
244 
245     /* The code below is required for installing the device portion of USBX. No call back for
246        device status change in this example. */
247     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
248                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
249                                        string_framework, STRING_FRAMEWORK_LENGTH,
250                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
251     if(status!=UX_SUCCESS)
252     {
253 
254         printf("Error on line %d, error code: %d\n", __LINE__, status);
255         test_control_return(1);
256     }
257 
258     /* Initialize the hid class parameters.  */
259     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
260     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
261     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
262 
263     /* Initilize the device hid class. The class is connected with interface 2 */
264     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
265                                                 1,2, (VOID *)&hid_parameter);
266     if(status!=UX_SUCCESS)
267     {
268 
269         printf("Error on line %d, error code: %d\n", __LINE__, status);
270         test_control_return(1);
271     }
272 
273     /* Initialize the simulated device controller.  */
274     status =  _ux_dcd_sim_slave_initialize();
275 
276     /* Check for error.  */
277     if (status != UX_SUCCESS)
278     {
279 
280         printf("Error on line %d, error code: %d\n", __LINE__, status);
281         test_control_return(1);
282     }
283 
284     /* Register all the USB host controllers available in this system */
285     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
286 
287     /* Check for error.  */
288     if (status != UX_SUCCESS)
289     {
290 
291         printf("Error on line %d, error code: %d\n", __LINE__, status);
292         test_control_return(1);
293     }
294 
295     /* Create the main host simulation thread.  */
296     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
297             stack_pointer, UX_DEMO_STACK_SIZE,
298             20, 20, 1, TX_AUTO_START);
299 
300     /* Check for error.  */
301     if (status != TX_SUCCESS)
302     {
303 
304         printf("Error on line %d, error code: %d\n", __LINE__, status);
305         test_control_return(1);
306     }
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_SLAVE_DEVICE                *device;
314 UX_SLAVE_INTERFACE             *interface;
315 UX_SLAVE_CLASS_HID             *device_hid;
316 UX_HOST_CLASS_HID_KEYBOARD     *keyboard;
317 
318 UX_SLAVE_CLASS_HID_EVENT        hid_event;
319 ULONG                           tmp_timeout;
320 
321 
322     /* Find the HID class */
323     status = demo_class_hid_get();
324     if (status != UX_SUCCESS)
325     {
326 
327         printf("Error on line %d, error code: %d\n", __LINE__, status);
328         test_control_return(1);
329     }
330 
331     /* Get the HID client */
332     hid_client = hid -> ux_host_class_hid_client;
333 
334     /* Check if the instance of the keyboard is live */
335     while (hid_client -> ux_host_class_hid_client_local_instance == UX_NULL)
336         tx_thread_sleep(10);
337 
338     /* Get the keyboard instance */
339     keyboard =  (UX_HOST_CLASS_HID_KEYBOARD *)hid_client -> ux_host_class_hid_client_local_instance;
340 
341     /* Derive device hid class. */
342     device =  &_ux_system_slave -> ux_system_slave_device;
343     interface = device -> ux_slave_device_first_interface;
344     device_hid = interface -> ux_slave_interface_class_instance;
345 
346     /**************************************************/
347     /** Test case: _ux_device_stack_transfer_request return with different value in case of no event. **/
348     /**************************************************/
349     /* Add an event to let the interrupt thread run. */
350     *(ULONG *)hid_event.ux_device_class_hid_event_buffer = 0xdeadbeef;
351     hid_event.ux_device_class_hid_event_length = 4;
352     hid_event.ux_device_class_hid_event_report_id = 0;
353     tmp_timeout = device_hid->ux_device_class_hid_event_wait_timeout;
354     /* Set the timeout to 2s and sleep 10s later to generate >=3 times of no event cases */
355     device_hid->ux_device_class_hid_event_wait_timeout = 2;
356     _ux_system_slave -> ux_system_slave_dcd.ux_slave_dcd_function = my_ux_dcd_sim_slave_function;
357     ux_device_class_hid_event_set(device_hid, &hid_event);
358     ux_utility_thread_sleep(10);
359 
360     /* restore ux_device_class_hid_event_wait_timeout */
361     device_hid->ux_device_class_hid_event_wait_timeout = tmp_timeout;
362     /* Restore state for next test. */
363     _ux_system_slave -> ux_system_slave_dcd.ux_slave_dcd_function = _ux_dcd_sim_slave_function;
364 
365     /**************************************************/
366     /** Test case: status =  _ux_utility_event_flags_get(&hid -> ux_device_class_hid_event_flags_group, UX_DEVICE_CLASS_HID_NEW_EVENT,
367                                                                                             TX_OR_CLEAR, &actual_flags, TX_WAIT_FOREVER); fails. **/
368     /**************************************************/
369     status = ux_utility_event_flags_delete(&device_hid -> ux_device_class_hid_event_flags_group);
370     if (status != UX_SUCCESS)
371     {
372 
373         printf("Error on line %d, error code: %d\n", __LINE__, status);
374         test_control_return(1);
375     }
376     tx_thread_sleep(10);
377 
378     /* And deinitialize the class.  */
379     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
380 
381     /* Deinitialize the device side of usbx.  */
382     _ux_device_stack_uninitialize();
383 
384     /* And finally the usbx system resources.  */
385     _ux_system_uninitialize();
386 
387     /* Successful test.  */
388     printf("SUCCESS!\n");
389     test_control_return(0);
390 }
391 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)392 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
393 {
394     return(UX_SUCCESS);
395 }
396