1 /* This file tests the ux_host_class_hid_mouse_activate API. */
2
3 #include "usbx_test_common_hid.h"
4
5 #include "ux_host_class_hid_keyboard.h"
6 #include "ux_host_class_hid_mouse.h"
7
8 #include "ux_test_hcd_sim_host.h"
9
10 #define DUMMY_USBX_MEMORY_SIZE (64*1024)
11 static UCHAR dummy_usbx_memory[DUMMY_USBX_MEMORY_SIZE];
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 static UX_TEST_HCD_SIM_ACTION stall_on_transfer_0[] = {
144 /* function, request to match,
145 port action, port status,
146 request action, request EP, request data, request actual length, request status,
147 status, additional callback,
148 no_return */
149 { UX_HCD_TRANSFER_REQUEST, UX_NULL,
150 UX_FALSE, 0,
151 UX_TEST_SIM_REQ_ANSWER, 0, UX_NULL, 0, UX_TRANSFER_STALLED,
152 UX_TRANSFER_STALLED},
153 { 0 }
154 };
155
156 static UX_TEST_HCD_SIM_ACTION error_on_transfer_0[] = {
157 /* function, request to match,
158 port action, port status,
159 request action, request EP, request data, request actual length, request status,
160 status, additional callback,
161 no_return */
162 { UX_HCD_TRANSFER_REQUEST, UX_NULL,
163 UX_FALSE, 0,
164 UX_TEST_SIM_REQ_ANSWER, 0, UX_NULL, 0, UX_ERROR,
165 UX_ERROR},
166 { 0 }
167 };
168
169 UINT _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
170
171
ux_system_host_change_function(ULONG a,UX_HOST_CLASS * b,VOID * c)172 static UINT ux_system_host_change_function(ULONG a, UX_HOST_CLASS *b, VOID *c)
173 {
174 return 0;
175 }
176
error_callback(UINT system_level,UINT system_context,UINT error_code)177 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
178 {
179 }
180
181 /* Define what the initial system looks like. */
182
183 #ifdef CTEST
test_application_define(void * first_unused_memory)184 void test_application_define(void *first_unused_memory)
185 #else
186 void usbx_ux_host_class_hid_mouse_activate_test_application_define(void *first_unused_memory)
187 #endif
188 {
189
190 UINT status;
191 CHAR * stack_pointer;
192 CHAR * memory_pointer;
193
194
195 /* Inform user. */
196 printf("Running ux_host_class_hid_mouse_activate Test....................... ");
197
198 /* Initialize the free memory pointer */
199 stack_pointer = (CHAR *) usbx_memory;
200 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
201
202 /* Initialize USBX. Memory */
203 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
204
205 /* Check for error. */
206 if (status != UX_SUCCESS)
207 {
208
209 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
210 test_control_return(1);
211 }
212
213 /* Register the error callback. */
214 _ux_utility_error_callback_register(error_callback);
215
216 /* The code below is required for installing the host portion of USBX */
217 status = ux_host_stack_initialize(ux_system_host_change_function);
218 if (status != UX_SUCCESS)
219 {
220
221 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
222 test_control_return(1);
223 }
224
225 status = ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
226 if (status != UX_SUCCESS)
227 {
228
229 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
230 test_control_return(1);
231 }
232
233 /* Register the HID client(s). */
234 status = ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
235 if (status != UX_SUCCESS)
236 {
237
238 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
239 test_control_return(1);
240 }
241
242 /* The code below is required for installing the device portion of USBX. No call back for
243 device status change in this example. */
244 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
245 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
246 string_framework, STRING_FRAMEWORK_LENGTH,
247 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
248 if(status!=UX_SUCCESS)
249 {
250
251 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
252 test_control_return(1);
253 }
254
255 /* Initialize the hid class parameters. */
256 hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
257 hid_parameter.ux_device_class_hid_parameter_report_length = HID_REPORT_LENGTH;
258 hid_parameter.ux_device_class_hid_parameter_callback = demo_thread_hid_callback;
259
260 /* Initilize the device hid class. The class is connected with interface 2 */
261 status = ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
262 1,2, (VOID *)&hid_parameter);
263 if(status!=UX_SUCCESS)
264 {
265
266 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
267 test_control_return(1);
268 }
269
270 /* Initialize the simulated device controller. */
271 status = _ux_dcd_sim_slave_initialize();
272
273 /* Check for error. */
274 if (status != UX_SUCCESS)
275 {
276
277 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
278 test_control_return(1);
279 }
280
281 /* Register all the USB host controllers available in this system */
282 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
283
284 /* Check for error. */
285 if (status != UX_SUCCESS)
286 {
287
288 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
289 test_control_return(1);
290 }
291
292 /* Create the main host simulation thread. */
293 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
294 stack_pointer, UX_DEMO_STACK_SIZE,
295 20, 20, 1, TX_AUTO_START);
296
297 /* Check for error. */
298 if (status != TX_SUCCESS)
299 {
300
301 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
302 test_control_return(1);
303 }
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 fake_hid;
311 UX_HOST_CLASS_HID_CLIENT fake_hid_client;
312 UX_HOST_CLASS_HID_CLIENT_COMMAND client_command;
313 ALIGN_TYPE tmp;
314 // UX_MEMORY_BLOCK *dummy_memory_block_first = (UX_MEMORY_BLOCK *)dummy_usbx_memory;
315 // UX_MEMORY_BLOCK *original_regular_memory_block = _ux_system -> ux_system_regular_memory_pool_start;
316 // UX_MEMORY_BLOCK *original_cache_safe_memory_block = _ux_system -> ux_system_cache_safe_memory_pool_start;
317
318 /* Find the HID class */
319 status = demo_class_hid_get();
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 /* Get the HID client */
328 hid_client = hid -> ux_host_class_hid_client;
329
330 /* Check if the instance of the keyboard is live */
331 while (hid_client -> ux_host_class_hid_client_local_instance == UX_NULL)
332 tx_thread_sleep(10);
333 #if 0 /* Tested in basic memory tests. */
334 /**************************************************/
335 /** Test case: Template for making ux_utility_memory_allocate() fail **/
336 /**************************************************/
337
338 client_command.ux_host_class_hid_client_command_instance = hid;
339
340 /* Set up the dummy memory block. */
341 dummy_memory_block_first -> ux_memory_block_next = UX_NULL;
342 dummy_memory_block_first -> ux_memory_block_previous = UX_NULL;
343 dummy_memory_block_first -> ux_memory_block_status = UX_MEMORY_UNUSED;
344 dummy_memory_block_first -> ux_memory_block_size = 0;
345 _ux_system -> ux_system_regular_memory_pool_start = dummy_memory_block_first;
346 _ux_system -> ux_system_cache_safe_memory_pool_start = dummy_memory_block_first;
347
348 status = _ux_host_class_hid_mouse_activate(&client_command);
349 if (status != UX_MEMORY_INSUFFICIENT)
350 {
351
352 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
353 test_control_return(1);
354 }
355
356 /* Restore state for next test. */
357 _ux_system -> ux_system_regular_memory_pool_start = original_regular_memory_block;
358 _ux_system -> ux_system_cache_safe_memory_pool_start = original_cache_safe_memory_block;
359 #endif
360 /**************************************************/
361 /** Test case: _ux_host_class_hid_report_id_get() fails. **/
362 /** Why direct: _ux_host_class_hid_mouse_activate() is the first one to call _ux_host_class_hid_report_id_get(), **/
363 /** and it only fails normally after being called more than once (by trying to get a report when you pass **/
364 /** it the last one in the list). **/
365 /**************************************************/
366
367 /* Set up command. */
368 fake_hid.ux_host_class_hid_client = &fake_hid_client;
369 client_command.ux_host_class_hid_client_command_instance = &fake_hid;
370
371 /* Make sure _ux_host_stack_class_instance_verify() in _ux_host_class_hid_report_id_get() doesn't find any classes. */
372 // tmp = _ux_system_host -> ux_system_host_max_class;
373 // _ux_system_host -> ux_system_host_max_class = 0;
374 tmp = _ux_system_host->ux_system_host_class_array->ux_host_class_status;
375 _ux_system_host->ux_system_host_class_array->ux_host_class_status = UX_UNUSED;
376
377 status = _ux_host_class_hid_mouse_activate(&client_command);
378 if (status != UX_HOST_CLASS_INSTANCE_UNKNOWN)
379 {
380
381 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
382 test_control_return(1);
383 }
384
385 /* Restore state for next test. */
386 // _ux_system_host -> ux_system_host_max_class = (UINT)tmp;
387 _ux_system_host->ux_system_host_class_array->ux_host_class_status = (UINT)tmp;
388
389 /**************************************************/
390 /** Test case: _ux_host_class_hid_report_callback_register() fails. **/
391 /** Why direct: _ux_host_class_hid_report_callback_register() loops through each input report until it finds the one passed to it;
392 /** the problem is that the one passed to it is always the first input report, so it always finds it. **/
393 /**************************************************/
394
395 /* Set up. */
396 tmp = (ALIGN_TYPE)hid -> ux_host_class_hid_parser.ux_host_class_hid_parser_input_report;
397 hid -> ux_host_class_hid_parser.ux_host_class_hid_parser_input_report = UX_NULL;
398 client_command.ux_host_class_hid_client_command_instance = hid;
399
400 status = _ux_host_class_hid_mouse_activate(&client_command);
401 if (status != UX_HOST_CLASS_HID_REPORT_ERROR)
402 {
403
404 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
405 test_control_return(1);
406 }
407
408 /* Restore state for next test. */
409 hid -> ux_host_class_hid_parser.ux_host_class_hid_parser_input_report = (UX_HOST_CLASS_HID_REPORT *)tmp;
410
411 /**************************************************/
412 /** Test case: _ux_host_class_hid_idle_set() stalled. **/
413 /** activate continues, and there may be periodic report start error. **/
414 /**************************************************/
415
416 ux_test_hcd_sim_host_set_actions(stall_on_transfer_0);
417 status = _ux_host_class_hid_mouse_activate(&client_command);
418 if (status != UX_HOST_CLASS_HID_PERIODIC_REPORT_ERROR && status != UX_SUCCESS)
419 {
420
421 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
422 test_control_return(1);
423 }
424
425 /**************************************************/
426 /** Test case: _ux_host_class_hid_idle_set() failed. **/
427 /**************************************************/
428
429 ux_test_hcd_sim_host_set_actions(error_on_transfer_0);
430 status = _ux_host_class_hid_mouse_activate(&client_command);
431 if (status != UX_ERROR)
432 {
433
434 printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
435 test_control_return(1);
436 }
437
438 /* Now disconnect the device. */
439 _ux_device_stack_disconnect();
440
441 /* And deinitialize the class. */
442 status = ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
443
444 /* Deinitialize the device side of usbx. */
445 _ux_device_stack_uninitialize();
446
447 /* And finally the usbx system resources. */
448 _ux_system_uninitialize();
449
450 /* Successful test. */
451 printf("SUCCESS!\n");
452 test_control_return(0);
453 }
454
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)455 static UINT demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
456 {
457 return(UX_SUCCESS);
458 }
459