1 /**************************************************/
2 /** Test case: _ux_host_class_hid_report_id_get(hid, &report_id) fails **/
3 /** Why direct: In order to make _ux_host_class_hid_report_id_get() fail, we must either make _ux_host_stack_class_instance_verify fail, or **/
4 /**	            _ux_utility_semaphore_get() fail. Either way, something else in USBX is going to break... **/
5 /** How: we run the call in a separate thread since the ux_host_class_hid_keyboard_thread never returns. **/
6 /**************************************************/
7 
8 #include "usbx_test_common_hid.h"
9 #include "ux_host_class_hid_keyboard.h"
10 
11 #include "tx_api.h"
12 #include "tx_thread.h"
13 
14 static TX_THREAD                            test_thread;
15 static UX_HOST_CLASS_HID_KEYBOARD           keyboard;
16 
17 static UCHAR hid_report_descriptor[] = {
18 
19     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
20     0x09, 0x06,                    // USAGE (Keyboard)
21     0xa1, 0x01,                    // COLLECTION (Application)
22     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
23     0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
24     0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
25     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
26     0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
27     0x75, 0x01,                    //   REPORT_SIZE (1)
28     0x95, 0x08,                    //   REPORT_COUNT (8)
29     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
30     0x95, 0x01,                    //   REPORT_COUNT (1)
31     0x75, 0x08,                    //   REPORT_SIZE (8)
32     0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
33     0x95, 0x05,                    //   REPORT_COUNT (5)
34     0x75, 0x01,                    //   REPORT_SIZE (1)
35     0x05, 0x08,                    //   USAGE_PAGE (LEDs)
36     0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
37     0x29, 0x05,                    //   USAGE_MAXIMUM (Kana)
38     0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
39     0x95, 0x01,                    //   REPORT_COUNT (1)
40     0x75, 0x03,                    //   REPORT_SIZE (3)
41     0x91, 0x03,                    //   OUTPUT (Cnst,Var,Abs)
42     0x95, 0x06,                    //   REPORT_COUNT (6)
43     0x75, 0x08,                    //   REPORT_SIZE (8)
44     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
45     0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
46     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
47     0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
48     0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
49     0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
50     0xc0                           // END_COLLECTION
51 };
52 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
53 
54 
55 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
56 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
57 
58     /* Device descriptor */
59         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
60         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
61         0x00, 0x01,
62 
63     /* Configuration descriptor */
64         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
65         0x32,
66 
67     /* Interface descriptor */
68         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
69         0x00,
70 
71     /* HID descriptor */
72         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
73         MSB(HID_REPORT_LENGTH),
74 
75     /* Endpoint descriptor (Interrupt) */
76         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
77 
78     };
79 
80 
81 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
82 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
83 
84     /* Device descriptor */
85         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
86         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
87         0x03, 0x01,
88 
89     /* Device qualifier descriptor */
90         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
91         0x01, 0x00,
92 
93     /* Configuration descriptor */
94         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
95         0x32,
96 
97     /* Interface descriptor */
98         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
99         0x00,
100 
101     /* HID descriptor */
102         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
103         MSB(HID_REPORT_LENGTH),
104 
105     /* Endpoint descriptor (Interrupt) */
106         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
107 
108     };
109 
110 
111     /* String Device Framework :
112      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
113      Byte 2       : Byte containing the index of the descriptor
114      Byte 3       : Byte containing the length of the descriptor string
115     */
116 
117 #define STRING_FRAMEWORK_LENGTH 40
118 static UCHAR string_framework[] = {
119 
120     /* Manufacturer string descriptor : Index 1 */
121         0x09, 0x04, 0x01, 0x0c,
122         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
123         0x6f, 0x67, 0x69, 0x63,
124 
125     /* Product string descriptor : Index 2 */
126         0x09, 0x04, 0x02, 0x0c,
127         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
128         0x6f, 0x61, 0x72, 0x64,
129 
130     /* Serial Number string descriptor : Index 3 */
131         0x09, 0x04, 0x03, 0x04,
132         0x30, 0x30, 0x30, 0x31
133     };
134 
135 
136     /* Multiple languages are supported on the device, to add
137        a language besides english, the unicode language code must
138        be appended to the language_id_framework array and the length
139        adjusted accordingly. */
140 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
141 static UCHAR language_id_framework[] = {
142 
143     /* English. */
144         0x09, 0x04
145     };
146 
147 
148 UINT  _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
149 
150 
ux_system_host_change_function(ULONG a,UX_HOST_CLASS * b,VOID * c)151 static UINT ux_system_host_change_function(ULONG a, UX_HOST_CLASS *b, VOID *c)
152 {
153     return 0;
154 }
155 
156 static void  test_thread_entry(ULONG arg);
157 
error_callback(UINT system_level,UINT system_context,UINT error_code)158 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
159 {
160 
161     if (error_code != UX_HOST_CLASS_INSTANCE_UNKNOWN)
162     {
163 
164         /* Something went wrong.  */
165         printf("Error on line %d\n", __LINE__);
166         test_control_return(1);
167     }
168 }
169 
170 /* Define what the initial system looks like.  */
171 
172 #ifdef CTEST
test_application_define(void * first_unused_memory)173 void test_application_define(void *first_unused_memory)
174 #else
175 void    usbx_ux_host_class_hid_keyboard_thread_test2_application_define(void *first_unused_memory)
176 #endif
177 {
178 
179 UINT status;
180 CHAR *                          stack_pointer;
181 CHAR *                          memory_pointer;
182 UINT                            descriptor_size = HID_REPORT_LENGTH;
183 
184     /* Inform user.  */
185     printf("Running ux_host_class_hid_keyboard_thread Test 2.................... ");
186 
187     /* Initialize the free memory pointer */
188     stack_pointer = (CHAR *) usbx_memory;
189     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
190 
191     /* Initialize USBX. Memory */
192     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
193 
194     /* Check for error.  */
195     if (status != UX_SUCCESS)
196     {
197 
198         printf("Error on line %d\n", __LINE__);
199         test_control_return(1);
200     }
201 
202     /* Register the error callback. */
203     _ux_utility_error_callback_register(error_callback);
204 
205     /* The code below is required for installing the host portion of USBX */
206     status =  ux_host_stack_initialize(ux_system_host_change_function);
207     if (status != UX_SUCCESS)
208     {
209 
210         printf("Error on line %d\n", __LINE__);
211         test_control_return(1);
212     }
213 
214     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
215     if (status != UX_SUCCESS)
216     {
217 
218         printf("Error on line %d\n", __LINE__);
219         test_control_return(1);
220     }
221 
222     /* Register the HID client(s).  */
223     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
224     if (status != UX_SUCCESS)
225     {
226 
227         printf("Error on line %d\n", __LINE__);
228         test_control_return(1);
229     }
230 
231     /* The code below is required for installing the device portion of USBX. No call back for
232        device status change in this example. */
233     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
234                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
235                                        string_framework, STRING_FRAMEWORK_LENGTH,
236                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
237     if(status!=UX_SUCCESS)
238     {
239 
240         printf("Error on line %d\n", __LINE__);
241         test_control_return(1);
242     }
243 
244     /* Initialize the hid class parameters.  */
245     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
246     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
247     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
248 
249     /* Initilize the device hid class. The class is connected with interface 2 */
250     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
251                                                 1,2, (VOID *)&hid_parameter);
252     if(status!=UX_SUCCESS)
253     {
254 
255         printf("Error on line %d\n", __LINE__);
256         test_control_return(1);
257     }
258 
259 
260     /* Initialize the simulated device controller.  */
261     status =  _ux_dcd_sim_slave_initialize();
262 
263     /* Check for error.  */
264     if (status != UX_SUCCESS)
265     {
266 
267         printf("Error on line %d\n", __LINE__);
268         test_control_return(1);
269     }
270 
271     /* Register all the USB host controllers available in this system */
272     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
273 
274     /* Check for error.  */
275     if (status != UX_SUCCESS)
276     {
277 
278         printf("Error on line %d\n", __LINE__);
279         test_control_return(1);
280     }
281 
282     /* Create the main host simulation thread.  */
283     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
284             stack_pointer, 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     /* Create the main host simulation thread.  */
296     status =  tx_thread_create(&test_thread, "test thread", test_thread_entry, 0,
297 		    stack_pointer + UX_DEMO_STACK_SIZE, 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\n", __LINE__);
305         test_control_return(1);
306     }
307 }
308 
test_thread_entry(ULONG arg)309 static void  test_thread_entry(ULONG arg)
310 {
311 
312 UINT                                status;
313 UX_HOST_CLASS_HID                   keyboard_hid;
314 
315 
316     /* Since keyboard_hid isn't located in a USBX class container, instance_veryify() will fail. */
317 	keyboard.ux_host_class_hid_keyboard_hid = &keyboard_hid;
318 
319     /* Create the semaphore used in _ux_host_class_hid_keyboard_thread(). */
320 	status = ux_utility_semaphore_create(&keyboard.ux_host_class_hid_keyboard_semaphore, "dummy", 1);
321 	if (status != UX_SUCCESS)
322 	{
323 
324         printf("Error on line %d; status code: %d\n", __LINE__, status);
325         test_control_return(1);
326 	}
327 
328     UX_THREAD_EXTENSION_PTR_SET(tx_thread_identify(), &keyboard)
329 	_ux_host_class_hid_keyboard_thread((ULONG)(ALIGN_TYPE)&keyboard);
330 }
331 
tx_demo_thread_host_simulation_entry(ULONG arg)332 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
333 {
334 
335 UINT        status;
336 
337 
338     /* Find the HID class */
339     status = demo_class_hid_get();
340     if (status != UX_SUCCESS)
341     {
342 
343         printf("Error on line %d; status code: %d\n", __LINE__, status);
344         test_control_return(1);
345     }
346 
347     /* Get the HID client */
348     hid_client = hid -> ux_host_class_hid_client;
349 
350     /* Check if the instance of the keyboard is live */
351     while (hid_client -> ux_host_class_hid_client_local_instance == UX_NULL)
352         tx_thread_sleep(10);
353 
354     /* Give test thread time to make call. */
355 	tx_thread_sleep(20);
356 
357     tx_thread_terminate(&test_thread);
358 
359     /* Now disconnect the device.  */
360     _ux_device_stack_disconnect();
361 
362     /* And deinitialize the class.  */
363     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
364 
365     /* Deinitialize the device side of usbx.  */
366     _ux_device_stack_uninitialize();
367 
368     /* And finally the usbx system resources.  */
369     _ux_system_uninitialize();
370 
371     /* Successful test.  */
372     printf("SUCCESS!\n");
373     test_control_return(0);
374 }
375 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)376 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
377 {
378     return(UX_SUCCESS);
379 }
380