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