1 /* This file tests _ux_device_class_hid_interrupt_thread(), which only runs when there
2 is a new event available in the queue. */
3
4 #include "usbx_test_common_hid.h"
5 #include "ux_host_class_hid_keyboard.h"
6
7 UINT callback_error_code;
8
9 #define DUMMY_USBX_MEMORY_SIZE (64*1024)
10 static UCHAR dummy_usbx_memory[DUMMY_USBX_MEMORY_SIZE];
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 callback_error_code = error_code;
155 }
156
157 /* Define what the initial system looks like. */
158
159 #ifdef CTEST
test_application_define(void * first_unused_memory)160 void test_application_define(void *first_unused_memory)
161 #else
162 void usbx_ux_device_class_hid_interrupt_thread_test_application_define(void *first_unused_memory)
163 #endif
164 {
165
166 UINT status;
167 CHAR * stack_pointer;
168 CHAR * memory_pointer;
169
170
171 /* Inform user. */
172 printf("Running ux_device_class_hid_interrupt_thread Test................... ");
173
174 /* Initialize the free memory pointer */
175 stack_pointer = (CHAR *) usbx_memory;
176 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
177
178 /* Initialize USBX. Memory */
179 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
180
181 /* Check for error. */
182 if (status != UX_SUCCESS)
183 {
184
185 printf("Error on line %d, error code: %d\n", __LINE__, status);
186 test_control_return(1);
187 }
188
189 /* Register the error callback. */
190 _ux_utility_error_callback_register(error_callback);
191
192 /* The code below is required for installing the host portion of USBX */
193 status = ux_host_stack_initialize(ux_system_host_change_function);
194 if (status != UX_SUCCESS)
195 {
196
197 printf("Error on line %d, error code: %d\n", __LINE__, status);
198 test_control_return(1);
199 }
200
201 status = ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_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 /* Register the HID client(s). */
210 status = ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
211 if (status != UX_SUCCESS)
212 {
213
214 printf("Error on line %d, error code: %d\n", __LINE__, status);
215 test_control_return(1);
216 }
217
218 /* The code below is required for installing the device portion of USBX. No call back for
219 device status change in this example. */
220 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
221 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
222 string_framework, STRING_FRAMEWORK_LENGTH,
223 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
224 if(status!=UX_SUCCESS)
225 {
226
227 printf("Error on line %d, error code: %d\n", __LINE__, status);
228 test_control_return(1);
229 }
230
231 /* Initialize the hid class parameters. */
232 hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
233 hid_parameter.ux_device_class_hid_parameter_report_length = HID_REPORT_LENGTH;
234 hid_parameter.ux_device_class_hid_parameter_callback = demo_thread_hid_callback;
235
236 /* Initilize the device hid class. The class is connected with interface 2 */
237 status = ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
238 1,2, (VOID *)&hid_parameter);
239 if(status!=UX_SUCCESS)
240 {
241
242 printf("Error on line %d, error code: %d\n", __LINE__, status);
243 test_control_return(1);
244 }
245
246 /* Initialize the simulated device controller. */
247 status = _ux_dcd_sim_slave_initialize();
248
249 /* Check for error. */
250 if (status != UX_SUCCESS)
251 {
252
253 printf("Error on line %d, error code: %d\n", __LINE__, status);
254 test_control_return(1);
255 }
256
257 /* Register all the USB host controllers available in this system */
258 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
259
260 /* Check for error. */
261 if (status != UX_SUCCESS)
262 {
263
264 printf("Error on line %d, error code: %d\n", __LINE__, status);
265 test_control_return(1);
266 }
267
268 /* Create the main host simulation thread. */
269 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
270 stack_pointer, UX_DEMO_STACK_SIZE,
271 20, 20, 1, TX_AUTO_START);
272
273 /* Check for error. */
274 if (status != TX_SUCCESS)
275 {
276
277 printf("Error on line %d, error code: %d\n", __LINE__, status);
278 test_control_return(1);
279 }
280 }
281
282 UINT _ux_dcd_sim_slave_function(UX_SLAVE_DCD *dcd , UINT function, VOID *parameter);
my_ux_dcd_sim_slave_function(UX_SLAVE_DCD * dcd,UINT function,VOID * parameter)283 static UINT my_ux_dcd_sim_slave_function(UX_SLAVE_DCD *dcd , UINT function, VOID *parameter)
284 {
285
286 UINT status;
287 UX_SLAVE_TRANSFER *transfer_request;
288
289
290 transfer_request = (UX_SLAVE_TRANSFER *)parameter;
291
292 /* Is this from the interrupt thread? */
293 if ((function == UX_DCD_TRANSFER_REQUEST) &&
294 (*(ULONG *)transfer_request -> ux_slave_transfer_request_data_pointer == 0xdeadbeef))
295
296 status = UX_ERROR;
297
298 else
299
300 status = _ux_dcd_sim_slave_function(dcd, function, parameter);
301
302 return status;
303 }
304
tx_demo_thread_host_simulation_entry(ULONG arg)305 static void tx_demo_thread_host_simulation_entry(ULONG arg)
306 {
307
308 UINT status;
309 UX_SLAVE_DEVICE *device;
310 UX_SLAVE_INTERFACE *interface;
311 UX_SLAVE_CLASS_HID *device_hid;
312 UX_HOST_CLASS_HID_KEYBOARD *keyboard;
313 UX_SLAVE_CLASS_HID_EVENT hid_event;
314
315
316 /* Find the HID class */
317 status = demo_class_hid_get();
318 if (status != UX_SUCCESS)
319 {
320
321 printf("Error on line %d, error code: %d\n", __LINE__, status);
322 test_control_return(1);
323 }
324
325 /* Get the HID client */
326 hid_client = hid -> ux_host_class_hid_client;
327
328 /* Check if the instance of the keyboard is live */
329 while (hid_client -> ux_host_class_hid_client_local_instance == UX_NULL)
330 tx_thread_sleep(10);
331
332 /* Get the keyboard instance */
333 keyboard = (UX_HOST_CLASS_HID_KEYBOARD *)hid_client -> ux_host_class_hid_client_local_instance;
334
335 /* Derive device hid class. */
336 device = &_ux_system_slave -> ux_system_slave_device;
337 interface = device -> ux_slave_device_first_interface;
338 device_hid = interface -> ux_slave_interface_class_instance;
339
340 /**************************************************/
341 /** Test case: _ux_device_stack_transfer_request fails. **/
342 /**************************************************/
343
344 /* Override device's entry function to intercept specific transfer request. */
345 _ux_system_slave -> ux_system_slave_dcd.ux_slave_dcd_function = my_ux_dcd_sim_slave_function;
346
347 /* Add an event to let the interrupt thread run. */
348 *(ULONG *)hid_event.ux_device_class_hid_event_buffer = 0xdeadbeef;
349 hid_event.ux_device_class_hid_event_length = 4;
350 hid_event.ux_device_class_hid_event_report_id = 0;
351 ux_device_class_hid_event_set(device_hid, &hid_event);
352
353 /* Wait for error in callback. */
354 while (callback_error_code != UX_ERROR)
355 ux_utility_thread_sleep(10);
356
357 /* Restore state for next test. */
358 _ux_system_slave -> ux_system_slave_dcd.ux_slave_dcd_function = _ux_dcd_sim_slave_function;
359
360 /**************************************************/
361 /** Test case: _ux_utility_thread_suspend() is called (meaning the device gets unconfigured). **/
362 /**************************************************/
363
364 /* Now disconnect the device. This is to get it out of the configured state. */
365 _ux_device_stack_disconnect();
366
367 /* Add an event to let the interrupt thread run. */
368 *(ULONG *)hid_event.ux_device_class_hid_event_buffer = 0xdeadbeef;
369 hid_event.ux_device_class_hid_event_length = 4;
370 hid_event.ux_device_class_hid_event_report_id = 0;
371 ux_device_class_hid_event_set(device_hid, &hid_event);
372
373 /* We have no way of checking if this succeeds... */
374 ux_utility_thread_sleep(10);
375
376 /* And deinitialize the class. */
377 status = ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
378
379 /* Deinitialize the device side of usbx. */
380 _ux_device_stack_uninitialize();
381
382 /* And finally the usbx system resources. */
383 _ux_system_uninitialize();
384
385 /* Successful test. */
386 printf("SUCCESS!\n");
387 test_control_return(0);
388 }
389
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)390 static UINT demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
391 {
392 return(UX_SUCCESS);
393 }
394