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