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