1 /* This test ensures that the parser generates an error upon pushing more than the allowable global states. */
2 
3 #include "usbx_test_common_hid.h"
4 #include "ux_host_class_hid_keyboard.h"
5 
6 
7 static UCHAR hid_report_descriptor[] = {
8 
9     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
10     0x09, 0x06,                    // USAGE (Keyboard)
11     0xa1, 0x01,                    // COLLECTION (Application)
12                                    /* Push more than the allowable states (4 is the max). */
13     0xa4,                          //   PUSH
14     0xa4,                          //   PUSH
15     0xa4,                          //   PUSH
16     0xa4,                          //   PUSH
17     0xa4,                          //   PUSH
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_PUSH_OVERFLOW &&
121         error_code != UX_DESCRIPTOR_CORRUPTED &&
122         error_code != UX_DEVICE_ENUMERATION_FAILURE)
123     {
124 
125         /* Something went wrong.  */
126         printf("Error on line %d\n", __LINE__);
127         test_control_return(1);
128     }
129 }
130 
131 /* Define what the initial system looks like.  */
132 
133 #ifdef CTEST
test_application_define(void * first_unused_memory)134 void test_application_define(void *first_unused_memory)
135 #else
136 void    usbx_hid_report_descriptor_push_overflow_tag_test_application_define(void *first_unused_memory)
137 #endif
138 {
139 
140 UINT status;
141 CHAR *                          stack_pointer;
142 CHAR *                          memory_pointer;
143 UINT                            descriptor_size = HID_REPORT_LENGTH;
144 
145 
146     /* Inform user.  */
147     printf("Running HID Report Descriptor Push Overflow Tag Test................ ");
148 
149     /* Initialize the free memory pointer */
150     stack_pointer = (CHAR *) usbx_memory;
151     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
152 
153     /* Initialize USBX. Memory */
154     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
155 
156     /* Check for error.  */
157     if (status != UX_SUCCESS)
158     {
159 
160         printf("Error on line %d\n", __LINE__);
161         test_control_return(1);
162     }
163 
164     /* Register the error callback. */
165     _ux_utility_error_callback_register(error_callback);
166 
167     /* The code below is required for installing the host portion of USBX */
168     status =  ux_host_stack_initialize(UX_NULL);
169     if (status != UX_SUCCESS)
170     {
171 
172         printf("Error on line %d\n", __LINE__);
173         test_control_return(1);
174     }
175 
176     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
177     if (status != UX_SUCCESS)
178     {
179 
180         printf("Error on line %d\n", __LINE__);
181         test_control_return(1);
182     }
183 
184     /* Register the HID client(s).  */
185     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
186     if (status != UX_SUCCESS)
187     {
188 
189         printf("Error on line %d, error code: %d\n", __LINE__, status);
190         test_control_return(1);
191     }
192 
193     /* The code below is required for installing the device portion of USBX. No call back for
194        device status change in this example. */
195     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
196                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
197                                        string_framework, STRING_FRAMEWORK_LENGTH,
198                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
199     if(status!=UX_SUCCESS)
200     {
201 
202         printf("Error on line %d\n", __LINE__);
203         test_control_return(1);
204     }
205 
206     /* Initialize the hid class parameters.  */
207     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
208     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
209     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
210 
211     /* Initilize the device hid class. The class is connected with interface 2 */
212     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
213                                                 1,2, (VOID *)&hid_parameter);
214     if(status!=UX_SUCCESS)
215     {
216 
217         printf("Error on line %d\n", __LINE__);
218         test_control_return(1);
219     }
220 
221 
222     /* Initialize the simulated device controller.  */
223     status =  _ux_dcd_sim_slave_initialize();
224 
225     /* Check for error.  */
226     if (status != UX_SUCCESS)
227     {
228 
229         printf("Error on line %d\n", __LINE__);
230         test_control_return(1);
231     }
232 
233     /* Register all the USB host controllers available in this system */
234     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
235 
236     /* Check for error.  */
237     if (status != UX_SUCCESS)
238     {
239 
240         printf("Error on line %d\n", __LINE__);
241         test_control_return(1);
242     }
243 
244     /* Create the main host simulation thread.  */
245     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
246             stack_pointer, UX_DEMO_STACK_SIZE,
247             20, 20, 1, TX_AUTO_START);
248 
249     /* Check for error.  */
250     if (status != TX_SUCCESS)
251     {
252 
253         printf("Error on line %d\n", __LINE__);
254         test_control_return(1);
255     }
256 }
257 
tx_demo_thread_host_simulation_entry(ULONG arg)258 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
259 {
260 
261 UINT status;
262 
263     /* Find the HID class */
264     status = demo_class_hid_get();
265     if (status == UX_SUCCESS)
266     {
267 
268         printf("Error on line %d, error code: %d\n", __LINE__, status);
269         test_control_return(1);
270     }
271 
272     /* Now disconnect the device.  */
273     _ux_device_stack_disconnect();
274 
275     /* And deinitialize the class.  */
276     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
277 
278     /* Deinitialize the device side of usbx.  */
279     _ux_device_stack_uninitialize();
280 
281     /* And finally the usbx system resources.  */
282     _ux_system_uninitialize();
283 
284     /* Successful test.  */
285     printf("SUCCESS!\n");
286     test_control_return(0);
287 }
288 
289 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)290 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
291 {
292     return(UX_SUCCESS);
293 }