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