1 /* This test ensures that USBX continues working when none of the registered HID clients match the device's report descriptor. */
2 
3 #include "usbx_test_common_hid.h"
4 
5 #include "ux_host_class_hid_keyboard.h"
6 
7 static UINT callback_error_code;
8 
9 static UCHAR hid_report_descriptor[] = {
10 
11                                    /* Set usage page and usage to unknown values */
12     0x07, 0xde, 0xad, 0xbe, 0xef,  // USAGE_PAGE (???)
13     0x0b, 0xde, 0xad, 0xbe, 0xef,  // USAGE (???)
14     0xa1, 0x01,                    // COLLECTION (Application)
15     0x75, 0x04,                    //   REPORT_SIZE (4)
16     0x95, 0x01,                    //   REPORT_COUNT (1)
17     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
18     0xc0                           // END_COLLECTION
19 };
20 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
21 
22 
23 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
24 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
25 
26     /* Device descriptor */
27         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
28         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
29         0x00, 0x01,
30 
31     /* Configuration descriptor */
32         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
33         0x32,
34 
35     /* Interface descriptor */
36         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
37         0x00,
38 
39     /* HID descriptor */
40         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
41         MSB(HID_REPORT_LENGTH),
42 
43     /* Endpoint descriptor (Interrupt) */
44         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
45 
46     };
47 
48 
49 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
50 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
51 
52     /* Device descriptor */
53         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
54         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
55         0x03, 0x01,
56 
57     /* Device qualifier descriptor */
58         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
59         0x01, 0x00,
60 
61     /* Configuration descriptor */
62         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
63         0x32,
64 
65     /* Interface descriptor */
66         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
67         0x00,
68 
69     /* HID descriptor */
70         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
71         MSB(HID_REPORT_LENGTH),
72 
73     /* Endpoint descriptor (Interrupt) */
74         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
75 
76     };
77 
78 
79     /* String Device Framework :
80      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
81      Byte 2       : Byte containing the index of the descriptor
82      Byte 3       : Byte containing the length of the descriptor string
83     */
84 
85 #define STRING_FRAMEWORK_LENGTH 40
86 static UCHAR string_framework[] = {
87 
88     /* Manufacturer string descriptor : Index 1 */
89         0x09, 0x04, 0x01, 0x0c,
90         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
91         0x6f, 0x67, 0x69, 0x63,
92 
93     /* Product string descriptor : Index 2 */
94         0x09, 0x04, 0x02, 0x0c,
95         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
96         0x6f, 0x61, 0x72, 0x64,
97 
98     /* Serial Number string descriptor : Index 3 */
99         0x09, 0x04, 0x03, 0x04,
100         0x30, 0x30, 0x30, 0x31
101     };
102 
103 
104     /* Multiple languages are supported on the device, to add
105        a language besides english, the unicode language code must
106        be appended to the language_id_framework array and the length
107        adjusted accordingly. */
108 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
109 static UCHAR language_id_framework[] = {
110 
111     /* English. */
112         0x09, 0x04
113     };
114 
115 
116 
error_callback(UINT system_level,UINT system_context,UINT error_code)117 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
118 {
119 
120     if (error_code != UX_HOST_CLASS_HID_UNKNOWN)
121     {
122 
123         /* Something went wrong.  */
124         printf("Error on line %d\n", __LINE__);
125         test_control_return(1);
126     }
127 }
128 
129 /* Define what the initial system looks like.  */
130 
131 #ifdef CTEST
test_application_define(void * first_unused_memory)132 void test_application_define(void *first_unused_memory)
133 #else
134 void    usbx_ux_host_class_hid_client_search_test_application_define(void *first_unused_memory)
135 #endif
136 {
137 
138 UINT status;
139 CHAR *                          stack_pointer;
140 CHAR *                          memory_pointer;
141 UINT                            descriptor_size = HID_REPORT_LENGTH;
142 
143 
144     /* Inform user.  */
145     printf("Running ux_host_class_hid_client_search.c Test...................... ");
146 
147     /* Initialize the free memory pointer */
148     stack_pointer = (CHAR *) usbx_memory;
149     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
150 
151     /* Initialize USBX. Memory */
152     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
153 
154     /* Check for error.  */
155     if (status != UX_SUCCESS)
156     {
157 
158         printf("Error on line %d\n", __LINE__);
159         test_control_return(1);
160     }
161 
162     /* Register the error callback. */
163     _ux_utility_error_callback_register(error_callback);
164 
165     /* The code below is required for installing the host portion of USBX */
166     status =  ux_host_stack_initialize(UX_NULL);
167     if (status != UX_SUCCESS)
168     {
169 
170         printf("Error on line %d\n", __LINE__);
171         test_control_return(1);
172     }
173 
174     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
175     if (status != UX_SUCCESS)
176     {
177 
178         printf("Error on line %d\n", __LINE__);
179         test_control_return(1);
180     }
181 
182     /* Register the HID client(s).  */
183     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
184     if (status != UX_SUCCESS)
185     {
186 
187         printf("Error on line %d\n", __LINE__);
188         test_control_return(1);
189     }
190 
191     /* The code below is required for installing the device portion of USBX. No call back for
192        device status change in this example. */
193     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
194                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
195                                        string_framework, STRING_FRAMEWORK_LENGTH,
196                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
197     if(status!=UX_SUCCESS)
198     {
199 
200         printf("Error on line %d\n", __LINE__);
201         test_control_return(1);
202     }
203 
204     /* Initialize the hid class parameters.  */
205     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
206     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
207     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
208 
209     /* Initilize the device hid class. The class is connected with interface 2 */
210     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
211                                                 1,2, (VOID *)&hid_parameter);
212     if(status!=UX_SUCCESS)
213     {
214 
215         printf("Error on line %d\n", __LINE__);
216         test_control_return(1);
217     }
218 
219 
220     /* Initialize the simulated device controller.  */
221     status =  _ux_dcd_sim_slave_initialize();
222 
223     /* Check for error.  */
224     if (status != UX_SUCCESS)
225     {
226 
227         printf("Error on line %d\n", __LINE__);
228         test_control_return(1);
229     }
230 
231     /* Register all the USB host controllers available in this system */
232     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
233 
234     /* Check for error.  */
235     if (status != UX_SUCCESS)
236     {
237 
238         printf("Error on line %d\n", __LINE__);
239         test_control_return(1);
240     }
241 
242     /* Create the main host simulation thread.  */
243     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
244             stack_pointer, UX_DEMO_STACK_SIZE,
245             20, 20, 1, TX_AUTO_START);
246 
247     /* Check for error.  */
248     if (status != TX_SUCCESS)
249     {
250 
251         printf("Error on line %d\n", __LINE__);
252         test_control_return(1);
253     }
254 }
255 
256 
tx_demo_thread_host_simulation_entry(ULONG arg)257 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
258 {
259 
260 UINT status;
261 
262     /* Find the HID class */
263     status = demo_class_hid_get();
264     if (status != UX_SUCCESS)
265     {
266 
267         printf("Error on line %d, error code: %d\n", __LINE__, status);
268         test_control_return(1);
269     }
270 
271     /* Now disconnect the device.  */
272     _ux_device_stack_disconnect();
273 
274     /* And deinitialize the class.  */
275     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
276 
277     /* Deinitialize the device side of usbx.  */
278     _ux_device_stack_uninitialize();
279 
280     /* And finally the usbx system resources.  */
281     _ux_system_uninitialize();
282 
283     /* Successful test.  */
284     printf("SUCCESS!\n");
285     test_control_return(0);
286 }
287 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)288 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
289 {
290     return(UX_SUCCESS);
291 }
292