1 /**************************************************/
2 /** Test case: if (callback -> ux_host_class_hid_report_callback_value < UX_HID_KEYBOARD_KEYS_UPPER_RANGE) fails **/
3 /**************************************************/
4 
5 #include "usbx_test_common_hid.h"
6 #include "ux_host_class_hid_keyboard.h"
7 
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     /* Set to something over the maximum amount. */
41     0x25, UX_HID_KEYBOARD_KEYS_UPPER_RANGE, //   LOGICAL_MAXIMUM (101)
42     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
43     0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
44     /* Set to something over the maximum amount. */
45     0x29, UX_HID_KEYBOARD_KEYS_UPPER_RANGE, //   USAGE_MAXIMUM (Keyboard Application)
46     0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
47     0xc0                           // END_COLLECTION
48 };
49 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
50 
51 
52 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
53 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
54 
55     /* Device descriptor */
56         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
57         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
58         0x00, 0x01,
59 
60     /* Configuration descriptor */
61         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
62         0x32,
63 
64     /* Interface descriptor */
65         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
66         0x00,
67 
68     /* HID descriptor */
69         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
70         MSB(HID_REPORT_LENGTH),
71 
72     /* Endpoint descriptor (Interrupt) */
73         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
74 
75     };
76 
77 
78 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
79 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
80 
81     /* Device descriptor */
82         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
83         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
84         0x03, 0x01,
85 
86     /* Device qualifier descriptor */
87         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
88         0x01, 0x00,
89 
90     /* Configuration descriptor */
91         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
92         0x32,
93 
94     /* Interface descriptor */
95         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
96         0x00,
97 
98     /* HID descriptor */
99         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
100         MSB(HID_REPORT_LENGTH),
101 
102     /* Endpoint descriptor (Interrupt) */
103         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
104 
105     };
106 
107 
108     /* String Device Framework :
109      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
110      Byte 2       : Byte containing the index of the descriptor
111      Byte 3       : Byte containing the length of the descriptor string
112     */
113 
114 #define STRING_FRAMEWORK_LENGTH 40
115 static UCHAR string_framework[] = {
116 
117     /* Manufacturer string descriptor : Index 1 */
118         0x09, 0x04, 0x01, 0x0c,
119         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
120         0x6f, 0x67, 0x69, 0x63,
121 
122     /* Product string descriptor : Index 2 */
123         0x09, 0x04, 0x02, 0x0c,
124         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
125         0x6f, 0x61, 0x72, 0x64,
126 
127     /* Serial Number string descriptor : Index 3 */
128         0x09, 0x04, 0x03, 0x04,
129         0x30, 0x30, 0x30, 0x31
130     };
131 
132 
133     /* Multiple languages are supported on the device, to add
134        a language besides english, the unicode language code must
135        be appended to the language_id_framework array and the length
136        adjusted accordingly. */
137 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
138 static UCHAR language_id_framework[] = {
139 
140     /* English. */
141         0x09, 0x04
142     };
143 
144 
145 UINT  _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
146 
147 
ux_system_host_change_function(ULONG a,UX_HOST_CLASS * b,VOID * c)148 static UINT ux_system_host_change_function(ULONG a, UX_HOST_CLASS *b, VOID *c)
149 {
150     return 0;
151 }
152 
error_callback(UINT system_level,UINT system_context,UINT error_code)153 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
154 {
155 
156     /* @BUG_FIX_PENDING: ux_dcd_sim_slave_function.c doesn't support transfer aborts, which happen during device unregistration of a class. */
157     if (error_code != UX_FUNCTION_NOT_SUPPORTED)
158     {
159 
160         /* Failed test.  */
161         printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
162         test_control_return(1);
163     }
164 }
165 
166 /* Define what the initial system looks like.  */
167 
168 #ifdef CTEST
test_application_define(void * first_unused_memory)169 void test_application_define(void *first_unused_memory)
170 #else
171 void    usbx_ux_host_class_hid_keyboard_callback_test2_application_define(void *first_unused_memory)
172 #endif
173 {
174 
175 UINT                            status;
176 CHAR *                          stack_pointer;
177 CHAR *                          memory_pointer;
178 
179 
180     /* Inform user.  */
181     printf("Running ux_host_class_hid_keyboard_callback Test 2.................. ");
182 
183     /* Initialize the free memory pointer */
184     stack_pointer = (CHAR *) usbx_memory;
185     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
186 
187     /* Initialize USBX. Memory */
188     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
189 
190     /* Check for error.  */
191     if (status != UX_SUCCESS)
192     {
193 
194         printf("Error on line %d, error code: %d\n", __LINE__, status);
195         test_control_return(1);
196     }
197 
198     /* Register the error callback. */
199     _ux_utility_error_callback_register(error_callback);
200 
201     /* The code below is required for installing the host portion of USBX */
202     status =  ux_host_stack_initialize(ux_system_host_change_function);
203     if (status != UX_SUCCESS)
204     {
205 
206         printf("Error on line %d, error code: %d\n", __LINE__, status);
207         test_control_return(1);
208     }
209 
210     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_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     /* Register the HID client(s).  */
219     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
220     if (status != UX_SUCCESS)
221     {
222 
223         printf("Error on line %d, error code: %d\n", __LINE__, status);
224         test_control_return(1);
225     }
226 
227     /* The code below is required for installing the device portion of USBX. No call back for
228        device status change in this example. */
229     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
230                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
231                                        string_framework, STRING_FRAMEWORK_LENGTH,
232                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
233     if(status!=UX_SUCCESS)
234     {
235 
236         printf("Error on line %d, error code: %d\n", __LINE__, status);
237         test_control_return(1);
238     }
239 
240     /* Initialize the hid class parameters.  */
241     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
242     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
243     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
244 
245     /* Initilize the device hid class. The class is connected with interface 2 */
246     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
247                                                 1,2, (VOID *)&hid_parameter);
248     if(status!=UX_SUCCESS)
249     {
250 
251         printf("Error on line %d, error code: %d\n", __LINE__, status);
252         test_control_return(1);
253     }
254 
255     /* Initialize the simulated device controller.  */
256     status =  _ux_dcd_sim_slave_initialize();
257 
258     /* Check for error.  */
259     if (status != UX_SUCCESS)
260     {
261 
262         printf("Error on line %d, error code: %d\n", __LINE__, status);
263         test_control_return(1);
264     }
265 
266     /* Register all the USB host controllers available in this system */
267     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
268 
269     /* Check for error.  */
270     if (status != UX_SUCCESS)
271     {
272 
273         printf("Error on line %d, error code: %d\n", __LINE__, status);
274         test_control_return(1);
275     }
276 
277     /* Create the main host simulation thread.  */
278     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
279             stack_pointer, UX_DEMO_STACK_SIZE,
280             20, 20, 1, TX_AUTO_START);
281 
282     /* Check for error.  */
283     if (status != TX_SUCCESS)
284     {
285 
286         printf("Error on line %d, error code: %d\n", __LINE__, status);
287         test_control_return(1);
288     }
289 
290     /* Create the main demo thread.  */
291     status =  tx_thread_create(&tx_demo_thread_slave_simulation, "tx demo slave simulation", tx_demo_thread_slave_simulation_entry, 0,
292             stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
293             20, 20, 1, TX_AUTO_START);
294 
295     /* Check for error.  */
296     if (status != TX_SUCCESS)
297     {
298 
299         printf("Error on line %d, error code: %d\n", __LINE__, status);
300         test_control_return(1);
301     }
302 }
303 
tx_demo_thread_host_simulation_entry(ULONG arg)304 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
305 {
306 
307 UINT                            status;
308 UX_HOST_CLASS_HID_KEYBOARD     *keyboard;
309 
310     /* Find the HID class */
311     status = demo_class_hid_get();
312     if (status != UX_SUCCESS)
313     {
314 
315         printf("Error on line %d, error code: %d\n", __LINE__, status);
316         test_control_return(1);
317     }
318 
319     /* Get the HID client */
320     hid_client = hid -> ux_host_class_hid_client;
321 
322     /* Check if the instance of the keyboard is live */
323     while (hid_client -> ux_host_class_hid_client_local_instance == UX_NULL)
324         tx_thread_sleep(10);
325 
326     /* Get the keyboard instance */
327     keyboard =  (UX_HOST_CLASS_HID_KEYBOARD *)hid_client -> ux_host_class_hid_client_local_instance;
328 
329     /* Wait for keyboard_callback() */
330     tx_thread_sleep(50);
331 
332     /* Now disconnect the device.  */
333     _ux_device_stack_disconnect();
334 
335     /* And deinitialize the class.  */
336     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
337 
338     /* Deinitialize the device side of usbx.  */
339     _ux_device_stack_uninitialize();
340 
341     /* And finally the usbx system resources.  */
342     _ux_system_uninitialize();
343 
344     /* Successful test.  */
345     printf("SUCCESS!\n");
346     test_control_return(0);
347 }
348 
tx_demo_thread_slave_simulation_entry(ULONG arg)349 static void  tx_demo_thread_slave_simulation_entry(ULONG arg)
350 {
351 
352 UX_SLAVE_DEVICE                 *device;
353 UX_SLAVE_INTERFACE              *interface;
354 UX_SLAVE_CLASS_HID              *hid;
355 UX_SLAVE_CLASS_HID_EVENT        hid_event;
356 UCHAR                           key;
357 
358     /* Get the pointer to the device.  */
359     device =  &_ux_system_slave -> ux_system_slave_device;
360 
361     /* Set the first key to 'a' which is 04.  */
362     key = 0x04;
363 
364     /* reset the HID event structure.  */
365     ux_utility_memory_set(&hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT));
366 
367     while(1)
368     {
369 
370         /* Is the device configured ? */
371         while (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
372 
373             /* Then wait.  */
374             tx_thread_sleep(10);
375 
376         /* Until the device stays configured.  */
377         while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
378         {
379 
380             /* Get the interface.  We use the first interface, this is a simple device.  */
381             interface =  device -> ux_slave_device_first_interface;
382 
383             /* Form that interface, derive the HID owner.  */
384             hid = interface -> ux_slave_interface_class_instance;
385 
386             /* Wait for 2 seconds. */
387             ux_utility_thread_sleep(20);
388 
389             /* Then insert a key into the keyboard event.  Length is fixed to 8.  */
390             hid_event.ux_device_class_hid_event_length = 8;
391 
392             /* First byte is a modifier byte.  */
393             hid_event.ux_device_class_hid_event_buffer[0] = 0;
394 
395             /* Second byte is reserved. */
396             hid_event.ux_device_class_hid_event_buffer[1] = 0;
397 
398             /* The 6 next bytes are keys. We only have one key here.  */
399             hid_event.ux_device_class_hid_event_buffer[2] = UX_HID_KEYBOARD_KEYS_UPPER_RANGE;
400 
401             /* Set the keyboard event.  */
402             ux_device_class_hid_event_set(hid, &hid_event);
403         }
404     }
405 }
406 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)407 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
408 {
409     return(UX_SUCCESS);
410 }
411